C program structure
Program Explanation
- The first line “#include” is called as pre-processor directive. “stdio.h” is called a header file.
- This header file has declarations about standard input and output functions.
- Next line is “int main()“. here main() is the starting point of any c program. There is a return type specifies “int”, that informs the compiler that the function returns a value is of type int.
- Next is opening brace ‘{‘, indicating the starting of function main().
- Next line is a “printf()” function. This is used to display the output to the standard console. Note that every statement inside a function has ended with a semicolon. This informs the compiler that the statement has ended. And semicolon is mandatory to terminate a statement.
- Next is “return 0;“. This will return the program execution back to the called function. In this case compiler. In C, returning value 0 indicates the program completed without any issues.
- Last line is closing brace ‘}‘, indicating the end of function main().
OUTPUT OF THE ABOVE PROGRAM IS:
C programming Rules
While writing any C program, we must follow below rules:
- Every program should have a main() to run and generate an output.
- All the statements should be terminated by a semicolon.
- If the programmer has written only semicolon in a line, then it is treated as an empty statement.
Ex:
- All the function names, variable names should be written in lower case, Upper case is used for symbolic constants.
- Every opening braces should have closing braces.
Important header files
stdio.h : Provides input, output functions like printf(), scanf().
conio.h : Console input and output, provides functions like clrscr(), getch().
alloc.h : Provides memory allocation functions like malloc(), calloc(), free().
math.h : Provides math related functions like abs(), squrt().
string.h : Provides string related functions like strcpy(), strcat().
assert.h : Provides macros like assert(int).
C Comments
Comments provide effective ways of knowing the functionality of a function. It is always a good practice to include comments in the beginning of a function with a short description of what it does.
The part written inside comments will be ignored by the compiler and the comments will be stripped off while compiling the program.
C supports 2 types of comments:
- Single line comment
// This is a single line comment
- Multiline comment
/* This is
an example of
multiline comment
*/
👌👌👌
ReplyDeleteThanks dear!
ReplyDelete