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

  • printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen.
  • 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.

printf() Function Example

Getting Output Using printf() Function


Output Of Above Program

scanf() Function

  • 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.

scanf() Example





  • 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.

clrscr() Function

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.

getch() Function

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.

Program Consists

A C program basically consists of the following parts −

  1. Preprocessor Commands
  2. Functions
  3. Variables
  4. Statements & Expressions

Hello World Program

Hello World Program in Turbo C


Output in Turbo C

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.