+ 13

Why does it output extra characters?

I'm tinkering with ways to output ASCII box drawing characters ╔, ═, ╗, ║, ╝, and ╚. For some reasons this program prints extra characters at the end. I have also noticed that those extra ones are all these characters that I assigned in variables and they are printed in reverse order of their variable initialization. But uncommenting line 23 makes everything fine (no extra characters printed). Why? https://code.sololearn.com/c7s0JnF8d2Z9/?ref=app

7th Jan 2019, 6:05 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
12 Respuestas
+ 6
This behaviour is because how local variables are pushed on the stack by compiler... In our case, first vars are to higher memory location on the stack then, when you print an char[] inited with not-0 terminated, cout will print the sequence of chars while an 0 is encountered in memory and, because previously allocated vars are store to higher memory locations, they will be printed
7th Jan 2019, 2:18 PM
KrOW
KrOW - avatar
+ 22
Giving the array "top" a size of 6 instead of 5 also seemed to work for me actually both these worked: char top[6] = {c,d,d,d,e}; char top[6] = {c,d,d,d,e,'\0'}; so my guess is the compiler autommatically regarded to the extra space as '\0' and without it in the original code, the compiler cannot know where the string pointer should ends, causing extra printing of garbage values (in this case, the rest of the declared variables). * it's been a while since i been in CPP course so take that into account as well 😅
7th Jan 2019, 6:37 AM
Burey
Burey - avatar
+ 11
Gordie is right Try this: char c=201; char d=205; char e=187; char f='A'; char g='B'; char h='C'; And run the code for interesting output
7th Jan 2019, 6:46 AM
Burey
Burey - avatar
+ 9
Jonathan Pizarra exactly :)
7th Jan 2019, 6:55 AM
Burey
Burey - avatar
+ 8
Ahh the null termination character. So it is only implicitly added if I used string instead of char array. Thanks Burey and Gordie.
7th Jan 2019, 6:49 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 4
KrOW thanks for making it clear. Кошка Вика your answer is informative but is not related to the question.
8th Jan 2019, 11:28 PM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
+ 4
Jonathan Pizarra You are welcome 👍👍👍
9th Jan 2019, 7:40 AM
KrOW
KrOW - avatar
+ 2
Кошка Вика I dont understand why you posted that comment... Its unrelated to problem
8th Jan 2019, 2:15 PM
KrOW
KrOW - avatar
+ 1
you have to specify the end other with one array more than ur entered elements or mentioning the null, when u dont specifying the end, system will print upto the last null which is placed by system automaticly, hope I am clear regards
8th Jan 2019, 4:55 PM
Masoud HMD
Masoud HMD - avatar
+ 1
you made me remember my programming classes with turbo c++ i used this characters to make a menu
9th Jan 2019, 1:53 AM
Roger C
Roger C - avatar
- 2
Yeetus McCletus
7th Jan 2019, 2:15 PM
Condage
- 2
In computer science, the time complexity is the computational complexity that measures or estimates the time taken for running an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm, supposing that an elementary operation takes a fixed amount of time to perform. Since an algorithm's running time may vary with different inputs of the same size, one commonly considers the worst-case time complexity expressed using Big-O notation, which is the maximum amount of time taken on inputs of a given size. For example, an algorithm with time complexity O(n) is a linear time algorithm.
8th Jan 2019, 2:08 PM
Кошка Вика
Кошка Вика - avatar