why gcc can not recognize ^M

Z

zhangyefei.yefei

why gcc can not recognize ^M? but cc of SunOS 5.8 can accept it
without error .

-bash-3.00$ gcc a.c -o a
a.c: In function 'main':
a.c:4: error: missing terminating " character
a.c:5: error: missing terminating " character
a.c:6: error: syntax error before 'return'

the following is a.c:
#include<stdio.h>
int main()
{
//^M is produced by ctrl+v,ctrl+m
char a[]={"abc^M"};
return 0;
}

can somebody give any hints ?
thanks in advance.
 
R

REH

why gcc can not recognize ^M? but cc of SunOS 5.8 can accept it
without error .

-bash-3.00$ gcc a.c -o a
a.c: In function 'main':
a.c:4: error: missing terminating " character
a.c:5: error: missing terminating " character
a.c:6: error: syntax error before 'return'

the following is  a.c:
#include<stdio.h>
int main()
{
       //^M is produced  by ctrl+v,ctrl+m
       char a[]={"abc^M"};
       return 0;

}

 can somebody give any hints ?
 thanks in advance.

You are trying to insert a carriage return character inside of the
literal string. C doesn't allow this. Use \n.

REH
 
K

Keith Thompson

REH said:
why gcc can not recognize ^M? but cc of SunOS 5.8 can accept it
without error .

-bash-3.00$ gcc a.c -o a
a.c: In function 'main':
a.c:4: error: missing terminating " character
a.c:5: error: missing terminating " character
a.c:6: error: syntax error before 'return'

the following is  a.c:
#include<stdio.h>
int main()
{
       //^M is produced  by ctrl+v,ctrl+m
       char a[]={"abc^M"};
       return 0;

}

 can somebody give any hints ?
 thanks in advance.

You are trying to insert a carriage return character inside of the
literal string. C doesn't allow this. Use \n.

But only if you really want a new-line character in the string.

On many systems, carriage return is the ASCII CR character, and '\n'
is the ASCII LF character. You probably want:

char a[]={"abc\r"};

or (better style IMHO):

const char a[] = "abc\r";

Incidentally, it appears that a conforming compiler is allowed
to accept a literal CR character in a string literal without a
diagnostic; either CR can be a member of the "extended source
character set", in which case it's legal, or it isn't, in which
case the behavior is undefined (C99 5.2.1, Character sets). Still,
it would be nice of the compiler warned you about it, and it's best
not to depend on this; escape sequences like "\r" exist for a reason.
 
D

Dik T. Winter

> why gcc can not recognize ^M? but cc of SunOS 5.8 can accept it
> without error . ....
> -bash-3.00$ gcc a.c -o a
> a.c: In function 'main':
> a.c:4: error: missing terminating " character
> a.c:5: error: missing terminating " character
> a.c:6: error: syntax error before 'return'
>
> the following is a.c:
> #include<stdio.h>
> int main()
> {
> //^M is produced by ctrl+v,ctrl+m
> char a[]={"abc^M"};
> return 0;
> }

This is not a.c, you added the comment when posting. If you had tried
to compile it as you did post it the problem would have been clear. gcc
treats ^M as an alternative for a newline, probably to allow copilation
of source files that come (unchanged) from systems where it actually
*is* a newline.
 
Z

zhangyefei.yefei

REH said:
why gcc can not recognize ^M? but cc of SunOS 5.8 can accept it
without error .
-bash-3.00$ gcc a.c -o a
a.c: In function 'main':
a.c:4: error: missing terminating " character
a.c:5: error: missing terminating " character
a.c:6: error: syntax error before 'return'
the following is  a.c:
#include<stdio.h>
int main()
{
       //^M is produced  by ctrl+v,ctrl+m
       char a[]={"abc^M"};
       return 0;
}
 can somebody give any hints ?
 thanks in advance.
You are trying to insert a carriage return character inside of the
literal string. C doesn't allow this. Use \n.

But only if you really want a new-line character in the string.

On many systems, carriage return is the ASCII CR character, and '\n'
is the ASCII LF character.  You probably want:

    char a[]={"abc\r"};

or (better style IMHO):

    const char a[] = "abc\r";

Incidentally, it appears that a conforming compiler is allowed
to accept a literal CR character in a string literal without a
diagnostic; either CR can be a member of the "extended source
character set", in which case it's legal, or it isn't, in which
case the behavior is undefined (C99 5.2.1, Character sets).  Still,
it would be nice of the compiler warned you about it, and it's best
not to depend on this; escape sequences like "\r" exist for a reason.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

thank you .
i got it .^M is same to \r.
thanks all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top