Skip to content
C Programming – Datatypes, Modifiers and Variables

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:

  1. The type of data that can be stored.
  2. The amount of memory normally required.
  3. The operations that can be performed.
  4. The range of values that can be represented.
Example: An 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
Important: The exact memory size of some C datatypes is implementation-dependent. Do not assume that every compiler uses exactly the same sizes.

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;
Note: The size and range of 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';
Important: '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.

ANSI: American National Standards Institute.
Character ASCII Value
'A' 65
'B' 66
'a' 97
'0' 48
Remember: Standard ASCII contains 128 characters (0–127). Some systems historically use an extended 8-bit character set containing 256 possible values, but those additional 128 values are not part of standard ASCII.

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
Important: For 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;
}
Why %zu? 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:

short long signed unsigned

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
These ranges assume a 32-bit 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
Do not determine datatype size solely from whether the computer is “16-bit” or “64-bit”. C defines relationships and minimum ranges, while the compiler/platform determines the actual sizes. Use 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;
}
Recommended practice: Whenever practical, initialize variables when they are declared. This makes programs easier to understand and reduces the risk of accidentally using an uninitialized variable.

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;
}
Important: Reading an uninitialized automatic/local variable can result in undefined behavior. It is better to initialize the variable before using it.

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;
}
Remember: 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;
}
A variable with the same name may exist in a different nested scope, but that is a separate concept called scope and should be studied separately.

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

1. Which datatype is generally used to store whole numbers?
  1. float
  2. char
  3. int
  4. double

Answer: c) int

2. How many bits are there in one byte?
  1. 4
  2. 8
  3. 16
  4. 32

Answer: b) 8

3. Which operator is used to find the size of a datatype or object?
  1. size
  2. length
  3. sizeof
  4. memory

Answer: c) sizeof

4. Which modifier is used to represent only non-negative integer values?
  1. signed
  2. unsigned
  3. short
  4. long

Answer: b) unsigned

5. Which datatype stores a single character?
  1. int
  2. float
  3. double
  4. char

Answer: d) char

B. Fill in the Blanks

1. One byte contains ______ bits.
2. The C datatype used to store a single character is ______.
3. The operator used to determine memory size is ______.
4. The keyword used to prevent modification of a variable through its identifier is ______.
5. ASCII stands for ______.

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

  1. Write a C program to declare an int, float, double, and char variable and print their values.
  2. Write a program to display the size of char, int, float, and double using sizeof().
  3. Write a program that declares three integer variables and assigns the same value to all three using chained assignment.
  4. Write a program that declares three integer variables and initializes them with three different values.
  5. Write a program demonstrating the difference between int and unsigned int.
  6. Write a program using const and try to modify the constant. Observe the compiler error.
  7. Write a program that stores a character in a char variable and prints it using %c.
  8. Write a program that prints the ASCII value of a character.

20. Challenge Questions

1. What is the difference between 5 and '5' in C?
2. Why is sizeof(char) always equal to 1, even though a byte may represent 8 bits?
3. Why should you use %zu with the result of sizeof()?
4. Why can the exact size of int differ between C implementations?
5. What happens when you assign a new value to an already initialized variable?
6. Explain the difference between declaration, initialization, and assignment.

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.

Key rule: When you are unsure about the size of a C datatype on a particular system, use sizeof() instead of assuming its size.
C Programming Notes — Datatypes, Modifiers & Variables