+ 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
12 Answers
+ 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
+ 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 😅
+ 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
+ 9
Jonathan Pizarra exactly :)
+ 4
KrOW thanks for making it clear.
Кошка Вика your answer is informative but is not related to the question.
+ 4
Jonathan Pizarra You are welcome 👍👍👍
+ 2
Кошка Вика I dont understand why you posted that comment... Its unrelated to problem
+ 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
+ 1
you made me remember my programming classes with turbo c++
i used this characters to make a menu
- 2
Yeetus McCletus
- 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.