News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Simple Programs for C

Started by thiruvasagamani, Sep 22, 2008, 10:29 AM

Previous topic - Next topic

thiruvasagamani

 Main():

Every 'c' program must contain main(),it is operating system which calling main function.When we run program.It is not possible to run a program without main.Main() is neither user defined nor reserved word.

Invalid: MAIN()
{
.....
.....
.....
}

2. Block :

{
st 1;
st 2;
.
.
.
}

Block is a collection of statements,it is also known as compound statement.Block must be enclose in braces.

{
declaration statements;------> F9
Executable statements;------ > Ctrl+F9
}

We must write declaration statements before executable statements.Declaration are processed by compiler. executable statements are processed by CPU.
Invalid:
{
int a;
a=10;
int b;
b=15;
int c;
c=a+b;
}
We cannot write declaration statements after executable statements.
{
int a=10;
}
{
printf("%d",a);---- > error
}
Variables declared in a block are not visible outside the block, they are lost when block terminates.

3. Nested Block :

{
int a= 10;
{

int b=20;
printf("%d\n",a);--- > 110
printf("%d\n",b);---- >20
} ----> b is lost
printf("%d\n",a);---- >10
printf("%d \n",b);//error

}------ > a is lost

Block in a block is nested block,'a' is outer block variable,'b' is inner block variable.Outer block variable can be accessed in a inner block, but not viceversa.

4.Comments :
/*
*/
Comments are used for more clarity & better redability.Write comments whereever clarification are needed.Comments are ignored by compiler .C file contains comments, .EXE file does not contains comments.We can write comments anywhere in the program.
NOTE: Nested comments are not allowed.
/* .......
/* .......
*/........
*/

5.# include :

We have to include header files before main functions for printf() and scanf() those are <stdio.h> , <conio.h>
#include<stdio.h>
#include<conio.h>
main()
{
.
.
}
When we call predefined function,corresponding file must be include.When we calling scanf() include file in the program . # is pre processor directive all functions in file are substituted in program by preprocessor.

All functions in stdio.h are substituted.
Thiruvasakamani Karnan