| Start |
|---|
| Comparing literal and variable Suppose you are comparing value with some variable Now suppose you have mistaken Then, if an equal sign is accidentally left out, the compiler will complain about an “attempted assignment to literal.” This won’t protect you when comparing two variables, but every little bit helps. See here (opens new window) for more info. |
| Do not leave the parameter list of a function blank — use void Suppose you are creating a function that requires no arguments when it is called and you are faced with the dilemma of how you should define the parameter list in the function prototype and the function definition. So, which is the correct choice? ANSWER: using the keyword void GENERAL ADVICE: If a language provides certain feature to use for a special purpose, you are better off using that in your code. For example, using C11 section 6.7.6.3 "Function declarators", paragraph 10, states: The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters. Paragraph 14 of that same section shows the only difference: … An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. A simplified explanation provided by K&R (pgs- 72-73) for the above stuff: Furthermore, if a function declaration does not include arguments, as in So this is how your function prototype should look: And this is how the function definition should be: One advantage of using the above, over This also means that you should define the Note that even though a function defined with an empty parameter list takes no arguments, it does not provide a prototype for the function, so the compiler will not complain if the function is subsequently called with arguments. For example: If that code is saved in the file If you compile with more stringent options, you get errors: If you give the function the formal prototype Moral — always make sure you have prototypes, and make sure your compiler tells you when you are not obeying the rules. |
Read Article
https://develop.horje.com/learn/1434/reference