C program to get input from the user

In this article, you will learn about and get code for getting user input at runtime using a C program. Here is the list of programs (based on user input type) available over here:

Get Integer Input in C

To receive or get an integer input from the user in C programming, use the function scanf(). This function takes two arguments. The first one is the format specifier for the input type. And the second parameter is the address of a variable related to input data. Let's take a look at the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    int num;
    printf("Enter the Number: ");
    scanf("%d", &num);
    printf("\nYou've entered: %d", num);
    getch();
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

receiving input in c programming

Now supply any integer value, say 10, and press the ENTER key to see the following output:

c get user input

Note The %d format specifier is used to scan an integer-type value.

As you can see from the above program, the function scanf() has two arguments. The first argument specifies the format specifier of the input data. The second argument specifies the variable to which the value will be saved.

Note: Don't forget to put the address of the (&) operator before the variable num inside the function scanf() to scan any integer value.

To print the value back on the output console, there is no need to use any "address of" operator. Simply use the format specifier and the variable that contains the value. Whereas, while scanning the input, the "address of" operator is used to put or store the entered value at the address of a given variable, say num.

In C, get Character Input

Now let's create another program that receives character-type input data from the user at run-time:

#include<stdio.h>
#include<conio.h>
int main()
{
    char ch;
    printf("Enter the Character: ");
    scanf("%c", &ch);
    printf("\nYou've entered: %c", ch);
    getch();
    return 0;
}

Here is its sample run:

c program get character input

The %c format specifier is used for character input and output.

What if the user enters more than one character?

If the user enters two or more characters as input, then the above program scans and initializes the first character only to the ch variable. The rest of the characters get skipped. Therefore, if the user enters "codescracker" as input, then the above program prints only c as output.

Get string input in C

This is the end of this article. This program scans the string input from the user. That is more than one character input from the user. For instance, "this is codescracker.com"

#include<stdio.h>
#include<conio.h>
int main()
{
    char str[100];
    printf("Enter any String: ");
    gets(str);
    printf("\nYou've entered: %s", str);
    getch();
    return 0;
}

Here's an example run, assuming the user enters "this is codescracker.com" as the string input:

get string input c

The %s format specifier is used for string input/output.

You can also use the scanf() function with the %s format specifier to scan the string from the user. But it is always recommended to scan any string using the gets() function. Because if you use the scanf() function to scan the string, then it skips all the things after the first space. That is, scanf() reads the string until a space occurs. But gets() reads the complete string along with spaces.

The same program in different languages

C Quiz


« Previous Program Next Program »


Follow/Like Us on Facebook


Subscribe Us on YouTube