Skip to content
Home » Programming » C Examples » C Program to take input from user and print them

C Program to take input from user and print them

c program to take input from user

In this post, we will write and explain a C program to take input from user and print them.

There are various data types in C like int, char, float, double, etc. The steps to take input are the same for all data types but each data type has a different format specifier which we need to know.

int - %d or %i (for integer type)
float - %f (for float type or number with decimals)
char - %c (for a character)
string - %s (for strings or words)

Program to take input from user in C

#include<stdio.h>

int main()
{
    int integer;
    float fraction;     
    char character;

    // For a Character
    printf("Enter a Character: ");
    scanf("%c",&character);
    printf("The entered character is %c.", character);
    
    // For integer number
    printf("\n\nEnter a integer number: ");
    scanf("%d", &integer);
    printf("The entered integer number is %d.", integer);

    // For floating point number
    printf("\n\nEnter a floating point number: ");
    scanf("%f", &fraction); 
    printf("The entered floating point number is %f.", fraction);
    
    return 0;
}

Output

Enter a Character: c                                                   
The entered character is c.                                             
                                                                       
Enter a integer number: 1                                              
The entered integer number is 1.                                       
                                                                       
Enter a floating point number: 1.2                                       
The entered floating point number is 1.200000.

Explanation

  • First, we have to declare the variables which we will be using to take inputs from the user. While declaring the variable, we also need to specify the data type of the variable.
int integer;
float fraction;
char character;

Here, we have declared variable integer of type int, variable fraction of type float and variable character of type char.

We can only store values in the variables which are compatible with the data type they were declared.

For example, we can only store integer values (i.e. number without decimal points) in the variable integer since we have declared it of type int.

  • After that we took input from the user and printed the output. For taking input,
scanf("%c", &character);
. . . 
scanf("%d", &integer);
. . .
scanf("%f", &fraction);
. . .

we have to use the function scanf() to take input in C. We have to specify the format specifier of the type of variable we are using. Here for taking character as input we are using %c. Also, we have to specify the variable. As you can see that we are using the variable character for format specifier %c.

It is the same for other data types as you can see in the examples given above.

  • For printing the values of variables,
printf("The entered character is %c.", character);
. . .
printf("The entered integer number is %d.", integer);
. . .
printf("The entered floating point number is %f.", fraction);
. . .

We use the function printf() to display text on the screen. We have to specify the format specifier and at last specify the variable.

To print the value of the variable integer we have specified the format specifier %d and respective format specifier are used for variables of other data types.

Leave a Reply

Your email address will not be published. Required fields are marked *