Basic data types, also known as primary or fundamental data types, are the core building blocks for storing data in C. They are predefined by the language and are used to represent single values like numbers and characters.
int
(Integer)
The int
data type is used to store whole numbers, both
positive and negative, without any decimal part.
路聽聽聽聽聽聽聽聽 Purpose: Storing integer values like age, roll numbers, or counts.
路聽聽聽聽聽聽聽聽 Size: Typically 4 bytes (32 bits), but this can vary by system.
路聽聽聽聽聽聽聽聽
Format
Specifier: %d
Example:
C
#include <stdio.h>
聽
int main() {
聽聽聽
// Declaring an integer variable
聽聽聽
int year =
2025;
聽
聽聽聽
// Declaring another integer for a negative value
聽聽聽
int temperature =
-5;
聽
聽聽聽
printf(
"The current year is: %d\n", year);
聽聽聽
printf(
"The temperature is: %d degrees Celsius\n", temperature);
聽
聽聽 聽
return
0;
}
聽
char
(Character)
The char
data type is used to store a single character, such
as a letter, a digit, or a special symbol. The character must be enclosed in
single quotes ('
). Internally, characters are stored as integers based
on the ASCII character set.
路聽聽聽聽聽聽聽聽 Purpose: Storing single characters like a grade, an initial, or a symbol.
路聽聽聽聽聽聽聽聽 Size: 1 byte.
路聽聽聽聽聽聽聽聽
Format
Specifier: %c
Example:
C
#include <stdio.h>
聽
int main() {
聽聽聽
char grade =
'A';
聽聽聽
char symbol =
'$';
聽
聽聽聽
printf(
"The student's grade is: %c\n", grade);
聽聽聽
printf(
"The currency symbol is: %c\n", symbol);
聽聽聽
聽聽聽聽
// Demonstrating the integer nature of char
聽聽聽
printf(
"The ASCII value of '%c' is %d\n", grade, grade);
聽
聽聽聽
return
0;
}
聽
float
and
double
(Floating-Point)
float
and double
are used to store real numbers (numbers with a
decimal point). The main difference is their precision and size.
路聽聽聽聽聽聽聽聽
float
: For single-precision floating-point numbers. It has
about 6-7 digits of precision.
o聽聽聽 Size: 4 bytes.
o聽聽聽 Format Specifier: %f
路聽聽聽聽聽聽聽聽
double
: For double-precision floating-point numbers. It
offers much higher precision (about 15-16 digits) and is generally preferred
for real-number calculations.
o聽聽聽 Size: 8 bytes.
o聽聽聽 Format Specifier: %lf
Example:
C
#include <stdio.h>
聽
int main() {
聽聽聽
// Use 'f' suffix for float literals
聽聽聽
float price =
499.99f;
聽聽聽聽
聽聽聽聽
double pi =
3.1415926535;
聽
聽聽聽
printf(
"The price is: %f\n", price);
聽聽聽
printf(
"The value of PI is: %lf\n", pi);
聽
聽聽聽
return
0;
}
聽
Type Modifiers
These keywords are used to modify the properties (like size or sign) of the basic integer and character types.
路聽聽聽聽聽聽聽聽
short
, long
: Affect the size of the data type. long int
can store larger integers than a plain int
.
路聽聽聽聽聽聽聽聽
signed
, unsigned
: Determine if the variable can hold negative values.
By default, types are signed
.
An unsigned
int
can only store non-negative
values (0 and positive), allowing it to hold a larger maximum positive value
compared to a signed
int
of the same size.
Modified Type |
Description |
|
Stores only non-negative integers. |
|
Stores a larger range of integers. |
|
Provides
even greater precision than |
|
Stores character values from 0 to 255. |
聽
The void
Type
The void
type is a special-purpose type that has no value. It
is primarily used in two scenarios:
1.聽聽 Function Return Type: To specify that a function does not return any value.
C
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽void printMessage() {
聽聽聽
printf(
"This function does not return a value.\n");
}
2.聽聽 Generic Pointers: To declare a generic pointer (void *
), which is an advanced concept.
聽