MacのターミナルでC言語コンパイルエラー

このQ&Aのポイント
  • MacのターミナルでC言語コンパイルエラーが発生しています。
  • コンパイル時に以下のエラーメッセージが表示されます。
  • プログラムはC言語で書かれており、コンパイルエラーの原因は識別子が期待された場所に存在していないことです。
回答を見る
  • ベストアンサー

MacのターミナルでC言語コンパイルエラー

Macのターミナルで"HelloWorld.c"のコンパイルで以下のエラーが出ます。 HelloWorld.c:1: error: expected identifier or ‘(’ before ‘{’ token HelloWorld.c:1: error: stray ‘\’ in program HelloWorld.c:1: error: stray ‘\’ in program HelloWorld.c:1: error: stray ‘\’ in program HelloWorld.c:1: error: stray ‘\’ in program HelloWorld.c:1: error: stray ‘\’ in program HelloWorld.c:2: error: stray ‘\’ in program HelloWorld.c:2: error: stray ‘\’ in program HelloWorld.c:2: error: stray ‘\’ in program HelloWorld.c:2: error: stray ‘\’ in program HelloWorld.c:3: error: stray ‘\’ in program HelloWorld.c:3: error: stray ‘\’ in program HelloWorld.c:3: error: stray ‘\’ in program HelloWorld.c:3: error: stray ‘\’ in program HelloWorld.c:4: error: stray ‘\’ in program HelloWorld.c:4: error: stray ‘\’ in program HelloWorld.c:4: error: stray ‘\’ in program HelloWorld.c:4: error: stray ‘\’ in program HelloWorld.c:4: error: stray ‘\’ in program HelloWorld.c:4: error: stray ‘\’ in program HelloWorld.c:4: error: stray ‘\’ in program HelloWorld.c:5: error: stray ‘\’ in program HelloWorld.c:6: error: stray ‘\’ in program HelloWorld.c:6: error: stray ‘\’ in program HelloWorld.c:6: error: stray ‘\’ in program HelloWorld.c:6: error: stray ‘\’ in program HelloWorld.c:8: error: stray ‘\’ in program HelloWorld.c:8: error: stray ‘\’ in program HelloWorld.c:8: error: stray ‘\’ in program HelloWorld.c:8: error: stray ‘#’ in program HelloWorld.c:8:24: warning: backslash-newline at end of file HelloWorld.c:8: error: stray ‘\’ in program HelloWorld.c:8: error: stray ‘\’ in program ソースは以下 #include int main(void) { printf("Hello, World!¥n"); return 0; } どなたかお助けを!! まだ、プログラム超初心者です。よろしくお願いします。

質問者が選んだベストアンサー

  • ベストアンサー
  • harawo
  • ベストアンサー率58% (3742/6450)
回答No.4

「テキストエディット」を使い、リッチテキストフォーマットで、コードを書いて保存し、「ターミナル」でビルドすると、まったくおなじエラーメッセージが出ました。なので、まず間違いなく、リッチテキストで保存したことが原因でしょう。 テキストエディットの、「フォーマット」メニューで「標準テキストにする」にして、プレーンテキストで保存し直してください。

その他の回答 (3)

回答No.3

当方、Macユーザーです。 hello.c ソースファイルを gccコンパイルすると hello.c:1:21: error: #include expects "FILENAME" or <FILENAME> hello.c: In function 'main': hello.c:4: warning: incompatible implicit declaration of built-in function 'printf' と3行のエラー表示がされ、あなたの質問のようなエラーは出ません。 MacOSX は UNIX です。UNIXの流儀に従いファイル名は、通常は小文字です。ですからソースファイル名に大文字を混ぜるのは Windows のように見えてしまうのですが・・・(失礼)。 /* hello.c プログラム */ #include <stdio.h> int main(void) { printf("Hello, World!\n"); return 0; }

  • kmee
  • ベストアンサー率55% (1857/3366)
回答No.2

> printf("Hello, World!¥n"); 使っているエディタや文字コードによりますが、現在のMacで一般的に使われるものでは、円記号とバックスラッシュは区別されます。 optionキーと同時に円記号キーを押せばバックスラッシュになるので、それを使ってください。 あと、こちらでやると、そんなに沢山のエラーはでないのですが。 1行目については > error: #include expects "FILENAME" or <FILENAME> と、ファイル名の指定が抜けていることを指摘されます。 これにともなって、printfが未定義になってしまいます。 でも、この2点の指摘しか出ません。 ・どんな引数を付けてコンパイルを実行していますか? ・HelloWorld.c はテキストファイルになってますか?書式付きのものになってないですか? ターミナルから file HelloWorld.c としたらどんな結果になりますか?

  • piroin654
  • ベストアンサー率75% (692/917)
回答No.1

#include を #include <stdio.h>

関連するQ&A

  • C言語、コンパイルエラーの原因?

    いつも大変お世話になり誠にありがとうございます。 標記の件。 shift jifでコンパイルしているのですが コンパイルエラーが発生します。 下記にコードとエラーメッセージを書きます。 コード #include <stdio.h> int main(void) { printf("8進数101の文字コードをもつ文字は%cです。\n", '\101'); printf("16進数61の文字コードをもつ文字は%cです。\n", '\x61');      return 0; } コンパイルエラー gcc text4.c -o text4 text4.c: In function 'main': text4.c:7:1: error: stray '\201' in program    ^ text4.c:7:2: error: stray '@' in program    ^ text4.c:7:3: error: stray '\201' in program    ^ text4.c:7:4: error: stray '@' in program    ^ text4.c:8:1: error: stray '\201' in program   return 0; ^ text4.c:8:2: error: stray '@' in program   return 0; ^ text4.c:8:3: error: stray '\201' in program   return 0; ^ text4.c:8:4: error: stray '@' in program   return 0; 上記のようになります。 どうすればいいでしょうか? ご多忙中申し訳ございません。 ご回答の程宜しくお願い申し上げます。           高橋耕造

  • エラーが出てコンパイルができません…

    #include<stdio.h> void main(void) { int i; i=0; while(i<3){ printf("おーい\n\"); printf(" やっほー\n\n\"); i=i+1; } printf("プログラムを終了します\"); } と本に書いてあった通りに入力したら、下のようなエラーが出てきました。 7: 文法エラー before \"i\" 9: stray \'\\\' in program 10: stray \'\\\' in program 10: 文法エラー before \"n\" 11: stray \'\\\' in program 11: 文法エラー before \"n\" 12: `i\' undeclared (first use in this function) 12: (Each undeclared identifier is reported only once 12: for each function it appears in.) トップレベル: 15: error: 文法エラー before string constant 15: conflicting types for built-in function `printf\' 15: data definition has no type or storage class 16:2: ファイル末尾に改行がありません 解決策と原因を教えて下さい。 宜しくお願い致します。

  • コンパイルエラーの英語の読み方

    #include <stdio.h> int main() { printf("Hello, World"; } と書くと error: expected ‘)’ before ‘;’ token error: expected ‘;’ before ‘}’ token と表示されます。 ;の前に)がないと言っているようですが、expectedは名詞の後にくるものだと思うので、 error: ‘)’expected before ‘;’ token だと思うのですが、これについて回答をよろしくお願いします。

  • C言語 どこがコンパイルエラーか解りません。

    いつも大変お世話になっております。 標記の件。 どこがエラーなのか解りません。 2時間くらい、にらめっこが続いています。 どこがエラーなのか教えて下さい。 ご回答のほどよろしくお願い申し上げます。 コード #include <stdio.h> int a = 0; void func(void) { int c = 2; printf("func関数では変数aとcが使えます。¥n”); printf("変数aの値は%dです。\n",a); /*printf("変数bの値は%dです。\n",b);*/ printf("変数cの値は%dです。\n",c); } int main(void) { int b = 1; printf("main関数では変数aとbが使えます。\n"); printf("変数aの値は%dです。\n", a); printf("変数bの値は%dです。\n", b); /*printf("変数cの値は%dです。\n", c); */ func(); return 0; } コンパイルエラー printf("func関数では変数aとcが使えます。¥n”); ^ text10.c:9:8: error: missing terminating " character printf("func関数では変数aとcが使えます。¥n”); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ text10.c:10:34: error: expected ')' before ';' token printf("変数aの値は%dです。\n",a); ^ text10.c:10:1: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [-Wint-conversion] printf("変数aの値は%dです。\n",a); ^~~~~~ In file included from text10.c:1:0: c:\mingw\include\stdio.h:454:38: note: expected 'const char *' but argument is of type 'int' _CRTIMP __cdecl __MINGW_NOTHROW int printf (const char *, ...); ^~~~~~ text10.c:13:1: error: expected ';' before '}' token } どうかよろしくお願いいたします。

  • C言語ファイルのコンパイルができない。

    先日からVineLinux3.1を使い始めました。 C言語ファイルa.c(「a」という文字を出力するだけのごく簡単なものです。このファイル自体には問題はありません。)をコンパイルしようと cc a.c と入力すると、 a.c:2:9: #include は”ファイル名”あるいは<ファイル名>を必要とします a.c:3: error: stray '\241' in program a.c:3: error: stray '\241' in program a.c:3: error: stray '\241' in program a.c:3: error: stray '\241' in program a.c: 関数 'main' 内: a.c:6: error: stray '\241' in program a.c:6: error: stray '\241' in program a.c:6: error: stray '\241' in program a.c:6: error: stray '\241' in program a.c:6: error: stray '\241' in program a.c:6: error: stray '\241' in program a.c:6: error: stray '\241' in program a.c:6: error: stray '\241' in program a.c:4: 警告: 'main'の戻り値の型が 'int'ではありません と出てコンパイルできません。 Linuxのことはまだほとんど分からないので、できるだけ丁寧におしえていただけるとうれしいです。 よろしくお願いします。

  • CGIへのコンパイルエラー

    初めまして。プログラミング暦はほとんどありません。javaを少しかじった程度です。今回Ajaxへ挑戦しようと思い、 http://itpro.nikkeibp.co.jp/article/COLUMN/20060115/227278/?ST=nettech の予測補完インタフェースを作成しよとしています。 環境はfedoracore5でXPからのputyからの遠隔です。 complete.cをcomplete.cgiへコンパイルするときに、 [root@localhost html]# gcc -Wall -O2 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include complete.c -o complete.cgi -lglib-2.0 -lsary -lpthread complete.c:1: error: stray ‘\357’ in program complete.c:1: error: stray ‘\273’ in program complete.c:1: error: stray ‘\277’ in program complete.c:1: error: stray ‘#’ in program complete.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token In file included from complete.c:2: /usr/include/string.h:39: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:43: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:52: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:59: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:62: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:66: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:88: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:96: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:102: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:109: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strxfrm’ /usr/include/string.h:184: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strcspn’ /usr/include/string.h:188: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strspn’ /usr/include/string.h:242: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strlen’ /usr/include/string.h:270: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:288: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:292: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:296: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:299: error: expected declaration specifiers or ‘...’ before ‘size_t’ /usr/include/string.h:329: error: expected declaration specifiers or ‘...’ before ‘size_t’ In file included from /usr/include/string.h:417, from complete.c:2: /usr/include/bits/string2.h:969: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__strcspn_c1’ /usr/include/bits/string2.h:971: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__strcspn_c1’ /usr/include/bits/string2.h:979: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__strcspn_c2’ /usr/include/bits/string2.h:982: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__strcspn_c2’ /usr/include/bits/string2.h:991: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__strcspn_c3’ /usr/include/bits/string2.h:994: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__strcspn_c3’ とエラーがでます。どうしたらいいでしょうか?(文字数の関係で半分しか掲載できませんでした。)よろしくお願いいたします。

    • ベストアンサー
    • CGI
  • typedef部分でのコンパイルエラー

    本に付属のプログラムを動かそうとしているのですが、コンパイルエラーが出て困っています。 言語はC++、コンパイル環境はCygwinでg++です。 typedef CellUtil<ExpressionCell> CellType; extern CellType *uCell; 上記の部分で 106: error: expected init-declarator before '<' token 106: error: expected `,' or `;' before '<' token 107: error: expected init-declarator before '*' token 107: error: expected `,' or `;' before '*' token とエラーが出ます。 CellType *uCell は別のcppファイルで宣言してあります。 typedefでは<>記号は使えたと思うので、何が原因か知っている方教えてください。 本自体も10年前に発行されたものなのでいろいろ困惑しています。 お願いします。

  • C言語、度々すいません。コンパイルエラーです。

    いつも大変お世話になり誠にありがとうございます。 下記コードがコンパイルエラーになり色々と 変えてみたのですが、どうしてもコンパイルエラー となります。 度々すいません。 ご回答の程宜しくお願い申し上げます。      記 1.コード #include <stdio.h> int main(void) { char ch; printf("アルファベットの最初の文字は何ですか?\n"); ch = getchar(); printf("アルファベットの最初の文字は%cです。\n" ch); return 0; } 2.コンパイルエラー C:\MinGW>gcc test8.c -o test8 test8.c: In function 'main': test8.c:9:51: error: expected ')' before 'ch' printf("アルファベットの最初の文字は%cです。\n" ch); 上記の通りエラーメッセージが出ます。 どうしたらいいでしょうか? ご回答の程宜しくお願い申し上げます。

  • MacのXcodeのObjective-C言語についてお伺い致します。

    MacのXcodeのObjective-C言語についてお伺い致します。 Objective-Cを入れたところ、以下の様なエラーメッセージが出ました。 !Expected';'before'}' token Mac初心者でよくわからないので申し訳ございませんが、 ご教示いただければ幸いです。

    • ベストアンサー
    • Mac
  • Macでテキストエディットを用いたC言語

    Macでテキストエディットを用いてC言語プログラミングをしました。hello.cを作りました。 #include<stdio.h> int main(void){ printf(“HelloWorld¥n”); return 0; } この上がソースコードです。そしてターミナルでコンパイルして実行したら以下のようなメッセージが出ました。どこが間違っているのでしょうか? gcc -o main hello.c hello.c:4:10: error: non-ASCII characters are not allowed outside of literals and identifiers printf(“HelloWorld¥n”); ^ hello.c:4:23: error: non-ASCII characters are not allowed outside of literals and identifiers printf(“HelloWorld¥n”); ^ hello.c:4:13: error: use of undeclared identifier 'HelloWorld' printf(“HelloWorld¥n”); ^ hello.c:4:26: error: non-ASCII characters are not allowed outside of literals and identifiers printf(“HelloWorld¥n”); ^ 4 errors generated.

専門家に質問してみよう