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 ?
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 .

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 .

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.