🚀 C# ile Programlamaya Giriş: Kodlamaya İlk Adımını At, Kendi Projelerini Geliştirmeye Başla!
00HOME
07GLOSSARY 08TAGS ♡MY LIST ⌕SEARCH
TRENG

Variables and data types

Store values with int, float, double and char; measure type sizes with sizeof, see the integer division trap and meet the fixed-width types of stdint.h.

The value you read from a temperature sensor, the size of a resistor in ohms, or which measurement a counter is on: you need to keep these somewhere in your program. Those "places" are variables. In C, every variable has a type, and the type decides how many bytes the value takes in memory and how it is interpreted. Since you will work with a few kilobytes of RAM on a microcontroller, choosing types in C matters much more than you might think.

Basic types

#include <stdio.h>

int main(void) {
    int adc_degeri = 512;       // 10 bit ADC okumasi
    float sicaklik = 23.5f;     // derece C
    double direnc = 4700.0;     // ohm
    char birim = 'V';

    printf("ADC: %d\n", adc_degeri);
    printf("Sicaklik: %.1f C\n", sicaklik);
    printf("Direnc: %.0f ohm\n", direnc);
    printf("Birim: %c\n", birim);
    return 0;
}
ADC: 512
Sicaklik: 23.5 C
Direnc: 4700 ohm
Birim: V
  • int: An integer. For counters, ADC values and quantities.
  • float: A single-precision decimal number with about 6-7 significant digits. The trailing f (23.5f) marks the constant as a float; without it the constant is a double.
  • double: A double-precision decimal number with about 15-16 significant digits. Make it your default for calculations on a computer.
  • char: A single character or a small integer. Character constants go in single quotes.

Variable names start with a letter or an underscore and contain no spaces or special (e.g. Turkish) characters. Choose meaningful names like adc_degeri; the person reading the code six months from now will most likely be you.

sizeof: how many bytes is a type?

The sizeof operator gives the number of bytes a type or variable takes in memory. Its result is printed with the %zu specifier.

#include <stdio.h>

int main(void) {
    printf("char  : %zu bayt\n", sizeof(char));
    printf("int   : %zu bayt\n", sizeof(int));
    printf("float : %zu bayt\n", sizeof(float));
    printf("double: %zu bayt\n", sizeof(double));
    return 0;
}
char  : 1 bayt
int   : 4 bayt
float : 4 bayt
double: 8 bayt

This output is the same on almost every modern 64-bit computer. But the C standard only guarantees that char is 1 byte; the rest depends on the platform. For example, on the 8-bit AVR processor of an Arduino Uno, int is 2 bytes and holds at most 32767. long is 4 bytes on Windows and 8 bytes on Linux.

A very old MinGW version may not recognise %zu and print something odd. If that happens, write (int)sizeof(int) and print it with %d, or update your compiler.

The integer division trap: a voltage divider

When you connect two resistors in series and take the voltage from the middle, the output is Vout = Vin · R2 / (R1 + R2). Let's compute it in C for 12 V, R1 = 10 kΩ, R2 = 4.7 kΩ:

#include <stdio.h>

int main(void) {
    int vin = 12, r1 = 10000, r2 = 4700;

    int yanlis = vin * r2 / (r1 + r2);
    double dogru = (double)vin * r2 / (r1 + r2);

    printf("Tamsayi bolme : %d V\n", yanlis);
    printf("Ondalikli     : %.3f V\n", dogru);
    return 0;
}
Tamsayi bolme : 3 V
Ondalikli     : 3.837 V

When two integers are divided, the result is also an integer; the fractional part is truncated (not rounded). 56400 / 14700 gives 3 instead of 3.83. Writing (double)vin is a type conversion (cast): once the first number is a double, the operation is done in floating point. Assigning the result to a double variable is not enough on its own; the conversion has to happen before the division.

stdint.h: types with a known width

Because the size of int changes from platform to platform, embedded code uses the types in the <stdint.h> header. The name tells you the number of bits: uint8_t (0…255), int16_t (−32768…32767), uint32_t (0…4 294 967 295). The leading u means "unsigned".

#include <stdio.h>
#include <stdint.h>

int main(void) {
    uint8_t sayac = 250;
    int16_t sicaklik_x10 = -125;    // -12.5 C, 10 ile carpilmis
    uint32_t enerji_wh = 3600000;

    sayac = sayac + 10;             // 260 sigmaz!
    printf("sayac: %d\n", sayac);
    printf("sicaklik: %.1f C\n", sicaklik_x10 / 10.0);
    printf("enerji: %lu kWh\n", (unsigned long)(enerji_wh / 1000));
    return 0;
}
sayac: 4
sicaklik: -12.5 C
enerji: 3600 kWh

A uint8_t holds at most 255. When you try to store 260, the value "wraps around" modulo 256 and becomes 4. This behaviour is well defined for unsigned types, but the fact that the error is silent makes it dangerous. Keeping the temperature as an integer multiplied by 10 is a very common trick in embedded systems: on small processors without floating-point hardware it saves both time and memory.

Common mistakes

Not initialising: If you write int toplam; and use it right away, it may contain a random "garbage" value. Always give local variables a starting value: int toplam = 0;

  • Forgetting integer division: 5 / 2 is 2. If you want a decimal result, write one of the numbers as 5.0 or convert it with (double).
  • Ignoring overflow: On AVR, int x = 1023 * 100; does not fit in 16 bits. Use int32_t or uint32_t for large intermediate results.

Exercise

A 2000 W heater runs 3.5 hours a day. Assuming an electricity price of 2.5 TL/kWh, print the daily energy (kWh) and the cost with two decimal places.

Hint: energy (kWh) = power (W) × time (hours) / 1000. Keep power and time as double.

Solution

#include <stdio.h>

int main(void) {
    double guc_w = 2000.0;
    double sure_saat = 3.5;
    double birim_fiyat = 2.5;      // TL/kWh (varsayim)

    double enerji_kwh = guc_w * sure_saat / 1000.0;
    double maliyet = enerji_kwh * birim_fiyat;

    printf("Enerji: %.2f kWh\n", enerji_kwh);
    printf("Maliyet: %.2f TL\n", maliyet);
    return 0;
}
Enerji: 7.00 kWh
Maliyet: 17.50 TL

You've learned about variables, type sizes and the integer division trap. In the next lesson we'll read values from the user and look at printf's formatting options in detail.

// Spot the bug +10 XP

Exactly one line below is wrong. Click the faulty line.

// Mini quiz

Click an option; you will see right away, with the reason, whether it is correct or not.

1. In C, with int a = 7, b = 2; what is the value of a / b?
2. What range of values can a uint8_t variable hold?
3. According to the C standard, which type is guaranteed to be exactly 1 byte on every platform?

Mark it when you finish: your progress is saved and you earn +20 XP.

Tip: ← → keys let you move between lessons.