C Programming - Library Functions - Discussion
Discussion Forum : Library Functions - Point Out Errors (Q.No. 3)
3.
Point out the error in the following program.
#include<stdio.h>
int main()
{
char str[] = "IndiaBIX";
printf("%.#s %2s", str, str);
return 0;
}
Discussion:
17 comments Page 1 of 2.
Jone said:
4 years ago
In my case, it is printing %.0#s IndiaBIX.
Please, anyone, help me by explaining this clearly.
Please, anyone, help me by explaining this clearly.
Jone said:
4 years ago
Why we used the #symbol in printf statement? Please explain to me.
Smita said:
7 years ago
It will print %.0#s IndiaBIX.
Prashant Salunke said:
8 years ago
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?
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?
Mahi said:
8 years ago
Thank you all for explaining it
Khush said:
9 years ago
Hello, I didn't get the last line that %2s adds space before printing str.
Please explain.
Please explain.
Tester said:
10 years ago
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.
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.
(1)
@brij said:
1 decade ago
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.
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.
(1)
Vasantha deepika said:
1 decade ago
It prints %.#s IndiaBix . can any one explain tracing of this programme.
Karthik said:
1 decade ago
Why dont print the %.#s ?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers