Basic elements of the C language
Basics of Programming 1
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES
G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz
7. September, 2022
Contents
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES1 Introduction Contact Requirements
Recommended literature 2 C language basics
History
The first program Variables
Scanning data, inputting 3 Structured programming
Introduction
Definition
Elements of structured programs
Theorem of structured programming
The structogram
4 Structured programming in C Sequence
Selection control in C Top-test loop
Application
Chapter 1
Introduction
Contact
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESBME Faculty of Electrical Engineering and Informatics Department of Networked Systems and Services Gábor Horváth
email: horvath.gabor@vik.bme.hu Important webpages for the course:
The main webpage (slides, exercises, solutions):
http://www.hit.bme.hu/~ghorvath/bop/
The portal managing the assignments:
https://cprog.eet.bme.hu
Requirements
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES1 Laboratory practices
Active participation on at least 70% of the labs Entrance test at the beginning of the labs Max. number of absences/falied entrance tests: 4 Solutions to lab problems must be uploaded to CProg
2 Classroom practices
Participation on at least 70% of the practices 6 mid-term “small tests” in total (10 pts each) The sum of best 4 must be above 20 pts No re-take available
3 “Big test”
There will be 2 mid-term “big tests”
Option 1: sum of the scores≥50%
Option 2: the score of the second one≥50%
The one with lower score can be re-taken
4 Homework project
Milestones will be published, extra score if deadlines are met Face-to-face presentation in mandatory
Recommended literature
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESAny book about Standard C programming language in your own language
Chapter 2
C language basics
Short history of C programming language
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES1972: Start of development at AT&T Bell Labs Most of the UNIX kernel was created in C
1978: K&R C – Brian Kernigham, Dennis Ritchie:
The C Programming Language
1989: Standardization: ANSI X3.159-1989 1999: C99-standard:
new data types (complex) international character encoding arrays with variable sizes
. . .
2007–: C1X standard, 2011: C11 standard C++ compatibility
multi-thread programs . . .
Main features of C
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESCompiled language program.c source code
program.exe executable file compiler, linker
”small language”: few (10) instructions, a lot of (>50) operators
concise syntax (”zipped”)
hard to read (must pay attention) easy to make a mistake
hard to find a mistake
it gives a code that can be optimized efficiently and runs fast easy to implement for different platforms
The first C program
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESThe source code of the minimum-program
1 /* f i r s t . c - - The f i r s t p r o g r a m */
2
3 int m a i n ()
4 {
5 r e t u r n 0;
6 } link
The program starts, and after that it ends (finishes its run) between/* and */there are comments: messages for the programmer
int main()– All C programs starts like this
int Main()– and not like this. C is ”case sensitive”
{ } – block, it encloses the program body return 0; – It marks the end of the program
The first C program
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES. . . that actually does something
1 /* H e l l o w o r l d . c - - My f i r s t p r o g r a m */
2 # i n c l u d e < s t d i o . h > /* n e e d e d for p r i n t f */
3
4 /* The m a i n p r o g r a m */
5 int m a i n ()
6 {
7 p r i n t f (" H e l l o w o r l d !\ n "); /* P r i n t i n g */
8 r e t u r n 0;
9 } link
After compiling and running it gives the following output:
Hello world!
#include– to insert other C program parts printf– printing, \n– new line (line feed)
A more complicated one
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESInstructions in a sequence
1 /* f o o t b a l l . c - - f o o t b a l l f a n s */
2 # i n c l u d e < s t d i o . h >
3 int m a i n ()
4 {
5 p r i n t f (" Are you "); /* no new l i n e h e re */
6 p r i n t f (" b l i n d ?\ n "); /* h er e is new l i n e */
7 p r i n t f (" Go Bayern , go ! ");
8 r e t u r n 0;
9 } link
Are you blind?
Go Bayern, go!
Printing the value of a variable
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES1 # i n c l u d e < s t d i o . h >
2 int m a i n ()
3 {
4 int n ; /* d e c l a r i n g an i n t e g e r var . , c a l l e d n */
5 n = 2; /* n < - 2 a s s i g n e m e n t of v a l u e */
6 p r i n t f (" The v a l u e is : % d \ n ", n ); /* p r i n t i n g */
7 n = -5; /* n < - -5 a s s i g n e m e n t of v a l u e */
8 p r i n t f (" The v a l u e is : % d \ n ", n ); /* p r i n t i n g */
9 r e t u r n 0;
10 } link
The value is: 2 The value is: -5
int n – declaration of variable.
int (integer number) is the type,n is the identifier n = 2 – assignement of value, variablen takes value of expression ”2”
. . . continued
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES1 # i n c l u d e < s t d i o . h >
2 int m a i n ()
3 {
4 int n ; /* d e c l a r i n g an i n t e g e r var . , c a l l e d n */
5 n = 2; /* n < - 2 a s s i g n e m e n t of v a l u e */
6 p r i n t f (" The v a l u e is : % d \ n ", n ); /* p r i n t i n g */
7 n = -5; /* n < - -5 a s s i g n e m e n t of v a l u e */
8 p r i n t f (" The v a l u e is : % d \ n ", n ); /* p r i n t i n g */
9 r e t u r n 0;
10 } link
printf(<format>, <what>) –
printing the value of expression <what>in the given <format>
format
%d– decimal (decimal number system)
Block and declaration
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESStructure of the block {
<declarations>
<instructions>
}
1 {
2 /* d e c l a r a t i o n s */
3 int n ;
4
5 /* i n s t r u c t i o n s */
6 n = 2;
7 p r i n t f (" % d \ n ", n );
8 }
Block and declaration
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESStructure of declaration
<type name> <identifier> [ = <initial value>]opt;
1 int n ; /* not i n i t i a l i z e d */
2 int n u m b e r _ o f _ d o g s = 2; /* i n i t i a l i z e d */
value of n is garbage from memory at the beginning value of number_of_dogs is2 at the beginning
Inputting data
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICES1 /* s q u a r e . c - - s q u a r e of a n u m b e r */
2 # i n c l u d e < s t d i o . h >
3 int m a i n ()
4 {
5 int num ; /* d e c l a r i n g an i n t e g e r var . */
6 p r i n t f (" P l e a s e g i v e an i n t e g e r v a l u e : "); /* i n f o */
7 s c a n f (" % d ", & num ); /* i n p u t t i n g */
8 /* p r i n t i n g the v a l u e of 2 e x p r e s s i o n s */
9 p r i n t f (" The s q u a r e of % d is : % d \ n ", num , num * num );
10 r e t u r n 0;
11 } link
Please give an integer value: 8 The square of 8 is: 64
scanf(<format>, &<where to>)–
Inputting (scanning) data in<format> format and putting it into <where to>variable
Inputting data
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESThis is another option, that gives the same result.
1 # include< s t d i o . h >int m a i n (){int num ; p r i n t f
2 (" P l e a s e g i v e an i n t e g e r v a l u e : "); s c a n f (" % d ",
3 & num ); p r i n t f (" The s q u a r e of % d is : % d \ n ",
4 num , num * num );r e t u r n
5 0;} link
Not recommended, unmanagable
Chapter 3
Structured programming
Algorithms
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESFinding zeros of functions
We are searching the zeros of functionf(x), a monotonically increasing function, between pointsn andp, withϵaccuracy.
x f(x)
n k p
1 p - n < eps ?
2 IF TRUE , J U M P TO 10
3 k ← ( n + p ) / 2
4 f ( k ) < 0?
5 IF TRUE , J U M P TO 8
6 p ← k ;
7 J U M P TO 1
8 n ← k ;
9 J U M P TO 1
10 The z e r o is : n
Algorithms
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESFinding zeros – a different approach
We are searching the zeros of functionf(x), a monotonically increasing function, between pointsn andp, withϵaccuracy.
x f(x)
n k p
1 W H I L E p - n > eps , r e p e a t
2 k ← ( n + p ) / 2
3 IF f ( k ) > 0
4 p ← k ;
5 O T H E R W I S E
6 n ← k ;
7 The z e r o is : n
Structured vs unstructured
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESTwo programs of the same algorithm
1 W H I L E p - n > eps , r e p e a t
2 k ← ( n + p ) / 2
3 IF f ( k ) > 0
4 p ← k ;
5 O T H E R W I S E
6 n ← k ;
7 The z e r o is : n
1 p - n < eps ?
2 IF TRUE , J U M P TO 10
3 k ← ( n + p ) / 2
4 f ( k ) < 0?
5 IF TRUE , J U M P TO 8
6 p ← k ;
7 J U M P TO 1
8 n ← k ;
9 J U M P TO 1
10 The z e r o is : n
Structured program easy to maintain complex control higher level
Unstructured program spaghetti-code easy control
”hardware-level”
Structured vs unstructured
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESHardware level languages Lot of simple instructions
Easy control (JUMP;IF TRUE, JUMP) Unstructured layout
The processor can interpret only this Higher level languages
Rather few, but complex instructions More difficult control (WHILE...REPEAT...;
IF...THEN...ELSE...) Structured layout
The processor is unable to interpret it.
The compiler transforms a high level structured program into a hardware level program, that is equivalent to the original one.
We create a high level structured program, we use the compiler to translate it,
and we execute the hardware level code.
Elements of structured programs
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESAll structured programs follow this simple scheme:
START Operation
STOP
The structure of the program is determined by the inner structure (layout)of Operation.
Operation can be:
Elementary operation (action) Sequence
Loop or repetition Selection
Elements of structured programs
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESElementary operation
that cannot be further expanded
Operation Elementary
op. E l e m e n t a r y op .
The empty operation (don’t do anything) is also an elementary operation
Elements of structured programs
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESSequence
Execution of two operations after eachother, in the given order
Operation
Op. 1 Op. 2
Op . 1 Op . 2
Elements of structured programs
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESEach element of the sequence itself is an operation, so they can be expanded into a sequence
Op. 1 Op. 2
Op. 2a Op. 2b Op. 1
The expansion can be continued, so a sequence can be an arbitrary long (finite) series of operations.
Elements of structured programs
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESCondition-based selection
Execution of one of two operations, depending on the logical value of a condition (true or false)
Operation
Condition Op.
if true
Op.
if false
T F
IF C o n d i t i o n Op . if t r u e E L S E
Op . if f a l s e
Elements of structured programs
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESOne of the branches can also be empty.
Operation
Condition Op. if true
T
F
IF C o n d i t i o n Op . if t r u e
Elements of structured programs
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESTop-test loop
Repetition of an operation as long as a condition is true.
Operation
Condition
Body T
F
W H I L E C o n d i t i o n B o d y of l o o p
Elements of structured programming
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESTheorem of structured programming By using only
elementary operation, sequence,
selection, and loop
ALLalgorithms can be constructed.
The structogram
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESThe flowchart
a tool for describing unstrutured programs
can ba translated (compiled) into an unstructured program immediately (IF TRUE, JUMP)
structued elements (esp. loops) are hard to recognize within it The structogram
a tool for representing structured programs only a structured program can be represented by it it is easily translated into a structured program
The structogram
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESThe program is a rectangle
Operation
it can be expanded into more rectangles with the elements below
Sequence Op. 1 Op. 2
Top-test loop Condition Body of the loop Selection
AA
Condition
Op. if true Op. if false
The structogram
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESFinding zeros – flowchart, structogram, structured pseudo-code
p ← k n ← x
f(k)>0
T F
k ← n+p2 p−n> ϵ
T START
STOP F
p−n> ϵ k← n+p2
AA
f(k) > 0 p←k n←k
1 W H I L E p - n > eps , r e p e a t
2 k ← ( n + p ) / 2
3 IF f ( k ) > 0
4 p ← k ;
5 O T H E R W I S E
6 n ← k ;
Chapter 4
Structured programming in C
Sequence in C
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESForming a sequence is listing instructions one after eachother
1 /* f o o t b a l l . c - - f o o t b a l l f a n s */
2 # i n c l u d e < s t d i o . h >
3 int m a i n ()
4 {
5 p r i n t f (" Are you "); /* no new l i n e h e re */
6 p r i n t f (" b l i n d ?\ n "); /* h er e is new l i n e */
7 p r i n t f (" Go Bayern , go ! ");
8 r e t u r n 0;
9 } link
Are you blind?
Go Bayern, go!
Selection control in C – the if statement
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESLet’s write a program, that decides if the inputted integer number is small (<10) or big (≥10)!
OUT: info IN:x AA
x < 10 OUT:small OUT: big Let x be an i n t e g e r OUT : i n f o
IN : x IF x < 10
OUT : s m a l l O T H E R W I S E
OUT : big
1 # i n c l u d e < s t d i o . h >
2 int m a i n ()
3 {
4 int x ;
5 p r i n t f (" P l e a s e e n t e r a n u m b e r : ");
6 s c a n f (" % d ", & x );
7 if ( x < 10) /* c o n d i t i o n */
8 p r i n t f (" s m a l l "); /* t r u e b r a n c h */
9 e l s e
10 p r i n t f (" big "); /* f a l s e b r a n c h */
11 r e t u r n 0;
12 } link
Please give an integer number: 5 small
Selection control – the if statement
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESSyntax of theif statement
if (<condition expression>) <statement if true>
[ else <statement if false> ]opt
1 if ( x < 10 ) /* c o n d i t i o n */
2 p r i n t f (" s m a l l "); /* t r u e b r a n c h */
3 e l s e
4 p r i n t f (" big "); /* f a l s e b r a n c h */
1 if ( a < 0) /* c r e a t i n g a b s o l u t e v a l u e */
2 a = - a ;
3 /* no f a l s e b r a n c h */
Top-test loop in C – the while statement
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESLet’s print the square of the integer numbers between 1 and 10!
n←1 n≤10
OUT:n·n n←n+1
Let n be an i n t e g e r
n ← 1
W H I L E n <= 10 OUT : n * n n ← n +1
1 # i n c l u d e < s t d i o . h >
2 int m a i n ()
3 {
4 int n ;
5 n = 1; /* i n i t i a l i z a t i o n */
6 w h i l e ( n <= 10) /* c o n d i t i o n */
7 {
8 p r i n t f (" % d ", n * n );/* p r i n t i n g */
9 n = n +1;
/* i n c r e m e n t */
10 }
11 r e t u r n 0;
12 } link
1 4 9 16 25 36 49 64 81 100
Top-testing loop – the while statement
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESSyntax of thewhile statement
while (<condition expression>) <instruction>
If <instruction> is a sequence, we enclose it in a{block}:
1 w h i l e ( n <= 10 )
2 {
3 p r i n t f (" % d ", n * n );
4 n = n +1;
5 }
In language C an instruction always can be replaced with a block.
A complex application
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESBy using sequence, loop and selection, we can construct everything!
We know enough to construct the algorithm of finding the zeros in C!
A new element: a type for storing real numbers is called doubletype (to be learned later)
1 d o u b l e a ; /* the r e al n u m b e r */
2 a = 2 . 0 ; /* a s s i g n e m e n t of v a l u e */
3 p r i n t f (" % f ", a ); /* p r i n t i n g */
Finding zero of a function
DEPARTMENT OF NETWORKED SYSTEMS AND SERVICESWe are searching the zeros of function f(x) =x2−2, between points n=0 and p =2, with ϵ=0,001 accuracy.
n←0 p←2 p−n> ϵ
k← n+p2
AA
k2−2>0 p←k n←k
OUT:n
1 # i n c l u d e < s t d i o . h >
2
3 int m a i n ()
4 {
5 d o u b l e n = 0.0 , p = 2 . 0 ;
6 w h i l e ( p - n > 0 . 0 0 1 )
7 {
8 d o u b l e k = ( n + p ) / 2 . 0 ;
9 if ( k * k -2 . 0 > 0 . 0 )
10 p = k ;
11 e l s e
12 n = k ;
13 }
14 p r i n t f (" The z e r o is : % f ", n );
15
16 r e t u r n 0;
17 } link
Thank you for your attention.