C printf() & scanf() Functions
- printf() and scanf() functions are inbuilt library functions in C which are available in C library by default. These functions are declared and related macros are defined in “stdio.h” which is a 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.
- printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
- We use printf() function with
%dformat specifier to display the value of an integer variable.
%cis used to display character,
%ffor float variable,
%sfor string variable,
%lffor double and
%xfor hexadecimal variable.
- To generate a newline,we use “n” in C printf() statement.
printf() Function Example
- scanf() function is used to read character, string, numeric data from keyboard
- Consider below example program where user enters a character. This value is assigned to the variable “ch” and then displayed.
- Then, user enters a string and this value is assigned to the variable “str” and then displayed.
- C language is case sensitive. For example, printf() and scanf() are different from Printf() and Scanf(). All characters in printf() and scanf() functions must be in lower case.
It is a predefined function in “conio.h” (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only.
It is a predefined function in “conio.h” (console input output header file) will tell to the console wait for some time until a key is hit given after running of program.
By using this function we can read a character directly from the keyboard. Generally getch() are placing at end of the program after printing the output on screen.
A C program basically consists of the following parts −
- Preprocessor Commands
- Statements & Expressions
Hello World Program
About Above Program
Let us take a look at the various parts of the above program −
The first line of the program #include is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.
The next line void main() is the main function where the program execution begins.
The next line printf(…) is another function available in C which causes the message “Hello, World” to be displayed on the screen.