Learn C - C Console Input






We can read input from keyboard in many ways.

The following is a list of function for reading input from keyboard:

getchar() 
gets() 
scanf() 

getchar() and putchar() functions

getchar() function is used to get a character from keyboard and putchar() function is used to print a character to Terminal.


  #include <stdio.h> 
/*from   ww  w.  j  a  va  2  s . c o  m*/
  void getchar_putchar(); 

  int main() { 

      getchar_putchar(); 

      return 0; 
  } 

  void getchar_putchar(){ 
      int c; 

      printf ("Type a character: "); 
      c = getchar(); 
      printf ("char: %c\n",c); 
      putchar(c); 
      printf("\n"); 
  } 

The code above generates the following result.





gets() and puts() functions

gets() is used to read a text. It will stop to read if you type newline (ENTER key).

We can use fgets() to read text with length limitation.


  #include <stdio.h> 
/*  www . j  av a2  s . c o  m*/
  void gets_puts(); 

  int main() { 
      gets_puts(); 
      return 0; 
  } 

  void gets_puts(){ 
      char name[256]; 

      printf ("Your name: "); 
      gets (name); 
      printf ("name: %s\n",name); 
      puts(name); 

      name[0] =  '\0'; // clear 
      printf ("Your name: "); 
      fgets(name,256,stdin); 
      printf ("name: %s\n",name); 
      puts(name); 
  } 

The code above generates the following result.





scanf() function

Another approach, we can use scanf() function to read a text.


  #include <stdio.h> 
//w ww  . j  a v a  2 s.  c o  m
  void scanf_demo(); 
  int main() { 
      scanf_demo(); 
      return 0; 
  } 

  void scanf_demo(){ 
      int num; 
      char c; 
      char city[15]; 

      float dec; 

      printf("Please enter an integer value: "); 
      scanf("%d", &num ); 

      // %c ignores space characters 
      printf("Please enter a character: "); 
      scanf(" %c", &c ); 

      printf("Please enter a city name (no space): "); 
      scanf("%s", city ); 

      printf("Please enter a decimal value:  "); 
      scanf("%f", &dec ); 

      printf("\n-----result-------\n"); 
      printf("number = %d\n", num ); 
      printf("character = %c\n", c ); 
      printf("city name = %s\n", city ); 
      printf("decimal number = %f\n", dec ); 
   } 

The code above generates the following result.

Reading Program Arguments

We can read arguments and its length from our program. This information already pass to main() function.


  #include <stdio.h> 
/*ww  w . j ava  2s  .  co m*/
  int main(int argc, const char* argv[]) { 

      int i; 

      printf("total argument: %d\n",argc-1); 
      if(argc>1){ 
          for(i=1;i<argc;i++){ 
              printf("%s\n",argv[i]); 
          } 
      } 
      return 0; 
  } 

The code above generates the following result.

scanf()

The following table lists Format Specifiers for Reading Data with scanf()

ActionRequired control string
To read a value of type short%hd
To read a value of type int%d
To read a value of type long%ld
To read a value of type float%f or %e
To read a value of type double%lf or %le

In the %ld and %lf format specifiers, l is lowercased.