-6
#include<stdio.h>
int main() {
    int j=65;
    printf("j>=65?%d:%c\n",j);
    return 0;
}

Ok it is understood that in place of %d ,value of j will be printed but why %c is replaced by ö ,i am unable to understand the output of this program , explain the printf statement.

user2736738
  • 30,591
  • 5
  • 42
  • 56
LocalHost
  • 910
  • 3
  • 8
  • 24
  • %c is used to print a char in C. Take a look the following link to understand how to format output: https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output – Kaan Burak Sener Dec 24 '17 at 17:22
  • Compile your code with all warnings and debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Use the `gdb` debugger to run it step by step. And first, read [documentation of standard IO functions](http://en.cppreference.com/w/c/io) – Basile Starynkevitch Dec 24 '17 at 17:23
  • 1
    BTW **your program is completely wrong.** Full of [undefined behavior](https://en.wikipedia.org/wiki/Undefined_behavior) and confusion between compile time and run time. Be **very [scared](https://stackoverflow.com/a/25636788/841108)**. Remember that C is sadly *not* [homoiconic](https://en.wikipedia.org/wiki/Homoiconicity) – Basile Starynkevitch Dec 24 '17 at 17:25
  • 1
    Two format specifiers but only one argument. – Weather Vane Dec 24 '17 at 17:26
  • 2
    From standard *The fprintf function writes output to the stream pointed to by stream, under control of the string pointed to by format that specifies how subsequent arguments are converted for output. **If there are insufficient arguments for the format, the behavior is undefined.*** – user2736738 Dec 24 '17 at 17:26
  • 2
    Consider reading a tutorial *before* asking. This information is not hard to find. – Hovercraft Full Of Eels Dec 24 '17 at 18:12

3 Answers3

1

You put doublequotes in a wrong place: you quoted the entire expression, rather than making your format string a conditional:

printf((j >= 65 ? "%d\n" : "%c\n"), j);

Your j >= 65 ? ... : ... expression is part of the string literal. C compiler does not "see" it as anything related to j. Hence the format string contains two format specifiers, with a single printed item; that's undefined behavior.

UB manifests itself in different ways; on your particular system a junk character 'ö' gets printed. This, however, is not a guaranteed behavior - on other systems you may get a different output, or a crash. See this Q&A for an explanation of UB.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

The short answer for this is that this is undefined behavior, the character that gets printed could be anything and the program may even crash.

The longer answer is that old compilers did not check printf strings against the arguments passed, and so by default compilers to not treat this as an error. If you enable the correct warnings (-Wformat) it will complain about this at compile time, and with -Werror the warning will be escalated to an error. Because this is not checked at compile time, as many arguments as are needed are fetched from where they should be on the call stack. This means that first argument after the last specified argument probably has to do with the return address for the stack frame or something, but after that you start to push into unallocated memory. Either way, the behavior is undefined.

If you're interested in more details, this stack overflow answer explains it well.

  • There is no requirement for the compiler to check the format string. And even for modern compilers this is not possible without a properly augmented standard header. Even if both provide this feature, it is not guaranteed as the necessary compiler extensions are not part of the standard (this is implied by the term "extensions") and a library supporting one compiler might not support a different. – too honest for this site Dec 24 '17 at 19:02
0

This is enough I suppose for explaining whatever you have shown. And the behavior you see can be anything given that it is undefined.

From standard

The fprintf function writes output to the stream pointed to by stream, under control of the string pointed to by format that specifies how subsequent arguments are converted for output. If there are insufficient arguments for the format, the behavior is undefined.

Emphasis mine.

user2736738
  • 30,591
  • 5
  • 42
  • 56