Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are primary data types in C language?
Primary data types, also known as fundamental data types, are built-in data types in C programming language. The C compiler supports four fundamental data types that serve as the building blocks for storing and working with different kinds of data −
- Integer − for whole numbers
- Character − for single characters
- Float − for decimal numbers
- Double − for high-precision decimal numbers
Syntax
// Declaration syntax for primary data types int variable_name; char variable_name; float variable_name; double variable_name;
Integer Data Type
Integer data types store whole numbers (positive or negative) without decimal points. They come in different sizes to accommodate various ranges of values −
| Type | Size (bytes) | Range | Format Specifier |
|---|---|---|---|
| short int | 2 | -32,768 to 32,767 | %hd |
| int | 4 | -2,147,483,648 to 2,147,483,647 | %d or %i |
| long int | 4/8 | -2,147,483,648 to 2,147,483,647 | %ld |
| unsigned int | 4 | 0 to 4,294,967,295 | %u |
Example
#include <stdio.h>
int main() {
int num = 100;
unsigned int uNum = 200;
short int sNum = -50;
long int lNum = 1000000;
printf("Integer: %d<br>", num);
printf("Unsigned Integer: %u<br>", uNum);
printf("Short Integer: %hd<br>", sNum);
printf("Long Integer: %ld<br>", lNum);
return 0;
}
Integer: 100 Unsigned Integer: 200 Short Integer: -50 Long Integer: 1000000
Character Data Type
Character data types store single characters and are internally represented as integers using ASCII values. Each character occupies 1 byte of memory −
| Type | Size (bytes) | Range | Format Specifier |
|---|---|---|---|
| char | 1 | -128 to 127 | %c |
| unsigned char | 1 | 0 to 255 | %c |
Example
#include <stdio.h>
int main() {
char ch = 'A';
unsigned char uCh = 'B';
printf("Character: %c<br>", ch);
printf("Unsigned Character: %c<br>", uCh);
printf("ASCII value of %c: %d<br>", ch, ch);
printf("ASCII value of %c: %d<br>", uCh, uCh);
return 0;
}
Character: A Unsigned Character: B ASCII value of A: 65 ASCII value of B: 66
Float Data Type
Float data types store decimal numbers with single precision. They are useful for calculations requiring fractional values −
| Type | Size (bytes) | Range | Precision | Format Specifier |
|---|---|---|---|---|
| float | 4 | 3.4E-38 to 3.4E+38 | 6-7 digits | %f |
Example
#include <stdio.h>
int main() {
float pi = 3.14159f;
float temperature = -12.5f;
printf("Pi value: %.2f<br>", pi);
printf("Temperature: %.1f°C<br>", temperature);
printf("Pi with 5 decimals: %.5f<br>", pi);
return 0;
}
Pi value: 3.14 Temperature: -12.5°C Pi with 5 decimals: 3.14159
Double Data Type
Double data types provide double precision for storing decimal numbers with higher accuracy than float −
| Type | Size (bytes) | Range | Precision | Format Specifier |
|---|---|---|---|---|
| double | 8 | 1.7E-308 to 1.7E+308 | 15-16 digits | %lf |
| long double | 16 | 3.4E-4932 to 1.1E+4932 | 19+ digits | %Lf |
Example
#include <stdio.h>
int main() {
double pi = 3.141592653589793;
double scientific = 1.23e-4;
printf("Pi (double): %.15lf<br>", pi);
printf("Scientific notation: %.6lf<br>", scientific);
printf("Comparison - float vs double:<br>");
printf("Float pi: %.7f<br>", (float)pi);
printf("Double pi: %.15lf<br>", pi);
return 0;
}
Pi (double): 3.141592653589793 Scientific notation: 0.000123 Comparison - float vs double: Float pi: 3.1415927 Double pi: 3.141592653589793
Conclusion
Primary data types in C provide the foundation for all programming operations. Choose int for whole numbers, char for characters, float for basic decimal calculations, and double for high-precision arithmetic. Understanding their memory requirements and ranges helps write efficient and accurate programs.
