0
How many time will this loop run?
For(char c; c<256; ++c) {}
7 Answers
+ 5
How many times?
ā” Never
error 1:'For' undefined //use for
error 2: c is uninitialised //:)
+ 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
+ 3
Run this code on IDe, u wil see best answers and it will make your concepts strong
+ 3
š®š³Omkarš Don't C++ give it a garbage value then? Instead of raising the error 2?
+ 2
š®š³Omkarš Did you mention the static storage class twice with a purpose? 3 and 5?
+ 1
Have you tested it out?
The best way to learn programming is to test code out for youself.
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!