-4

I am learning conversion specifier part of the C language. I know that %d works for printing value behind commas but, I don't understand why printf prints something when there are no values behind comma. There are also no compile errors.

#include <stdio.h>
int main(void) {

printf("%d");
return 0;
}

result : 13242433

Can anyone tell me why this randomly numeral appears when I run this code?

Jake Symons
  • 468
  • 2
  • 10
  • 19
  • 5
    undefined behavior – user2736738 Dec 16 '17 at 15:48
  • 2
    If you're not getting compiler errors, then you need to turn up your compiler's error level: https://ideone.com/tT4tu7 – Oliver Charlesworth Dec 16 '17 at 15:50
  • 1
    You lied to the compiler, saying "there's an integer over there" even though you didn't put an integer there, but it had to fetch something because you told it to, so it took whatever happened to be there. It's a bit like when you were little and your nasty uncle Leonard (loved by all your cousins, even Joe with the funny ear who was always the butt of Lenny's jokes) handed you a little candy box and said "here, have some candy" but it wasn't candy and it took days to wash the taste from your mouth. – molbdnilo Dec 16 '17 at 15:53
  • then what is that the fetched value? is that just garbage value? Is this similar to print unexpected value when i print string without NULL? – ghdrlfehd123 Dec 16 '17 at 16:12
  • The answer is "undefined behavior". Looking for explanation beyond this is a mostly pointless endeavor. Also, there is no such thing as a string without a null-terminator; strings in C are null-terminated character arrays _by definition_. But, attempting to print a character array which is not null-terminated using `%s` is also undefined behavior, so similar in that sense. – ad absurdum Dec 16 '17 at 16:17
  • Sidenote: _conversion **type** specifiers_ are not part of the C language, but the `printf` (and `scanf`) family of functions. Read the documentation of the functions you use! – too honest for this site Dec 16 '17 at 19:20
  • Pro tips for asking: (1) choose a username that is not apparently bashing the keyboard to get a random string - we get that a lot. (2) Avoid "wanna" and other slang you'd use in a chat room. Note that posts made with zero or minimal effort are usually given a poor reception here - if you want an effort made in an answer, it is usually expected that the question should be of good quality as well. – halfer Dec 16 '17 at 19:49
  • @molbdnilo "You lied to the compiler, …" - No. The compiler is not really aware of which arguments the format sting specifies. It is a matter of the implementation for `printf` in the standard library used. Modern compilers **can** warn, but they need support from the headers and that is no requirement. – too honest for this site Dec 16 '17 at 20:57

3 Answers3

2

What is it?

Undefined behavior.(Check ref-1, ref-2)

Why is it so?

From standard 7.21.6.1

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

How to avoid?

Also when compiled with

gcc -Wall -Wextra -Werror progname.c

gives the error (due to -Werror)

error: format ‘%d’ expects a matching ‘int’ argument [-Werror=format=]
 printf("%d");
          ^

This was clear enough to tell you what is going wrong. But you didn't check.

then what is that the fetched value? is that just garbage value?

it is most likely that the printf when sees the %d specifier tries to read one int variable's value from memory. But alas that memory don't contain anything meaningful to you. (Even accessing that memory might not be permitted.) And yes it's just some value - garbage value. Don't even think that everytime you will get some garbage value - don't rely on it or anything similar. It's undefined behavior. Next time it may crash your program or simply print my contact number.1,2

1. This last part of the answer explains the possible reason for that garbage print.

2. print my contact number - is simply pointing out that it is just a garbage value which you shouldn't care about. Moreover it's undefined behavior - seeing even a garbage value is not something that is guaranteed to happen every single time.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

This is undefined behavior. When %d is used in printf then it expects an argument of type int.

haccks
  • 104,019
  • 25
  • 176
  • 264
-1

Well, gcc shows a warning when compiling your code:

test.c: In function ‘main’:
test.c:5:12: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
 printf("%d");
         ^

I'm not sure but If you used Visual Studio, there might be some bug. In that case you should post your problem here.

Jeff Holt
  • 2,940
  • 3
  • 22
  • 29
C0deDaedalus
  • 361
  • 1
  • 4
  • 19
  • There is no requirement for the comiler to warn, as this is not a constraint violation. It just happens gcc is a good and nice compiler warning about a lot of potential problems **if allowed to**. This has been answered here roughly a thousand times. Nevertheless, this does not explain what OP asks. – too honest for this site Dec 16 '17 at 20:54