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.
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)
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?
@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)
Vigneshwaran.L said:
1 decade ago
%.# 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
Note:it based on machine code representation
Sundar said:
1 decade ago
Output:
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.
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:
1 decade ago
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??
In floating point numbers, it will tell the number of decimals, but here in a string, what does it mean??
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.
Khush said:
9 years ago
Hello, I didn't get the last line that %2s adds space before printing str.
Please explain.
Please explain.
Puneet said:
1 decade ago
The output is : %.#s %2s , string for second format %2s was also not printed why?
Vasantha deepika said:
1 decade ago
It prints %.#s IndiaBix . can any one explain tracing of this programme.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers