Interview Questions

It is legal in ANSI C

C Interview Questions and Answers


(Continued from previous question...)

It is legal in ANSI C

Q:Is char a[3] = "abc"; legal?

A;It is legal in ANSI C (and perhaps in a few pre-ANSI systems), though useful only in rare circumstances. It declares an array of size three, initialized with the three characters 'a', 'b', and 'c', without the usual terminating '\0' character. The array is therefore not a true C string and cannot be used with strcpy, printf's %s format, etc.

Most of the time, you should let the compiler count the initializers when initializing arrays (in the case of the initializer "abc", of course, the computed size will be 4).

(Continued on next question...)

Other Interview Questions