C programming 3 Flashcards

takes material from book "C programming a modern approach" and sololearn course

1
Q

can one of the expressions in for loops be omitted ?

A

you can omit any of them , but keep the semicolons .

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

what happens if the second expression is missed in for loop ?

A

it defaults to true (one) .

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

can the first expression of if statement be replaced with a declaration in same location ?

A

yes

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

what happens if we declare a variable that already exists , as the first expression of if statement ?

A

another new version of it is created to be used solely inside the if statement , it won’t be visible outside it.

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

are variables declared by a for statement (as it’s first expression) visible outside it or not ?

A

not visible , they cannot be accessed outside the body of the loop .

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

can we declare multiple variables inside the if statement (as its first expression) ?

A

yes . provided they are of the same type .

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

what is comma expression for ?

A

it is for gluing two expressions together , possibly to use them where only one expression is allowed normally.

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

what to do if we want to write a for statement with two or more initialization expressions or ones that increment or decrement several variables in each iteration ?

A

we use comma operator as much as required .

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

describe the form a comma operator takes .

A

expression1 , expression2

bascially two expressions glued with a comma .

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

describe how comma operator is evaluated .

A

first expression is evaluated then discarded then the second expression is evaluated and it’s value becomes the value of the whole expression .

so there is no purpose of the first expression unless it has side effects .

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

is the comma operator left or associative ?

A

left associative .

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

if comma operators glue expressions together then how to glue statements together ?

A

we can glue statements together using a compound statement .

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

where can you find comma operators often ?

A

with for statements and certain macro definitions .

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

can we determine the order expressions separated by comma operators will be evaluated in ?

A

yes , from left to right .

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

must the 3 expressions in for statement be related ?

A

no , C puts no restrictions on the type of expressions used with for statement .

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

what to do if we need to make an exit point in the middle of the loop or more than one exit point ?

A

use break statement .

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

where can we use break statements ?

A

with :

for loop , while loop , do while loop , and switch statement .

17
Q

why is goto not widely used ?

A

because of the availability of break and continue statements .

18
Q

how many levels of nesting can the break statement escape ?

A

only one level .

19
Q

where can continue statement be used ?

A

it is limited to loops .

20
Q

to where do break statements transfer control to ?

A

to the end point just past the loop or switch .

21
Q

to where does continue statement transfer control to ?

A

to the point just before the end of the loop .

22
Q

to where can goto jump statement transfer control ?

A

to any statement that follows s label , within a function , given that the statement has a label .
C99 adds a restriction that goto cannot be used to bypass the declaration of a variable-length array .

23
Q

what is a label ?

A

it is an identifier placed at the beginning of a statement .

24
Q

what form do labels take ?

A

identifier : statement

they must be separated by a colon.

25
Q

what form does goto statement take (syntax) ?

A

goto identifier ;

26
Q

what purpose goto statement may serve given that the other jump statements are available ?

A

goto statement is useful for exiting nested loops , or exiting a loop from within a statement .

27
Q

what is a null statement syntax and what is it good for ?

A

it is written devoid of all symbols except the terminating semicolon .

it is used to create loops with empty bodies , which can be handy for reading character data .

28
Q

how to use null statement to rewrite this part of the prime finding loop : ?

for (d = 2 ; d < n ; d++)
if (n % d == 0)
break ;

A

for (d = 2 ; d < n && n % d != 0 ; d++ )
;

/* we put the null statement on it’s own line otherwise readers may get confused,we can also add the following : */

if ( d < n)
printf( “ %d is divisible by %d \n” , n , d )
else
printf(“ %d is prime \n” , n)

28
Q

how to use null statement to rewrite this part of the prime finding loop : ?

for (d = 2 ; d < n ; d++)
if (n % d == 0)
break ;

A

for (d = 2 ; d < n && n % d != 0 ; d++ )
;

/* we put the null statement on it’s own line otherwise readers may get confused ,we can also add the following : */

if ( d < n)
printf( “ %d is divisible by %d \n” , n , d )
else
printf(“ %d is prime \n” , n)

29
Q

why do programmers put null statement on it’s own line ?

A

to avoid confusing the reader who may think that the next statement may be actually the loop’s body .

30
Q

what is the main effect of putting a semicolon after while , for and if statement’s control expression’s parentheses ?

A

it makes a null statement and thus terminates the loop or if statement prematurely .

31
Q

what is the result of the following code ?

if ( d == 0) ;
printf(“ ERROR : Division by zero \n”)

A

the if statement is followed by a null statement because it’s followed by a semicolon , thus it will has no effect and the next statement will print the division by zero error no matter what’s the value of “d” variable ,zero or not .

32
Q

what is the result of the following code ?

i = 10 ;
while (i > 0) ;
{
    printf(" %d and counting \n" , i) ;
    --i ;
 }
A

it is an infinite loop because “i” value will not be decremented and it will be stuck at 10 and doing nothing as a result of the null statement as it’s body .

33
Q

what is the result of the following code ?

i = 11 ;
while(–i > 0) ;
printf(“%d and counting down \n” , i) ;

A

it would run the printf function once because the while loop has null statement only .

34
Q

what is the result of the following code ?

for (i = 10 ; i > 0 ; i–) ;
printf(“%d and counting down \n” , i ) ;

A

due to null statement , it will exit the loop when “i” reaches value zero then it will print once only .

35
Q

name a rare case where while loop cannot be converted to a for loop in a standard pattern .

A

when the control variable is incremented at the end of while loop ,but there is a continue statement before that that will execute when one specific value is entered .

when we try to convert it to for loop using standard pattern it will be different because it would still increment even if the specific value is entered , because the incrementing was moved inside for loop’s third expression .

36
Q

which form of infinite loop is preferable while(1) or for( ; ; )?

A

older compilers used to check the infinite while loop’s control expression every time , making it more efficient , but now newer compilers does optimization and therefore there is no difference in performance .

37
Q

what is so bad about goto statement ?

A

it is not inherently bad , but there are better alternatives that are more readable and easier to modify.
programs that use more than a few goto statements can quickly degenerate into spaghetti code (with control jumping from here to there all time)

goto statements are usually harder to read because they jump back and forth through the program unlike continue and break which only jump forward .

and they make programs harder to modify because the statements that follow the label may have more than one purpose , and maybe reached in more than one way either by normal flow from previous statement or by a jump from goto statement.

38
Q

does the null statement have any other uses beside making a loop statement with an empty body ?

A

it has potential uses , though practically the only other use is rare and it is used for putting a label at the end of a compound statement by adding a null statement after the label ,as it requires one statement at least .

39
Q

are there any other ways to make an empty loop besides putting a null statement on it’s own line ?

A

yes , using continue statement as the only statement in for loop’s body , or using an empty compound statement , both are ways for clarity .