0

How many time will this loop run?

For(char c; c<256; ++c) {}

19th May 2019, 5:43 AM
thįŗ£o nguyį»…n
thįŗ£o nguyį»…n - avatar
7 Answers
+ 5
How many times? āž” Never error 1:'For' undefined //use for error 2: c is uninitialised //:)
19th May 2019, 9:17 AM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 5
Seb TheS Good point ;) Thanks for rectifying me! In c and cpp also there are 4 storage classes: 1.auto /local 2.extern /global 3.static 4.register In general any variable defined without declaring their storage class is a auto/local variable The default value of the auto variable is a garbage value. . For static and extern classes this value is 0 by default For register class it's garbage. The variable defined in loop is treated as local variable so I was expecting that it'll get garbage value but actually SL compiler is assigning value 0 to it not garbage :( I'm confused at this point why value is 0. because I think it is a local variable... as we can't access it outside of loop You can try : for(int i; i<=10;i++) cout<<i; /*it prints numbers from 0 to 10 means compiler is assigning it 0 implicitly */ I'll try to ask the question in another thread :) By the way thįŗ£o nguyį»…n answer is āž” 256 because count is starting from 0 and according to loops condition it'll stop when c becomes 256
19th May 2019, 10:48 AM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 3
Run this code on IDe, u wil see best answers and it will make your concepts strong
19th May 2019, 8:51 AM
Muhammad Rashid
Muhammad Rashid - avatar
+ 3
šŸ‡®šŸ‡³OmkaršŸ•‰ Don't C++ give it a garbage value then? Instead of raising the error 2?
19th May 2019, 9:42 AM
Seb TheS
Seb TheS - avatar
+ 2
šŸ‡®šŸ‡³OmkaršŸ•‰ Did you mention the static storage class twice with a purpose? 3 and 5?
20th May 2019, 6:13 AM
Seb TheS
Seb TheS - avatar
+ 1
Have you tested it out? The best way to learn programming is to test code out for youself.
19th May 2019, 6:24 AM
Dragonxiv
Dragonxiv - avatar
0
Thank you guys for all the advice and very detailed answers. I met this question in a random challenge and the answer is "Infinitely", which confused me a lot!
22nd May 2019, 5:11 PM
thįŗ£o nguyį»…n
thįŗ£o nguyį»…n - avatar