Facts and Question related to Style of writing programs in C/C++
Here are some questions related to Style of writing C programs:
Why i++ executes faster than i + 1 ?
The expression i++ requires a single machine instruction such as INR to carry out the increment operation whereas, i + 1 requires more instructions to carry out this operation.
Is writing if(! strcmp(s1, s2)
) a good style ?
No, this is not a good style because if(! strcmp(s1, s2) ) invokes undefined behavior, so it might be confusing .
What is the best style for code layout in C ?
There are many systems of indentation advocated, but all of them have the same basic flaw . They will mislead the reader when the actual code logic does not follow the indentation . It is better to avoid indentation entirely, so the reader will not be misled .
a good thing or a bad thing ?
We should avoid using goto statement, Use it only when necessary .
operator is faster than post-increment operator ?
Evaluation of any expression is from left to right . Preincrement operator is faster than Postincrement operator because it does not save the current value for next instruction, whereas Postincrement needs to save the current value to be incremented after execution of the current instruction.
What is the output of printf(“%d”) ?
printf(“%d”, i) means that the compiler will print the value of i, since there is nothing after %d so the compiler will show garbage value in output window .
Share your thoughts in the comments
Please Login to comment...