In C language to display output on screen ready-made library function printf() is used. printf() function defined in a Header file stdio.h so if you want to use printf function in your program then first you have to include the header file stdio.h in your program.
The general form of printf() function is,
printf("format string",list of variables);
- %d used in format string to print an integer.
- %f used in format string to print a real number.
- %c used in format string to print a character.
#include<stdio.h> #include<conio.h> int main() { int a = 50; float b = 120.54; char c = 'R'; printf("Value of a = %d \n",a); printf("Value of b = %f \n",b); printf("Value of c = %c ",c); getch(); return 0; }
Output
Value of a = 50 Value of b = 120.540000 Value of c = R
\n in above program is an Escape Sequence that is used to take cursor to the next line. if you remove this then output will display on a single line.
- printf() can not only print a value of a variable, it can also print the result of an expression and a constant.
printf("%d%d%d",14,12-4+26,i+j);
A Progaram in C Language to add two integer
#include<stdio.h> #include<conio.h> int main() { int a,b,sum; a = 20; b = 50; sum = a+b; printf("Addition of %d and %d = %d",a,b,sum); getch(); return 0; }
Output
Addition of 20 and 50 = 70
Before going to next chapter you first need to write some programs in c like.
- To subtract two numbers.
- To Multiply two numbers.
- To Divide two numbers.