C printf and scanf

The printf() function is used to display output and the scanf() function is used to take input from users.

The printf() and scanf() functions are commonly used functions in C Language. These functions are inbuilt library functions in header files of C programming.


printf() Function

In C Programming language, the printf() function is used for output.

printf() function can take any number of arguments. First argument must be enclosed within the double quotes "hello" and every other argument should be separated by comma ( , ) within the double quotes.

Important points about printf():

  • printf() function is defined in stdio.h header file. By using this function, we can print the data or user defined message on monitor (also called console).
  • printf() can print the different kind of data format on the output string.
  • To print on a new line on the screen, we use “\n” in printf() statement.

C language is case sensitive programming language. For example, printf() and scanf() in lowercase letters treated are different from Printf() and Scanf(). All characters in printf() and scanf() builtin functions must be in lower case.

Syntax

printf("format specifier",argument_list);

The format string for output can be %d (integer), %c (character), %s (string), %f (float) %lf (double) and %x (hexadecimal) variable.

Simple Example of printf() Function

Run : http://ideone.com/bQ3SGW


scanf() Function

The scanf() function is used to read input data from the console.

The scanf() function is builtin function available in C library. scanf() function can read character, string, numeric & other data from keyboard in C language.

scanf() reads formatted data from user and assign them in the variables provided the additional arguments. Additional arguments must point to variables that have same datatype as of user input data format.

Syntax

scanf("format specifier",argument_list);

Simple Example of scanf() Function

Run : http://ideone.com/fL7mnO


Congratulations! Chapter Finished. Do you want to practice more?
Exercises & Assignments
  • No Content Found.
  • Interview FAQs
  • No Content Found.