C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

C Program: Input and Display Integer, Float, and Character

Write a program to input an integer, a float, and a character from the user, and then display them back.

C Program: Input and Display Integer, Float, and Character

C

#include <stdio.h>   // Standard I/O header

 

int main() {

    int num;          // variable for integer

    float fnum;       // variable for float

    char ch;          // variable for character

 

    // Taking input from user

    printf("Enter an integer: ");

    scanf("%d", &num);   // %d is for integer

 

    printf("Enter a float: ");

    scanf("%f", &fnum);  // %f is for float

 

    printf("Enter a character: ");

    scanf(" %c", &ch);   // space before %c to avoid newline issue

 

    // Displaying entered values

    printf("\nYou entered:\n");

    printf("Integer  : %d\n", num);

    printf("Float    : %.2f\n", fnum);   // .2f limits float to 2 decimals

    printf("Character: %c\n", ch);

 

    return 0; // Successful termination

}

Output

 
OUTPUT :

Enter an integer: 10
Enter a float: 12.345
Enter a character: A

You entered:
Integer  : 10
Float    : 12.35
Character: A

Explanation:

  1. #include <stdio.h>
    • Includes the Standard Input/Output header file.
    • Required for printf() and scanf() functions.
  2. int num; float fnum; char ch;
    • Declares three variables:
      • num → integer
      • fnum → floating-point number
      • ch → character
  3. scanf("%d", &num);
    • Reads an integer from user input.
    • %d format specifier is used for integers.
    • &num → address of the variable num.
  4. scanf("%f", &fnum);
    • Reads a float value.
    • %f format specifier is for floats.
  5. scanf(" %c", &ch);
    • Reads a character.
    • The leading space before %c ensures it skips leftover newline characters from previous inputs.
  6. printf("Integer : %d", num);
    • Displays the integer entered.
  7. printf("Float : %.2f", fnum);
    • Displays the float with 2 decimal places.
  8. printf("Character : %c", ch);
    • Displays the character entered.
  9. return 0;
    • Exits the program successfully.