Why does this program below not show any error ?
int main (void) { "ANGUS"; 1; 3.14; return 0; }
Not in standard C, since there are no classes or objects (as in "class instances, i.e. collections of data with associated functions"), there's nothing for the function to be const "against".
The const keyword Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.
Constants are used when you want to assign a value that doesn't change. This is helpful because if you try to change this, you will receive an error. It is also great for readability of the code. A person who reads your code will now know that this particular value will never change.
The disadvantage of #define is that is replaces every occurence of the name, while const variables get normal lookup, so you have less risk of naming conflicts and it's not typesafe. The advantage of #define is that it guarantees constness and therefore there will be no backing variable.
Each of those statements are expressions that evaluate to a value, which is then discarded. Compare it to if you called a function that returned an int, a char or a float, without using the return value. That's also an expression that evaluates to a value.
It is not uncommon to have functions that return values that the caller may or may not be interested in, like for example where printf("%d", 9001)
returns the number of characters printed. A caller can use the number returned, or they can just ignore it.
If the compiler complained whenever you ignored a return value, you'd have a very noisy build log. Some compilers however diagnose (if sufficient warning flags are enabled) such pointless side-effect-less uses of literals and variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With