[check the description] : does the strinig terminator "\0" included before x since i used i<8, but while printing it, the output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[check the description] : does the strinig terminator "\0" included before x since i used i<8, but while printing it, the output

CODE : int main() { char firstName[] = "ANIRBAN"; for (int i = 0; i < 8; i++) { printf("%c \t", firstName[i]); } printf("%c", 'x'); return 0; } OUTPUT : A N I R B A N x

13th May 2022, 4:57 AM
blueshipswims
blueshipswims - avatar
4 Answers
+ 4
Print firstName[ i ] as char and integer to find out whether the null terminator was also printed. printf("%c (%d)\t", firstName[i], firstName[i]); But as you probably know already, null terminator is non printable character, thus you won't likely see any character prior to the '(0)' printed by '(%d)' format specifier. And iterating a string past its length is not a safe thing to do, so evade it best you can ...
13th May 2022, 6:54 AM
Ipang
+ 1
Glad you got it figured out 👌
13th May 2022, 8:41 AM
Ipang
0
Can you point out what's wrong? In this code every character in the array is printed separated by a space character and a tab character (\t). Because of the null-terminator, after 'N' there is double the space (" \t \t"), probably causing the 'x' going on newline because of all the spacing before
13th May 2022, 5:57 AM
OrHy3
OrHy3 - avatar
0
@Ipang i tried it....and actually it works ... this is the output : A (65) N (78) I (73) R (82) B (66) A (65) N (78) (0) x
13th May 2022, 8:19 AM
blueshipswims
blueshipswims - avatar