C programming 3 Flashcards
takes material from book "C programming a modern approach" and sololearn course
can one of the expressions in for loops be omitted ?
you can omit any of them , but keep the semicolons .
what happens if the second expression is missed in for loop ?
it defaults to true (one) .
can the first expression of if statement be replaced with a declaration in same location ?
yes
what happens if we declare a variable that already exists , as the first expression of if statement ?
another new version of it is created to be used solely inside the if statement , it won’t be visible outside it.
are variables declared by a for statement (as it’s first expression) visible outside it or not ?
not visible , they cannot be accessed outside the body of the loop .
can we declare multiple variables inside the if statement (as its first expression) ?
yes . provided they are of the same type .
what is comma expression for ?
it is for gluing two expressions together , possibly to use them where only one expression is allowed normally.
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 ?
we use comma operator as much as required .
describe the form a comma operator takes .
expression1 , expression2
bascially two expressions glued with a comma .
describe how comma operator is evaluated .
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 .
is the comma operator left or associative ?
left associative .
if comma operators glue expressions together then how to glue statements together ?
we can glue statements together using a compound statement .
where can you find comma operators often ?
with for statements and certain macro definitions .
can we determine the order expressions separated by comma operators will be evaluated in ?
yes , from left to right .
must the 3 expressions in for statement be related ?
no , C puts no restrictions on the type of expressions used with for statement .
what to do if we need to make an exit point in the middle of the loop or more than one exit point ?
use break statement .