Ad
About Block Scope Chaining, No Error, No Warning, But The Result Is Surprised And I Can Not Figure Out Why
#include <stdio.h>
int main(void) {
int count = 22;
{
int count = count * 2;
printf("inner: %d\n", count);
}
printf("outer: %d\n", count);
return 0;
}
output:
inner: 65532
outer: 22
The output is surprised, I can not figure out why.
edit: compile method: gcc test.c
Ad
Answer
int count = count * 2;
in count * 2
count is not initialized because it is the inner count, not the outer count as you probably supposed
so your code is equivalent to
int main(void) {
int outer_count = 22;
{
int inner_count = inner_count * 2;
printf("inner: %d\n", inner_count);
}
printf("outer: %d\n", outer_count);
return 0;
}
but not to
int main(void) {
int outer_count = 22;
{
int inner_count = outer_count * 2;
printf("inner: %d\n", inner_count);
}
printf("outer: %d\n", outer_count);
return 0;
}
no warning
I do not know what compiler and options you use, but :
[email protected]:/tmp $ gcc -Wall c.c
c.c: In function ‘main’:
c.c:6:9: warning: ‘count’ is used uninitialized in this function [-Wuninitialized]
int count = count * 2;
^~~~~
[email protected]:/tmp $
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS Backend Loging Hash Error
- → "failed to open stream" error when executing "migrate:make"
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Create plugin that makes objects from model in back-end
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → How to install console for plugin development in October CMS
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → How to update data attribute on Ajax complete
- → October CMS - Conditionally Load a Different Page
Ad