0

i have used the code.

char *y;
y="hello world";
printf("%c",y);

it just shows something useless. What mistake i am making.

3 Answers3

4

Change :

printf("%c",y);

to :

printf("%s",y);

as %c specifier indicates a char. To identify a string, you need the specifier %s.

Marievi
  • 4,951
  • 1
  • 16
  • 33
1

To print a c-string use the %s format specifier in printf. What is happening now is that y evaluates to some address and then you are trying to print that address using %c which causes UB.

babon
  • 3,615
  • 2
  • 20
  • 20
1

It is undefined behaviour because you have used wrong format specifier.

C11 Standard: §7.21.6.1: Paragraph 9:

If a conversion specification is invalid, the behavior is undefined.225) If any argument is not the correct type for the corresponding coversion specification, the behavior is undefined.

So, use %s instead of %c for character string.

msc
  • 33,420
  • 29
  • 119
  • 214