A data type tells the C compiler what kind of value a variable can store. It also determines how the value is interpreted and which operations can be performed on it.
char,
int, float, and double. C also provides
other types and type modifiers for different programming requirements.
A data type specifies the kind of data that a variable can represent. For example:
int age = 20;
Here, int tells the compiler that age is an integer
object.
Data types help the compiler understand:
C data types can be broadly grouped into:
void typeIn this lesson, we will focus mainly on fundamental data types and commonly used type modifiers.
The int type is used for integer values, such as positive numbers,
negative numbers, and zero.
int age = 20;
int marks = 85;
int temperature = -5;
The char type is used to store a single character.
char grade = 'A';
char choice = 'Y';
Character constants are normally written inside single quotation marks.
The float type is used for floating-point values.
float price = 99.50f;
float height = 5.8f;
The f suffix can be used to indicate a float
literal.
The double type represents floating-point values and generally
provides more precision than float.
double salary = 25000.75;
double pi = 3.1415926535;
The void type represents the absence of a value or type of
value in certain contexts.
It is commonly used with functions that do not return a value.
void display()
{
printf("Hello");
}
#include <stdio.h>
int main()
{
int age = 20;
printf("%d", age);
return 0;
}
The %d conversion specifier is commonly used to print an
int value.
#include <stdio.h>
int main()
{
char grade = 'A';
printf("%c", grade);
return 0;
}
The %c conversion specifier is used to print a character.
#include <stdio.h>
int main()
{
float price = 99.50f;
printf("%f", price);
return 0;
}
The %f conversion specifier can be used with floating-point
values in printf().
#include <stdio.h>
int main()
{
double salary = 25000.75;
printf("%.2f", salary);
return 0;
}
The precision .2 requests two digits after the decimal point
in this output.
An integer type can be signed, allowing values on both sides of zero.
signed int temperature = -10;
For integer types, signed is the default unless another
appropriate form is specified.
An unsigned integer type represents non-negative integer values.
unsigned int age = 25;
An unsigned integer does not represent negative values.
The short type modifier can be used with integer types when a
smaller integer range is appropriate.
short int number = 100;
The exact size and range depend on the implementation.
The long type modifier can be used when a larger integer range
may be required.
long int population = 1000000L;
The exact size of long depends on the C implementation.
The long long type provides an integer type with at least
64 bits of width in standard C implementations that support it.
long long population = 9000000000LL;
Common integer type modifiers include:
signedunsignedshortlongThey can be combined with compatible integer types to describe different ranges and representations.
The sizeof operator determines the size in bytes of a type or
object.
printf("%zu", sizeof(int));
The result is of type size_t.
The size of a data type can vary between systems. You can check it using
sizeof.
#include <stdio.h>
int main()
{
printf("int: %zu bytes\n", sizeof(int));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
printf("char: %zu bytes\n", sizeof(char));
return 0;
}
The C language guarantees that sizeof(char) is exactly
1.
printf("%zu", sizeof(char));
One byte in C is the size of a char; the number of bits in a
byte is implementation-defined.
| Data Type | Common Use | Example |
|---|---|---|
char |
Single character | 'A' |
int |
Integer value | 100 |
float |
Floating-point value | 10.5f |
double |
Higher-precision floating-point value | 10.5 |
void |
No value/type | void |
Choose a data type according to the kind of data your program needs to represent.
int age = 25;
float height = 5.8f;
double salary = 25000.75;
char grade = 'A';
Using an appropriate type makes the purpose of the variable clearer.
The general syntax for declaring a variable is:
data_type variable_name;
For example:
int age;
float price;
char grade;
double salary;
A program can use variables with different data types.
#include <stdio.h>
int main()
{
int age = 20;
float height = 5.8f;
double salary = 25000.75;
char grade = 'A';
printf("Age = %d\n", age);
printf("Height = %.1f\n", height);
printf("Salary = %.2f\n", salary);
printf("Grade = %c\n", grade);
return 0;
}
printf().'A' with a string such as
"A".A character and a string are different concepts.
char grade = 'A';
printf("A");
'A' is a character constant, while "A" is a
string literal.
Values of different arithmetic types can be converted during expressions. For example:
int a = 10;
double b = 2.5;
double result = a + b;
The integer value participates in the calculation after the appropriate conversion.
#include <stdio.h>
int main()
{
int age = 20;
unsigned int students = 50;
float height = 5.8f;
double salary = 25000.75;
char grade = 'A';
printf("Age = %d\n", age);
printf("Students = %u\n", students);
printf("Height = %.1f\n", height);
printf("Salary = %.2f\n", salary);
printf("Grade = %c\n", grade);
return 0;
}
Create a program that stores information about a student using appropriate data types.
#include <stdio.h>
int main()
{
int age = 18;
float percentage = 85.5f;
char grade = 'A';
printf("Age = %d\n", age);
printf("Percentage = %.1f\n", percentage);
printf("Grade = %c\n", grade);
return 0;
}
Try changing the values and observe the output.
char is commonly used for a single character.int is commonly used for integer values.float and double are floating-point types.void represents the absence of a value in certain contexts.signed, unsigned, short, and
long are commonly used type specifiers/modifiers.sizeof can be used to determine the size of a type or object.printf().Question: Which data type is commonly used to store a whole-number value in C?