C - Reading values with scanf()

Introduction

The scanf() function can read in any value specified by a conversion character.

The following scanf() reads an Integer.

Demo

#include <stdio.h> 

int main() /* www. j  a  v a 2s.  c o  m*/
{ 
      int f; 

      printf("What is your favorite number: "); 
      scanf("%d",&f); 
      printf("%d is my favorite number, too!\n",f); 
      return(0); 
}

Result

The %d conversion character is used, just like printf().

That character directs scanf() to look for an int value for variable f.

The ampersand (&) in the scanf() function is a C memory address operator.

Related Topic