Home C C Data types tutorial with code examples

C Data types tutorial with code examples

In C programming, understanding data types is essential since they define the type of data that can be stored in a variable, as well as the memory allocated and the operations that can be performed.

C provides several basic data types, including integer types, floating-point types, characters, and more.

Here's a tutorial covering each data type with examples.

1. Basic Data Types in C

C has four basic data types:

  • int: Stores integer values (whole numbers).
  • float: Stores single-precision floating-point values (decimal numbers).
  • double: Stores double-precision floating-point values.
  • char: Stores single characters (like ‘a', ‘B', ‘1').

Each of these data types has variations and qualifiers that modify their behavior and memory usage.

2. Integer Data Types

Example of int, short, long, and long long

Integer data types store whole numbers and vary in size:

  • int: Usually 4 bytes on most systems (range: -2,147,483,648 to 2,147,483,647).
  • short: Typically 2 bytes, smaller range (range: -32,768 to 32,767).
  • long: At least 4 bytes, sometimes 8 bytes on 64-bit systems.
  • long long: Typically 8 bytes, supports very large integers.
#include <stdio.h>

int main() {
    int a = 100;
    short b = 200;
    long c = 30000;
    long long d = 5000000000;

    printf("int a: %d\n", a);
    printf("short b: %d\n", b);
    printf("long c: %ld\n", c);
    printf("long long d: %lld\n", d);

    return 0;
}

In this example:

  • int is a standard integer type.
  • short is a smaller integer, taking up less memory.
  • long and long long are used for larger integer values.

Example of Unsigned Integers

All integer types can also be unsigned, which means they only store non-negative values but have double the positive range.

#include <stdio.h>

int main() {
    unsigned int u = 4294967295; // Max value for unsigned int (4 bytes)

    printf("unsigned int u: %u\n", u);

    return 0;
}

Here, unsigned int can store values up to 4,294,967,295.

3. Floating-Point Data Types

Floating-point types represent real numbers with a fractional part.

  • float: Single-precision, typically 4 bytes (6-7 decimal places of precision).
  • double: Double-precision, typically 8 bytes (15-16 decimal places of precision).
  • long double: Extended precision, often 12 or 16 bytes, more precision than double.
#include <stdio.h>

int main() {
    float pi = 3.14159f;
    double e = 2.718281828459045;
    long double phi = 1.618033988749895L;

    printf("float pi: %.5f\n", pi);
    printf("double e: %.15f\n", e);
    printf("long double phi: %.15Lf\n", phi);

    return 0;
}

In this example:

  • float values are declared with a suffix f.
  • double can hold more precision than float.
  • long double offers even greater precision and is declared with an L suffix.

4. Character Data Type

The char type is used to store a single character (like ‘a' or ‘$') or small integers (ASCII values).

#include <stdio.h>

int main() {
    char letter = 'A';
    char number = 65; // ASCII value of 'A'

    printf("Character letter: %c\n", letter);
    printf("ASCII value of letter: %d\n", letter);
    printf("Character from ASCII code 65: %c\n", number);

    return 0;
}

In this example:

  • char can store both characters and their ASCII codes.
  • %c is used to print characters, while %d prints their integer ASCII values.

5. void Data Type

The void type represents the absence of any value. It is often used in functions that do not return a value or as a generic pointer.

#include <stdio.h>

void sayHello() {
    printf("Hello, World!\n");
}

int main() {
    sayHello();
    return 0;
}

Here, void is used to indicate that sayHello does not return a value.

6. Using Size Qualifiers with Data Types

In C, you can use size qualifiers with integer types to specify their range explicitly. Common qualifiers include short, long, and unsigned.

#include <stdio.h>

int main() {
    short int smallNum = 32767;   // Maximum value for short int
    long int largeNum = 1000000L; // Long integer
    unsigned int positiveNum = 4000000000U; // Unsigned integer

    printf("short int: %d\n", smallNum);
    printf("long int: %ld\n", largeNum);
    printf("unsigned int: %u\n", positiveNum);

    return 0;
}

In this example:

  • short int is smaller than the standard int.
  • long int allows larger values than int.
  • unsigned int does not store negative numbers but has a larger positive range.

7. Enumerated Data Type (enum)

An enum is a user-defined data type that can take one of a specified set of values. It is often used to represent a group of related constants.

#include <stdio.h>

enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY };

int main() {
    enum Day today = WEDNESDAY;

    printf("Today is day number: %d\n", today); // Output will be 3, as enum starts from 0 by default

    return 0;
}

In this example:

  • enum Day assigns integer values starting from 0 to each day.
  • today is assigned WEDNESDAY, so printf outputs 3.

8. typedef for Creating Aliases

typedef lets you create an alias for a data type, which can make code easier to read.

#include <stdio.h>

typedef unsigned int uint;

int main() {
    uint age = 25;
    printf("Age: %u\n", age);

    return 0;
}

Here, typedef creates uint as an alias for unsigned int, simplifying the code.

 

Summary Table of Basic Data Types in C

Data Type Description Size (Bytes) Range (Approximate)
int Integer 4 -2,147,483,648 to 2,147,483,647
short int Short integer 2 -32,768 to 32,767
long int Long integer 4 or 8 -2,147,483,648 to 2,147,483,647 (or more)
long long Long long integer 8 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned int Unsigned integer 4 0 to 4,294,967,295
float Single precision floating-point 4 ±3.4e-38 to ±3.4e+38
double Double precision floating-point 8 ±1.7e-308 to ±1.7e+308
long double Extended precision floating-point 12 or 16 Larger than double
char Character 1 -128 to 127 or 0 to 255 (unsigned)
void No storage (for functions and pointers) 0 N/A

This guide provides a foundational understanding of C data types

You may also like