Question:
C Programming Question Related to Character Arrays?
?
2017-07-20 08:16:04 UTC
Hello, I have programming related question that I couldn't find a satisfying answer to. I don't think I understand what I came up with. Therefore I hope someone can help me clarify it.

So, let's say I want to declare a character array in C that holds only one letter. We do this by

char x[] = "a";

This part is clear to me. I also do understand that character arrays are terminated by a null character '\0'. So in our university textbook I came across another declaration of single letter character array (which was unfortunately not explained).

char x[] = {'a', 0};

Naturally I had to dig into the problem and so I've found a site (link down below) where it says that when initializing a character array by listings all its characters separately you must supply the '\0' character explicitly. According to their example, I've tested a code

char x[] = {'a', '\0'};

which worked flawlessly. This makes me wonder, why is plain 0 same as '\0'?
Funny part it, I went even a step further, running a code

char x[] = {'a', '0'};

which turned out being a0@.

Would somebody be kind enough to help me understand what I'm looking at? Or at least copy some links to where I can actually read about it?

Link: http://www.studytonight.com/c/string-and-character-array.php
Three answers:
?
2017-07-20 10:32:18 UTC
In the first example (char x[] = "a";), the double quotes mean take whatever is inside the quotes and treat this as a string of characters, and add a NULL at the end of the string to mark the end of the string. So



char x[] = {"abcd"};



would define a 5 character string with the character values (in decimal) of 97, 98, 99, 100, 0. Where 97 is the decimal value for the character 'a', and so on, and 0 is the terminating NULL character,



These characters can be represented in hexadecimal as 0x61, 0x62, 0x63, 0x64, 0x00. The 0x is a prefix that C and many other languages take to mean hexadecimal. They can also be represent in octal as 0141, 0142, 0143, 0144, 0. In this case, the leading 0 is treated as an instruction to treat the number as octal.



However, when you use single quotes, C takes the value as a single character. Each single character is still valid as normal, but there is no terminating NULL character provided as single quotes denote individual characters rather than strings.



Without any quotes, C takes a numeric value at its face value, so when you say:



char x[] = {'a', 0};



The 'a' is taken as a single character, which is followed by another character of numeric value 0. This is the NULL that will terminate the string x.



Now, when you use



char x[] = {'a', '0'};



you are defining two characters in x with decimal values of 97 and 48, which are the ASCII codes for a and 0.



When you use a back slash character '\', you are using this as an escape character to allow you to define a character that is not normally available. For example, '\n' means a newline character (decimal value 10); '\r' is a return character (decimal value 13); and '\\' is used to mean a real single back slash. For '\0', this is taken as defining the NULL character, and will produce the decimal value 0 rather than 48 that occurs when you use '0' in single quotes but without the back slash.



Finally, when you used



char x[] = {'a', '0'};



You defined two characters in the array 'x', but you did not terminate the array. x[0] will have the value 'a' and x[1] will have the value 0 (decimal code 48 not the NULL character) as it was in single quotes. The array 'x' is defined as being only two characters long, but if you treat it as a string, then C will run through the following memory locations until it encounters a NULL character, which it will take as the end of the string. Often this will be another variable in the program. For example, with my compiler:



char b[] = {"test"}, a[] = {'d', '0'};

printf("%s\n",a);



produces the result



d0test



My compiler has allocated memory for arrays in reverse order from the order they were defined in the source code. Array 'a' is two characters 'd0' but the printf ran on through the following array until it encountered a NULL character marking the end of the string.



I hope this helps.
Kush
2017-07-20 09:40:46 UTC
a single character we enclose it between single quotes ('), like 'x'.

what you declared was a string (which generally consists of more than one character) we enclose it between double quotes
?
2017-07-20 09:14:20 UTC
In the first example (char x[] = "a";) the null character is implicitly added. You understood this part all right.



In the second example, you explicitly add the null character as '\0'

What you are doing between the quotes is escaping the character 0 (which in ascii is 48) to have a special meaning. What would this special meaning be? A byte with all its bits 0 ↔ the same as the value 0 would have (without quotes)



In memory a string of characters would look like:

F0 A1 22 3E 49 03 AE 00

→→→→→→→→→→ ↑

every function that works with arrays of characters would do this:

-get a pointer to a part of memory (the beginning of the string)

-do stuff with it until a 0 byte is encountered (0 byte has been chosen arbitrarily to represend the end of a string)





Finally in your third example you got lucky that your function printed only "a0@". In a worse case it would have printed 3 screens full of gibberish that it found in memory until it found a 0 byte. Remember, C evaluates everything as a number → characters become ascii.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...