C - Data Types

Hello there, future programmers! Welcome to our exciting journey into the world of C programming. Today, we're going to explore the fascinating realm of data types in C. Don't worry if you're new to programming; I'll be your friendly guide, and we'll tackle this topic step by step. So, grab your favorite beverage, get comfortable, and let's dive in!

C - Data Types

Integer Data Types in C

Let's start with something we're all familiar with - whole numbers. In C, we call these integers, and they come in different sizes and flavors.

The Basic Integer Types

Here's a table of the most common integer types in C:

Data Type Size (bytes) Range
char 1 -128 to 127 or 0 to 255
short 2 -32,768 to 32,767
int 4 -2,147,483,648 to 2,147,483,647
long 4 or 8 -2,147,483,648 to 2,147,483,647 or -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Now, let's see these in action:

#include <stdio.h>

int main() {
    char my_char = 65;
    short my_short = 32000;
    int my_int = 2000000000;
    long my_long = 9000000000L;

    printf("char: %c\n", my_char);
    printf("short: %d\n", my_short);
    printf("int: %d\n", my_int);
    printf("long: %ld\n", my_long);

    return 0;
}

When you run this code, you'll see:

char: A
short: 32000
int: 2000000000
long: 9000000000

Let's break this down:

  • The char value 65 corresponds to 'A' in the ASCII table. Surprise! char can store numbers too!
  • short and int work as expected for medium-sized numbers.
  • For long, we add an 'L' at the end to tell C it's a long number.

Unsigned Integers

Sometimes, we only need positive numbers. That's where unsigned integers come in handy:

unsigned char my_uchar = 255;
unsigned short my_ushort = 65000;
unsigned int my_uint = 4000000000U;

printf("unsigned char: %u\n", my_uchar);
printf("unsigned short: %u\n", my_ushort);
printf("unsigned int: %u\n", my_uint);

Output:

unsigned char: 255
unsigned short: 65000
unsigned int: 4000000000

By using unsigned, we can store larger positive numbers in the same amount of memory. It's like magic, but it's just clever use of bits!

Floating-Point Data Types in C

Now, let's float into the world of decimal numbers. C provides three floating-point types:

Data Type Size (bytes) Precision
float 4 6-7 decimal digits
double 8 15-16 decimal digits
long double 16 19-20 decimal digits

Let's see them in action:

#include <stdio.h>

int main() {
    float pi_float = 3.14159265358979323846f;
    double pi_double = 3.14159265358979323846;
    long double pi_long_double = 3.14159265358979323846L;

    printf("float: %.7f\n", pi_float);
    printf("double: %.16f\n", pi_double);
    printf("long double: %.20Lf\n", pi_long_double);

    return 0;
}

Output:

float: 3.1415927
double: 3.1415926535897931
long double: 3.14159265358979323846

Notice how float loses precision after 7 digits, while double and long double maintain more accuracy. It's like zooming in on a digital photo - at some point, you start seeing pixels!

User-defined Data Types in C

C allows us to create our own data types. It's like being a chef and creating your own recipes!

Structures

Structures allow us to group related data together:

#include <stdio.h>
#include <string.h>

struct Student {
    char name[50];
    int age;
    float gpa;
};

int main() {
    struct Student alice;
    strcpy(alice.name, "Alice");
    alice.age = 20;
    alice.gpa = 3.8;

    printf("Name: %s\n", alice.name);
    printf("Age: %d\n", alice.age);
    printf("GPA: %.1f\n", alice.gpa);

    return 0;
}

Output:

Name: Alice
Age: 20
GPA: 3.8

Here, we've created a Student type that bundles a name, age, and GPA together. It's like creating a form for student information!

Enumerations

Enumerations are great for creating a set of named constants:

#include <stdio.h>

enum Days {SUN, MON, TUE, WED, THU, FRI, SAT};

int main() {
    enum Days today = WED;
    printf("Today is day number %d\n", today);
    return 0;
}

Output:

Today is day number 3

Enums automatically assign increasing integer values starting from 0. It's like giving nicknames to numbers!

The void Data Type in C

void is a special type in C. It's like a blank canvas - it represents the absence of data. We mainly use it in three scenarios:

  1. Function returns nothing:

    void sayHello() {
     printf("Hello, World!\n");
    }
  2. Function takes no parameters:

    int getRandomNumber(void) {
     return 4;  // chosen by fair dice roll. guaranteed to be random.
    }
  3. Generic pointers (we'll cover this in the pointers section)

Arrays Data Type in C

Arrays are like a line of pigeonholes, each holding a value of the same type:

#include <stdio.h>

int main() {
    int scores[5] = {85, 92, 78, 90, 88};

    printf("First score: %d\n", scores[0]);
    printf("Last score: %d\n", scores[4]);

    float average = 0;
    for(int i = 0; i < 5; i++) {
        average += scores[i];
    }
    average /= 5;

    printf("Average score: %.2f\n", average);

    return 0;
}

Output:

First score: 85
Last score: 88
Average score: 86.60

Remember, in C, array indices start at 0. It's like floor numbers in Europe - the ground floor is 0!

Pointers Data Type in C

Pointers are like signposts that point to locations in memory. They're powerful but can be tricky:

#include <stdio.h>

int main() {
    int x = 10;
    int *p = &x;

    printf("Value of x: %d\n", x);
    printf("Address of x: %p\n", (void*)&x);
    printf("Value of p: %p\n", (void*)p);
    printf("Value pointed by p: %d\n", *p);

    *p = 20;
    printf("New value of x: %d\n", x);

    return 0;
}

Output (addresses will vary):

Value of x: 10
Address of x: 0x7ffd5e8e3964
Value of p: 0x7ffd5e8e3964
Value pointed by p: 10
New value of x: 20

Here, p is a pointer that stores the address of x. We can use *p to access or modify the value at that address. It's like having a remote control for x!

And there you have it! We've covered the main data types in C. Remember, understanding data types is crucial because it helps you manage memory efficiently and avoid bugs. Keep practicing, and soon you'll be juggling these types like a pro! Happy coding!

Credits: Image by storyset