Skip to content

C_Data_Types.c

ani9977 edited this page Sep 11, 2024 · 1 revision

Here's the code with comments and explanations:

#include <stdio.h>  // Includes the standard input-output library for input-output operations

int main() {  // Main function where program execution begins
    int myInt = 10;  // Declares an integer variable 'myInt' and assigns the value 10 to it
    float myFloat = 3.14;  // Declares a float variable 'myFloat' and assigns the value 3.14
    char myChar = 'A';  // Declares a character variable 'myChar' and assigns the value 'A'

    // Prints the values of myInt, myFloat, and myChar using format specifiers
    // %d for integers, %.2f for floats with two decimal precision, and %c for characters
    printf("Integer: %d, Float: %.2f, Char: %c\n", myInt, myFloat, myChar);

    return 0;  // Returns 0, indicating the program executed successfully
}

Explanation:

  • int myInt = 10;: Declares an integer variable myInt and initializes it with the value 10.
  • float myFloat = 3.14;: Declares a floating-point variable myFloat and assigns it the value 3.14.
  • char myChar = 'A';: Declares a character variable myChar and assigns it the character 'A'.
  • printf("Integer: %d, Float: %.2f, Char: %c\n", myInt, myFloat, myChar);: This printf function prints the values of the three variables.
    • %d is the format specifier for integers.
    • %.2f prints the float with 2 decimal places.
    • %c is used to print characters.
  • return 0;: This indicates the program completed successfully.

In this program, we demonstrate the use of different data types in C: int, float, and char, and how to format their output using the printf function.

Clone this wiki locally