-8
#include<stdio.h>
#include<string.h>

void terminateString(char *str){
    str[3] = 0;
    printf("string after termination is:%s\n",str);
}

int main(){
    char str[]="abababcdfef";
    terminateString(str);
    return 0;
}

Output:

string after termination is:aba

We are only assigning element at index '3' to 0, but why are all characters after that index are ignored? Can someone please explain this behavior?

Toby
  • 9,696
  • 16
  • 68
  • 132
Sukeesh
  • 183
  • 2
  • 10
  • 2
    Possible duplicate of [String termination - char c=0 vs char c='\0'](https://stackoverflow.com/questions/16955936/string-termination-char-c-0-vs-char-c-0) – Manvir Aug 10 '17 at 05:00
  • c strings are NULL terminated, however c++ std::strings are not. – Pruthvikar Aug 10 '17 at 05:17
  • 6
    Do ***not*** edit your question to add a new chunk of code after you start getting answers. That is *unfair* to those who are trying to help you. Ask your complete question from the start — or ask two separate questions. Chameleon questions are extremely (and justifiably) unpopular on SO. – Jonathan Leffler Aug 10 '17 at 05:25
  • @JonathanLeffler Sorry for that. I had doubt between C and C++. Hence, asked the question which I again thought would require more details for to the point answers. But the edited question is completely related to the old one and would obviously help who want to compare C strings with C++ strings and also old answers are related for edited one too. – Sukeesh Aug 10 '17 at 05:29
  • 1
    The trouble is that Jeremy answered your original question. Then you edited the question, and made his answer incomplete. If you'd posted both parts up front, you'd probably have got an answer from him. iBug and paxdiablo also answered the original question; in fact, so did Austin and Doug — that's five of five answers. It's just unfair to revise it once you've gotten answers! By all means ask the related C++ question as a separate question. Or ask the question as 'compare and contrast' from the start. It's rewriting (doubling the size of) the question after you get answers that is not OK. – Jonathan Leffler Aug 10 '17 at 05:32
  • Incidentally, you should probably send the output of your C++ code through a hex dump program; you'll probably see the null byte in the output that way. It won't display as anything on a terminal, though. And it was puzzling to see you using `` rather than `` in your C++ code. – Jonathan Leffler Aug 10 '17 at 05:36
  • Are you looking for answers in C *or* C++? You have tagged both yet they are different languages and each follows different rules. Please only tag the language for which you would like answers. As the code you have shown is C, i will suggest and edit to remove the C++ tag for now. – Toby Aug 10 '17 at 14:15

5 Answers5

7

We are only assigning element at index '3' to 0, but why do all characters after that index are ignored? Can someone please explain this behavior?

The convention with a zero-terminated string is that the 0 byte is what indicates the end of the string. So when printf() encounters the zero-byte at position 3, it stops printing.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
3

The ISO C standard defines a string as follows (see, for example, C11 7.1.1 Definition of terms), emphasis is mine:

A string is a contiguous sequence of characters terminated by and including the first null character.

Hence, when you have the character sequence abababcdfef\0, that is indeed a string.

However, when you put a null at offset 3, the string is not aba\0abcdfef\0 but, by virtue of the fact it's only a string up to and including the first null, it is aba\0.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

C-string is Null-terminated string. With null-terminated it means "a null character terminates (indicates the end of) the string".

A null character is a character with all its bits set to 0, or \0, presented in memory as 0x00.

When you set str[3] = 0 you're changing str[3] to the terminator token, so when printf reads the terminator, it thinks the string is end and only prints "aba".

iBug
  • 35,554
  • 7
  • 89
  • 134
1

What you are demonstrating is the difference in c++ between strings and char arrays. Strings are a sequence of characters that continue up to and including the first null character. A character array is a memory allocation unit. A string might not use the entire character array allocated for it (indeed it is possible that it may even exceed the bounds of the containing array). If you want to diagnostically print an array rather than a string, you would need to iterate over the array in a loop. See below:

#include<stdio.h>
#include<string.h>

void terminateString(char *str){
    str[3] = 0;
    printf("string after termination is:%s\n",str);
}

int main(){
    char str[]="abababcdfef";
    terminateString(str);
    for (int i = 0; i < sizeof(str)/sizeof(str[0]); i++) {
        (str[i] != 0) ? printf("%c ", str[i]) : printf("\\0 ");
    }
    printf("\n");
    return 0;
}
// OUTPUT
// string after termination is:aba
// a b a \0 a b c d f e f \0
Doug Coburn
  • 2,485
  • 27
  • 24
-3

c/c++ doesn't really distinguish between 0, '\0', and NULL, they're all just 0 in memory. c style strings are a sequence of characters that end with '\0', so every function that works with them ends after it finds this char. When you assign str[3]=0; it's the same as str[3]='\0'; i.e. stop the string after 3 chars. If you want the letter 0, do str[3]='0';, where the single quotes let the compiler know you want the character 0, ascii 48

Edit:

Note that NULL is a macro that evaluates to 0, not the same as nullptr apparently starting with C++11 NULL can evaluate to nullptr, in C or C++98, it is 0 http://www.cplusplus.com/reference/cstring/NULL/

Austin_Anderson
  • 900
  • 6
  • 16