c
1#include <stdio.h> 2 3#define swap(type, x, y) do {type t = x; x = y; y = t;} while (0) 4 5void bubble(int ary[], int elem) { 6 int k=0; 7 8 while (k < elem - 1) { 9 10 int j; 11 int last = elem - 1; 12 for (j = elem - 1; j > k; j--) { 13 if (ary[j - 1] > ary[j]) { 14 swap(int, ary[j - 1], ary[j]); 15 last = j; 16 } 17 } 18 k = last; 19 } 20} 21 22int main(void) { 23 int i; 24 int ary[8]; 25 size_t elem; 26 27 elem = sizeof(ary) / sizeof(ary[0]); 28 printf("type 8 numbers\n"); 29 30 for (i = 0; i < elem; i++) { 31 printf("ary[%d] is ? ", i); scanf("%d", &ary[i]); 32 } 33 bubble(ary, elem); 34 35 for (i = 0; i < elem; i++) { 36 printf("%d\n", ary[i]); 37 } 38 return 0; 39}
上のコードがgccコンパイル通らないのですが原因がわかりません。
コードの内容はバブルソートのアルゴリズムです。
エラーメッセージが無限ループのように大量に出力されてしまいます。
よろしくお願いいたします。
エラーメッセージ一部
10.c:1:1: error: stray '\377' in program 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:2: error: stray '\376' in program 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:3: error: stray '#' in program 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:4: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:6: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:5: error: unknown type name 'i' 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:8: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'c' 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:10: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:9: error: unknown type name 'c' 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:12: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:14: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:16: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:18: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:22: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:24: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:26: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:28: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:30: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:32: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:34: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:36: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:1:38: warning: null character(s) ignored 1 | # i n c l u d e < s t d i o . h > | ^ 10.c:2:1: warning: null character(s) ignored 2 | | ^ 10.c:3:1: warning: null character(s) ignored 3 | # d e f i n e s w a p ( t y p e , x , y ) d o { t y p e t = x ; x = y ; y = t ; } w h i l e ( 0 ) | ^ 10.c:4:1: warning: null character(s) ignored 4 | | ^ 10.c:5:1: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:3: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:4: error: invalid preprocessing directive #d 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:5: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:7: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:9: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:11: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:13: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:15: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:19: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:21: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:23: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:25: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:27: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:29: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:31: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:33: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:35: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:37: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:41: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:43: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:47: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:49: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:57: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:59: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:63: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:65: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:67: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:69: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:71: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:75: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:79: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:83: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) { | ^ 10.c:5:85: warning: null character(s) ignored 5 | v o i d b u b b l e ( i n t a r y [ ] , i n t e l e m ) {
環境
windows 10
gcc version 9.2.0(MinGW.org GCC Build-2)
エラーメッセージを提示しましょう
ほんの一部ですが追記しました。長すぎて入らないです。
https://paiza.io/projects/LKAmdESfn6fWjWApvEh_Ww
手元の環境でも、オンライン実行環境でもエラー無く動いてます。
回答2件
あなたの回答
tips
プレビュー