C programming 2 Flashcards
What is an Lvalue?
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.
What are some tips to avoid undefined behavior?
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.
Name the operators that modify their operands.
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.
What is fetching in C programming and how does it function?
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.
What is undefined behavior in C ?
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.
What is an Expression statement in C ?
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 do C compilers deal with expression statements that do nothing?
Some of them may show a warning of “statement with no effect”.
How to raise a number to a power “exponentiation” in C language?
It’s best done by repeated multiplication ,for noninteger power use pow function.
How to apply the % operator to a floating-point operand ?
The % operand requires integer operands ,try the fmod function instead.
In C Standard ,What is the guaranteed result of : (a / b )* b + (a%b) ?
a
Why does C provide the ++ and – operators, are they faster or just more convenient?
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.
Do ++ and – work with floats ?
Yes.
When using postfix ++ or – when do the increment or decrement be performed?
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”.
What are some kinds of sequence points in C? explain them briefly and give situations that impose them .
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)
What’s meant by that statements like ( i = 1; )is discarded?
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.
If n is an integer , what’s remarkable about the result of n % 10 ,and n / 10 ?
n % 10 equals the last digit in n .
And n / 10 is n with the last digit removed.
Does C have relatively many or few statements?
Few ( relatively).
Other than expression statement and return statement , what are the 3 main categories that most statements fall into ?
Selection statements.
Iteration statements.
Jump statements.
Name two selection statements.
1 - if .
2 - switch .
Name three iteration statements.
1- while .
2- do .
3 - for .
Name 4 jump statements.
1- break.
2- continue.
3- goto.
4 - return.
What do selection statements do ?
They allow a program to select a particular execution path from a set of alternatives .
Examples : if and switch statements.
What are iteration statements for? ( Briefly)
They are for looping.
Examples: while , do and for statements.
What are jump statements for ?
They cause unconditional jump to some other place in the program.
Examples : break , continue , goto and return statements.
What statements are there in C other than selection, iteration,jump and expression statements?
Compound statement and null statement left , there’s no more.
What does a compound statement do ? (Briefly)
They group several statements into a single statement.
What does a null statement do ?
It performs no action.
What does a comparison yield in C language?
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.
Do relational operators work with floats and mixed types in C ?
Yes they do.
How do relational operators precedence compare to arithmetic operators?
They have lower precedence than arithmetic ones.
Are relational operators left or right associative?
Left associative.
What does the expression i < j < k do?
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 .
Name 4 relational operators in C language.
1- less than: <
2- greater than: >
3-less than or equal to: <=
4-greater than or equal to: >=
Name 2 equality operators in C language and explain what they do .
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.
Are equality operators left or right associative in C?
They are left associative just like relational operators.
How does the precedence of equality operators compare to the precedence of relational operators?
They have lower precedence than the relational operators.
Name 3 logical operators.
Logical negation : !
Logical and : &&
Logical or : ||
Is the logical operator “!” Unary or binary?
It’s unary.
What do logical operators produce ? Explain.
Either 1 or 0 , any nonzero operand will be treated as true and any zero operand will be treated as false
In the following expression is j incremented or not?
i > 0 && ++j > 0
If i > 0 is false then the part after the && isn’t evaluated then no incrementing happens due to short-circuit evaluation.