Printing of string with %s format specifier yields no output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Printing of string with %s format specifier yields no output

So I came across a question in a C challenge, and I just want to make sure I understand why the answer is the way it is. The question is asking for the output of the following code: char p[20]; char *s = "string"; int length = strlen(s); for (int i = 0; i < length; i++) p[i] = s[length - i]; printf("%s", p); The correct answer is No output. At first I thought it was because '\0' was assigned to the first character, so maybe the characters after it weren't read when printing. I know C strings cannot be reassigned as a whole, but their elements can, similar to arrays of other data types. I know this because I could reassign and print out the other characters of the string individually. I changed the initialization of i in the for loop to 1, the condition to i <= length, and even added a '\0' after the last character in the array, but it still does not output anything when I use the %s format specifier. What's going on?

29th Sep 2020, 2:58 PM
Zerokles
Zerokles - avatar
5 Answers
+ 3
<length> gets 6 from strlen(). So in the for-loop - when you do this p[i] = s[length - i]; And <i> value was zero, you copy s[6 - 0] (which is a string terminator) into p[0]. This means the first character in <p> is a null character. %s normally reads characters from pointed address (first char address) forward, until string terminator was found. But in this case, because p[0] is null character, it stops there assuming the string (char array) was empty.
29th Sep 2020, 3:16 PM
Ipang
+ 1
Ipang Yes, but I've also tried moving the initial value of i to 1. It still does not output anything. Edit: Nevermind. It does work when i's initial value is 1, but for some reason, p[length] = '\0'; causes it to have no output again even when p[length] is after the other characters.
29th Sep 2020, 3:29 PM
Zerokles
Zerokles - avatar
+ 1
Put i=1 don't change p[0] which is '\0', If it is. Try like int length = strlen(s) - 1; And loop condition as i<=length; Or also p[i] = s[length-i-1]; may works i think. (Not tested.)
29th Sep 2020, 3:37 PM
Jayakrishna 🇮🇳
+ 1
Initializing <i> value by 1 wouldn't change the behaviour if your compiler initializes <p> with nulls. The first character in <p> will likely be a null char anyways. If your compiler doesn't initialize <p> with nulls, then you *may* see weird character printed; as the first character in <p> is not a null character. Try to print each character casted into `int` to verify this ...
29th Sep 2020, 3:39 PM
Ipang
+ 1
Thanks Ipang and Jayakrishna🇮🇳 . Both of these are correct. The reason why it still had no output after I made some changes on the for loop was the '\0' I added at the end of p while testing. Don't know why that happens though since the '\0' was placed after the other characters.
29th Sep 2020, 3:52 PM
Zerokles
Zerokles - avatar