These are the fundamental data types in C, primarily used to define variables for storing simple values like integers and characters.
int: Used to store integers (whole numbers) without decimal points.
Example: int age = 25;
Size: Typically 4 bytes.
Range: Depends on the system (e.g., -2,147,483,648 to 2,147,483,647 on 32-bit systems).
float: Used to store single-precision floating-point numbers (numbers with decimal points).
Example: float height = 5.9;
Size: Typically 4 bytes.
Precision: Up to 6-7 decimal digits.
double: Used to store double-precision floating-point numbers for higher precision than float.
Example: double distance = 12345.6789;
Size: Typically 8 bytes.
Precision: Up to 15-16 decimal digits.
char: Used to store single characters. It’s also treated as an integer data type in C because each character has an ASCII code.
Example: char grade = 'A';
Size: 1 byte.
Range: -128 to 127 or 0 to 255, depending on if char is signed or unsigned.
int main() {
int a = 10, b = 20;
int sum = a + b;
printf("Sum of %d and %d is %d\n", a, b, sum);
return 0;
}
int main() {
float radius = 7.5;
float area = 3.14 * radius * radius;
printf("Area of the circle with radius %.2f is %.2f\n", radius, area);
return 0;
}
int main() {
double num1 = 0.123456789012345, num2 = 1.987654321098765;
double result = num1 * num2;
printf("Multiplication of %.15lf and %.15lf is %.15lf\n", num1, num2, result);
return 0;
}
int main() {
char initial = 'J';
printf("The initial of the name is %c\n", initial);
return 0;
}
void greet() {
printf("Hello from a void function!\n");
}
int main() {
greet();
return 0;
}
int main() {
int num = 5;
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
int main() {
float a = 5.5, b = 2.0;
printf("Addition: %.2f\n", a + b);
printf("Subtraction: %.2f\n", a - b);
printf("Multiplication: %.2f\n", a * b);
printf("Division: %.2f\n", a / b);
return 0;
}
int main() {
char name[] = "Alice";
printf("Hello, %s!\n", name);
return 0;
}
int main() {
int age = 30;
char grade = 'B';
float gpa = 3.75;
printf("Age: %d, Grade: %c, GPA: %.2f\n", age, grade, gpa);
return 0;
}
int main() {
double lightSpeed = 299792458.0; // Speed of light in m/s
double time = 1.5; // Time in seconds
double distance = lightSpeed * time;
printf("Light travels %.2lf meters in %.2lf seconds.\n", distance, time);
return 0;
}
In C, the primitive data types include int, float, double, char, and void. Let's write a live program that uses these types effectively:
#include <stdio.h>
int main() {
int age = 25; // Integer data type
printf("Integer: Age = %d\n", age);
// Float data type
float height = 5.9; // Height in feet
printf("Float: Height = %.1f\n", height);
// Double data type (higher precision than float)
double pi = 3.141592653589793;
printf("Double: Value of Pi = %.15lf\n", pi);
// Character data type
char grade = 'A';
printf("Character: Grade = %c\n", grade);
// Void type usage
// Void is typically used in functions that do not return a value
void printMessage(); // Function declaration
printMessage();
return 0;
}
// Function definition for void usage
void printMessage() {
printf("Void: This function does not return anything.\n");
}
int: Used to store whole numbers.
float: Stores decimal numbers with single precision.
double: Stores decimal numbers with higher precision than float.
char: Stores single characters enclosed in single quotes.
void: Used for functions that do not return any value.
When run, the program outputs:
Integer: Age = 25
Float: Height = 5.9
Double: Value of Pi = 3.141592653589793
Character: Grade = A
Void: This function does not return anything.