1

I was practicing c questions through "Test your skills in c" books where I found this question with following code

char str[5]="abhisheksoni";

and this question was "Will this generate any kind of error" and answer given was "No compiler never detects the error if bounds of an array are exceeded." But when i code this program an error "Too many initialization " generated??

Barmar
  • 741,623
  • 53
  • 500
  • 612
user3335653
  • 133
  • 1
  • 8
  • A compiler is free to choose how it treats programs which exhibit undefined behavior. – Paul Hankin Feb 22 '14 at 08:34
  • Q: Was it a compiler "error" or a "warning"? ALSO: the book is correct that you won't necessarily get a *RUNTIME* error when the code is *EXECUTED*. – FoggyDay Feb 22 '14 at 08:34
  • It is trivial for a compiler to detect this error. But "No compiler never detects ..." has two negatives, so it really means that all compilers sometimes detect this :-) – juanchopanza Feb 22 '14 at 08:35

4 Answers4

4

The book is incorrect. The declaration is invalid (i.e. ill-formed in C++ terminology, or contains constraint violation in C terminology), which is what we informally call a compile error. It means that any conforming compiler is required to issue a diagnostic message for this declaration.

It is worth adding that C and C++ languages are slightly different with regard to string literal initializers that are too long by one character. C allows the terminating zero to fall-off the end of the array, while C++ doesn't.

char str[4] = "abcd"; // OK in C - produces a non-zero-terminated array
                      // Error in C++

But your original example, where the initializer literal is too long by more than one character, is ill-formed in both languages.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
3

No compiler never detects the error if bounds of an array are exceeded.

That's a double negative. In English, that's the same as "every compiler at least sometimes detects exceeding the bounds of an array." Providing too many initializers may or may not be considered as such.

Anyway, it is forbidden by C11 §6.7.9/2:

No initializer shall attempt to provide a value for an object not contained within the entity being initialized.

and C++11 §8.5.2/2 [dcl.init.string]:

There shall not be more initializers than there are array elements.

However, C and C++ do not require anything besides a message to be printed when your program contains something illegal. The message could be labelled merely as a warning, and the program still compiles and runs. (C++11 §1.4/2; C11 §5.1.1.3/1 and footnote.)

Oddly enough, Clang and GCC mark it as a warning when compiling C and an error for C++. This perhaps reflects something about usage in practice.

Since the declaration looks like an error, it's reasonable to expect at least a warning message from some compiler, and unreasonable to do it on purpose in any case. Why ask the compiler when you know it's wrong?

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • -1 because C doesn't allow this either any more than C++ does. –  Feb 22 '14 at 08:44
  • @hvd Thanks for the correction; I lazily took remyabel's word for it. – Potatoswatter Feb 22 '14 at 09:18
  • Downvote removed, your answer looks good to me now. Now I'm just getting confused by the question (whether the OP got an error or a warning, and if an error, whether the OP attempted to compile as C or as C++) :) –  Feb 22 '14 at 09:22
2

It is not necessary that every compiler will raise any error or warning for this. Some compilers may throw a warning(in C99 mode compiled with: -Wall -Wextra -pedantic -g3 -std=c99):

[Warning] initializer-string for array of chars is too long [enabled by default]  

Better to use

char str[]="abhisheksoni";
haccks
  • 104,019
  • 25
  • 176
  • 264
-1

There is no guarentee that compiler will ignore or report undefined behavior

char ch[5]="abhisheksoni";

Means 5 bytes are reserved but you entered more so this is undefined behaviour and may overwrite important data so it is not guranteed that compiler will show error as in my system it is working

OldSchool
  • 2,123
  • 4
  • 23
  • 45
  • It is not undefined behavior; it is an error requiring diagnosis. If the compiler chooses to produce an executable after complaining that the program is wrong, then the behavior of the executable is undefined, but it's outside the scope of the standard. – Potatoswatter Feb 22 '14 at 09:29