structures, operators - basics of programming 1
TRANSCRIPT
![Page 1: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/1.jpg)
Structures Typedef Operators Type conversion
Structures, OperatorsBasics of Programming 1
DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
G. Horváth, A.B. Nagy, Z. Zsóka, P. Fiala, A. Vitéz
14 October, 2020
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 1 / 43
![Page 2: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/2.jpg)
Structures Typedef Operators Type conversion
Content DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 StructuresMotivationDefinitionAssignment of value
2 Typename-assignment
3 OperatorsDefinitionsOperatorsPrecedence
4 Type conversion
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 2 / 43
![Page 3: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/3.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Chapter 1
Structures
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 3 / 43
![Page 4: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/4.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
User defined types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Built-in types of C language sometimes are not appropriate forstoring more complex data.
Types introduced by the user (programmer)
EnumerationStructures
← today’s topic
BitfieldsUnion
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 4 / 43
![Page 5: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/5.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
User defined types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Built-in types of C language sometimes are not appropriate forstoring more complex data.
Types introduced by the user (programmer)
EnumerationStructures ← today’s topicBitfieldsUnion
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 4 / 43
![Page 6: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/6.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Storing date
1 int year;2 int month;3 int day;
Storing student data
1 char neptun [6];2 unsigned int smalltests;3 unsigned int missings;
Data of a chess game(white player, black player,when, where, moves, result)Data of one move(chess piece, from where, whereto)Data of one square of the board(column, row)
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 5 / 43
![Page 7: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/7.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Storing date
1 int year;2 int month;3 int day;
Storing student data
1 char neptun [6];2 unsigned int smalltests;3 unsigned int missings;
Data of a chess game(white player, black player,when, where, moves, result)Data of one move(chess piece, from where, whereto)Data of one square of the board(column, row)
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 5 / 43
![Page 8: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/8.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Storing date
1 int year;2 int month;3 int day;
Storing student data
1 char neptun [6];2 unsigned int smalltests;3 unsigned int missings;
Data of a chess game(white player, black player,when, where, moves, result)
Data of one move(chess piece, from where, whereto)Data of one square of the board(column, row)
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 5 / 43
![Page 9: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/9.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Storing date
1 int year;2 int month;3 int day;
Storing student data
1 char neptun [6];2 unsigned int smalltests;3 unsigned int missings;
Data of a chess game(white player, black player,when, where, moves, result)
Data of one move(chess piece, from where, whereto)Data of one square of the board(column, row)
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 5 / 43
![Page 10: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/10.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Storing date
1 int year;2 int month;3 int day;
Storing student data
1 char neptun [6];2 unsigned int smalltests;3 unsigned int missings;
Data of a chess game(white player, black player,when, where, moves, result)Data of one move(chess piece, from where, whereto)
Data of one square of the board(column, row)
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 5 / 43
![Page 11: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/11.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Storing date
1 int year;2 int month;3 int day;
Storing student data
1 char neptun [6];2 unsigned int smalltests;3 unsigned int missings;
Data of a chess game(white player, black player,when, where, moves, result)Data of one move(chess piece, from where, whereto)
Data of one square of the board(column, row)
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 5 / 43
![Page 12: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/12.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Storing date
1 int year;2 int month;3 int day;
Storing student data
1 char neptun [6];2 unsigned int smalltests;3 unsigned int missings;
Data of a chess game(white player, black player,when, where, moves, result)Data of one move(chess piece, from where, whereto)Data of one square of the board(column, row)
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 5 / 43
![Page 13: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/13.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Storing data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Let’s write a function to calculate scalar product (dot product)of 2D vectors!
1 double v_scalarproduct(double x1 , double y1,2 double x2 , double y2)3 {4 ...5 }
How shall we pass coupled parameters?The number of parameters may become too large
Let’s write a function to calculate difference of two vectors!1 ?????? v_difference(double x1 , double y1,2 double x2 , double y2)3 {4 ...5 }
How does the function returns with coupled data?
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 6 / 43
![Page 14: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/14.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Storing data elements that are coupled DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Let’s write a function to calculate scalar product (dot product)of 2D vectors!
1 double v_scalarproduct(double x1 , double y1,2 double x2 , double y2)3 {4 ...5 }
How shall we pass coupled parameters?The number of parameters may become too largeLet’s write a function to calculate difference of two vectors!
1 ?????? v_difference(double x1 , double y1,2 double x2 , double y2)3 {4 ...5 }
How does the function returns with coupled data?© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 6 / 43
![Page 15: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/15.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Encapsulation DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Structurecompound data type consisting of data elements (maybe ofdifferent types) that are coupled (belong together)
neptun
small test resultsmissings
student data elements are called fields or memberscan be copied with one assignmentcan be parameter of functioncan be return value of function
This is the most effective type of C language
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 7 / 43
![Page 16: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/16.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Structures in C DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 struct vector { /* definition of structure type */2 double x; double y;3 };4
5 struct vector v_difference(struct vector a,6 struct vector b) {7 struct vector c;8 c.x = a.x - b.x;9 c.y = a.y - b.y;
10 return c;11 }12
13 int main(void) {14 struct vector v1 , v2, v3;15 v1.x = 1.0; v1.y = 2.0;16 v2 = v1;17 v3 = v_difference(v1, v2);18 return 0;19 }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 8 / 43
![Page 17: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/17.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Declaration of structuresstruct [<structure label>]opt{<structure member declarations>}[<variable identifiers>]opt;
1 /* structure type for storing date */2 struct date {3 int year;4 int month;5 int day;6 } d1 , d2; /* two instances (variables) */
[<structure label>]optcan be omitted if we don’t refer to it later
[<variable identifiers>]optdeclaration of variables of structure type
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 9 / 43
![Page 18: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/18.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Declaration of structuresstruct [<structure label>]opt{<structure member declarations>}[<variable identifiers>]opt;
1 /* structure type for storing date */2 struct date {3 int year;4 int month;5 int day;6 } d1 , d2; /* two instances (variables) */
[<structure label>]optcan be omitted if we don’t refer to it later
[<variable identifiers>]optdeclaration of variables of structure type
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 9 / 43
![Page 19: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/19.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Declaration of structuresstruct [<structure label>]opt{<structure member declarations>}[<variable identifiers>]opt;
1 /* structure type for storing date */2 struct date {3 int year;4 int month;5 int day;6 } d1 , d2; /* two instances (variables) */
[<structure label>]optcan be omitted if we don’t refer to it later
[<variable identifiers>]optdeclaration of variables of structure type
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 9 / 43
![Page 20: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/20.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Declaration of structuresstruct [<structure label>]opt{<structure member declarations>}[<variable identifiers>]opt;
1 /* structure type for storing date */2 struct date {3 int year;4 int month;5 int day;6 } d1 , d2; /* two instances (variables) */
[<structure label>]optcan be omitted if we don’t refer to it later
[<variable identifiers>]optdeclaration of variables of structure type
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 9 / 43
![Page 21: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/21.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Declaration of structuresstruct [<structure label>]opt{<structure member declarations>}[<variable identifiers>]opt;
1 /* structure type for storing date */2 struct date {3 int year;4 int month;5 int day;6 } d1 , d2; /* two instances (variables) */
[<structure label>]optcan be omitted if we don’t refer to it later
[<variable identifiers>]optdeclaration of variables of structure type
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 9 / 43
![Page 22: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/22.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Declaration of structuresstruct [<structure label>]opt{<structure member declarations>}[<variable identifiers>]opt;
1 /* structure type for storing date */2 struct date {3 int year;4 int month;5 int day;6 } d1 , d2; /* two instances (variables) */
[<structure label>]optcan be omitted if we don’t refer to it later
[<variable identifiers>]optdeclaration of variables of structure type
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 9 / 43
![Page 23: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/23.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 24: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/24.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 25: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/25.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 26: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/26.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 27: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/27.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 28: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/28.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 29: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/29.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 30: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/30.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Syntax of structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Using structure typeDeclaration of variablesstruct <structure label> <variable identifiers>;
Accessing structure members<structure identifier>.<member identifier>
Structure members can be used in the same way as variables
1 struct date d1 , d2;2 d1.year = 2012;3 d2.year = d1.year;4 scanf("%d", &d2.month);
Initialization of structures is possible in the same way as forarrays:
1 struct date d3 = {2011 , 5, 2};
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 10 / 43
![Page 31: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/31.jpg)
Structures Typedef Operators Type conversion Motivation Definition Assignment of value
Assignment of value to structures DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Value of a structure variable (value of all members) can beupdated with one single assignment.
1 struct date d3 = {2013 , 10, 22}, d4;2 d4 = d3;
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 11 / 43
![Page 32: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/32.jpg)
Structures Typedef Operators Type conversion
Chapter 2
Typename-assignment
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 12 / 43
![Page 33: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/33.jpg)
Structures Typedef Operators Type conversion
Definition DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
We can rename types in C
1 typedef int rabbit;2
3 rabbit main() {4 rabbit i = 3;5 return i;6 }
Typename-assignment
typedef assigns a nickname to the type.It does not create a new type, the type of all variables createdwith the nicname will be the original type.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 13 / 43
![Page 34: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/34.jpg)
Structures Typedef Operators Type conversion
Definition DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
We can rename types in C
1 typedef int rabbit;2
3 rabbit main() {4 rabbit i = 3;5 return i;6 }
Typename-assignment
typedef assigns a nickname to the type.It does not create a new type, the type of all variables createdwith the nicname will be the original type.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 13 / 43
![Page 35: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/35.jpg)
Structures Typedef Operators Type conversion
What is the use of it? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
More meaningful source code, more easy to read
1 typedef double voltage;2
3 voltage V1 = 1.0;4 double c = 2.0;5 voltage V2 = c * V1;
Easy to maintain
We can get rid of typenames of more than one word
1 typedef struct vector vector;
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 14 / 43
![Page 36: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/36.jpg)
Structures Typedef Operators Type conversion
What is the use of it? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
More meaningful source code, more easy to read
1 typedef long double voltage; /* we need more accuracy */2
3 voltage V1 = 1.0;4 double c = 2.0;5 voltage V2 = c * V1;
Easy to maintain
We can get rid of typenames of more than one word
1 typedef struct vector vector;
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 14 / 43
![Page 37: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/37.jpg)
Structures Typedef Operators Type conversion
What is the use of it? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
More meaningful source code, more easy to read
1 typedef float voltage; /* we need a smaller */2
3 voltage V1 = 1.0;4 double c = 2.0;5 voltage V2 = c * V1;
Easy to maintain
We can get rid of typenames of more than one word
1 typedef struct vector vector;
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 14 / 43
![Page 38: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/38.jpg)
Structures Typedef Operators Type conversion
What is the use of it? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
More meaningful source code, more easy to read
1 typedef float voltage; /* we need a smaller */2
3 voltage V1 = 1.0;4 double c = 2.0;5 voltage V2 = c * V1;
Easy to maintain
We can get rid of typenames of more than one word
1 typedef struct vector vector;
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 14 / 43
![Page 39: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/39.jpg)
Structures Typedef Operators Type conversion
Vector example with typedef DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 struct vector { /* new structure type */2 double x; double y;3 };4 typedef struct vector vector; /* renaming */5
6 vector v_difference(vector a, vector b) {7 vector c;8 c.x = a.x - b.x;9 c.y = a.y - b.y;
10 return c;11 }12
13 int main(void) {14 vector v1, v2 , v3;15 v1.x = 1.0; v1.y = 2.0;16 v2 = v1;17 v3 = v_difference(v1, v2);18 return 0;19 }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 15 / 43
![Page 40: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/40.jpg)
Structures Typedef Operators Type conversion
Vector example with typedef DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 typedef struct vector { /* done in one step */2 double x; double y;3 } vector;4
5
6 vector v_difference(vector a, vector b) {7 vector c;8 c.x = a.x - b.x;9 c.y = a.y - b.y;
10 return c;11 }12
13 int main(void) {14 vector v1, v2 , v3;15 v1.x = 1.0; v1.y = 2.0;16 v2 = v1;17 v3 = v_difference(v1, v2);18 return 0;19 }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 15 / 43
![Page 41: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/41.jpg)
Structures Typedef Operators Type conversion
Vector example with typedef DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 typedef struct { /* we can omit the label */2 double x; double y;3 } vector;4
5
6 vector v_difference(vector a, vector b) {7 vector c;8 c.x = a.x - b.x;9 c.y = a.y - b.y;
10 return c;11 }12
13 int main(void) {14 vector v1, v2 , v3;15 v1.x = 1.0; v1.y = 2.0;16 v2 = v1;17 v3 = v_difference(v1, v2);18 return 0;19 }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 15 / 43
![Page 42: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/42.jpg)
Structures Typedef Operators Type conversion
A more complex structure DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 typedef struct {2 double x;3 double y;4 } vector;5
6 typedef struct {7 vector centrepoint;8 double radius;9 } circle;
1 circle k = {{3.0, 2.0}, 1.5};2 vector v = k.centrepoint;3 k.centrepoint.y = -2.0;
x
y
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 16 / 43
![Page 43: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/43.jpg)
Structures Typedef Operators Type conversion
A more complex structure DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 typedef struct {2 double x;3 double y;4 } vector;5
6 typedef struct {7 vector centrepoint;8 double radius;9 } circle;
1 circle k = {{3.0, 2.0}, 1.5};2 vector v = k.centrepoint;3 k.centrepoint.y = -2.0;
x
y
k
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 16 / 43
![Page 44: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/44.jpg)
Structures Typedef Operators Type conversion
A more complex structure DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 typedef struct {2 double x;3 double y;4 } vector;5
6 typedef struct {7 vector centrepoint;8 double radius;9 } circle;
1 circle k = {{3.0, 2.0}, 1.5};2 vector v = k.centrepoint;3 k.centrepoint.y = -2.0;
x
y
k v
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 16 / 43
![Page 45: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/45.jpg)
Structures Typedef Operators Type conversion
A more complex structure DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
1 typedef struct {2 double x;3 double y;4 } vector;5
6 typedef struct {7 vector centrepoint;8 double radius;9 } circle;
1 circle k = {{3.0, 2.0}, 1.5};2 vector v = k.centrepoint;3 k.centrepoint.y = -2.0;
x
y
k
v
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 16 / 43
![Page 46: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/46.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Chapter 3
Operators
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 17 / 43
![Page 47: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/47.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operations DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Denoted with operators (special symbols)They work with operandsThey result a data with type
Polymorphic: have different behaviour on different operandtypes
2.0 ↓ ↓ 3.0
0.6̇ ↓
/
2 ↓ ↓ 3
0 ↓
/
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 18 / 43
![Page 48: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/48.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operations DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Denoted with operators (special symbols)They work with operandsThey result a data with typePolymorphic: have different behaviour on different operandtypes
2.0 ↓ ↓ 3.0
0.6̇ ↓
/
2 ↓ ↓ 3
0 ↓
/
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 18 / 43
![Page 49: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/49.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expressions and operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Expressions
eg. 8 + 5 < -a - 2Built up of constants, variable references and operations
<
+
8 5
13
-
-
a (3)
-3
2
-5
FALSE
by evaluating them the result is one data element with type.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 19 / 43
![Page 50: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/50.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expressions and operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Expressionseg. 8 + 5 < -a - 2
Built up of constants, variable references and operations
<
+
8 5
13
-
-
a (3)
-3
2
-5
FALSE
by evaluating them the result is one data element with type.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 19 / 43
![Page 51: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/51.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expressions and operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Expressionseg. 8 + 5 < -a - 2Built up of constants, variable references and operations
<
+
8 5
13
-
-
a (3)
-3
2
-5
FALSE
by evaluating them the result is one data element with type.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 19 / 43
![Page 52: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/52.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expressions and operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Expressionseg. 8 + 5 < -a - 2Built up of constants, variable references and operations
<
+
8 5
13
-
-
a (3)
-3
2
-5
FALSE
by evaluating them the result is one data element with type.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 19 / 43
![Page 53: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/53.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operands
unary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operand
arithmeticrelationallogicalbitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 54: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/54.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-a
binary – with two operands1+2
Considering the interpretation of the operand
arithmeticrelationallogicalbitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 55: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/55.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operand
arithmeticrelationallogicalbitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 56: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/56.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operand
arithmeticrelationallogicalbitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 57: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/57.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operandarithmetic
relationallogicalbitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 58: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/58.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operandarithmeticrelational
logicalbitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 59: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/59.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operandarithmeticrelationallogical
bitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 60: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/60.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operandarithmeticrelationallogicalbitwise
misc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 61: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/61.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Types of operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Considering the number of operandsunary – with one operand-abinary – with two operands1+2
Considering the interpretation of the operandarithmeticrelationallogicalbitwisemisc
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 20 / 43
![Page 62: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/62.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Arithmetic operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
unary plus +<expression>unary minus -<expression>addition <expression> + <expression>
subtraction <expression> - <expression>
multiplication <expression> * <expression>
division <expression> / <expression>type of the result depends on type of the operands, ifboth are integer, then it is an integer divisionmodulus <expression> % <expression>
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 21 / 43
![Page 63: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/63.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
True or false – Boolean in C (repeated) DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Every boolean like result is int type, and its value is0, if false1, if true
1 printf("%d\t%d", 2<3, 2==3);
1 0
A value interpreted as boolean isfalse, if its value is represented with 0 bits onlytrue, if its value is represented with not only 0 bits
1 while (1) { /* infinite loop */ }2 while (-3.0) { /* infinite loop */ }3 while (0) { /* this here is never executed */ }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 22 / 43
![Page 64: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/64.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
True or false – Boolean in C (repeated) DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Every boolean like result is int type, and its value is0, if false1, if true
1 printf("%d\t%d", 2<3, 2==3);
1 0
A value interpreted as boolean isfalse, if its value is represented with 0 bits onlytrue, if its value is represented with not only 0 bits
1 while (1) { /* infinite loop */ }2 while (-3.0) { /* infinite loop */ }3 while (0) { /* this here is never executed */ }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 22 / 43
![Page 65: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/65.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Relational operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
relational operators
<left value> < <expression>
<left value> <= <expression>
<left value> > <expression>
<left value> >= <expression>
checking equality <left value> == <expression>
checking non-equality <left value> != <expression>
They give logical value (int, 0 or 1) as result.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 23 / 43
![Page 66: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/66.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Logical operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
logical NOT (complement) !<expression>
1 int a = 0x5c; /* 0101 1100, true */2 int b = !a; /* 0000 0000, false */3 int c = !b; /* 0000 0001, true */
Conlusion: !!a 6= a, only if we look at their boolean value.
1 int finish = 0;2 while (! finish) {3 int b;4 scanf("%d", &b);5 if (b == 0)6 finish = 1;7 }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 24 / 43
![Page 67: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/67.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Logical operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
logical NOT (complement) !<expression>
1 int a = 0x5c; /* 0101 1100, true */2 int b = !a; /* 0000 0000, false */3 int c = !b; /* 0000 0001, true */
Conlusion: !!a 6= a, only if we look at their boolean value.
1 int finish = 0;2 while (! finish) {3 int b;4 scanf("%d", &b);5 if (b == 0)6 finish = 1;7 }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 24 / 43
![Page 68: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/68.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Logical operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
logical AND <expression> && <expression>
logical OR <expression> || <expression>
Logical short-cut: Operands are evaluated from left to right. Butonly until the result is not obvious.We make use of this feature very often.
1 int a[5] = {1, 2, 3, 4, 5};2 int i = 0;3 while (i < 5 && a[i] < 20)4 i = i+1; /* no over -indexing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 25 / 43
![Page 69: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/69.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Logical operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
logical AND <expression> && <expression>
logical OR <expression> || <expression>Logical short-cut: Operands are evaluated from left to right. Butonly until the result is not obvious.
We make use of this feature very often.
1 int a[5] = {1, 2, 3, 4, 5};2 int i = 0;3 while (i < 5 && a[i] < 20)4 i = i+1; /* no over -indexing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 25 / 43
![Page 70: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/70.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Logical operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
logical AND <expression> && <expression>
logical OR <expression> || <expression>Logical short-cut: Operands are evaluated from left to right. Butonly until the result is not obvious.We make use of this feature very often.
1 int a[5] = {1, 2, 3, 4, 5};2 int i = 0;3 while (i < 5 && a[i] < 20)4 i = i+1; /* no over -indexing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 25 / 43
![Page 71: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/71.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Some more operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
We have used them so far, but never have called them operatorsbefore.
operation syntax
function call <function>(<actual arguments>)array reference <array>[<index>]structure-reference <structure>.<member>
1 c = sin (3.2); /* () */2 a[28] = 3; /* [] */3 v.x = 2.0; /* . */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 26 / 43
![Page 72: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/72.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operators with side effects DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Some operators have side effectsmain effect: calculating the result of evaluationside effect: the value of the operand is modified
Simple assignment operator =In C language, assignment is an expression!its side effect is the assignment (a ismodified)its main effect is the new value of a
=
a 2
2
Because of its main effect, this is also meaningful:
1 int a;2 int b = a = 2;
b is initialised with the value of expression a=2 (this also has aside effect), and the side effect of it is that a is also modified.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 27 / 43
![Page 73: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/73.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operators with side effects DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Some operators have side effectsmain effect: calculating the result of evaluationside effect: the value of the operand is modified
Simple assignment operator =In C language, assignment is an expression!its side effect is the assignment (a ismodified)its main effect is the new value of a
=
a 2
2
Because of its main effect, this is also meaningful:
1 int a;2 int b = a = 2;
b is initialised with the value of expression a=2 (this also has aside effect), and the side effect of it is that a is also modified.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 27 / 43
![Page 74: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/74.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operators with side effects DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Some operators have side effectsmain effect: calculating the result of evaluationside effect: the value of the operand is modified
Simple assignment operator =In C language, assignment is an expression!its side effect is the assignment (a ismodified)its main effect is the new value of a
=
a 2
2
Because of its main effect, this is also meaningful:
1 int a;2 int b = a = 2;
b is initialised with the value of expression a=2 (this also has aside effect), and the side effect of it is that a is also modified.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 27 / 43
![Page 75: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/75.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Left-value DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Assignement operator modifies value of the left side operand.There can be only ”modifiable entity” on the left side.
Left-value (lvalue)
An expression that can appear on the left side of the assignment.
As far as we know now, left-value can bea variable reference a = 2element of an array array[3] = 2member of a structure v.x = 2. . .
Examples for non-left-value expressionsconstant 3 = 2 errorarithmetic expression a+4 = 2 errorlogical expression a>3 = 2 errorfunction value sin(2.0) = 2 error
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 28 / 43
![Page 76: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/76.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Left-value DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Assignement operator modifies value of the left side operand.There can be only ”modifiable entity” on the left side.
Left-value (lvalue)
An expression that can appear on the left side of the assignment.
As far as we know now, left-value can bea variable reference a = 2element of an array array[3] = 2member of a structure v.x = 2. . .
Examples for non-left-value expressionsconstant 3 = 2 errorarithmetic expression a+4 = 2 errorlogical expression a>3 = 2 errorfunction value sin(2.0) = 2 error
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 28 / 43
![Page 77: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/77.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Left-value DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Assignement operator modifies value of the left side operand.There can be only ”modifiable entity” on the left side.
Left-value (lvalue)
An expression that can appear on the left side of the assignment.
As far as we know now, left-value can bea variable reference a = 2element of an array array[3] = 2member of a structure v.x = 2. . .
Examples for non-left-value expressionsconstant 3 = 2 errorarithmetic expression a+4 = 2 errorlogical expression a>3 = 2 errorfunction value sin(2.0) = 2 error
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 28 / 43
![Page 78: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/78.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Left-value DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Assignement operator modifies value of the left side operand.There can be only ”modifiable entity” on the left side.
Left-value (lvalue)
An expression that can appear on the left side of the assignment.
As far as we know now, left-value can bea variable reference a = 2element of an array array[3] = 2member of a structure v.x = 2. . .
Examples for non-left-value expressionsconstant 3 = 2 errorarithmetic expression a+4 = 2 errorlogical expression a>3 = 2 errorfunction value sin(2.0) = 2 error
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 28 / 43
![Page 79: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/79.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expression or statement? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
An operation that has side effect can be a statement in a program.
Expression statement
<Expression>;
Expression is evaluated, but the result is thrown away (but allside effects are completed).
1 a = 2 /* expression , its value is 2, it has side effect */
1 a = 2; /* statement , it has no value */2 /* generates a side effect */
As the main effect is surpressed, there is no sense of makingexpression statements if the expression has no side effect.
1 2 + 3; /* valid statement , it generates nothing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 29 / 43
![Page 80: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/80.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expression or statement? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
An operation that has side effect can be a statement in a program.
Expression statement
<Expression>;
Expression is evaluated, but the result is thrown away (but allside effects are completed).
1 a = 2 /* expression , its value is 2, it has side effect */
1 a = 2; /* statement , it has no value */2 /* generates a side effect */
As the main effect is surpressed, there is no sense of makingexpression statements if the expression has no side effect.
1 2 + 3; /* valid statement , it generates nothing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 29 / 43
![Page 81: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/81.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expression or statement? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
An operation that has side effect can be a statement in a program.
Expression statement
<Expression>;
Expression is evaluated, but the result is thrown away (but allside effects are completed).
1 a = 2 /* expression , its value is 2, it has side effect */
1 a = 2; /* statement , it has no value */2 /* generates a side effect */
As the main effect is surpressed, there is no sense of makingexpression statements if the expression has no side effect.
1 2 + 3; /* valid statement , it generates nothing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 29 / 43
![Page 82: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/82.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expression or statement? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
An operation that has side effect can be a statement in a program.
Expression statement
<Expression>;
Expression is evaluated, but the result is thrown away (but allside effects are completed).
1 a = 2 /* expression , its value is 2, it has side effect */
1 a = 2; /* statement , it has no value */2 /* generates a side effect */
As the main effect is surpressed, there is no sense of makingexpression statements if the expression has no side effect.
1 2 + 3; /* valid statement , it generates nothing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 29 / 43
![Page 83: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/83.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Expression or statement? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
An operation that has side effect can be a statement in a program.
Expression statement
<Expression>;
Expression is evaluated, but the result is thrown away (but allside effects are completed).
1 a = 2 /* expression , its value is 2, it has side effect */
1 a = 2; /* statement , it has no value */2 /* generates a side effect */
As the main effect is surpressed, there is no sense of makingexpression statements if the expression has no side effect.
1 2 + 3; /* valid statement , it generates nothing */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 29 / 43
![Page 84: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/84.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Assignement operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
expression syntax
compound assignment
<left-value> += <expression>
<left-value> -= <expression>
<left-value> *= <expression>
<left-value> /= <expression>
<left-value> %= <expression>
Almost: <left-value>=<left-value><op><expression>
1 a += 2; /* a = a + 2; */2 t[rand ()] += 2; /* NOT t[rand ()] = t[rand ()] + 2; */
Left-value is evaluated only once.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 30 / 43
![Page 85: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/85.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Assignement operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
expression syntax
compound assignment
<left-value> += <expression>
<left-value> -= <expression>
<left-value> *= <expression>
<left-value> /= <expression>
<left-value> %= <expression>
Almost: <left-value>=<left-value><op><expression>
1 a += 2; /* a = a + 2; */2 t[rand ()] += 2; /* NOT t[rand ()] = t[rand ()] + 2; */
Left-value is evaluated only once.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 30 / 43
![Page 86: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/86.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators with side effects DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
expression syntax
post increment <left-value> ++post decrement <left-value> --it is increased/decreased by one after evaluation
pre increment ++<left-value>pre decrement --<left-value>it is increased/decreased by one before evaluation
1 b = a++; /* b = a; a += 1; */2 b = ++a; /* a += 1; b = a; */
1 for (i = 0; i < 5; ++i) { /* five times */ }
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 31 / 43
![Page 87: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/87.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
modifying type(<type>)<expression>
(casting)
size for storage (in bytes) sizeof <expression>
the expression is not evaluated
1 int a1=2, a2=3, storagesize;2 double b;3 b = a1/(double)a2;4 storagesize = sizeof 3/a1;5 storagesize = sizeof(double)a1;6 storagesize = sizeof(double);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 32 / 43
![Page 88: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/88.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
modifying type(<type>)<expression>
(casting)
size for storage (in bytes) sizeof <expression>
the expression is not evaluated
1 int a1=2, a2=3, storagesize;2 double b;3 b = a1/(double)a2;4 storagesize = sizeof 3/a1;5 storagesize = sizeof(double)a1;6 storagesize = sizeof(double);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 32 / 43
![Page 89: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/89.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
modifying type(<type>)<expression>
(casting)
size for storage (in bytes) sizeof <expression>
the expression is not evaluated
1 int a1=2, a2=3, storagesize;2 double b;3 b = a1/(double)a2;4 storagesize = sizeof 3/a1;5 storagesize = sizeof(double)a1;6 storagesize = sizeof(double);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 32 / 43
![Page 90: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/90.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
modifying type(<type>)<expression>
(casting)
size for storage (in bytes) sizeof <expression>
the expression is not evaluated
1 int a1=2, a2=3, storagesize;2 double b;3 b = a1/(double)a2;4 storagesize = sizeof 3/a1;5 storagesize = sizeof(double)a1;6 storagesize = sizeof(double);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 32 / 43
![Page 91: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/91.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
modifying type(<type>)<expression>
(casting)
size for storage (in bytes) sizeof <expression>
the expression is not evaluated
1 int a1=2, a2=3, storagesize;2 double b;3 b = a1/(double)a2;4 storagesize = sizeof 3/a1;5 storagesize = sizeof(double)a1;6 storagesize = sizeof(double);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 32 / 43
![Page 92: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/92.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
modifying type(<type>)<expression>
(casting)
size for storage (in bytes) sizeof <expression>
the expression is not evaluated
1 int a1=2, a2=3, storagesize;2 double b;3 b = a1/(double)a2;4 storagesize = sizeof 3/a1;5 storagesize = sizeof(double)a1;6 storagesize = sizeof(double);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 32 / 43
![Page 93: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/93.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
comma <expression> , <expression>
Operands are evaluated from left to right.Value of first expression is thrown away.Value and type of the entire expression is the value and type ofthe second expression.
1 int step , j;2 /* two -digit numbers with increasing step size */3 for(step=1,j=10; j <100; j+=step , step ++)4 printf("%d\n", j);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 33 / 43
![Page 94: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/94.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
comma <expression> , <expression>
Operands are evaluated from left to right.Value of first expression is thrown away.Value and type of the entire expression is the value and type ofthe second expression.
1 int step , j;2 /* two -digit numbers with increasing step size */3 for(step=1,j=10; j <100; j+=step , step ++)4 printf("%d\n", j);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 33 / 43
![Page 95: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/95.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
comma <expression> , <expression>
Operands are evaluated from left to right.Value of first expression is thrown away.Value and type of the entire expression is the value and type ofthe second expression.
1 int step , j;2 /* two -digit numbers with increasing step size */3 for(step=1,j=10; j <100; j+=step , step ++)4 printf("%d\n", j);
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 33 / 43
![Page 96: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/96.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Other operators DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
operation syntax
(ternary) conditional expr. <cond.> ? <expr.1> : <expr.2>
if <cond.> is true, then <expr.1>, otherwise <expr.2>.only one of <expr.1> and <expr.2> is evaluated.It does not subtitute the if statement.
1 a = a < 0 ? -a : a; /* determining absolute value */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 34 / 43
![Page 97: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/97.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Features of operations performed on data DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
PrecedenceIf there are different operations, which is evaluated first?
1 int a = 2 + 3 * 4; /* 2 + (3 * 4) */
Associativity
If there are equivalent operations, which is evaluated first?(Does it bind from left to right or from right to left?)
1 int b = 11 - 8 - 2; /* (11 - 8) - 2 */
Instead of memorizing the rules, use parentheses!
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 35 / 43
![Page 98: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/98.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Features of operations performed on data DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
PrecedenceIf there are different operations, which is evaluated first?
1 int a = 2 + 3 * 4; /* 2 + (3 * 4) */
Associativity
If there are equivalent operations, which is evaluated first?(Does it bind from left to right or from right to left?)
1 int b = 11 - 8 - 2; /* (11 - 8) - 2 */
Instead of memorizing the rules, use parentheses!
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 35 / 43
![Page 99: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/99.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Features of operations performed on data DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
PrecedenceIf there are different operations, which is evaluated first?
1 int a = 2 + 3 * 4; /* 2 + (3 * 4) */
Associativity
If there are equivalent operations, which is evaluated first?(Does it bind from left to right or from right to left?)
1 int b = 11 - 8 - 2; /* (11 - 8) - 2 */
Instead of memorizing the rules, use parentheses!
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 35 / 43
![Page 100: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/100.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
List of operators in C DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Operateors are listed top to bottom, in descending precedence(operators in the same row have the same precedence)
1 () [] . -> /* highest */2 ! ~ ++ -- + - * & (<type >) sizeof3 * / %4 + -5 << >>6 < <= > >=7 == != /* forbidden to learn! */8 & /* use parentheses! */9 ^
10 |11 &&12 ||13 ?:14 = += -= *= /= %= &= ^= |= <<= >>=15 , /* lowest */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 36 / 43
![Page 101: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/101.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operators of C language DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
SummarizedA lot of effective operators
Some operators have side effects that will occur duringevaluationWe always try to separate main and side effectsInstead of this:
1 t[++i] = func(c-=2);
we rather write this:1 c -= 2; /* means the same */2 ++i; /* not less effective */3 t[i] = func(c); /* and I will understand it tomorrow too */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 37 / 43
![Page 102: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/102.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operators of C language DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
SummarizedA lot of effective operatorsSome operators have side effects that will occur duringevaluation
We always try to separate main and side effectsInstead of this:
1 t[++i] = func(c-=2);
we rather write this:1 c -= 2; /* means the same */2 ++i; /* not less effective */3 t[i] = func(c); /* and I will understand it tomorrow too */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 37 / 43
![Page 103: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/103.jpg)
Structures Typedef Operators Type conversion Definitions Operators Prec.
Operators of C language DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
SummarizedA lot of effective operatorsSome operators have side effects that will occur duringevaluationWe always try to separate main and side effectsInstead of this:
1 t[++i] = func(c-=2);
we rather write this:1 c -= 2; /* means the same */2 ++i; /* not less effective */3 t[i] = func(c); /* and I will understand it tomorrow too */
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 37 / 43
![Page 104: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/104.jpg)
Structures Typedef Operators Type conversion
Chapter 4
Type conversion
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 38 / 43
![Page 105: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/105.jpg)
Structures Typedef Operators Type conversion
What is that? DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
In some cases the C-program needs to convert the type of ourexpressions.
1 long func(float f) {2 return f;3 }4
5 int main(void) {6 int i = 2;7 short s = func(i);8 return 0;9 }
In this example: int → float → long → short
int → float rounding, if the number is largefloat → long may cause overflow, rounding to integerlong → short may cause overflow
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 39 / 43
![Page 106: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/106.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principle
preserve the value, if possibleIn case of overflow
the result is theoretically undefined
Conversion with one operand (we have seen that)
at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 107: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/107.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflow
the result is theoretically undefined
Conversion with one operand (we have seen that)
at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 108: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/108.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflow
the result is theoretically undefined
Conversion with one operand (we have seen that)
at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 109: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/109.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflowthe result is theoretically undefined
Conversion with one operand (we have seen that)
at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 110: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/110.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflowthe result is theoretically undefined
Conversion with one operand (we have seen that)
at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 111: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/111.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflowthe result is theoretically undefined
Conversion with one operand (we have seen that)at assignment of value
at calling a function (when actualising the formal parameters)Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 112: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/112.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflowthe result is theoretically undefined
Conversion with one operand (we have seen that)at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 113: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/113.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflowthe result is theoretically undefined
Conversion with one operand (we have seen that)at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )
evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 114: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/114.jpg)
Structures Typedef Operators Type conversion
Converting types DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Basic principlepreserve the value, if possible
In case of overflowthe result is theoretically undefined
Conversion with one operand (we have seen that)at assignment of valueat calling a function (when actualising the formal parameters)
Conversion with two operands (eg. 2/3.4 )evaluating an operation
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 40 / 43
![Page 115: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/115.jpg)
Structures Typedef Operators Type conversion
Conversion with two operands DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
The conversion of the two operands to the same, common typehappens according to these rules
operand one the other operand common, new type
long double anything long double
double anything double
float anything float
unsigned long anything unsigned long
long anything (int, unsigned) long
unsigned anything (int) unsigned
int anything (int) int
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 41 / 43
![Page 116: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/116.jpg)
Structures Typedef Operators Type conversion
Type conversions DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Example for conversion
1 int a = 3;2 double b = 2.4;3 a = a*b;
1 3 → 3.02 3.0 ∗ 2.4 → 7.23 7.2 → 7
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 42 / 43
![Page 117: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/117.jpg)
Structures Typedef Operators Type conversion
Type conversions DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Example for conversion
1 int a = 3;2 double b = 2.4;3 a = a*b;
1 3 → 3.0
2 3.0 ∗ 2.4 → 7.23 7.2 → 7
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 42 / 43
![Page 118: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/118.jpg)
Structures Typedef Operators Type conversion
Type conversions DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Example for conversion
1 int a = 3;2 double b = 2.4;3 a = a*b;
1 3 → 3.02 3.0 ∗ 2.4 → 7.2
3 7.2 → 7
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 42 / 43
![Page 119: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/119.jpg)
Structures Typedef Operators Type conversion
Type conversions DEPARTMENT OFNETWORKED SYSTEMSAND SERVICES
Example for conversion
1 int a = 3;2 double b = 2.4;3 a = a*b;
1 3 → 3.02 3.0 ∗ 2.4 → 7.23 7.2 → 7
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 42 / 43
![Page 120: Structures, Operators - Basics of Programming 1](https://reader031.vdocument.in/reader031/viewer/2022012101/61db912debb2a50fbb51be62/html5/thumbnails/120.jpg)
Structures Typedef Operators Type conversion
Thank you for your attention.
© based on slides by Zsóka, Fiala, Vitéz Structures, Operators 14 October, 2020 43 / 43