Lesson 11 of 60 – Data Types in C
18%

Data Types in C

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.

Note: Common C data types include char, int, float, and double. C also provides other types and type modifiers for different programming requirements.

1. What is a Data Type?

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.

2. Why are Data Types Important?

Data types help the compiler understand:

  • What kind of value is being stored.
  • How an object can be used.
  • Which operations are appropriate.
  • How expressions involving the object are interpreted.

3. Main Categories of C Data Types

C data types can be broadly grouped into:

  • Basic or fundamental types
  • Derived types
  • User-defined types
  • The void type

In this lesson, we will focus mainly on fundamental data types and commonly used type modifiers.

4. Integer Type – int

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;

5. Character Type – char

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.

6. Floating-Point Type – float

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.

7. Double Type

The double type represents floating-point values and generally provides more precision than float.

double salary = 25000.75;
double pi = 3.1415926535;

8. void Type

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");
}

9. Integer Example

#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.

10. Character Example

#include <stdio.h>

int main()
{
    char grade = 'A';

    printf("%c", grade);

    return 0;
}

The %c conversion specifier is used to print a character.

11. Float Example

#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().

12. Double Example

#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.

13. Signed Integer

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.

14. Unsigned Integer

An unsigned integer type represents non-negative integer values.

unsigned int age = 25;

An unsigned integer does not represent negative values.

15. short Integer

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.

16. long Integer

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.

17. long long Integer

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;

18. Type Modifiers

Common integer type modifiers include:

  • signed
  • unsigned
  • short
  • long

They can be combined with compatible integer types to describe different ranges and representations.

19. sizeof Operator

The sizeof operator determines the size in bytes of a type or object.

printf("%zu", sizeof(int));

The result is of type size_t.

20. Checking Data Type Sizes

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;
}

21. char Size

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.

22. Data Type Comparison

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

23. Choosing the Right Data Type

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.

24. Data Type and Variable Declaration

The general syntax for declaring a variable is:

data_type variable_name;

For example:

int age;
float price;
char grade;
double salary;

25. Multiple Variables

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;
}

26. Common Data Type Mistakes

  • Using the wrong type for the required value.
  • Using an inappropriate format specifier with printf().
  • Assuming every C data type has the same size on every system.
  • Ignoring possible loss of information when converting between types.
  • Confusing a character such as 'A' with a string such as "A".

27. Character vs String

A character and a string are different concepts.

char grade = 'A';

printf("A");

'A' is a character constant, while "A" is a string literal.

28. Data Type Conversion

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.

29. Complete Data Types Example

#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;
}

30. Practice Program

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.

📌 Key Points

  • A data type tells C what kind of value an object can represent.
  • 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.
  • The exact size and range of many C types are implementation-dependent.
  • sizeof can be used to determine the size of a type or object.
  • Use the appropriate format specifier when displaying values with printf().

🧠 Quick Quiz

Question: Which data type is commonly used to store a whole-number value in C?