Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Data types in C

Common data types in C programming:

Data TypeSize (bytes)Format SpecifierDescription
int4%dUsed to store integer values.
float4%fUsed to store single-precision floating-point values.
double8%lfUsed to store double-precision floating-point values.
char1%cUsed to store a single character.
bool1%dUsed to store true or false values. (Note: The C standard library does not define a boolean data type. Instead, 0 represents false, and any non-zero value represents true.)
voidUsed to indicate that a function returns no value.
arrayA collection of values of the same data type.
pointer%pUsed to store the memory address of a variable.

Note that the actual size of each data type may vary depending on the system architecture and the compiler used. Additionally, the format specifier for bool is %d, since it is stored as an integer value, not as a separate data type.

Examples of variable declarations using different data types in C:

int age = 25;
float salary = 5000.50;
double pi = 3.14159265358979;
char grade = 'A';
bool isDone = false;
void sayHello() { printf("Hello, world!\n"); }
int numbers[5] = {1, 2, 3, 4, 5};
int *p = &age;

Practice Problems on Data Types:

Problem 1:

Write a program to calculate the area of a circle. The program should prompt the user to enter the radius of the circle and display the calculated area. Use the float data type for the radius and area.

#include <stdio.h>

int main() {
    float radius, area;
    
    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);
    
    area = 3.14159 * radius * radius;
    
    printf("The area of the circle is: %.2f\n", area);
    
    return 0;
}

Explanation:

In this program, we declare two variables of type float: radius and area. The user is prompted to enter the radius of the circle using scanf(). The formula to calculate the area of a circle (area = π * radius * radius) is then used to calculate the area. Finally, the result is displayed using printf().

Output:

Enter the radius of the circle: 5
The area of the circle is: 78.54

Problem 2:

Write a program to convert temperature from Celsius to Fahrenheit. The program should ask the user to enter the temperature in Celsius and display the equivalent temperature in Fahrenheit. Use the float data type for the temperature variables.

#include <stdio.h>

int main() {
    float celsius, fahrenheit;
    
    printf("Enter the temperature in Celsius: ");
    scanf("%f", &celsius);
    
    fahrenheit = (celsius * 9 / 5) + 32;
    
    printf("The temperature in Fahrenheit is: %.2f\n", fahrenheit);
    
    return 0;
}

Explanation:

In this program, we declare two variables of type float: celsius and fahrenheit. The user is prompted to enter the temperature in Celsius using scanf(). The formula to convert Celsius to Fahrenheit (fahrenheit = (celsius * 9 / 5) + 32) is then used to perform the conversion. The result is displayed using printf().

Output:

Enter the temperature in Celsius: 40
The temperature in Fahrenheit is: 104.00

Problem 3:

Write a program to find the sum of two numbers. The program should prompt the user to enter two integers and display their sum. Use the int data type for the numbers and the result.

#include <stdio.h>

int main() {
    int num1, num2, sum;
    
    printf("Enter the first number: ");
    scanf("%d", &num1);
    
    printf("Enter the second number: ");
    scanf("%d", &num2);
    
    sum = num1 + num2;
    
    printf("The sum of the two numbers is: %d\n", sum);
    
    return 0;
}

Explanation: In this program, we declare three variables of type int: num1, num2, and sum. The user is prompted to enter two numbers using scanf(). The sum of the two numbers is calculated using the + operator and stored in the variable sum. The result is displayed using printf().

Output:

Enter the first number: 10
Enter the second number: 20
The sum of the two numbers is: 30

Problem 4:

Write a program to calculate the area of a rectangle. The program should prompt the user to enter the length and width of the rectangle and display the calculated area. Use the double data type for the length, width, and area.

#include <stdio.h>

int main() {
    double length, width, area;
    
    printf("Enter the length of the rectangle: ");
    scanf("%lf", &length);
    
    printf("Enter the width of the rectangle: ");
    scanf("%lf", &width);
    
    area = length * width;
    
    printf("The area of the rectangle is: %.2lf\n", area);
    
    return 0;
}

Explanation: In this program, we declare three variables of type double: length, width, and area. The user is prompted to enter the length and width of the rectangle using scanf(). The area of the rectangle is calculated by multiplying the length and width, and the result is stored in the variable area. Finally, the calculated area is displayed using printf().

Output:

Enter the length of the rectangle: 10
Enter the width of the rectangle: 5
The area of the rectangle is: 50.00