For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
When you're learning C programming, it's natural to focus on writing logic and solving problems. But one foundational concept that quietly shapes everything you code is data types in C. Just like how data structures in C help organize data efficiently, data types decide how that data is represented and manipulated. Without a solid understanding of data types, even simple programs can behave unpredictably.
Imagine debugging a program only to realize a variable isn't holding what you expected because of a mismatch in type. It's a common frustration many beginners face. That's why understanding data types in C isn't just about syntax; it's about controlling your program's memory, precision, and performance. Let's explore what makes data types such a critical part of C programming.
Also explore Online Software Development Courses from top universities.
In C programming, data types specify the type of data a variable can store. They help the compiler understand how much memory to allocate and what kind of operations are valid for that data. Every variable, constant, and function in C is associated with a data type.
Boost your skills by enrolling in these top-rated programs:
The basic syntax to declare a variable with a data type in C is:
<data_type> <variable_name>;
For example:
int number;
float average;
char grade;
Explanation: Here, int, float, and char are data types that declare variables number, average, and grade respectively. Each tells the compiler to allocate memory and expect data of a specific type.
When using data types in C, follow these rules:
Breaking these rules can lead to warnings or runtime errors.
We use data types in C for several reasons:
Without data types, handling different kinds of data in C would be chaotic and error-prone.
Declaring data types in C is simple. You specify the data type followed by the variable name. You can declare multiple variables of the same type in one line by separating them with commas.
Example:
int age, year;
float price;
char grade;
Explanation: This declares age and year as integers, price as a float, and grade as a character.
You can also initialize variables at the time of declaration:
int age = 25;
float price = 99.99;
char grade = 'A';
Explanation: This allocates memory and assigns initial values to the variables.
To store fixed values that won’t change, you should use constants in C.
C offers four main categories of data types: primary (basic), derived, user-defined, and void. Each serves a different purpose depending on what kind of data you need to handle.
Primary data types include int, float, char, and double.
Example:
#include <stdio.h>
int main() {
int num = 10;
float pi = 3.14;
char letter = 'C';
printf("num = %d\n", num);
printf("pi = %.2f\n", pi);
printf("letter = %c\n", letter);
return 0;
}
Output:
num = 10
pi = 3.14
letter = C
Explanation: int holds an integer, float holds a decimal, and char holds a character. Each uses different memory and behaves differently in arithmetic or logical operations.
Derived data types are built from primary types. Examples include arrays, pointers, structures, and unions.
Example (array):
#include <stdio.h>
int main() {
int marks[3] = {85, 90, 78};
printf("First mark = %d\n", marks[0]);
return 0;
}
Output:
First mark = 85
Explanation: An array holds multiple values of the same data type. Here, marks is an array of three integers.
User-defined data types let programmers create their own data structures using struct, union, or typedef.
Example (structure):
#include <stdio.h>
struct Student {
int id;
char name[20];
};
int main() {
struct Student s1 = {1, "Vicky"};
printf("ID = %d\n", s1.id);
printf("Name = %s\n", s1.name);
return 0;
}
Output:
ID = 1
Name = Vicky
Explanation: struct groups different types under one name. Student holds an integer and a string together.
void indicates the absence of a data type. It's used for functions that return nothing or pointers to unknown types.
Example:
#include <stdio.h>
void greet() {
printf("Hello, World!\n");
}
int main() {
greet();
return 0;
}
Output:
Hello, World!
Explanation: greet returns nothing, so it uses void as its return type.
Take a look at storage classes in C.
Data types in C can be classified based on their functionality:
ALT Text : Image gives an overview of the Data types in C
#include <stdio.h>
struct Student {
int roll;
float marks;
};
int main() {
int num = 5;
float avg = 85.5;
struct Student s1 = {101, 92.3};
printf("num = %d, avg = %.1f, roll = %d, marks = %.1f\n", num, avg, s1.roll, s1.marks);
return 0;
}
Output:
num = 5, avg = 85.5, roll = 101, marks = 92.3
Explanation: This example shows usage of a primary type (int), a floating-point type (float), and a user-defined type (struct).
To convert variables between data types safely, check out type casting in C.
C allows modifying data types to extend or restrict their range. Modifiers include signed, unsigned, short, and long.
These modifiers apply to integer and character types to change their storage size or value range.
#include <stdio.h>
int main() {
unsigned int u = 4294967295;
long int l = 2147483647;
printf("Unsigned int = %u\n", u);
printf("Long int = %ld\n", l);
return 0;
}
Output:
Unsigned int = 4294967295
Long int = 2147483647
Explanation: unsigned int allows only positive numbers with a larger range. long int increases the maximum size of an integer.
For calling functions dynamically, you might want to understand function pointer in C.
Here's a mixed example showing different data types working together:
#include <stdio.h>
struct Book {
char title[30];
float price;
};
int main() {
int quantity = 3;
struct Book b1 = {"C Programming", 299.99};
float total = b1.price * quantity;
printf("Book: %s\n", b1.title);
printf("Price: %.2f\n", b1.price);
printf("Quantity: %d\n", quantity);
printf("Total: %.2f\n", total);
return 0;
}
Output:
Book: C Programming
Price: 299.99
Quantity: 3
Total: 899.97
Explanation: This combines a structure, a float, an integer, and a string to calculate the total cost.
You can also explore the tokens in C, which form the basic building blocks of any C program.
Understanding data types in C lays the groundwork for writing reliable, efficient programs. From managing memory to ensuring correct calculations, data types control every variable's behavior. By choosing the right type and modifier, you not only optimize your code but also avoid subtle bugs. Whether you're dealing with integers, floating points, characters, or custom structures, mastering data types makes you a better C programmer.
Using the wrong data type can lead to memory issues or incorrect output. Mismatched types can cause compiler errors or undefined behavior at runtime, resulting in unstable or erroneous program execution.
Yes, assigning a float to an int causes implicit conversion, truncating the decimal part. To control conversion, explicitly cast the value, using (int) to ensure the proper rounding or truncation behavior.
C does not assign a default data type. Each variable must be explicitly declared with a type. Failure to do so results in a compile-time error, ensuring precision and strict typing for all variables.
No, C lacks a built-in string type. Strings are represented as character arrays terminated by a null character ('\0'). String manipulation is done via arrays or pointers, often using standard library functions for convenience.
Data type sizes vary by system. Typically, int uses 4 bytes, char 1 byte, float 4 bytes, and double 8 bytes. Use the sizeof() operator to confirm exact sizes based on your system's architecture.
Yes, using different data types in C programs optimizes memory and logic. For example, integers for counts, floats for prices, and chars for characters help manage various kinds of data effectively within the same program.
The sizeof operator returns the memory size of a variable or data type in bytes. This is particularly useful for writing portable programs, where data type sizes may vary depending on the system architecture and compiler.
Yes, user-defined types like struct and union can be passed to functions. They can be passed by value or via pointers, allowing modification of the original data or managing memory more efficiently.
No, modifiers like short, long, signed, and unsigned are optional. They are used to modify the size or range of a data type based on specific needs, such as storing larger values or optimizing memory usage.
The scope of a variable in C depends on where it is declared, not its data type. Data types define how data is represented in memory and how operations are performed, while scope controls its visibility within the program.
Yes, a void* pointer in C can point to any data type. However, it must be cast to the correct type before dereferencing. This flexibility is useful for functions like malloc() and creating generic data structures.
Enumerated types, defined with the enum keyword, allow you to create named integer constants. They enhance code readability by replacing magic numbers with descriptive names, often used in switches, flags, or representing states.
Signed types can represent both positive and negative numbers, while unsigned types only store non-negative values. Unsigned types have a wider range, allowing for larger positive numbers, as in unsigned int versus int.
No, float and double differ in precision. A double has higher precision and a larger range, making it suitable for more accurate calculations, whereas float consumes less memory and is used for less precise calculations.
Choose a data type based on the kind of data and the range of values it will store. Use int for integers, float or double for floating-point values, and char for characters or small integers like ASCII codes.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author|900 articles published
Previous
Next
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.