C program to perform input output of all basic data types

Topics

  • #include <stdio.h> – This line includes the standard input/output library, which provides functions for reading input from the user and printing output to the console.
  • int main() { – This is the main function of the program, which is the entry point for the program execution. The int return type indicates that the program should return an integer value when it completes.
  • int integer; – This declares an integer variable called integer that will be used to store user input.
  • printf("Enter an integer: "); – This prints a prompt message to the console asking the user to enter an integer.
  • scanf("%d", &integer); – This reads an integer input from the user and stores it in the integer variable using the scanf function. The %d format specifier is used to indicate that an integer input is expected. The & symbol before the variable name is used to pass the memory address of the variable to the scanf function, so that it can write the user input to that location in memory.
  • printf("The integer you entered is: %d\n", integer); – This prints the integer value entered by the user to the console using the printf function. The %d format specifier is used to indicate that an integer value should be printed, and the \n character is used to add a newline after the output.
  • Steps 3-6 are repeated for each of the other data types (floating-point, double-precision, character, and boolean), with appropriate format specifiers (%f, %lf, %c, and %d) and variable types (float, double, char, and int) used.
  • return 0; – This statement indicates that the program has completed successfully and returns the value 0 to the operating system. The 0 value indicates that the program has completed without errors.
#include <stdio.h>

int main() {
    // Input/output for integer data type
    int integer;
    printf("Enter an integer: ");
    scanf("%d", &integer);
    printf("The integer you entered is: %d\n", integer);

    // Input/output for float data type
    float floating_point;
    printf("Enter a floating-point number: ");
    scanf("%f", &floating_point);
    printf("The floating-point number you entered is: %f\n", floating_point);

    // Input/output for double data type
    double double_precision;
    printf("Enter a double-precision number: ");
    scanf("%lf", &double_precision);
    printf("The double-precision number you entered is: %lf\n", double_precision);

    // Input/output for character data type
    char character;
    printf("Enter a character: ");
    scanf(" %c", &character);
    printf("The character you entered is: %c\n", character);

    // Input/output for boolean data type (using int to represent boolean)
    int boolean;
    printf("Enter a boolean value (0 for false, 1 for true): ");
    scanf("%d", &boolean);
    printf("The boolean value you entered is: %d\n", boolean);

    return 0;
}

OUTPUT

Enter an integer: 42
The integer you entered is: 42
Enter a floating-point number: 3.14
The floating-point number you entered is: 3.140000
Enter a double-precision number: 3.14159265359
The double-precision number you entered is: 3.141593
Enter a character: a
The character you entered is: a
Enter a boolean value (0 for false, 1 for true): 1
The boolean value you entered is: 1

Enter an integer: 42
The integer you entered is: 42

Enter a floating-point number: 3.14
The floating-point number you entered is: 3.140000

Enter a double-precision number: 3.14159265359
The double-precision number you entered is: 3.141593

Enter a character: a
The character you entered is: a

Enter a boolean value (0 for false, 1 for true): 1
The boolean value you entered is: 1

Related Posts

Leave a Reply

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