为什么这个文件不能在gcc中编译?它在VS中运行良好

use*_*897 0 c++ linux g++

g ++甚至不会编译它.我哪里错了?这些是错误消息:

gcc sign.c sign.c:在函数âmainâ:sign.c:35:2:警告:格式不是字符串文字而没有格式参数[-Wformat- security]

================================================== ================================

#include "stdio.h"

int string_length(char str[]);
void string_sort(char s[]);

void string_sort(char s[])
{
    char tmpt;
    int i, j, len;
    len=string_length(s);
    for(i=0; i<len-1; i++){
            for (j=i+1; j<len; j++){
                    if (s[i] > s[j]){
                            tmpt=s[i];
                            s[i]=s[j];
                            s[j]=tmpt;
                    }
            }
    }
}


int string_length(char str[]){
    int i;
    for(i=0; i<80; i++){
            if(str[i]=='\0'){
                    return(i);
            }
    }
}

int main(){
    char words[80];
scanf("%s", words);
    printf(words);
    string_sort(words);
    printf(" ");
    printf(words);
    printf("\n");




    while ( words != " "){
            scanf("%s", words);
            printf(words);
            string_sort(words);
            printf(" ");
            printf(words);
            printf("\n");
    }
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*ill 11

首先,这只是一条警告消息,这意味着编译器检测到可能出错的内容但无论如何编译了您的代码.正如您所注意到的,并非所有编译器都会发出相同的警告.

问题是这一行(以及所有其他类似的行):

printf(words);
Run Code Online (Sandbox Code Playgroud)

使用时printf,必须使用格式字符串,如下所示:

printf("%s", words);
Run Code Online (Sandbox Code Playgroud)

否则,如果您正在打印的东西(words)中碰巧有任何%字符,那么printf()将把它们视为格式化说明符并尝试读取您未提供的参数.

如果您只想自己打印一个字符串,那么puts可能很有用:

puts(words);
Run Code Online (Sandbox Code Playgroud)

打印words后跟换行.