C Programming - Library Functions - Discussion


Point out the error in the following program.


int main()
    char str[] = "IndiaBIX";
    printf("%.#s %2s", str, str);
    return 0;

[A]. Error: in Array declaration
[B]. Error: printf statement
[C]. Error: unspecified character in printf
[D]. No error

Answer: Option D


No answer description available for this question.

Neeraj Kumar said: (Sep 12, 2010)  
How it will print like this "%.#s %2s"?

Sms said: (Oct 2, 2010)  
It prints %.#s IndiaBIX

Sundar said: (Oct 3, 2010)  

In DOS 16 bit platform (Turbo C): %.#s %2s

In Linux 32 bit platfomr (GCC) : %.0#s IndiaBIX

Note: All answers given in C Programming section are based on 16-bit platform.

Sowmya said: (Jul 6, 2011)  
Some one plz xlpn, whats the role of %.# here??
In floating point numbers, it will tell the number of decimals, but here in a string, what does it mean??

Vigneshwaran.L said: (Jul 12, 2011)  
%.# is not operator. it is one of the symbol representation just like hex code reperesentation.like #ffff or #defe.So the linux platform (GCC )print the output is:%.0#s IndiaBix
Note:it based on machine code representation

Puneet said: (Oct 18, 2011)  
The output is : %.#s %2s , string for second format %2s was also not printed why?

Xyz said: (Feb 3, 2012)  
Why it is printing IndiaBIX in place of %2s?

Karthik said: (Mar 17, 2012)  
Why dont print the %.#s ?

Vasantha Deepika said: (Aug 27, 2012)  
It prints %.#s IndiaBix . can any one explain tracing of this programme.

@Brij said: (Jun 22, 2013)  
char str[] = "IndiaBIX"; :- Declaration of character array.

printf("%.#s %2s", str, str); :- Its print %.#s due any symbol as u

print like anything in printf function and its print IndiaBix due %s, str here one more str which does not error in the program.

Tester said: (Sep 12, 2015)  
This program is actually really very simple to understand. Let's just go step wise.

char str[] = "IndiaBIX"; this judt defines str.

printf ("%. #s %2s", str, str);

It seems a bit complex lets break it into two halves.

printf ("%. #s", str); this statement I syntactically correct i will tell you how.

This statement when executed prints %. #s in turbo C while %. 0's in GCC you must know that we have certain formatters like %. (integer)'s eg: % 5s this means the first five char are printed.

So when the compiler reads this statement after reading %. It expects an integer but it does not get that it reads # which is not an integer so it does not know what to do so in turbo c it simply treats this as a string not a formatter and simply prints %. #s while in GCC it does consider it as a string but itself adds 0 between. And # and then simply prints it.

Now coming onto the second halve i.e 5.

printf ("%2s", str); this is again for formatting one of the formatter it just adds 2 spaces before printing str.

Hope it is clear now.

Khush said: (Aug 30, 2016)  
Hello, I didn't get the last line that %2s adds space before printing str.

Please explain.

Mahi said: (Jun 20, 2017)  
Thank you all for explaining it

Prashant Salunke said: (Dec 21, 2017)  
It shows following error.

In function \'main\':
demo6.c:6:12: warning: unknown conversion type character \'#\' in format [-Wformat=]
printf("%.#s %2s", str, str);
demo6.c:6:12: warning: too many arguments for format [-Wformat-extra-args]

Can anyone help me to get it?

Smita said: (Aug 17, 2018)  
It will print %.0#s IndiaBIX.

Jone said: (Apr 5, 2021)  
Why we used the #symbol in printf statement? Please explain to me.

Jone said: (Apr 5, 2021)  
In my case, it is printing %.0#s IndiaBIX.

Please, anyone, help me by explaining this clearly.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.