What are the specifics of the definition of a string in C?

Viewed 266

I am supposed to answer a homework question for one of my classes. Specifically, I am supposed to say if certain arrays in C are considered strings or not. Based on this article (https://www.geeksforgeeks.org/strings-in-c-2/) I know that strings are an array of characters with the null terminator at the end.

My main hangup is a part of the question that asks about an array that looks like this:

char c1[] = { 'C', 'S', '\0', '3', '2', '4', '\0' };

This is obviously an array of characters with a null terminating character at the end. However, is it still considered a string since it also has a null terminating character in the middle? How will that affect the string?

EDIT: Based on comments, I have provided the actual wording of the question:

"Which of the following arrays can be considered "strings" for the purposes of using them as arguments to strcpy(), strncpy(), strcmp(), strncmp(), and similar string functions (indicate all the apply)?"

EDIT: I emailed my professor about it since the question seemed ambiguously worded (as several people pointed out). If anyone is curious, he told me "Yes it is a string. The key is that there is a null character. But of course that will effect any string operations; the string ends at the null character."

3 Answers

c1 is mostly [1] equivalent to &c1[0], which is holding one string, "CS".

There's a second string lurking in there, "324", starting at &c1[3] -- but as long as you access c1 as c1, the string "CS" is all the functions strcpy() et al. would see.


[1]: c1 is an array, &c1[0] is a pointer.

If you want to know the specifics of the definition of a string in C, go to the source.

From the C90 standard:

7 Library

7.1 Introduction

7.1.1 Definitions of terms
A string is a contiguous sequence of characters terminated by and including the first null character. A “pointer to” a string is a pointer to its initial (lowest addressed) character. The “length” of a string is the number of characters preceding the null character and its “value” is the sequence of the values of the contained characters, in order.

(There were no relevant changes in later standards.)

Thus, c1 contains two consecutive strings, "CS" and "324", but is not itself a string.

If we pass an array to a function, it decays to a pointer to its first element, thus +c1 points to a string (the first one), which is good enough for any function expecting a pointer to string. It doesn't point to a string "CS\0324", but that's probably good enough for your instructors question, which is ambiguous.

Adding to @DevSolar's answer, something I discovered after toying around with the given string, if it were to be:

char c1[] = { 'C', 'S', '\\0', '3', '2', '4', '\\0' };

If you output this string, you will get CS03240 and the size of this string is 7. As far as my understanding goes, \\0 is used to denote the null character (i.e. \0). If you do:

printf("\0");

You don't see anything on the output log, but if you do:

printf("\\0");

You see a \0, something which is expected because to output special characters such as backslashes or quotes, you need to use a \ along with them.

Something which puzzles me is the output CS03240 and it's size 7. It is common understanding that the size of a string is the number of characters in it plus one(for the null character). Also, the size is 7 even for the string, char c1[] = { 'C', 'S', '\0', '3', '2', '4', '\0' };.

So maybe a follow up to this question, what is going on here?

Related