codescracker
c

C Program to Get Input from User



« Previous Program Next Program »

In this article, you will learn and get code on getting input from user at run-time using a C program. Here are the list of programs (based on user input-type) available over here:

Get Integer Input

To receive or get an integer input from the user in C programming, use the function scanf(). This function takes two argument. First one is the format specifier of input type. And second parameter is the address of 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 build and run under Code::Blocks IDE. Here is its sample run:

receiving input in c programming

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

c get user input

Note - The %d format specifier is used to scan 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 input data. And the second argument is the variable to which the value is going to be saved.

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

To print the value, back on output screen, you have not to use any address of operator. Simply use the format specifier and the variable that contains the value. While scanning the input, the address of operator is used to put/store the entered value at the address of given variable say num.

Get Character Input

Now let's create another program that receives a character type input data from 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/output.

What if User enters more than One Character

If user enters two or more characters as input, then above program scans and initialized the first character only to ch variable. Rest of the characters gets skipped. Therefore, if user enters codescracker as input, then above program prints only c as output.

Get String Input

This is the last program of this article. This program scans the string input from user. That is more than one character input from user. For example, 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 is its sample run assuming that, user enters this is codescracker.com as string input:

get string input c

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

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

Same Program in Other Languages

C Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker