C Programming language

Day 2: Flow of C program with example, main(), printf(), scanf()
Previous Home Next
#include <stdio.h>
Preprocessor directive

The ‘#include’ in the first line of the program is called a preprocessor directive. A preprocessor is a program that processes the C program before the compiler. All the lines in the C program beginning with a hash (#) sign are processed by the preprocessor.

"Standard Input/Output Header"


stdio.h, stands for "Standard Input/Output Header", ‘stdio.h’ refers to a file supplied along with the C compiler. It contains ordinary C statements. These statements give information about many other functions that perform input-output roles. Library functions are declared in header files. stdio.h provide the information to printf() that what should it do.

main() Function
	statement 1;
	statement 2;

The next statement is the main() function. As you already know, this is the place where the execution of the C program begins. Without this function, your C program cannot execute.

  • main() is a function which is entry point of any program.
  • main() is collective name which is given to a set of statements.
  • This name has to be main(). It can not be change.
  • Statements within a main() should be enclosed within a pair of { }.

Difference between main() and other functions

  • parameters to main() are passed from command line,
  • main() is the only function declared by the compiler and defined by the user,
  • main() is by convention a unique external function,
  • main() is the only function with implicit return 0; at the end of main(). When control crosses the ending ‘}’ for main it returns control to the operating system by returning 0 to it (if there is no explicit return statement with a return value). The OS normally treats return 0 as the successful termination of the program.
  • return type for main() is always is an int, (some compilers may accept void main() or any other return type, but they are actually treated as if it is declared as int main(). It makes the code non-portable. Always use int main().
Open and closing brasses { }

Open and closinng brasses tells the starting and ending of the statement.

What is printf?

The printf statement allows you to send output on screen and see the output of that program. that is following given bellow.


#include <stdio.h>
int main()
    int x, y, n;
    x = 2;
    y = 5;
    n = x + y;
    printf("%d + %d = %d\n", x, y, n);
    return 0;
Output :

2 5 7

What is scanf()
  • The scanf function allows you to accept input from users via keyboard. which for us is generally the keyboard.
  • The scanf function can do a lot of different things, but it is generally unreliable unless used in the simplest ways.
  • It is unreliable because it does not handle human errors very well. But for simple programs it is good enough and easy-to-use.
  • "scanf" stands for "scan format", because it scans the input for valid tokens and parses them according to a specified format.
scanf("%d", &b):


The scanf function uses the same placeholders as printf

int uses %d
    float uses %f
    char uses %c
    character strings  use %s

Example of the scanf

#include <stdio.h>
 int main()
    int a, b, c;
    printf("Enter the first value:");
    scanf("%d", &a);
    printf("Enter the second value:");
    scanf("%d", &b);
    c = a + b;
    printf("%d + %d = %d\n", a, b, c);
    return 0;

a=2 b=3 c=5

Previous Home Next