Output Statements
In C, there are many pre-defined functions are available to display output on the screen. Three of them are-
- putch ( ) function
- puts ( ) function
- printf ( ) function
putch ( ) function
This function is declared in conio.h header file. It is used to display single character on the screen. Example
char ch=’a’;
putch ( ch ); // Output – a
putch ( ‘a’ ); // Output – a
puts ( ) function
This function is declared in stdio.h header file. It is used to display string on the screen. Example
char ch[30]=”Subharti University”;
puts ( ch ); // Output – Subharti University
putch ( ”Subharti University” ); // Output – Subharti University
printf ( ) function – most commonly used
This function is declared in stdio.h header file. It is used to display value of single character, integer, floating point and string values on the screen. Example-
Output Character
char ch=’a’;
printf ( “ %c”, ch ); // Output - a
Output Integer
int first= 10 ;
printf ( “ %d”, first ); // Output - 10
Output Float
int f1= 10.5 ;
printf ( “ %f”, f1 ); // Output – 10.5
Output String
char ch[30]=”Subharti University”;
printf ( “ %s “, ch ); // Subharti University
int first= 10, second= 20 ;
printf ( “ %d + %d = %d”, first, second, first+second ); // Output – 10 + 20 = 30
int first= 10, second= 20, third ;
third = first + second ;
printf ( “ Sum of %d and %d = %d”, first, second, third );
// Output – Sum of 10 and 20 = 30
printf ( “ %d + %d = %d”, 25, 35, 10 ); // Output – 25 + 35 = 10
We can also display messages using printf ( ) function. C compiler will not check the validity of message rather will display on the screen. Hence, it is the responsibility of the programmer to check that message should be appropriate.
printf (“ Hello, How are you ! “ ); // Output- Hello, How are you !
printf (“ Helo, Hew r youe ! “ ); // Output- Helo, Hew r youe !
printf (“ A cow has 10 legs “ ); // Output- A cow has 10 legs
printf (“ Enter any number “ ); // Output- Enter any number