Prog 3
Prog 3
The C Language :
Common Programming
Concepts
David Bouchet
[email protected]
1
Integer Types
Signed Integers
(signed) char // 8 bits
(signed) short // 16 bits
(signed) int // 32 bits
(signed) long // 64 bits
(signed) long long // 64 bits
Unsigned Integers
unsigned char // 8 bits
unsigned short // 16 bits
unsigned int // 32 bits
unsigned long // 64 bits
unsigned long long // 64 bits
Sizes are given for 64-bit architectures (LP64 data model on Linux). 2
Floating-Point Types
3
Other Types
Absence of Type
● Used as function return type when no
Examples
5
Constants
Example
6
Enumerations
Declaration
enum <enum_name> Example
{
const_1;
const_2;
// ...
const_N;
}
enum <enum_name>
{
const_1 = 0;
const_2 = 15;
// ...
const_N = 3;
}
7
Functions
8
The main() Function
● EXIT_FAILURE
9
Formatting and Printing Data (1)
c = A
c = 65
c = 0x41
h = 100
i = 200
l = 300
f = 400.000000
d = 500.000000
string = hello
See printf(3)
10
Formatting and Printing Data (2)
c = A
c = 65
c = 0x41
h = 100
i = 200
l = 300
z = 400
See printf(3)
11
Conditions and Relational Operators
No Boolean Type!
Conditions use integers
● 0 is equivalent to FALSE
● ≠ 0 is equivalent to TRUE
a == 5 => 1
a != 5 => 0
a > 5 => 0
a >= b => 0
a < b => 1
a <= b => 1
!a => 0
!c => 1
12
The if, else if and else Statements
a is positive.
a is not null.
The condition is TRUE.
if (condition)
{
// ...
}
else if (condition)
{
// ...
}
else
{
// ...
}
14
The while Statement
while (condition)
{
// ... (-3) * (-3) = 9
} (-2) * (-2) = 4
(-1) * (-1) = 1
0 * 0 = 0
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9
15
The do...while Statement
do
{
// ... (-3) * (-3) = 9
} while (condition); (-2) * (-2) = 4
(-1) * (-1) = 1
0 * 0 = 0
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9
16
The break and continue Statements
● break:
break Terminates the loop.
● continue:
continue Goes to the next iteration.
17
The switch...case Statement
switch (value)
{
case const_1:
// ...
break;
case const_2:
// ...
break;
// etc.
default:
// ... a is not null.
} a is not one hundred.
18
Type Casting
i = 35
19
Type Casting
i = 35
f1 = 35.000000
20
Type Casting
i = 35
f1 = 35.000000
f2 = 35.000000
--------------
21
Type Casting
i = 35
f1 = 35.000000
f2 = 35.000000
--------------
f1 = 17.000000
22
Type Casting
i = 35
f1 = 35.000000
f2 = 35.000000
--------------
f1 = 17.000000
f2 = 17.000000
23
Type Casting
i = 35
f1 = 35.000000
f2 = 35.000000
--------------
f1 = 17.000000
f2 = 17.000000
f3 = 17.500000
24
Type Casting
i = 35
f1 = 35.000000
f2 = 35.000000
--------------
f1 = 17.000000
f2 = 17.000000
f3 = 17.500000
f4 = 17.500000
--------------
25
Type Casting
i = 35
f1 = 35.000000
f2 = 35.000000
--------------
f1 = 17.000000
f2 = 17.000000
f3 = 17.500000
f4 = 17.500000
--------------
f1 = 325.534210
i = 325
26
Overflow
27
Overflow
c = 1
28
Overflow
c = 1
uc = 1
---------
29
Overflow
c = 1
uc = 1
---------
c = -128
30
Overflow
c = 1
uc = 1
---------
c = -128
uc = 128
---------
31
Overflow
c = 1
uc = 1
---------
c = -128
uc = 128
---------
c = -1
32
Overflow
c = 1
uc = 1
---------
c = -128
uc = 128
---------
c = -1
uc = 255
---------
33
Overflow
c = 1
uc = 1
---------
c = -128
uc = 128
---------
c = -1
uc = 255
---------
c = -127
---------
34
Overflow
c = 1
uc = 1
---------
c = -128
uc = 128
---------
c = -1
uc = 255
---------
c = -127
---------
uc = 4
---------
35
Overflow
c = 1
uc = 1
---------
c = -128
uc = 128
---------
c = -1
uc = 255
---------
c = -127
---------
uc = 4
---------
uc = 255
36
Types Matter
facto_int(20) = -2102132736
facto_uint(20) = 2192834560
facto_ulong(20) = 2432902008176640000
37
Readable ?
38