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() //from w ww. ja  va2s.c om
{ 
      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