Professional Documents
Culture Documents
Curs 1
Curs 1
1st course
What we’ll go through
• Data types
• Preprocessing, compilation, linking
• In/out
• Expressions & Operators
Data types
• A data type defines:
• the set of values that data of that type can take
• how it is represented in memory
• All possible operations and their meaning
• int, bool, char, wchar_t
• Modifiers - short, long, long long, signed/unsigned
• Real: float, double (double accepts long modif)
• Implementation(compiler, architecture)-defined sizes, with a minimum guarantee
• Octal(042 == 34), decimal(42), hexa(0x42 == 66base10)
• Suffix: 1U (unsigned int), 1L (signed long int), 1UL(unsigned long int)
• https://en.cppreference.com/w/cpp/language/types
Preprocessing
• Takes place before compilation
• Is basically a search-replace of preprocessing directives
• #include, #define, #if, #ifdef, #else, #endif, #ifndef, #elif, #undef etc
Compilation
• Translation of source code to object code by a compiler
• Produces object files, which may contain undefined references
• Throws compilation errors
Linkage
• Produces the final output (library or executable)
• Replaces references to undefined symbols with the correct addresses (from
other object files or other libraries)
• Throws linker errors (undefined references, duplicate definitions)
Data in/out in C/C++
• C++: streams (bunch of characters that “flow” from keyboard to
memory or from the memory to the console)
• cin & cout (console input/output)
• <<, >>
• C: scanf, printf; both functions take as parameters format+arguments;
e.g: printf(“int: %d”, integerToBePrinted);
• d, o, u, x, f, c, s etc.
Expressions
• An expression is a succession of operand tied together by operators.
• Operands: constant, variable, function or another expression in paren
• Operators can be classified as 16 different classes according to priority
(1-16, with 1 being the most important)
• Arithmetic, increment/decrement, relational, equality, logical, bitwise
etc
• Unary or binary(are applied to 1 or 2 operands)
Arithmetic operators
• ‘+’, ‘-’ (unary – pos/neg) -> prio 2
• ‘*’, ‘/’, ‘%’ (binary – multiplication, division, remainder after division)
-> prio 4
• Notes: ‘/’ can be applied to both integers (returns the quotient) and
real numbers (returns the actual result); ‘%’ can only be applied to
integers and returns the remainder of the division
• ‘+’, ‘-’ (binary – addition and subtraction) -> prio 5
Increment/decrement operators
• ‘++’, ‘--’
• Unary
• Prefix or postfix
• Prefix: operand is incremented/decremented first, then used in the
rest of the expression
• Postfix: operand is used in the expression, and
incrementation/decrementation happens after.
• Can’t be applied to expressions
• Prio 2
Equality operators
• ‘<‘, ‘>’, ‘<=‘, ‘>=‘
• They return 1 if the operands are in the relation indicated by the
operator, and 0 otherwise
• Priority 7
Relational operators
• ‘==‘, ‘!=‘
• They return 1 if the operands are in the relation indicated by the operator,
and 0 otherwise
• Priority 8
Global logical operators
• ‘!’ (unary) -> prio 2
• ‘&&’ (binary) -> prio 12
• ‘||’ (binary) -> prio 13
Conditional operators
• ‘?’ and ‘:’
• Always used together
• Prio 14
Reference operator
• ‘&’ (unary, returns the memory address where a variable is located)
• Prio 2
Comma operator
• ‘,’
• Prio 16
a. binary;
b. octal;
c. hexa.
Practice
10. Using bitwise operators, solve the following:
12. Read a string from console and print to console only the first half of it.
13. Create a menu by reading an option from the user from the console:
Options:
0 - tell the user a joke;
1 - calculate the sum of two numbers - the numbers must be read from console;
2 - end the program.