Reading data from input devices and displaying the results on the screen are the two main tasks of any program which performed by printf and scanf.
Both functions are inbuilt library functions, defined in stdio.h (header file). We have to include “stdio.h” file as shown in below C program to make use of these printf() and scanf() library functions in C language.
The printf() function is used for output. It prints the given statement to the console.
- We use printf() function with %d format specifier to display the value of an integer variable.
- Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.
- To generate a newline,we use “\n” in C printf() statement.
- C language is case sensitive. All characters in printf() and scanf() functions must be in lower case.
char ch = 'G';
char str = "goeduhub.com";
float flt = 40.123;
int no = 10;
double dbl = 10.123456;
printf("Character is %c \n", ch);
printf("String is %s \n" , str);
printf("Float value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("Octal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
The scanf() function is used for input. It reads the input data from the console.
printf("Enter any character \n");
printf("Entered character is %c \n", ch);
printf("Enter any string ( upto 100 character ) \n");
printf("Entered string is %s \n", str);