What is scanf() function - C Language Basics

C examples for Language Basics:scanf

Introduction

The scanf() function is a function provided by the standard input output library <stdio.h>.

It reads input from the keyboard and stores it in declared variables.

It takes two arguments.

scanf("conversion specifier", variable); 

The conversion specifier argument tells scanf() how to convert the incoming data.

The following conversion specifiers can be used to read int, float and char type data.

Conversion Specifier Description
%d Receives integer value
%f Receives floating-point numbers
%c Receives character

The following code uses the scanf() function to read in two integers and add them together.

Demo Code

#include <stdio.h> 
int main() { //from w  w  w  . java  2 s .  c o  m
   int iOperand1 = 0; 
   int iOperand2 = 0;  

   printf("\nEnter first operand: "); 
   scanf("%d", &iOperand1); 
   printf("Enter second operand: "); 
   scanf("%d", &iOperand2); 
   printf("The result is %d\n", iOperand1 + iOperand2); 
}

Result


Related Tutorials