简体   繁体   中英

printf "%n" specifier not working properly

I copied this code from geeks for geeks.

#include<stdio.h>
  
int main()
{
  int c;
  printf("geeks for %ngeeks ", &c);
  printf("%d", c);
  getchar();
  return 0;
}

It should print the characters from start to %n followed by the number of printed characters:

在此处输入图像描述

But when I execute it, it's printing this:

在此处输入图像描述

It seems that the problem lies in the fact that older versions of MingW do not set __USE_MINGW_ANSI_STDIO by default, which is not the case for newer versions, what you can do is to manually define it in your program:

# define __USE_MINGW_ANSI_STDIO

#include <stdio.h>

int main()
{
    //...
}

Or use it directly on the compilation command:

gcc main.c -o main.exe -D __USE_MINGW_ANSI_STDIO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM