2

I'm facing a weird problem, I don't know why the code below won't print anything after %n and It just prints Hi, It even won't save the result in res variable. Am I doing something wrong?

#include <stdio.h>
int main()
{
    int res;
    printf("Hi %n there",&res);
    printf("\n%d",res);
    return 0;
}

The result

Hi 
4194432
Shawn Vn
  • 297
  • 1
  • 14
  • 1
    [Works for me](https://coliru.stacked-crooked.com/a/afdc52732e3afa6d) (yes that's C; note `-x c` but I can't control the filename) – Asteroids With Wings Jul 31 '20 at 21:58
  • @AsteroidsWithWings That's why it's weird it should work just fine, but I have no idea why it's not working (btw I'm using `gcc` on windows could it be a compiler bug ?) – Shawn Vn Jul 31 '20 at 22:06
  • How do you know the `res` isn't assigned a value after the `printf` call? By the way, try to append a `\n` to the format string of `printf`. – M. Nejat Aydin Jul 31 '20 at 22:06
  • Try adding `\n` to the end of your format string to print a newline and flush the output buffer. – dbush Jul 31 '20 at 22:07
  • Try disabling your anti-virus software. – Asteroids With Wings Jul 31 '20 at 22:09
  • 3
    @dbush, those buffers should be flushed on program exit. That may not happen if the program aborts but I don't think that's the case here. – paxdiablo Jul 31 '20 at 22:09
  • @M.NejatAydin because when I print it, it prints the memory address. – Shawn Vn Jul 31 '20 at 22:09
  • That's because you probably wrote `printf("%d", &res)` instead of `printf("%d", res)`. See my example (linked above) for how to do it properly. _"It prints the memory address"_ is not evidence that a value wasn't written; it's evidence that you're printing wrong. And next time show your _complete_ testcase in the question up-front. :) – Asteroids With Wings Jul 31 '20 at 22:09
  • 1
    @ShawnVn Your code as posted isn't printing `res`. Please update with the *actual* code you're running. – dbush Jul 31 '20 at 22:10
  • @AsteroidsWithWings Nope I didn't I wrote `printf(%d, res)` and the result is `4194432` which clearly is a memory address (btw I've disabled my anti-virus ) – Shawn Vn Jul 31 '20 at 22:11
  • 2
    _"which clearly is a memory address"_ I disagree. It's just a random-ish number. There's no reason to think it's a memory address. But, okay, great, that _does_ show that the value you want isn't being written to `res`. Show your actual code please. – Asteroids With Wings Jul 31 '20 at 22:12
  • Guys, you seem to be missing one of the points, that it doesn't print the "there". – paxdiablo Jul 31 '20 at 22:12
  • @paxdiablo I don't think we're missing the point at all. The OP complained about both things. That neither "there" prints nor `res` is populated, suggests a crash when the `%n` part is reached. They're almost certainly linked symptoms. We're currently attempting to get to the bottom of it; feel free to jump in with some constructive advice to help. :) – Asteroids With Wings Jul 31 '20 at 22:13
  • @AsteroidsWithWings I've updated my question – Shawn Vn Jul 31 '20 at 22:13
  • 1
    Try checking the return value of the first `printf`. If it's negative, call `perror` to print the error string. – dbush Jul 31 '20 at 22:14
  • That's a good idea. – Asteroids With Wings Jul 31 '20 at 22:14
  • That's still not your code, @Shawn. `print` should be `printf`. **Copy/paste** your **actual** code that you are running that doesn't work. – Asteroids With Wings Jul 31 '20 at 22:14
  • 3
    What platform are you using? The posted code does not work with me on Visual Studio 2017, because the `%n` format specifier is disabled by default on that platform, for security reasons. See [this link to the official Microsoft documentation](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/set-printf-count-output?view=vs-2019) for further information. – Andreas Wenzel Jul 31 '20 at 22:15
  • Also, with that fixed, it still [works for me](https://coliru.stacked-crooked.com/a/961ce91ca5d01e65) and I see no reason for it not to work for you. Have you run this under a debugger? – Asteroids With Wings Jul 31 '20 at 22:15
  • @AndreasWenzel A comment above says GCC (probably MinGW) – Asteroids With Wings Jul 31 '20 at 22:15
  • 1
    @AndreasWenzel Ah, apologies, it doesn't matter because MinGW uses the underlying MS library. That's the badger. This is a dupe :) – Asteroids With Wings Jul 31 '20 at 22:16
  • @AndreasWenzel yes, that was the problem, `%n` is disabled apparently. Thank you! – Shawn Vn Jul 31 '20 at 22:19

0 Answers0