Advertisement

Responsive Advertisement

Data Types in C

 

Learning Sections          show

Data Types in C

Integer Data Type


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Integer data type
    int num = 10;  // Integer variable

    // Print the value of the integer variable
    printf("Integer: %d\n", num);

    return 0;
}
        

Explanation: In this example, int is used to declare an integer variable num which is initialized to 10. The value is printed using printf.

Floating-Point Data Type


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Floating-point data type
    float fnum = 3.14;  // Floating-point variable

    // Print the value of the floating-point variable
    printf("Float: %.2f\n", fnum);

    return 0;
}
        

Explanation: In this example, float is used to declare a floating-point variable fnum which is initialized to 3.14. The value is printed using printf with 2 decimal places.

Double Data Type


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Double data type
    double dnum = 3.1415926535;  // Double variable

    // Print the value of the double variable
    printf("Double: %.10f\n", dnum);

    return 0;
}
        

Explanation: In this example, double is used to declare a double-precision floating-point variable dnum which is initialized to 3.1415926535. The value is printed using printf with 10 decimal places.

Character Data Type


#include<stdio.h>  // Include the standard input-output library

int main() {
    // Character data type
    char ch = 'A';  // Character variable

    // Print the value of the character variable
    printf("Character: %c\n", ch);

    return 0;
}
        

Explanation: In this example, char is used to declare a character variable ch which is initialized to 'A'. The value is printed using printf.

String Data Type


#include<stdio.h>  // Include the standard input-output library

int main() {
    // String data type
    char str[100];  // String variable (array of characters)

    // Prompt the user to enter a string
    printf("Enter a string: ");
    scanf("%s", str);

    // Print the value of the string variable
    printf("String: %s\n", str);

    return 0;
}
        

Explanation: In this example, char is used to declare a string variable str (array of characters) with a maximum length of 100 characters. The user is prompted to enter a string, which is then printed using printf.

Boolean Data Type


#include<stdio.h>  // Include the standard input-output library
#include <stdbool.h>  // Include the boolean library

int main() {
    // Boolean data type
    bool isTrue = true;  // Boolean variable

    // Print the value of the boolean variable
    printf("Boolean: %d\n", isTrue);

    return 0;
}
        

Explanation: In this example, bool is used to declare a boolean variable isTrue which is initialized to true. The value is printed as an integer (1 for true, 0 for false) using printf.

Explanation:

This HTML document contains several simple C programs with syntax highlighting using CSS. Each section demonstrates a different data type in C:

  • Integer Data Type: int is used to declare variables that can hold integer values.
  • Floating-Point Data Type: float is used to declare variables that can hold single-precision floating-point numbers.
  • Double Data Type: double is used to declare variables that can hold double-precision floating-point numbers.
  • Character Data Type: char is used to declare variables that can hold a single character.
  • String Data Type: char[] is used to declare arrays of characters, which can be used to store strings.
  • Boolean Data Type: bool (from stdbool.h) is used to declare variables that can hold boolean values (true or false).