020 Variables - en
020 Variables - en
Dimitrios Michail
0
4
8
12 byte
16
20
24
28 32-bit word
32
36
40
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
int main() {
int x1, x2, sum;
x1 = 1;
x2 = 2;
sum = x1 + x2;
}
In the program above we inform the compiler that we are going to require
the use of three memory locations in order to store integer numbers.
These 3 memory locations are going to be named x1, x2, and sum
respectively.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Memory
0
4
8
12
16
20
24
28
32
36
40
x1
x2
sum
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
int main() {
int x1, x2, sum;
x1 = 1;
x2 = 2;
sum = x1 + x2;
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
#include <stdio.h>
int main() {
int x1, x2, sum;
x1 = 1;
x2 = 2;
sum = x1 + x2;
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
0
4
8
12
16
20
24
28
32
36
40
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
Name
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
Type
There are more data types. We are going to see them in more detail after
we learn a few things about number representation in a computer.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
int main() {
int x, y, z; /* declare all variables */
x = 1;
y = 2;
z = x + y;
int i; /* ΝΟ! */
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Variables in C
From C99 and afterwards, we can also declare variable in other locations.
int main() {
int x, y, z; /* declare some variables */
x = 1;
y = 2;
z = x + y;
int i; /* YES! */
}
It is nowadays considered best practice to declare variables as close to
their use as possible.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Type int
Type int stores integers and its size depends on the architecture of the
computer that we are compiling our program.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Type char
int main() {
char c;
c = 'x';
}
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Type char
Decimal Hex Char Decimal Hex Char Decimal Hex Char Decimal Hex Char
#include <stdio.h>
int main() {
char c = 'a';
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Type float
#include <stdio.h>
int main() {
float pi = 3.14159265;
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Maximum and Minimum Values for Integer Types
Recent compilers have larger bounds. For example type int usually has
the bounds that are shown for long in the table above.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Types and Limits of Floating Point Numbers
Modern compilers have larger limits from the ones mentioned in the
ANSI C specification. For example, gcc on my computer has:
−1.175494E − 38 ≤ x ≤ 3.402823E + 38
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Function printf
The parameter format contains the text to be printed along with special
character sequences which help to print the expressions that follow.
These special sequences have the general form:
%[flags][width][.precision][length]specifier
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Specifiers of printf()
Line
printf("%d, %o and %x\n", 27, 27, 27);
prints 27, 33 and 1b.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
printf() and padding
The width is a number which tells printf how many spaces to add to
the output in order for the result to have the specified characters.
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 100;
int b = 1000;
int c = 10000;
printf("%7d\n", a);
printf("%7d\n", b);
printf("%7d\n", c);
printf("%7d\n", 1000000);
}
prints
100
1000
10000
1000000 .
.
.
.
.
. . . . .
. . . .
. . . .
. . . .
. . . .
. . . . .
.
.
.
.
.
.
.
.
.
printf() and precision
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
int and precision
Denotes the least amount of digits to be printed (possible by including
additional zeros a the beginning)
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 100;
int b = 10000;
int c = 1000000;
printf( "%10.7d\n", a );
printf( "%.7d\n", b );
printf( "%.7d\n", c );
return 0;
}
prints
0000100
0010000
1000000 .
.
.
.
.
. . . . .
. . . .
. . . .
. . . .
. . . .
. . . . .
.
.
.
.
.
.
.
.
.
float and precision
int main() {
float pi = 3.14159265;
printf( "%.3f\n", pi );
printf( "%.10f\n", pi );
return 0;
}
prints
3.142
3.1415927410
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
printf() and Special Characters
The special characters in C can be found in the following table:
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Constants
count = 3;
▶ symbolic constants
▶ declared constants using const
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Constants
There are several different kind of constants:
▶ literals
▶ symbolic constants: the pre-processor allows us to define symbols
which during the pre-processing phase are replaced with
corresponding values.
#include <stdio.h>
int main() {
int x = MAX;
printf( "%d\n", x );
return 0;
}
▶ declared constants using const
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
Constants
x = 5; /* Compilation error! */
}
If anyone tries to change the value, the compiler reports an error.
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .