Answers/Solutions to Exercises in Chapter 6, Exercise 6

E6: Will this compile? If so, will it run correctly?

char a[3] = {'a','b','c'};
...
printf("%s\n",a);
...

A6: Yes, it will compile fine. And it may run OK (if by a pure chance there is '\0' stored right behind 'c'), or it may run incorrectly (the most likely case), displaying abc...... and where ...... could be anything (if there is a '\0' to the right of 'c' in the memory belonging to this program), or it can be terminated for memory access violation, if there is no '\0' to the right of 'c' in the memory belonging to this program.

Back to Answers/Solutions Index                          Back to Answers/Solutions for Chapter 6 Index