Error on “{\rtf1\ansi” when compiling C program [closed]

我怕爱的太早我们不能终老 提交于 2020-12-08 05:16:46

问题


#include <stdio.h>

main()
{
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;

    while (fahr <= upper) {
        celsius =(5.0/9.0)*(fahr-32.0);
        printf( “%3.0f %6.1f\n”,fahr,celsius );
        fahr = fahr + step;
    }
}

I am using a mac. In terminal, I compile with gcc -g file.c and get the following errors and warnings:

file.c:1:1: error: expected identifier or '('
{\rtf1\ansi\ansicpg1252\cocoartf1265\cocoasubrtf190
^
file.c:21:11: warning: missing terminating ' character [-Winvalid-pp-token]
        printf( \'93%3.0f %6.1f\\n\'94,fahr,celsius );\
                 ^

I took the code straight out of "The C Programming Language." Why isn't it working? Also, I created the text file using text editor. However, whenever I make changes to it and save it, it reverts back to a .rtf. How do I fix that?


回答1:


You saved this as an RTF file. The input file to the compiler must be plain text. If you're using the OS X TextEdit.app program, use the Format menu Make Plain Text action to convert it to plain text, then re-save it.

In addition (as mentioned by the other answers), you need to replace the special typographical quote characters ( and ) with straight double-quote characters (").




回答2:


Your double-quotes are strange. Try with printf( "%3.0f %6.1f\n",fahr,celsius );. With this change, it compiles for me on OSX.




回答3:


your quotes are richtext angled quotes. try replacing them with standard " quotes. this sometimes happens when coppying and pasting text from certain text editors, or the internet. The following compiles and runs for me on linux

    #include <stdio.h>

    main()
    {
    float fahr, celsius;
    int lower, upper, step;

    lower = 0;
    upper = 300;
    step = 20;

    fahr = lower;

    while (fahr <= upper) {
        celsius =(5.0/9.0)*(fahr-32.0);
    printf( "%3.0f %6.1f\n",fahr,celsius );
    fahr = fahr + step;
        }
    }


来源:https://stackoverflow.com/questions/23669037/error-on-rtf1-ansi-when-compiling-c-program

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!