-1

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.

Dinesh
  • 16,014
  • 23
  • 80
  • 122

3 Answers3

4

If you mean something like this:

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

then yes, it's fine.

Barmar
  • 741,623
  • 53
  • 500
  • 612
2

Then you don't even need %d

printf("Hello : 7 \n");

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
0

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 .

rootavish
  • 122
  • 12