0% found this document useful (0 votes)
26 views38 pages

Prog 3

Uploaded by

capiron22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views38 pages

Prog 3

Uploaded by

capiron22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Practical Programming

The C Language :
Common Programming
Concepts
David Bouchet
david.bouchet.epita@gmail.com
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

IEEE 754 Standard


float // 32 bits (single precision)
double // 64 bits (double precision)

3
Other Types

Similar to Unsigned Integers


size_t // 64 bits Used for size measurement
(e.g. sizes of arrays, array indexes)

Absence of Type
● Used as function return type when no

void return value is expected.


● Can be used as function parameter type

when no parameters are passed into the


function.
4
Variables

General syntax for declaration, definition and initialization


<type> <identifier> = <value>;

Examples

5
Constants

General syntax for declaration, definition and initialization


const <type> <identifier> = <value>;

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

The main() function is the entry point of the program.


It should return an ‘int’ value.
● If no error occurred → Should return 0

● If any error occurred → Should return a value different from 0

We can also use labels defined in <stdlib.h>:


● EXIT_SUCCESS

● 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
{
// ...
}

The else and else if statements are optional. 13


The for Statement

for (init; condition; post)


{
// ... n = 0
} n = 1
n = 2
(-3) * (-3) = 9
(-2) * (-2) = 4
(-1) * (-1) = 1
0 * 0 = 0
1 * 1 = 1
2 * 2 = 4
3 * 3 = 9

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

The break and continue statements can


be used in loop bodies
(e.g. for,
for while,
while do...while)
do...while

● 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

You might also like