Reputation: 11
char c[] = "hello";
printf("%*d", c);
the output is :
infinite loop of spaces
Upvotes: 1
Views: 255
Reputation: 163
c is an array of char which holds hello but what is an array. it is a pointer with a chunk of memory mean above we create an array means we create a pointer whose point blocks of memory which hold hello string like c variable. it means c contain Adresse of the first block of hello.
printf("%*d")
printf("%*d")
*
represent the width and second parameter d represent a integer
*
symbol work in below exampleprintf("output is:%*c",3,c): its output:' h'
here is width three and print a char which hold c and that is h
now see why screen has infinite loop let's see printf("%'*'d",c)
we know the first argument is the width and second output is data here our first argument is c which is an array and u know array hold address of the first block Code executed it give an address to width and u know the address is too long and it sometimes negative then why your screen has too much space not infinite because u give width as a long as a address further doubt comment me
Upvotes: 0
Reputation: 32596
Can anyone explain me the output of this code?
infinite loop of spaces
char c[] = "hello";
printf("%*d", c);
the %*d
say the first arg after the format indicates the width, here it is the address of c interpreted as a huge number, and the default added character to respect the width is a space.
note there is a missing arg normaly giving the value to print
if I use a valid code like that :
#include <stdio.h>
int main()
{
printf("%0*d\n", 3, 1);
return 0;
}
the result is 001 because I ask for to write '1' with a width of 3 and the added character is '0'
Upvotes: 11