问题
#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