473,474 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

different results for C/C++ compiler

char s[7] = "1234567";

when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).

any story for this one?

Vol
Jun 27 '08 #1
16 1373
Vols wrote:
char s[7] = "1234567";
This is an error since you do not
end the array with zero You put 7
chars, in a 7 position array, then the string
is not zero terminated.

legal in C, illegal in C++.

when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).

any story for this one?

Vol


--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Jun 27 '08 #2
Vols wrote:
char s[7] = "1234567";

when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).

any story for this one?
Too many initialisers. In C++, the array is initialised from the
array that is the string literal. The string literal "1234567" has
8 (eight) characters in it, the numbers and the terminating zero.
C++ does not allow to provide more initialisers to array elements
than the array size. Drop the size, just do

char s[] = "1234567";

or change it to 8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #3
The reason I asked this question is: why is it legal in C?
So we don't need to end with '0' in C?
Thanks.

Vol
On Apr 24, 10:30 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Vols wrote:
char s[7] = "1234567";
when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).
any story for this one?

Too many initialisers. In C++, the array is initialised from the
array that is the string literal. The string literal "1234567" has
8 (eight) characters in it, the numbers and the terminating zero.
C++ does not allow to provide more initialisers to array elements
than the array size. Drop the size, just do

char s[] = "1234567";

or change it to 8.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
Vols <vo********@gmail.comwrote in news:0fe872a8-eb69-437f-bf7b-
47**********@d1g2000hsg.googlegroups.com:
The reason I asked this question is: why is it legal in C?
So we don't need to end with '0' in C?
Thanks.
C is not nearly as strongly typed as C++ and therefore allows a bit more
sloppyness/or is a bit more tolerant depending on your POV.

joe
Jun 27 '08 #5
On Apr 24, 10:54*am, Vols <volunte...@gmail.comwrote:
The reason I asked this question is: why is it legal in C?
So we don't need to end with '0' in C?
Thanks.

Vol

On Apr 24, 10:30 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Vols wrote:
char s[7] = "1234567";
when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).
any story for this one?
Too many initialisers. *In C++, the array is initialised from the
array that is the string literal. *The string literal "1234567" has
8 (eight) characters in it, the numbers and the terminating zero.
C++ does not allow to provide more initialisers to array elements
than the array size. * Drop the size, just do
* * char s[] = "1234567";
or change it to 8.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -
An array in C need not be terminated with a NUL character.
Such an array is not considered to be a string unless it is
NUL terminated.

Most C library functions (strxxx, printf, etc.) expect
their char* arguments to be strings.

--
Fred Kleinschmidt
Jun 27 '08 #6
fr*****************@boeing.com wrote in
news:1f**********************************@z24g2000 prf.googlegroups.com:
>
An array in C need not be terminated with a NUL character.
Such an array is not considered to be a string unless it is
NUL terminated.

Most C library functions (strxxx, printf, etc.) expect
their char* arguments to be strings.
I think it's less to do with the null and arrays than with the fact that in
C++

"Hello" is of type char const[6]

and trying to assign that to a char[5] will fail. (There is a special rule
to get around the constness problem, but not the size problem.)

Of course, I am not really a language lawyer type, so I could be off base,
but that is what I think is happening.

joe
Jun 27 '08 #7
Joe Greer wrote:
fr*****************@boeing.com wrote in
news:1f**********************************@z24g2000 prf.googlegroups.com
:

An array in C need not be terminated with a NUL character.
Such an array is not considered to be a string unless it is
NUL terminated.

Most C library functions (strxxx, printf, etc.) expect
their char* arguments to be strings.

I think it's less to do with the null and arrays than with the fact
that in C++

"Hello" is of type char const[6]

and trying to assign that to a char[5] will fail. (There is a
special rule to get around the constness problem, but not the size
problem.)
No, it doesn't. There's no assignment going on. You can't assign to an
array. The string literal is an initializer.

The rules for C allow that initialization, the rules of C++ don't.

Brian
Jun 27 '08 #8
Victor Bazarov wrote:
Vols wrote:
char s[7] = "1234567";

when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).

any story for this one?

Too many initialisers. In C++, the array is initialised from the
array that is the string literal. The string literal "1234567" has
8 (eight) characters in it, the numbers and the terminating zero.
C++ does not allow to provide more initialisers to array elements
than the array size. Drop the size, just do

char s[] = "1234567";

or change it to 8.
And for why in C, it's allowed by the Standard. The following is from
the C99 draft:

6.7.8 Initialization

[#14] An array of character type may be initialized by a
character string literal, optionally enclosed in braces.
Successive characters of the character string literal
(including the terminating null character if there is room
or if the array is of unknown size) initialize the elements
of the array.

[#32] EXAMPLE 8 The declaration

char s[] = "abc", t[3] = "abc";

defines ``plain'' char array objects s and t whose elements
are initialized with character string literals. This
declaration is identical to

char s[] = { 'a', 'b', 'c', '\0' },
t[] = { 'a', 'b', 'c' };

Brian
Jun 27 '08 #9
"Vols" <vo********@gmail.comwrote in message
news:0f**********************************@d1g2000h sg.googlegroups.com...
: The reason I asked this question is: why is it legal in C?
: So we don't need to end with '0' in C?
....
: char s[7] = "1234567";

I believe that it used to be illegal in C as well. This "feature"
may(?) have been added in the C99 revision of the C standard.

The point would have been that some embedded developers found
that the NUL char, when not needed, was wasting space (yes,
this can matter on chips with only few kilobytes of ram).

So if you want portable code with the NUL char,
you can just write:
char s[] = "1234567";

If you want portable code *without* the NUL,
you can write instead:
char s[7] = {'1','2','3','4','5','6','7'};
// can get cumbersome for large data tables...

The "in-between" style is to be avoided in portable code.
It just happens to be one of the (too many, some futile)
areas where the C and C++ standard have chosen different
routes...
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Jun 27 '08 #10
On 2008-04-24, Ivan Vecerina <_I*******************@ivan.vecerina.comwrote:
"Vols" <vo********@gmail.comwrote in message
news:0f**********************************@d1g2000h sg.googlegroups.com...
: The reason I asked this question is: why is it legal in C?
: So we don't need to end with '0' in C?
...
: char s[7] = "1234567";

I believe that it used to be illegal in C as well. This "feature"
may(?) have been added in the C99 revision of the C standard.
According to the C90 specification: "An array of character type may be
initialized by a character string literal, optionally enclosed in
braces. Successive characters of the character string literal
(including the terminating null character if there is room or if the
array is of unknown size) initialize the elements of the array."

An example in the specification states that the declaration

char s[] = "abc", t[3] = "abc";

is equivalent to

char s[] = { 'a', 'b', 'c', '\0' },
t[] = { 'a', 'b', 'c' };
Jun 27 '08 #11
"Default User" <de***********@yahoo.comwrote in news:67c8jnF2np5qcU1
@mid.individual.net:
>
No, it doesn't. There's no assignment going on. You can't assign to an
array. The string literal is an initializer.

The rules for C allow that initialization, the rules of C++ don't.

Technically, of course you are correct, but that is entirely beside the
point. The type mismatch is still the problem.

joe
Jun 27 '08 #12
Joe Greer wrote:
"Default User" <de***********@yahoo.comwrote in news:67c8jnF2np5qcU1
@mid.individual.net:

No, it doesn't. There's no assignment going on. You can't assign to
an array. The string literal is an initializer.

The rules for C allow that initialization, the rules of C++ don't.

Technically, of course you are correct, but that is entirely beside
the point. The type mismatch is still the problem.
Sorry, but you don't know what you are talking about. The type of the
initializer has nothing to with it.

Brian
Jun 27 '08 #13
On 24 Apr, 19:53, Joe Greer <jgr...@doubletake.comwrote:
fred.l.kleinschm...@boeing.com wrote innews:1f**********************************@z24g20 00prf.googlegroups.com:
An array in C need not be terminated with a NUL character.
Such an array is not considered to be a string unless it is
NUL terminated.
Most C library functions (strxxx, printf, etc.) expect
their char* arguments to be strings.

I think it's less to do with the null and arrays than with the fact that in
C++

"Hello" is of type char const[6]

and trying to assign that to a char[5] will fail. *(There is a special rule
to get around the constness problem, but not the size problem.)

Of course, I am not really a language lawyer type, so I could be off base,
but that is what I think is happening.
try this:

int main (void)
{
char s7[7] = "1234567";
char s8[8] = "1234567";
char s9[9] = "1234567";

return 0;
}

according to your logic s9[] will also give an error in C++.

The fact is this is an odd corner of C. I thing "sloppiness"
is the wrong term. I think this was intentional. I believe
Unix used to have some odd things that were arrays of chars
(or small numbers) of fixed size. They were not terminated
by a zero. C provided means to manipulate them like
strncpy() and the initialisation stuff above.
--
Nick Keighley
Jun 27 '08 #14
Nick Keighley wrote:
On 24 Apr, 19:53, Joe Greer <jgr...@doubletake.comwrote:
fred.l.kleinschm...@boeing.com wrote
innews:1f**********************************@z24g20 00prf.googlegroups
.com:
An array in C need not be terminated with a NUL character.
Such an array is not considered to be a string unless it is
NUL terminated.
Most C library functions (strxxx, printf, etc.) expect
their char* arguments to be strings.
I think it's less to do with the null and arrays than with the fact
that in C++

"Hello" is of type char const[6]

and trying to assign that to a char[5] will fail. (There is a
special rule to get around the constness problem, but not the size
problem.)

Of course, I am not really a language lawyer type, so I could be
off base, but that is what I think is happening.

try this:

int main (void)
{
char s7[7] = "1234567";
char s8[8] = "1234567";
char s9[9] = "1234567";

return 0;
}
according to your logic s9[] will also give an error in C++.
And this works in C:

int main (void)
{
const char s7[7] = "1234567";

return 0;
}

I don't follow at all what he thinks the type of the initalizer has to
do with anything.
The fact is this is an odd corner of C. I thing "sloppiness"
is the wrong term. I think this was intentional.
As it is specifically mentioned in the C standard, with an example, I
think "intentional" is accurate.


Brian
Jun 27 '08 #15
"Default User" <de***********@yahoo.comwrote in news:67el3lF2nfuj8U1
@mid.individual.net:
>
And this works in C:

int main (void)
{
const char s7[7] = "1234567";

return 0;
}

I don't follow at all what he thinks the type of the initalizer has to
do with anything.
Well, apparently it doesn't have anything to do with it for char arrays.
Try initializing an object with the wrong type for most other things though
an you will have a problem. Anyway, I finally did what I should have done
to begin with and looked it up in the standard. In [dcl.init.string]/1-2
it says:

1. A char array (whether plain char, signed char, or unsigned char)
can be initialized by a stringliteral (optionally enclosed in
braces); a wchar_t array can be initialized by a wide stringliteral
(optionally enclosed in braces); successive characters of the
stringliteral initialize the members of the array.

[Example:
char msg[] = "Syntax error on line %s\n";
shows a character array whose members are initialized with a
stringliteral.
Note that because ’\n’ is a single character and because a trailing
’\0’ is appended, sizeof(msg) is 25. ]

2 There shall not be more initializers than there are array elements.
[Example:
char cv[4] = "asdf"; // error
is illformed since there is no space for the implied trailing ’\0’.
]

So, for whatever it's worth, they seem to consider a string literal to be
null terminated and refuse to let the compiler do the truncating for you.

joe
Jun 27 '08 #16
Joe Greer wrote:
"Default User" <de***********@yahoo.comwrote in news:67el3lF2nfuj8U1
@mid.individual.net:

And this works in C:

int main (void)
{
const char s7[7] = "1234567";

return 0;
}

I don't follow at all what he thinks the type of the initalizer has
to do with anything.

Well, apparently it doesn't have anything to do with it for char
arrays.
Which is what we are discussing.
Try initializing an object with the wrong type for most
other things though an you will have a problem.
Really? Try this:

int main(void)
{
const int ci = 3;
int i = ci;
double d = ci;

return 0;
}

There's a wide variety of compatible types in initalization. Some are
not. You could NOT for instance do:

int main(void)
{
const int ci = 3;
double *d = &ci;

return 0;
}

Anyway, I finally
did what I should have done to begin with and looked it up in the
standard. In [dcl.init.string]/1-2 it says:
[snip]
So, for whatever it's worth, they seem to consider a string literal
to be null terminated and refuse to let the compiler do the
truncating for you.
In C++. Not in C, where it is specifically allowed. Nothing to do with
the type of a string literal, but rather the fact that it is a string
literal.

Are we clear now?


Brian
Jun 27 '08 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Niels Dekker (no reply address) | last post by:
Is it possible for a standard compliant C++ compiler to have ( sizeof(short) < sizeof(int) ) and ( sizeof(short) == sizeof((short)0 + (short)0) ) ? Regards, Niels Dekker...
4
by: Mark Stijnman | last post by:
A while ago I posted a question about how to get operator behave differently for reading and writing. I basically wanted to make a vector that can be queried about whether it is modified recently...
3
by: Tao.Young | last post by:
Hi, I met with a very strange problem: the same function will generate different results, but when i uncomment Line1, it'll get the same results. anyone can tell me why? the code is...
82
by: zardoz | last post by:
I've got this problem: unsigned long long lTemp; char cLargeNum="1324567890"; sscanf(clargeNum,"%llu",&lTemp); which under Win32 isn't working*. My program needs to compile under posix so...
7
by: alex | last post by:
BlankThis maybe a known issue, but I stepped on it just now. Basically it boils down to: in C# int x = 0; Console.Write("{0}", x++ + x++); gives 1, when in C/C++ int x = 0; printf("%d\n",...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
2
by: marsarden | last post by:
here is the code: int main() { const int a = 1; int *p = const_cast<int*>(&a); *p = 2; cout << "value a=" << a << endl; cout << "value *p=" <<*p << endl;
6
by: Avi | last post by:
I need to implement the following calculation: f = (a*b) + (c*d) where a,b,c,d are given double values and f is a double variable for the result I found out that using two different...
87
by: pereges | last post by:
I have a C program which I created on Windows machine. I have compiled and executed the program on windows machine and it gives me the consistent output every time i run it. for eg. input a = 2,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.