Apply for Zend Framework Certification Training

c



< Constants in C Last >



Data Types in C
Data types are an important part of C programming. They tell the compiler what kind of data a variable will store and how much memory should be reserved for it.
For example, a variable may store a whole number, a decimal value, or a single character. The data type determines the kind of value that can be stored and the operations that can be performed on it.
Why Are Data Types Important?
Data types help the compiler to:
Allocate the required memory for a variable.
Decide what type of value can be stored.
Determine the valid operations on that value.
Handle data correctly during program execution.
Classification of Data Types in C
C data types can be mainly divided into the following categories:
Basic/Primitive Data Types
Derived Data Types
User-Defined Data Types
1. Primitive Data Types
Primitive data types are the fundamental data types provided by C. They are used to store simple values such as numbers, characters, and decimal values.
The commonly used primitive data types are:
int
char
float
double
void
Integer Data Type
The int data type is used to store whole numbers. It can hold positive numbers, negative numbers, and zero.
Example
#include
int main() {
    int number = 22;
    printf("Number = %d", number);
    return 0;
}
Output
Number = 22
Typical size: 4 bytes
Common range: -2,147,483,648 to 2,147,483,647
Format specifier: %d
The exact size and range of int can depend on the compiler and system.
Character Data Type
The char data type is used to store a single character.
Characters can include letters, digits, and special symbols.
Example
#include
int main() {
    char grade = 'A';
    printf("Grade = %c", grade);
    return 0;
}
Output
Grade = A
Typical size: 1 byte
Format specifier: %c
A character value is written inside single quotation marks, such as 'A', 'b', or '5'.
Float Data Type
The float data type is used for storing decimal or fractional numbers.
Example
#include
int main() {
    float price = 12.45f;
    printf("Price = %f", price);
    return 0;
}
Output
Price = 12.450000
Typical size: 4 bytes
Format specifier: %f
A float generally provides about 6–7 decimal digits of precision.
Double Data Type
The double data type is also used for decimal numbers. It normally provides more precision than float.
Example
#include
int main() {
    double value = 1.4521;
    printf("Value = %f", value);
    return 0;
}
Output
Value = 1.452100
Typical size: 8 bytes
For printf(), %f is normally used with a double.
For scanf(), %lf is used to read a double.
Void Data Type
The void type means "no value".
It is commonly used with functions that do not return a value.
Example
#include
void greet() {
    printf("Hello, welcome!\n");
}
int main() {
    greet();
    return 0;
}
Output
Hello, welcome!
void is also used with generic pointers such as void *.
2. Derived Data Types
Derived data types are formed using the basic data types. They allow us to work with collections of data, memory addresses, and functions.
Common derived types include:
Arrays
Pointers
Functions
Arrays
An array is used to store multiple values of the same data type.
The elements are stored in consecutive memory locations.
Example
int marks[5] = {80, 75, 90, 85, 88};
Here, marks can store five integer values.
Pointers
A pointer is a variable that stores the memory address of another variable.
Example
int x = 10;
int *ptr = &x;
Here:
x stores the value 10.
&x gives the address of x.
ptr stores that address.
Pointers are very important when working with arrays, functions, and dynamic memory.
Functions
A function is a block of code designed to perform a particular task. Functions can accept parameters and may return a value.
Example
int add(int a, int b) {
    return a + b;
}
This function accepts two integers and returns their sum.
3. User-Defined Data Types
C also allows programmers to create their own data types according to their requirements.
The commonly used user-defined types are:
Structure
Union
Enumeration (enum)
typedef
Structure
A structure allows us to combine different types of related data under one name.
Example
struct Student {
    char name[50];
    int age;
};
Here, Student can contain both a character array and an integer.
Union
A union is similar to a structure, but all its members use the same memory location.
Example
union Data {
    int number;
    float value;
};
In a union, only one member is normally used to store a meaningful value at a time.
Enumeration
An enumeration, or enum, is used to create a set of named integer constants.
Example
enum Day {
    MON,
    TUE,
    WED,
    THU,
    FRI,
    SAT,
    SUN
};
This makes programs easier to read because meaningful names can be used instead of numeric values.
Finding the Size of Data Types
The amount of memory occupied by a data type can vary depending on the compiler and computer architecture.
C provides the sizeof operator to determine the size of a data type or variable.
Example
#include
int main() {
    printf("Size of int: %zu bytes\n", sizeof(int));
    printf("Size of char: %zu byte\n", sizeof(char));
    printf("Size of float: %zu bytes\n", sizeof(float));
    printf("Size of double: %zu bytes\n", sizeof(double));
    return 0;
}
Sample Output
Size of int: 4 bytes
Size of char: 1 byte
Size of float: 4 bytes
Size of double: 8 bytes
The output may be different on another system because the size of some C data types depends on the implementation.
Data Type Modifiers
C provides several modifiers that can be used with certain data types to change their range or storage characteristics.
Common modifiers include:
short
long
signed
unsigned
Examples
short int a;
long int b;
signed int c;
unsigned int d;
For example, an unsigned int can store only zero and positive values, allowing it to represent a larger positive range than a signed integer of the same width.
Quick Summary
Category                                 Examples                                      Purpose
Primitive                                 int, char, float, double, void            Store basic values
Derived                                  Array, Pointer, Function                   Build more useful data structures and operations
User-defined                           struct, union, enum, typedef           Create customized data types

< Constants in C Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top