Input Statements

View


In C, there are many pre-defined functions are available to take input from the user into variable used in C program. Three of them are-

  1. getch ( ) function
  2. gets ( ) function
  3. scanf ( ) function

getch ( ) function - most commonly used for input character values
This function is declared in conio.h header file. It is used to take only single character as input from the user. Example

char ch;                                                            // Variable declaration
printf( “ Press any button on keyboard “);        // Message display for user
ch = getch ( );                                                   // Input by user – b
putch ( ch );                                                      // Output to user – b

Note- Before every input statement, we should display appropriate message to the user, so that, user may know what is to be input to the computer.

gets ( ) function - most commonly used for input string values
This function is declared in stdio.h header file. It is used to take string (group of characters is called string) as input from the user. Example

char ch[30];                                                       // Variable (String) declaration
printf( “ Enter your university name “);              // Message for user
gets ( ch );                                                         // Input – Subharti University
printf ( “ %s ”, ch ); OR puts ( ch );                   // Output – Subharti University

scanf ( ) function – most commonly used for input integer and float values
This function is declared in stdio.h header file. It is used to take single character, integer, floating point and string values as input from the user. Example-

Input Character
char ch;
printf(“ Enter any character “);
scanf ( “ %c”, &ch );                                // Input character will be stored in variable ch

Input Integer
int first;
printf(“ Enter any integer number “);
scanf ( “ %d”, &first );                              // Input integer will be stored in variable first

Input Float
int f1;
printf(“ Enter any number “);
scanf ( “ %f”, &f1 );                                  // Input float will be stored in variable f1

Input String
char ch[30];
printf(“ Enter any string “);
scanf ( “ %s “, ch );                                 // Input string will be stored in variable ch- no &    

                                                                   symbol is required.

Last modified: Tuesday, 21 September 2021, 9:38 AM