Understanding C Data Types: With Syntax, Examples & Outputs
When you write code in C, you work with data—numbers, characters, decimal values, and so on. But the computer doesn’t automatically know what kind of data you're using. That’s why we use data types. They help tell the compiler: “This variable is going to store an integer,” or “This one holds a letter.” Without this clarity, the program would be confused about how much space to reserve in memory or how to work with the variable.
What Are Data Types in C?
A data type defines the kind of data a variable can store—like an integer, a floating-point number, or a single letter. In C, you must declare the type of a variable before using it. This helps the compiler handle the data properly and prevents unexpected errors.
Why Do We Use Data Types?
Imagine you're building an inventory management system. For each product, you need to store:
Product ID → a whole number
Product Name → a set of characters (a string)
Product Price → a decimal number
Now, if you accidentally try to store a product name in a variable that was meant for numbers, your program won't work correctly. That’s exactly why data types are necessary.
They help with:
- Correct Data Storage: Each variable holds exactly the kind of value it's meant to.
- Efficient Memory Usage: Different data types use different amounts of memory.
- Error Checking: The compiler will show an error if you try to do something incorrect with the data.
- Better Code Clarity: Other developers (or even you later) can quickly understand what each variable is used for.
Types of Data Types in C
C has three main categories:
- Primitive Data Types: The basic types like numbers (int, float, double), characters (char), and true/falsevalues (_Bool).
- Derived Data Types: Built from basic types, like lists (arrays), references (pointers), and functions.
- User-Defined Data Types: Custom types created by programmers, like groups of variables (struct), sets of constants (enum), and new names for types (typedef).
Primitive Data Types
Data Type | Size (in bytes) | Range | Format Specifier | Description |
---|---|---|---|---|
int | 4 | -2,147,483,648 to 2,147,483,647 | %d | Integer numbers |
float | 4 | ±1.2E-38 to ±3.4E+38 | %f | Single-precision floating-point numbers |
double | 8 | ±2.3E-308 to ±1.7E+308 | %lf | Double-precision floating-point numbers |
char | 1 | -128 to 127 (or 0 to 255 for unsigned) | %c | Single character |
void | 0 | N/A | N/A | Indicates no value (e.g., void functions) |
_Bool | 1 | 0 (false) or 1 (true) | %d | Boolean type (introduced in C99) |
The size of each data type (e.g., int, float, double, long) can vary depending on the compiler and system architecture (e.g., 32-bit or 64-bit).
To Check Size of Each Data Type
Syntax:
sizeof(dataType)
Example:
#include <stdio.h>
int main() {
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of long: %zu bytes\n", sizeof(long));
return 0;
}
Sample Output (may vary):
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
Size of long: 8 bytes
int
The most commonly used integer data type. Represents whole numbers(like 1, -5, 42).
Syntax:
int variableName = value;
Size & Range: Typically 4 bytes and -2,147,483,648 to 2,147,483,647
Example:
#include <stdio.h>
int main() {
int productId = 344334;
printf("%d\n", productId);
return 0;
}
Output: 344334
float
A single-precision floating point number used for decimal values. It’s useful when you need to represent approximate decimal values.
Syntax:
float variableName = value;
Size & Range: Typically 4 bytes and ±3.4e−38 to ±3.4e+38
Example:
#include <stdio.h>
int main() {
float productPrice = 99.36f;
printf("%.1f\n", productPrice);
return 0;
}
Output: 99.36
double
A double-precision floating point number for more precise decimal values.
Syntax:
double variableName = value;
Size & Range: Typically 8 bytes and ±1.7e−308 to ±1.7e+308
Example:
#include <stdio.h>
int main() {
double distance = 9.346899;
printf("%lf\n", distance);
return 0;
}
Output: 9.346899
char
Represents a single character. It’s useful for making decisions in programs based on conditions.
Syntax:
char variableName = 'A';
Size & Range: 1 byte and typically 0 to 255
Example:
#include <stdio.h>
int main() {
char response = 'Y';
printf("%c\n", response);
return 0;
}
Output: Y
_Bool
Represents boolean values (true/false) in C.
Syntax:
_Bool variableName = 1; // 1 for true, 0 for false
Example:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool batteryLow = true;
printf("%d\n", batteryLow);
return 0;
}
Output: 1
void
Indicates the absence of a value. Commonly used for functions that do not return any result or to represent a placeholder.
Official C Documentation: C Data Types Reference
Common Errors in C
Here are some common beginner mistakes when working with different data types in C. Watch out for these to avoid unexpected behavior!
int
Declaring a variable without specifying its type:
productId = 1234; // Error: type not specifiedAlways declare the type explicitly: int productId = 1234;
float
Forgetting to add 'f' at the end of a float literal:
float temperature = 98.6; // Implicit conversion from doubleUse: float temperature = 98.6f;
Mixing up float and double format specifiers in printf():
printf("%lf", temperature); // Works but %f is standardUse: printf("%f", temperature);
double
Using %f instead of %lf in scanf():
double value; scanf("%f", &value); // Error: %f reads floatUse: scanf("%lf", &value);
In printf(), %f and %lf both work for double:
printf("%f", value); // or printf("%lf", value);Both are valid in printf().
char
Trying to assign multiple characters to a char variable:
char letter = 'AB'; // Error: char can store only one characterUse: char letter = 'A';
_Bool
Forgetting to include <stdbool.h> when using bool:
bool flag = true; // Error: 'bool' undeclaredInclude: #include <stdbool.h>
Using %d with bool in printf() but expecting true/false:
printf("%d", flag); // Outputs 1 or 0Use: printf("%s", flag ? "true" : "false");
void
Trying to declare a variable of type void:
void result; // Error: void is not a variable typeUse void only for functions that don't return values.
Remember: Always double-check your variable types and format specifiers when working with C. Small mistakes can cause big issues!
Related Posts
Derived Data Types
Explore how C's derived data types, such as arrays, pointers, functions are used to create complex data models.
Read MoreC Control Structures
Explore C control structures like if-else, switch, and loops to manage program flow.
Read MoreMastering C Operators
Learn how to use C arithmetic, relational, logical, and bitwise operators to perform calculations and make decisions in your code.
Read More