snprintf 函数未声明?

Ang*_*yan 3 c printf stdio

我正在尝试使用snprintf基于我读过的手册的函数是<stdio.h>标题的一部分,但是我收到一个错误,它被隐式声明。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct users {
    char* user_id;
};
    typedef struct users users_t;

int save_user_detail(user_t);

int main() {
    users_t users;
    save_user_detail(users);
    return 0;
}

int save_user_detail(users_t users)
{

    printf("Type the filename = ");
    scanf("%s", users.user_id);
    char* extension = ".txt";
    char fileSpec[strlen(users.user_id)+strlen(extension)+1];
    FILE *file;
    snprintf(fileSpec, sizeof(fileSpec), "%s%s", users.user_id, extension);
    file = fopen(fileSpec, "w");
    if(file==NULL) 
    {
        printf("Error: can't open file.\n");
        return 1;
    }
    else 
    {
        printf("File written successfully.\n");
        fprintf(file, "WORKS!\r\n");
    }
    fclose(file);
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

错误

chq*_*lie 8

您似乎在使用gcc,但此编译器不一定使用符合 C 标准并支持snprintf.

在 Windows 体系结构上,您可能正在使用 Microsoft C 库,而在旧版本中没有snprintf或将其重命名为_snprintf.

您可以通过以下两种方式尝试解决问题:

编译器应该停止抱怨缺少原型,如果运行时库确实有一个snprintf具有匹配调用约定的符号,它将链接到它并且程序应该按预期运行。