C Programming

Wait for User Input: C Language

In C programming language there are some input functions. Using these functions, we can wait for user input. In this tutorial we are going to discuss the input functions in details.

Format String

Format string is the format for entered data. This format starts with % sign and followed by format specifier. Format specifier is a specific character which is used for which type of data is being read.

arg1, arg2, arg3… are the addresses of the variables where the entered data will be stored.

scanf() Function

Syntax:  int scanf (“format string”, arg1, arg2, arg3…)

Some of the frequently used specifiers are as follows:

  • d – Used for integer values.
  • f – Used for floating number.
  • c – Used for single character value.
  • s – Used for strings.

Using single scanf() function, one or multiple input can be taken from the user.

The scanf() function takes input from the standard input (keyboard) and store the value in a variable. The function waits for the user input until the user press the enter key. Entered value is being stored in a buffer. When enter key is being pressed, scanf() function starts to read.

Example1: Integer Input

//Example1.c
#include

int main(){

    int i;
    printf("Enter 1st integer value : ");
    scanf("%d",&i);
    printf("You Entered : %d\n",I);

    printf("Enter 2nd integer value : ");
    scanf("%d",&i);
    printf("You Entered : %d\n",I);

    return 0;
}

In Example1.c, we have seen that when we entered integer values and press the enter key. The scanf() function takes the value and stored it in the variable. If we enter the values separated by space, the function returns when space is encountered but the values after space remains in the input buffer. That’s why the second scanf() function will not wait for user input, instead it takes the input from the buffer.

Example 2: Single Character Input

//Example2.c
#include

int main(){

    char c;
    printf("Enter a character: ");
    scanf("%c",&c);
    printf("You Entered : %c\n",c);

    return 0;
}

In Example2.c, we have seen that when we use %c specifier, the scanf() function takes only one character even if we have entered more than one character.

Example 3: Single Character Input (Multiple Times)

//Example3.c
#include

int main(){

    char c;
    printf("Enter 1st character: ");
    scanf("%c",&c);
    printf("You Entered : %c\n",c);

    printf("Enter 2nd character : ");
    scanf("%c",&c);
    printf("You Entered : %c\n",c);

    return 0;
}

Example 4:

//Example4.c
#include

int main(){

    char c;
    printf("Enter 1st character: ");
    scanf("%c",&c);
    printf("You Entered : %c\n",c);

    printf("Enter 2nd character : ");
    scanf(" %c",&c);
    printf("You Entered : %c\n",c);

    return 0;
}

Example 5: String Input

//Example5.c
#include

int main(){

    char name[15];
    printf("Enter Your Name: ");
    scanf("%s",name);
    printf("You Entered : %s\n",name);

    return 0;
}

getc() Function

Syntax: int getc(FILE *stream)

getc() function is used to read a character from the FILE pointer (stream). To read from keyboard, we have to use stdin. This function returns an integer value of read character.

Example 6:

//Example6.c
#include

int main(){

    char c;
    printf("Enter 1st character: ");
    while((c=getc(stdin))=='\n');
    printf("You Entered : %c\n",c);

    while(getc(stdin)!='\n');

    printf("Enter 2nd character : ");      
    while((c=getc(stdin))=='\n');
    printf("You Entered : %c\n",c);

    return 0;
}

getchar() Function

Syntax: int getchar(void)

getchar() function is same as getc(). The only difference is that getc() function can read from any input stream, whereas getchar() function reads from standard input only.

Example 7:

//Example7.c
#include

int main(){

    char c;
    printf("Enter 1st character: ");
    while((c=getchar())=='\n');
    printf("You Entered : %c\n",c);

    while(getchar()!='\n');

    printf("Enter 2nd character : ");      
    while((c=getchar())=='\n');
    printf("You Entered : %c\n",c);

    return 0;
}

Conclusion

In this article, we have seen how input functions are used in C language. All these functions are taken input from the input buffer and when we use these functions multiple times, we have to clear the buffer. Otherwise, the functions will not wait for user input and take the input from the buffer.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com