Look at it from a character stand point:
char a[]="Visual C++";
printf("length = %d\n", strlen(a));
for(int i = 0; i<strlen(a); i++)
printf("a[%d] = %c (ASCII %d)", i, a[i], a[i]);
With code like this, you'll get
length = 11
a[0] = V (86)
a[1] = i (105)
a[2] = s (115)
a[3] = u (117)
a[4] = a (97)
a[5] = l (108)
a[6] = (32)
a[7] = C (67)
a[8] = + (43)
a[9] = + (43)
a[10] = (0)
Checking those values against an ASCII Table you can see why it shows 11 (the NULL
terminator)
char b[] = "Visual\C++";
Your second string has an escape char in it \
, there's lots of lists of them, but it basically tells the compiler to ignore the next character because it's not to be printed, but something special. Just like the newline character: '\n'
Based on your comments on the original post, I think I need to clarify two extra things:
Extra Note 1:
A special char, such as newline '\n'
or null terminator '\0'
only takes 1 extra byte of space.
Extra Note 2:
sizeof(a)
will give you the size (number of characters) of your array because it's full of characters which only take 1 byte each. When you're using this on other types that take up more space you need to do one more step:
int arr[4] = {0};
int size_of_arr = sizeof(arr/sizeof(int));