C Programming: Datatypes, Modifiers & Variables
Learn how C stores data, how much memory it uses, and how variables behave.
1. Datatypes and Their Properties
A datatype tells the C compiler what kind of data a variable is intended to store.
A datatype determines:
- The type of data that can be stored.
- The amount of memory normally required.
- The operations that can be performed.
- The range of values that can be represented.
int is normally used for whole numbers,
while a float is used for numbers containing
fractional or decimal parts.
2. Memory and Bytes
Computer memory is commonly measured in bytes. A byte consists of 8 bits.
| Unit | Relationship |
|---|---|
| 1 bit | 0 or 1 |
| 1 byte | 8 bits |
| 1 KB | 1024 bytes |
| 1 MB | 1024 KB |
| 1 GB | 1024 MB |
| 1 TB | 1024 GB |
3. Basic C Datatypes
| Datatype | Purpose | Typical Size | Example |
|---|---|---|---|
int |
Stores whole numbers | Usually 4 bytes | 45 |
float |
Stores single-precision floating-point numbers | Usually 4 bytes | 23.4567f |
double |
Stores double-precision floating-point numbers | Usually 8 bytes | 23.4567892345 |
char |
Stores a single character | 1 byte | 'A' |
3.1 int
The int datatype is used to store integer
(whole-number) values.
int age = 45;
int marks = 95;
int temperature = -10;
int depend on the C implementation.
On many modern systems, int is 4 bytes.
3.2 float
float is used for floating-point numbers,
which can contain a fractional part.
float price = 23.45f;
float temperature = 36.5f;
The suffix f indicates that the numeric literal
is a float.
3.3 double
double generally provides more precision than
float.
double pi = 3.141592653589793;
double distance = 12345.678901;
3.4 char
The char datatype stores a single character.
Character constants are written inside single quotes.
char grade = 'A';
char symbol = '$';
char digit = '5';
'5' is a character, whereas 5 is an integer.
They are not the same thing.
4. ASCII
ASCII stands for American Standard Code for Information Interchange.
ASCII assigns numeric codes to commonly used characters. Standard ASCII uses values from 0 to 127.
| Character | ASCII Value |
|---|---|
'A' |
65 |
'B' |
66 |
'a' |
97 |
'0' |
48 |
5. Example Using Different Datatypes
#include <stdio.h>
int main(void)
{
int x = 45;
float y = 2.34f;
double z = 48.369;
char ch = 'A';
printf("x = %d\n", x);
printf("y = %f\n", y);
printf("z = %f\n", z);
printf("ch = %c\n", ch);
return 0;
}
Common Format Specifiers
| Datatype | printf Format Specifier |
|---|---|
int |
%d |
float |
%f |
double |
%f |
char |
%c |
printf(), %f is used to print both
float and double values because
float arguments are promoted to double.
6. sizeof Operator
The sizeof operator is used to determine the amount
of memory, in bytes, occupied by a datatype or object.
Syntax:
sizeof(variable)
sizeof(datatype)
Example
#include <stdio.h>
int main(void)
{
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 byte\n", sizeof(char));
return 0;
}
sizeof returns a value of type size_t,
so %zu is the appropriate printf
format specifier.
7. Datatype Modifiers
C provides modifiers that can be used with certain datatypes to change their range or representation.
The commonly used modifiers are:
They are particularly important when working with integer types.
8. Signed vs Unsigned
signed
A signed integer can represent both positive and negative values.
signed int temperature = -20;
int marks = 90;
int is signed by default unless explicitly declared
as unsigned.
unsigned
An unsigned integer represents non-negative values (zero and positive values).
unsigned int count = 100;
unsigned int population = 500000;
For an integer type of a given width, using unsigned generally allows a larger maximum positive value because negative values are not represented.
| Type | Typical 32-bit Range |
|---|---|
int |
-2,147,483,648 to 2,147,483,647 |
unsigned int |
0 to 4,294,967,295 |
int. The C standard
specifies minimum ranges, but the exact range depends on the
implementation.
9. short vs long
short and long can be used to select
integer types with different minimum ranges and potentially
different sizes.
short int smallNumber;
long int largeNumber;
short smallNumber2;
long largeNumber2;
| Type | Minimum Standard Range |
|---|---|
short |
-32,767 to 32,767 |
long |
-2,147,483,647 to 2,147,483,647 |
sizeof() when you need to know the actual size.
Checking the Size
#include <stdio.h>
int main(void)
{
short int x;
printf("Memory allocated for x = %zu bytes\n", sizeof(x));
return 0;
}
10. Variables in C
A variable is a named memory location used to store a value that can change during program execution.
Declaration
A variable must be declared before it is used.
int x;
Assignment
A value can then be assigned to the variable.
x = 10;
Complete Example
#include <stdio.h>
int main(void)
{
printf("C Program\n");
int x;
x = 10;
printf("x = %d\n", x);
return 0;
}
11. Initialization and Assignment
Giving a variable its first value is called initialization.
int x;
x = 10; // initialization
After initialization, the variable can be assigned new values.
x = 20; // reassignment
x = 30; // reassignment
Example
#include <stdio.h>
int main(void)
{
int x;
x = 10;
printf("x = %d\n", x);
x = 20;
printf("x = %d\n", x);
x = 30;
printf("x = %d\n", x);
return 0;
}
12. Declaration with Initialization
A variable can be declared and initialized in the same statement.
int x = 10;
Example
#include <stdio.h>
int main(void)
{
int x = 10;
printf("x = %d\n", x);
return 0;
}
13. Uninitialized Variables
A local variable declared without an initial value does not automatically contain a useful value.
#include <stdio.h>
int main(void)
{
int x;
printf("x = %d\n", x); // Do NOT do this
return 0;
}
14. const Keyword
The const qualifier can be used when a variable
should not be modified through that identifier after initialization.
#include <stdio.h>
int main(void)
{
const int x = 4;
printf("x = %d\n", x);
// x = 12; // Error
return 0;
}
const means the object should not be modified
through that variable. It is useful for values that should
remain unchanged during a particular part of a program.
15. Redeclaring a Variable
A variable cannot be declared again with the same name in the same scope.
#include <stdio.h>
int main(void)
{
int x;
x = 12;
printf("x = %d\n", x);
// float x; // Error: x already declared in this scope
return 0;
}
16. Declaring Multiple Variables
Multiple variables of the same datatype can be declared in one statement using commas.
int a, b, c;
Assigning the Same Value
#include <stdio.h>
int main(void)
{
int a, b, c;
a = b = c = 10;
printf("a = %d\n", a);
printf("b = %d\n", b);
printf("c = %d\n", c);
return 0;
}
The expression is evaluated from right to left:
c = 10;
b = c;
a = b;
Therefore, all three variables finally contain 10.
17. Assigning Different Values
Multiple variables can also be declared and initialized with different values in one statement.
#include <stdio.h>
int main(void)
{
int x = 5, y = 10, z = 15;
printf("x = %d\n", x);
printf("y = %d\n", y);
printf("z = %d\n", z);
return 0;
}
18. Quick Revision
| Concept | Remember |
|---|---|
| Datatype | Defines the kind of data a variable can represent. |
| Byte | 1 byte = 8 bits. |
| int | Normally used for whole numbers. |
| float | Single-precision floating-point type. |
| double | Usually provides greater precision than float. |
| char | Stores a single character; sizeof(char) is always 1. |
| sizeof | Returns the size of an object/type in bytes. |
| signed | Allows negative and non-negative values for integer types. |
| unsigned | Represents non-negative values for integer types. |
| short / long | Integer type modifiers affecting minimum range and possibly size. |
| const | Prevents modification through that identifier. |
| Initialization | Giving a variable its initial value. |
| Assignment | Giving a variable a value, including changing an existing value. |
19. Practice Questions
A. Multiple Choice Questions
- float
- char
- int
- double
Answer: c) int
- 4
- 8
- 16
- 32
Answer: b) 8
- size
- length
- sizeof
- memory
Answer: c) sizeof
- signed
- unsigned
- short
- long
Answer: b) unsigned
- int
- float
- double
- char
Answer: d) char
B. Fill in the Blanks
C. Predict the Output
#include <stdio.h>
int main(void)
{
int x = 10;
printf("%d\n", x);
x = 20;
printf("%d\n", x);
return 0;
}
Question: What will be the output?
#include <stdio.h>
int main(void)
{
int a, b, c;
a = b = c = 25;
printf("%d %d %d\n", a, b, c);
return 0;
}
Question: What will be the output?
D. Find the Error
#include <stdio.h>
int main(void)
{
int x = 10;
float x = 20.5f;
printf("%d\n", x);
return 0;
}
Identify the error and explain why the program is invalid.
E. Programming Exercises
-
Write a C program to declare an
int,float,double, andcharvariable and print their values. -
Write a program to display the size of
char,int,float, anddoubleusingsizeof(). - Write a program that declares three integer variables and assigns the same value to all three using chained assignment.
- Write a program that declares three integer variables and initializes them with three different values.
-
Write a program demonstrating the difference between
intandunsigned int. -
Write a program using
constand try to modify the constant. Observe the compiler error. -
Write a program that stores a character in a
charvariable and prints it using%c. - Write a program that prints the ASCII value of a character.
20. Challenge Questions
5 and '5'
in C?
sizeof(char) always equal to 1,
even though a byte may represent 8 bits?
%zu with the result of
sizeof()?
int differ between
C implementations?
21. Final Summary
Datatypes are fundamental to C programming because they tell the compiler how data should be represented and manipulated. Variables provide named storage locations for that data.
The most commonly used basic datatypes are
int, float, double,
and char. Modifiers such as
signed, unsigned, short,
and long can be used with appropriate integer types.
The sizeof operator helps determine the actual
size of a datatype or object on the current system.
Variables should be initialized before they are used, and
const can be used when a value should not be modified
through a particular identifier.
sizeof() instead of assuming its size.