https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
C-Basics
quiz solution
KEROLES SHENOUDA
1
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Learn-in-depth
progress Sequence
2
Start study
Solve the
assignments
Put your
understandi
ng level
Start quiz
www.Learn-in-depth.com
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
C Basics Tricks part 1
3
Operators priorities & Implicit Type Conversion
float g = 2 / 3.0 + 5 ;
float g = 2 / 3 + 5 ;
g =5.6
g =5
Signed Data type Range (+/-)
The sign of the result of a remainder operation, according to C99, is the same as
the dividend's one.
Let's see some examples (dividend / divisor):
And Two's complement
Asci with Octa number
10
Bit shifting is not multiplication. It can be used in
certain circumstances to have the same effect as a
multiplication by a power of two
addition(+) operator has higher precedence than
shift(<<) operator
1<<5<<4 == 1<<9 == 1*2^9 =512
Int i= 1,2,3  i= 3
Optimization will ignore the operation
Ans. 4 15
a==b==c (Multiple Comparison) evaluates in C
programming
(==) operates from left to right
Expression a==b==c is actually (a==b) ==c,
if(a==b==c)
Set Bit
X |= 1<<n ;
Reset specific Bit
X &= ~(1<<n) ;
Toggle Specific Bit
X ^= 1<<n ;
Implicit/
explicit
tricks
Prefix and postfix
13 12
evaluation takes place from left to right and
will be stopped if one of its components
evaluates to true(a non zero value).
Ans. 1 1 2 2
In case of GCC Compiler
Output will be 12,10. Output
may very from compiler to
compiler because order of
evaluation inside printf
The reason for undefined
behavior in PROGRAM 1 is,
the operator ‘+’ doesn’t have
standard defined order of
evaluation for its operands
1
2
3
4
5
6
7
8
9
10 11
12
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q1
4
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q2
5
 Evaluate the following expressions
 g = big / 2 + big * 4 / big - big + abc / 3 ; (abc = 2.5, big = 2, assume g to be a float)
 on = ink * act / 2 + 3 / 2 * act + 2 + tig ; (ink = 4, act = 1, tig = 3.2, assume on to be an int)
 s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ; (qui = 4, add = 2, god = 2, assume s to be an int)
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
6
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
7
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think
8
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
9
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q2
10
 Evaluate the following expressions
 g = big / 2 + big * 4 / big - big + abc / 3 ; (abc = 1.5, big = 3, assume g to be a float)
 on = ink * act / 2 + 3 / 2 * act + 2 + tig ; (ink = 3, act = 2, tig = 3.2, assume on to be an int)
 s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / god ; (qui = 2, add = 4, god = 3, assume s to be an int)
Two tricks
Int/int = int Float/int = float
Operation priorities
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q3
11Two tricks
Int/int = int Float/int = float
Operation priorities
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q3
12Two tricks
Int/int = int Float/int = float
Operation priorities
0 2 0.0 2.0
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q4 no tricks
13
Compiler
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q5/6/7/8 No tricks
14
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q9 no tricks
15
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q10
16
Two tricks
Int/int = int Float/int = float
Operation priorities
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q10
17
Two tricks
Int/int = int Float/int = float
Operation priorities
7/22*5.14*3/5
0*5.14*3/5
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q11
18
One trick
Signed int Range for 2 bytes
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q11
19
One trick
Signed int Range for 2 bytes
First calculate the expression = 32768
If size of a signed data type is n bytes, it
ranges from -28n-1 to 28n-1-1
So, a short(usually 2 bytes) ranges from -32768
to 32767 and an unsigned short ranges from 0 to
65535
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q12
20
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
21
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
22
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q12
23
One trick
The sign of the result of a remainder operation, according to C99, is the same as the dividend's one.
Let's see some examples (dividend / divisor):
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q13
24
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q13
25
Two's complement
0111 1001 (+121)
-X
-X will be -121
Signed Data type Range (+/-)
One trick
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q14
26
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q14
27
X=0xFFFF FFFF
y=0xFFFF FFFF
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q15 one trick
28
Int/int = int Float/int = float
Operation priorities
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q15 one trick
29
Int/int = int Float/int = float
Operation priorities
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q16
30
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q16
31
Asci with Octa number
One trick
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q17
32
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
33
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
34
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q17
35
Bit shifting is not multiplication. It can be used in
certain circumstances to have the same effect as a
multiplication by a power of two
1<<5<<4 == 1<<9 == 1*2^9 =512
One trick
Explanation: The main logic behind the program is the
precedence and associativity of the operators. The
addition(+) operator has higher precedence than shift(<<)
operator. So, the expression boils down to 1 << (2 + 3) <<
4 which in turn reduces to (1 << 5) << 4 as the shift
operator has left-to-right associativity
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q18
36
Int i= 1,2,3 == i= 3
One trick
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q19
37
Optimization tricks
One trick
4 15
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q20
38
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
39
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Think 
40
See the program, the values of a, b and c is 100 and you are thinking how
condition is false hereand why output is "False..."?
The expression is a==b==c which will evaluates like (a==b)==c now what will
be the result?
•The result of (a==b) is 1 (i.e. true).
•And (1)==c will be 0 (i.e. false) because the value of c is 100 and 100 is
equal not to 1.
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q20
41
Output:
FALSE
a==b==c (Multiple Comparison) evaluates in C programming
(==) operates from left to right
Expression a==b==c is actually (a==b) ==c,
One trick
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q21
42
= 1
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Q22/23/24
43
reset
Set
toggle
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Implicit/explicit
tricks
44
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Type Conversion in C
45
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Implicit Type Conversion
46
Also known as ‘automatic type conversion’.
•Done by the compiler on its own, without any external trigger from the user.
•Generally takes place when in an expression more than one data type is present. In
such condition type conversion (type promotion) takes place to avoid lose of data.
•All the data types of the variables are upgraded to the data type of the variable with
largest data type.
•It is possible for implicit conversions to lose information, signs can be lost (when signed is
implicitly converted to unsigned), and overflow can occur (when long long is implicitly converted
to float).
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Explicit Type Conversion
47
This process is also called type casting and it is user defined. Here the user can type
cast the result to make it of a particular data type.
The syntax in C:
(type) expression
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
Implicit/explicit tricks 48
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q30
49The reason for undefined behavior in PROGRAM 1 is, the operator ‘+’
doesn’t have standard defined order of evaluation for its operands
Explanation: See https://www.geeksforgeeks.org/sequence-points-in-c-set-1/ for explanation.
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q31
50
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q31
51
Prefix and postfix
One trick
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q32
52
Prefix and postfix
One trick
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q32
53
Explanation: In C, prefix and postfix operators
need l-valueto perform operation and return
r-value. The expression (++i)++ when executed
increments the value of variable i(i is a l-value) and
returns r-value. The compiler generates the error(l-
value required) when it tries to post-incremeny the
value of a r-value.
Prefix and postfix
One trick
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q33
54
In case of GCC Compiler Output will be 12,10. Output
may very from compiler to compiler because order of
evaluation inside printf
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q34
55
X = (j || (i++ && 1));
Note that precedence does not equate to order of
execution in general. In this case, we have the following
evaluation logic:
To evaluate = we need to evaluate its right-hand operand
To evaluate (j || stuff...) we first evaluate j
j is non-zero, so the result of (j || stuff...) is 1, and we do
not evaluate stuff due to the short-circuit behaviour of ||
Now we have determined that the right-hand operand of =
has evaluated to 1, so assign 1 to X.
Final result: X == 1, and i and j unchanged.
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q35
56
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q35
57
Answer: Option B
Solution:
In an expression involving || operator, evaluation takes
place from left to right and will be stopped if one of its
components evaluates to true(a non zero value).
So in the given expression m = i++ || j++ || k++.
It will be stop at j and assign the current value of j in m.
therefore m = 1 , i = 1, j = 2 and k = 2 (since k++ will not
encounter. so its value remain 2)
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
q36
58
Answer: Option A
Solution:
-1 will be represented in binary form as:
1111 1111 1111 1111
Now -1<<4 means 1 is Shifted towards left by 4 positions,
hence it becomes:
1111 1111 1111 0000 in hexadecimal form - fff0.
https://www.facebook.com/groups/embedded.system.KS/
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
embedded_system
59
ENG. Keroles Shenouda
https://www.facebook.com/groups/embedded.system.KS/

C basics quiz part 1_solution