Click to See Complete Forum and Search --> : Printf output question


prasanna0071
02-02-2004, 03:12 AM
Hi..

Following code is giving a different output than expected can someone
explain this output

****************
#include<stdio.h>
int main()
{
printf("%s","??<");
printf("%s","??>");
return 0;
}
*****************
Output Expected
??<
??>

*************
Actual output shown on the screen
{
}
*******************

hammer123
02-02-2004, 04:06 AM
#include<stdio.h>
int main()
{
printf("??<\n");
/* I don't know what the %s was for but it was formatted as a string and printf only takes 1 string argument. the \n adds a newline.
*/
printf("??>\n");
return 0;
}

psi42
02-02-2004, 04:28 AM
Well, I compiled the code exactly as you had it:


#include <stdio.h>

int main()
{
printf("%s","??<");
printf("%s","??>");
return 0;
}


And:


maedhros@penguin:~/C$ gcc prftst.c -o prftst
maedhros@penguin:~/C$ ./prftst
??<??>maedhros@penguin:~/C$


And so it works for me.....
No newlines of course :)


~psi42

psi42
02-02-2004, 04:30 AM
Originally posted by prasanna0071

Actual output shown on the screen
{
}



Wait you mean it literally output a bracket, a newline, a bracket, and then another newline? Now that's odd........

prasanna0071
02-02-2004, 07:36 AM
Ya it shows { } on the screen.

sub_slack
02-02-2004, 12:01 PM
in C there are special signs beginning with ?? , they are called trigraphs. when the compiler encounters a trigraph it substitutes it with other symbol, for example :
??( is [
??) is ]
??> is }
??< is {
??' is ^
??= is #
??- is ~
??! is |
??/ is \


so it is perfectly legal to write :

int main (void)
??<
return 0;
??>

bwkaz
02-02-2004, 07:51 PM
And you can have the compiler warn you when it sees a trigraph by passing -Wtrigraphs on the compile line.

I think you can disable them by not specifying -ansi. You may also be able to pass -no-trigraphs, but I'm not sure if that's legal or not. It's not here (gcc 3.3.2).

prasanna0071
02-03-2004, 04:14 AM
Hi,

Thanx a lot for your valuable information about trigraphs and helping
me understand the output.

Regards
Prasanna