mercredi 9 août 2017

String termination C/C++ char = 0

#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 do all characters after that index are ignored? Can someone please explain this behavior?

Aucun commentaire:

Enregistrer un commentaire