Question

This question is little bit weird.

Just out of curiosity, is it possible to use the literals without variable assignment in C ?

Generally, we do like as follows,

#include<stdio.h>
int main()
{
 // Here we are using the literal '7' and assigning it to variable 'a'  which will hold it in some address space
 int a = 7;
 printf("Hello : %d\n",a);
 return 0;
}

So, is it possible to use the literals without variables ?

Thanks in advance.

Était-ce utile?

La solution

If you mean something like this:

printf("Hello : %d\n", 7);

then yes, it's fine.

Autres conseils

Then you don't even need %d

printf("Hello : 7 \n");

yes , they are used all the time , in return statements ,as constants etc . , even in your printf statement the %d is a placeholder for an int( or integer) , i.e. you can always replace a by 7 , however this approach of defining constants is favoured as if you will be using some number many times in your code , (ex. pi=3.14 in a math program) , its value needs to be edited at one place only and variable naming provides a easier reference system , and is hence more common practice .

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top