- Declare variables of different types and assign values to them
- Find out with
sizeofhow many bytes a type takes in memory - Create constants with
constand#define - Calculate correctly with integer division, the
%remainder and casts
A supermarket till has to remember how many items you bought, their prices and the total. Programs also keep information in variables. But C is strict: for every variable you must say in advance what its type is — whether it holds a whole number, a fractional number or a character. In return, C uses memory very economically and runs very fast.
Variables and basic types
A named place in memory with a type. The type decides which values can be stored there and how many bytes are reserved for it.
A variable is declared like this: first the type, then the name, optionally a starting value after =, and a ; at the end. Names may contain letters, digits and _, but cannot start with a digit. age and Age are two different variables. After the declaration you can assign a new value — without writing the type again.
#include <stdio.h>
int main(void) {
int apples = 7;
double price = 0.85;
char size = 'L';
printf("Apples: %d\n", apples);
printf("Price: %.2f AZN\n", price);
printf("Total: %.2f AZN\n", apples * price);
printf("Size: %c\n", size);
apples = apples + 3;
printf("Now: %d apples\n", apples);
return 0;
}Apples: 7 Price: 0.85 AZN Total: 5.95 AZN Size: L Now: 10 apples
| Type | What it stores | Typical size | printf |
|---|---|---|---|
int | a whole number, about −2.1 billion to +2.1 billion | 4 bytes | %d |
float | a fractional number, about 6–7 exact digits | 4 bytes | %f |
double | a fractional number, about 15–16 exact digits | 8 bytes | %f |
char | one character (really a small integer) | 1 byte | %c |
char is always 1 byte.For fractional numbers you usually choose double: it takes twice as much space as float but is much more precise. The sizeof operator tells you how many bytes a type takes. Its result has the special type size_t and is printed with %zu.
#include <stdio.h>
int main(void) {
printf("char: %zu byte\n", sizeof(char));
printf("int: %zu bytes\n", sizeof(int));
printf("float: %zu bytes\n", sizeof(float));
printf("double: %zu bytes\n", sizeof(double));
return 0;
}char: 1 byte int: 4 bytes float: 4 bytes double: 8 bytes
int has 32 bits.A char is really a small integer: the computer stores every character as a code. In the ASCII table the code of 'A' is 65 and the code of 'B' is 66. So if you print a character with %d you see its code, and if you add 1 to it you get the next letter.
#include <stdio.h>
int main(void) {
char letter = 'A';
printf("%c has code %d\n", letter, letter);
printf("Next letter: %c\n", letter + 1);
return 0;
}A has code 65 Next letter: B
Constants
Some values must never change while the program runs: the number π, the maximum score, a tax rate. There are two ways to make them constant. The const keyword makes a variable read-only: if you try to give it a new value, the compiler reports an error. #define is a preprocessor directive: before compiling, it simply replaces the name with the value in the text. Constant names are usually written in capital letters.
#include <stdio.h>
#define MAX_SCORE 100
int main(void) {
const double PI = 3.14159;
double r = 2.0;
int score = 87;
printf("Circle area: %.2f\n", PI * r * r);
printf("Missing points: %d\n", MAX_SCORE - score);
return 0;
}Circle area: 12.57 Missing points: 13
PI = 3.0;, the program will not compile.Arithmetic operators and integer division
C has the operators +, -, *, / and %. The most important detail: when you divide two integers, the result is an integer too — the fractional part is simply dropped, not rounded. The % operator gives the remainder of the division and works only with integers. If at least one operand is a fractional number, the division is done the normal way.
#include <stdio.h>
int main(void) {
int a = 17, b = 5;
printf("%d + %d = %d\n", a, b, a + b);
printf("%d - %d = %d\n", a, b, a - b);
printf("%d * %d = %d\n", a, b, a * b);
printf("%d / %d = %d\n", a, b, a / b);
printf("%d %% %d = %d\n", a, b, a % b);
printf("17.0 / 5 = %f\n", 17.0 / 5);
return 0;
}17 + 5 = 22 17 - 5 = 12 17 * 5 = 85 17 / 5 = 3 17 % 5 = 2 17.0 / 5 = 3.400000
Casts and shorthand operators
Suppose Murad wants to share 7 manats between 2 friends. 7 / 2 gives 3. To get the correct answer, you must turn one of the values into a double with a cast: (double) 7 / 2. Careful: if you write (double) (7 / 2), the integer division happens first and you get 3.0. In the other direction, (int) 9.99 drops the fractional part and gives 9.
#include <stdio.h>
int main(void) {
int total = 7, people = 2;
printf("%d\n", total / people);
printf("%.1f\n", (double) total / people);
printf("%.1f\n", (double) (total / people));
printf("%d\n", (int) 9.99);
int count = 10;
count += 5;
count++;
printf("%d\n", count);
return 0;
}3 3.5 3.0 9 16
count += 5 means count = count + 5, and count++ increases the variable by 1.Key points
- Every variable has a type:
intholds whole numbers,doubleandfloathold fractions,charholds one character. sizeofgives the size of a type in bytes and is printed with%zu;sizeof(char)is always 1.constmakes a variable unchangeable;#definereplaces a name with a value before compiling.- Dividing two integers gives an integer:
17 / 5= 3,17 % 5= 2. - For a fractional result, cast before dividing:
(double) a / b.
Check yourself
10 questions. Every correct answer earns XP.
int a = 17, b = 5;, what is a / b?