Sie sind auf Seite 1von 23

C/C++

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

Global logical operators


• ‘~’ (unary, bitwise not) -> prio 2
• ‘<<‘, ‘>>’ (binary, left&right shift) -> prio 6
• ‘&’ (binary, conjunction) -> prio 9
• ‘^’ (binary, exclusive disjunction) -> prio 10
• ‘|’ (binary, disjunction) -> prio 11
Assignment operator
• 1 + 15: ‘=‘, ‘+=‘, ‘-=‘, etc
• Prio 15 for all of them

Conditional operators
• ‘?’ and ‘:’
• Always used together
• Prio 14
Reference operator
• ‘&’ (unary, returns the memory address where a variable is located)
• Prio 2

Explicit cast operator


• Allows explicit (forced) conversion of a type to another one
• Prio 2
Sizeof operator
• ‘sizeof’ (unary)
• Prio 2

Comma operator
• ‘,’
• Prio 16

Scope resolution operator


• ‘::’
• Prio 1
Expression evaluation
• First anything between parentheses, starting with innermost
parentheses
• According to operator prio
• If equal prios, according to operator associativity (left->right except
for unary, conditional and assignment, which are right->left)
• Implicit casts if not all operands have the same type: the operand
with the type that has smaller set of possible values is converted to
the type that has the larger set of values
Practice
• 1. What is the value of a after running the following program:
int a = 13, b=4;
b = a + b/2;
a = a – b/2*a;
2. Which of the following expressions are true if and only if x is an odd negative
number?
a. (x % 2 == 1) && (x<0) d. !((x%2==0) && (x>=0))
b. (x % 2 != 0) || (x<0) e. x%2=1 && x<0
c. !((x%2 == 0) || (x>=0))
Practice
3. unsigned char x = 250, z=x+7, a=‘8’;
What does the following expression evaluate to? z|(a-’0’)
a. Syntax error
b. 1
c. 265
d. True
e. 0
f. 9
Practice
4. Let x,y and z be 3 integers. Which of the following expressions has a
value different from 0 if and only if y = max(x,y,z)?
a. x>z?y>=x?1:0:y>=z?1:0
b. !(y<x || y<z)
c. !(y<x && y<z)
d. x>z && y>x || z>x && y>z
e. y>x && y>z
Practice
5. Write a program that reads the length of a side of an equilateral
triangle and prints its height and its surface.
6. Let A and B be two points specified by their Cartesian coordinates.
Write a program that calculates and prints the length of the AB
segment.
7. A turtle walks D kilometers in H hours. Write a program that reads D
and H from the keyboard and prints the turtle’s speed, in meters per
second.
Practice
8. Write a program that reads two non-null integers a and b from the
keyboard and prints the following:
a. a random natural number smaller than a
b. a random integer in the [-a, b] interval
c. a random natural number in the [a, b] interval
d. a random real number in the [-a, b] interval
Practice
• 9. Give a decimal number (read from console), print to console the
value of that number in:

a. binary;
b. octal;
c. hexa.
Practice
10. Using bitwise operators, solve the following:

a. Find whether a number (read from console) is a power of 2.


Print to the console "yes" in the positive case, "no" in the negative case.

b. Find whether two numbers (read from console) are equal.

c. Check whether a number is odd or even.


Print "odd" / "event" on the console on the appropriate cases.

d. Set the n-th bit of x to "1".


n, x shall be read from console.

e. Toggle the n-th bit of x.


n, x shall be read from console.

eg: n = 10, x = 1 --> output: 11


Practice
11. Given the following array

double x[] = {1, 2, 3, 4, 5, 6, 7, 8,9};

calculate the size dinamically.

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.

Das könnte Ihnen auch gefallen