Facts and Question related to Style of writing programs in C/C++
Here are some questions related to Style of writing C programs:
Question-1: Why i++ executes faster than i + 1 ?
Answer-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.
Question-2: Is writing if(! strcmp(s1, s2) ) a good style ?
Answer-2: No, this is not a good style because if(! strcmp(s1, s2) ) invokes undefined behavior, so it might be confusing .
Question-3: What is the best style for code layout in C ?
Answer-3: 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 .
Question-4: Is goto a good thing or a bad thing ?
Answer-4: We should avoid using goto statement, Use it only when necessary .
Question-5: Why pre-increment operator is faster than post-increment operator ?
Answer-5: 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.
Question-6: What is the output of printf(“%d”) ?
Answer-6: 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 .
Please Login to comment...