C programming 2 Flashcards

1
Q

What is an Lvalue?

A

The assignment operator requires an Lvalue as it’s left operand ,a variable for example.

An Lvalue is an object stored in computer memory not a constant or a result of computation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are some tips to avoid undefined behavior?

A

Avoiding writing expressions that access the value of a variable and also modify it in same expression.
Some compilers may produce a warning.

It’s also a good practice to avoid using assignment operators in subexpressions , and using a series of separate assignments instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name the operators that modify their operands.

A

Assignment operators, decrement and increment are the only operators that modify their operands.
And care must be taken to not write an Expression that depend on a particular order of evaluation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is fetching in C programming and how does it function?

A

Fetching a variable means to retrieve the value of the variable from memory, a later change to the variable won’t affect fetched variable which is typically stored in register inside the CPU.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is undefined behavior in C ?

A

When a program ventures into the realm of undefined behavior all bets are off , it may behave differently when compiled with different compilers, or might not compile at all , or may compile but not run , or runs but then crashes , behave erratically or produce meaningless results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an Expression statement in C ?

A

C has an unusual rule that any expression can be used as a statement , It’s done by turning an expression into a statement by adding a semicolon at the end.

Which makes sense only if it has side effects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do C compilers deal with expression statements that do nothing?

A

Some of them may show a warning of “statement with no effect”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to raise a number to a power “exponentiation” in C language?

A

It’s best done by repeated multiplication ,for noninteger power use pow function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to apply the % operator to a floating-point operand ?

A

The % operand requires integer operands ,try the fmod function instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In C Standard ,What is the guaranteed result of : (a / b )* b + (a%b) ?

A

a

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why does C provide the ++ and – operators, are they faster or just more convenient?

A

Historically C inherited it from B language , Thompson apparently created these operators because his B compiler could generate a more compact translation for ++i than for i = i + 1.

But it’s continued popularity is due to it’s brevity. And convenience.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Do ++ and – work with floats ?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When using postfix ++ or – when do the increment or decrement be performed?

A

Unfortunately it’s difficult to answer , C standard introduce the concept of “sequence point” and says that “updating the stores value of the operand shall occur between the previous and next sequence point”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some kinds of sequence points in C? explain them briefly and give situations that impose them .

A

One kind is the end of an expression statement , by the end of it all increments and decrements within the statement must have been performed (after the previous sequence point and before the next one), the next statement can’t begin until this condition has been met.

Certain operators like logical and , logical or , conditional and comma also impose sequence points.

Also function calls does , because it’s arguments must be evaluated before the function is called ( they may contain an increment or decrement)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What’s meant by that statements like ( i = 1; )is discarded?

A

They modify i , but the Expression is considered discarded because the assignment is an operator that produces a value , it’s not a great loss at all.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If n is an integer , what’s remarkable about the result of n % 10 ,and n / 10 ?

A

n % 10 equals the last digit in n .

And n / 10 is n with the last digit removed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Does C have relatively many or few statements?

A

Few ( relatively).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Other than expression statement and return statement , what are the 3 main categories that most statements fall into ?

A

Selection statements.
Iteration statements.
Jump statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Name two selection statements.

A

1 - if .

2 - switch .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Name three iteration statements.

A

1- while .
2- do .
3 - for .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Name 4 jump statements.

A

1- break.
2- continue.
3- goto.
4 - return.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What do selection statements do ?

A

They allow a program to select a particular execution path from a set of alternatives .

Examples : if and switch statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What are iteration statements for? ( Briefly)

A

They are for looping.

Examples: while , do and for statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What are jump statements for ?

A

They cause unconditional jump to some other place in the program.
Examples : break , continue , goto and return statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What statements are there in C other than selection, iteration,jump and expression statements?

A

Compound statement and null statement left , there’s no more.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What does a compound statement do ? (Briefly)

A

They group several statements into a single statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What does a null statement do ?

A

It performs no action.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What does a comparison yield in C language?

A

It yields an integer: 0 to indicate it’s false or 1 to mark it as true , unlike other languages that have Boolean or logical type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Do relational operators work with floats and mixed types in C ?

A

Yes they do.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How do relational operators precedence compare to arithmetic operators?

A

They have lower precedence than arithmetic ones.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Are relational operators left or right associative?

A

Left associative.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What does the expression i < j < k do?

A

Because the relational operators are left associative the expression equals : ( i < j ) < k.

The expression first evaluates if i < j is true and if it’s it yields 1 or 0 if false and evaluates that the value 1 or the value 0 is lower than the value of k .

It doesn’t test whether j lies between i and k .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Name 4 relational operators in C language.

A

1- less than: <
2- greater than: >
3-less than or equal to: <=
4-greater than or equal to: >=

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Name 2 equality operators in C language and explain what they do .

A

Equality operator is very different from the assignment operator “=” in it’s function but it appears a bit similar which is “==” for equal to , and “!=” For not equal to .

They evaluates the two sides similarly to the relational operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Are equality operators left or right associative in C?

A

They are left associative just like relational operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

How does the precedence of equality operators compare to the precedence of relational operators?

A

They have lower precedence than the relational operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Name 3 logical operators.

A

Logical negation : !

Logical and : &&

Logical or : ||

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Is the logical operator “!” Unary or binary?

A

It’s unary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

What do logical operators produce ? Explain.

A

Either 1 or 0 , any nonzero operand will be treated as true and any zero operand will be treated as false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

In the following expression is j incremented or not?

i > 0 && ++j > 0

A

If i > 0 is false then the part after the && isn’t evaluated then no incrementing happens due to short-circuit evaluation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

What is the precedence of the ! Operator?

A

Same as unary plus and unary minus

42
Q

What is the precedence of && and || operators?

A

Lower than that of relational and equality operators

43
Q

What is the associativity of logical operators : ! , && , || ?

A

! Is right associative.

The && and || are left associative.

44
Q

Are parentheses around the expression of an if statement optional or mandatory?

A

Mandatory, as a part of the if statement not the expression.

45
Q

When is if statement executed?

A

When the expression inside the parentheses evaluates to a nonzero.

46
Q

How to be able to make if statement control more than one statement?

A

By using compound statements , putting the statements inside curly brackets -braces- to form a compound statement.
With each statement ending in a semicolon but not the compound statement itself ,hence no semicolon after the curly brackets.

47
Q

Does a compound statement end with a semicolon?

A

No ,but every statement within it will end in a semicolon including the last statement before enclosing the curly brackets.

48
Q

When does the statement after else clause execute?

A

If the expression of the if statement (inside the parentheses) evaluates to zero.

49
Q

Layout issue: how to position the statements after if statement and after the else clause?

A

We either indent them in a new line after “ if” and then after “else”and end them both with semicolons.
Or if they’re short we can keep them on same line as “if” and “else” and end both with semicolons.

50
Q

Are there restrictions on what kind of statements can appear inside an if statement?

A

No , there’s not any, they can even be nested!

51
Q

How should else clause be aligned as a convention for readability?

A

It should be aligned with the matching if statement.

Curly brackets can be added to eliminate confusion or even a possibility of compiling issue.

52
Q

How to style the nested if statements and their else clauses?

A

We can position the starting curly bracket at the end of if statement’s expression (at the end of the line).
And then we align the closing curly bracket with the matching if statement.

We also indent else clauses and put them on same line with matching if statement’s closing bracket .

We put the starting curly brackets of else clause in same line as well , but we align their closing curly brackets with the matching if statement.

53
Q

Name 2 advantages for using curly brackets/ braces even when they’re not required.

A

1- it becomes easier to modify.
2- it helps avoiding errors that can result from forgetting to use braces when adding statements to an if or else clause.

54
Q

How to style cascaded if statements with their else clause included?

A

We can either match else with it’s if statement and indent the next if in the next line .
Or as most programmers and the usual we align every “else if “ and “else” with the first original “if” ,if this is followed.

55
Q

Is else statement always present at the end of cascaded if statements?

A

No ,not always.

56
Q

Name two benefits of using cascaded if statements.

A

1- it avoid the problem of excessive indentation. When the number of tests is large .
2- it assures the reader that the statements are just a series of tests.

57
Q

What are cascaded if statements?

A

A cascaded if isn’t some new type of statements,they are simply if statements that happen to have an else clause with another if statement in it (ad infinitum).

58
Q

What is the dangling else problem? Explain

A

When if statements are nested and there’s an else clause ,it belongs to the nearest if statement that has not been paired with an else ,even if indentation suggests it’s paired with another .

To change that use indentation on the outer “if” and keep else outside it. (Inner if enclosed in brackets along with everything that should be considered inside the outer if statement)

59
Q

How many operands the conditional operator requires?

A

3 operators, unlike others which require 2 or 1 and that makes it unique and sometimes it’s called a ternary operator.

60
Q

What does the conditional operator consist of?

A

Two symbols and 3 operands.
Which must be used together in the following way:

expr1 ? expr2 : expr3

It actually means : if expression 1 is true then expression 2 else expression 3

61
Q

How does the conditional expression work in the following code:

Int i , j ,k ;

i = 1
j = 2
k = i > j ? i : j ;
A

its layout is :
k = expression 1 ? expression 2 : expression 3

I > j Is evaluated first if it’s value is nonzero then 2 is evaluated and it’s value is the value of the entire conditional expression therefore it’s assigned to k , else expression 3 is evaluated as the value of the entire conditional expression.

62
Q

When are parentheses necessary in the conditional expression?

A

It’s necessary if there’s at least one expression or number that is outside the conditional expression but is processed with it , for example added to it as a whole after it’s evaluation.

63
Q

What is the precedence of conditional operator?

A

It’s less than most others including relational and logical operators , but is higher than that of assignment.

64
Q

Is it possible to use conditional expressions in certain kinds of macro definitions?

A

Yes ,they are common in some.

65
Q

what are logical expressions built of besides operands?

A

operators , more specifically relational ,equality and logical operators.

66
Q

what is a short-circuit evaluation?

A

its performed when the value of an expression as a whole can be determined before evaluating the rest of the expression, a logical expression for example.

67
Q

does C89 have a boolean type built in?

A

no.

68
Q

how to work around the lack of boolean type in C89?

A

define BOOL int

we can assign 0 or 1 to a flag if some program needs a variable with assigned boolean type, it would let logical expressions evaluate flag as true or false by the 1 and 0 assignment respectively.

a better way would be to define macros with the names TRUE and FALSE .
and we can even define our own integer type to indicate its a boolean but it will behave as integer :

69
Q

what is stdbool header used for ?

A

it is a header in C99 that make it easier to work with boolean types , and it supplies the program with the macro Bool to stand for _Bool type , and true and false to stand for 1 and 0 respectively .

70
Q

what is a good alternative for cascaded if statements?

A

the switch statement

71
Q

what are some advantages of using switch statements instead of cascaded if statements?

A

it is usually easier to read and is often faster especially when there is more than a handful of cases.

72
Q

what are the components of a switch statement ?

A

controlling expression : the switch statement must be followed by an integer expression inside parentheses , characters are treated as integers in C and therefore can be tested in switch statements , floating-point numbers and strings do not qualify.

case labels : each case label starts with the keyword case or the keyword default and followed by a constant expression directly which must evaluate into an integer (characters are accepted) and then a colon .

after that statements are written (no braces are required) and the last statement in each group of statements is normally the break statement , but several cases can precede the same group of statements .
several case labels can be written on same line too.

73
Q

is it possible to write a case label that specifies a range of values in C language?

A

no it is not possible at least until C99.

74
Q

is default case label required in switch statement?

A

no it is not.

75
Q

must the default label be the last one in switch statements?

A

no , it is not required that the cases should be ordered.

76
Q

what happens if you omit the break statement in case labels of switch statement?

A

it would flow to the first statement of the next case without checking its case label.

77
Q

what is a case label in switch statement ?

A

it is a marker that indicates a position within the switch , as switch is a form of computed jump .

78
Q

what convention you have to do when you have to omit a break statement in a switch statement’s case label ?

A

you should write a comment indicating that it is done deliberately to share code between some case labels .

79
Q

is n + 1 a constant expression ?

A

no unless n is a macro defined constant .

80
Q

how to force a compiler to notice a suspicious use of = instead of a == ?

A

some programmers put the integer value where an lvalue would normally come , so it throws an error if it is supposed to be an equality check instead of an assignment .

some compilers are able to do checks if enabled .

for example with GCC compiler if the -Wparentheses option is used or -Wall (all warnings).

81
Q

how to make the compiler ignore a particular case in if statement’s expression when checking for suspicious ( = instead of == ) use , is enabled ?

A

by putting the expression inside an additional set of parentheses .

82
Q

what styles are there for braces and indentation for compound statements ?

A

4 of them :

K&R .
allman.
whitesmiths.
GNU.

83
Q

what is K&R style in indentation and braces placement ?

A

it puts the left brace on the same line as “if” , and align the other brace with if’s start .
its most often used in JAVA.

if (line_num == MAX_LINES ) {
line_num = 0 ;
page_num++ ;
}

84
Q

what is the style of allman in indentation and braces placement ?

A

it puts the first brace on a separate line :

if (line_num == MAX_LINES)
{
  line_num = 0 ;
  page_num++ ;
}
85
Q

what is whitesmiths style in indentation and braces placement ?

A

it indents the braces and align them with the start of statements .

if (line_num == MAX_LINES)
  {
  line_nums = 0 ;
  page_num++ ;
  }
86
Q

what is the GNU style in indentation and braces placement ?

A

it indents the braces and further indents the statements more than the braces.
its used in GNU project.

if (line_num == MAX_LINE)
  {
    line_num = 0 ;
    page_num++ ;
  }
87
Q

if i is an integer value and f is a float value , what type is the value of the conditional expression (i > 0 ? i : f) ?

A

if conditional expressions are of mixed type (float and integer) , the expression is of type float , so if i > 0 is true then i is the value of the expression after its converted into float .

88
Q

why does not C99 have a better name for its _Bool type ?

A

because bool or boolean may break some old programs as programmeers may have used them in their programs before adding _Bool , _Bool would not break the code as C89 standard say that names starting with underscore then an uppercase letter are reserved for future use .

89
Q

what are the 2 most common methods of indentation inside the switch statement ? and which one is the best ?

A

the first is to indent the case labels and put the statements in same line as the case label and the break statement at next line aligned with the beginning of the first statement above it .
a variation of it is to put the break statement as the same line as the other statements if there is only one statement before break.

the second is to put statements under case label and in one variation of it each case label is aligned under the word switch so they end at same level.

90
Q

what is a loop in C language ? describe what it does.

A

a loop is a statement that its job is to execute other statements (loop body ) each time (iteration of the loop) as long as its controlling expression evaluates to true which means to 1 . if that expression is still true then it keeps executing the loop body until controlling expression evaluates to false.

91
Q

what is the difference between “do” ,”while” and “for” loops ?

A

while : is used if we want the controlling expression to be evaluated before each iteration.

do : used if we want that expression to be evaluated after each iteration ,which means it runs once at least.

for : it is used when the loop increment or decrement a counting variable to control when it runs or stop.

92
Q

what does the break statement do in loops ?

A

they stop the loop (break it) and move to the next statement after the loop .

93
Q

what does the continue statement do in loops ?

A

it skips the rest of the current iteration .

94
Q

what does the goto statement do ?

A

it jumps to any statement within a function.

95
Q

what can a null statement do ?

A

it can create a loop with empty body .

96
Q

when does a loop terminate ?

A

when the control expression evaluates to false (zero) .

97
Q

does the while loop’s body have to be a single statement ?

A

yes but it could be a one compound statement that consist of many sub-statements .
we do that by using braces .

98
Q

can variables be incremented inside printf function?

A

yes .

99
Q

what is the widely used idiomatic expression used while making an infinite loop ?

A

while (1) { insert statements here}

a while statement written with the above syntax will execute infinitely unless we use a statement inside it that transfers control out of the loop , such as return ,break , or goto or some function that does it .

100
Q

what is the syntax for “for” statement ?

A

for (expression1 ; expression2 ; expression3)

expression1 is for initialization ,expression2 is for comparison ,and the last is for incrementing or decrementing .