0

Why does the following code produce an error? I don't understand why the curly braces are making a difference.

#include<stdio.h>

int main(void)
{
    {
        int a=3;
    }

    {
        printf("%d", a); 
    }

    return 0;
}
5
  • 16
    Learn about scope of a variable in C.
    – Eugene Sh.
    Commented Oct 29, 2018 at 15:28
  • As far I know Scope of variable is a concept when more than one function is concerned sir. But here I am having one function only i.e main( ) Commented Oct 29, 2018 at 15:30
  • 4
    Scopes is related to blocks, and the curly braces introduce a new block. Commented Oct 29, 2018 at 15:31
  • Does that mean the declaration of a , should be outside block and Inside block there should be only a=3 ? Commented Oct 29, 2018 at 15:38
  • @DebmalyaPanday probably yes, it depends on what you want to do. Commented Oct 29, 2018 at 15:57

3 Answers 3

10

The scope of a local variable is limited to the block between {}.

In other words: outside the block containing int a=3; a is not visible.

#include<stdio.h>
int main()
{
    {
      int a=3;
      // a is visible here
      printf("1: %d", a);  
    }

    // here a is not visible
    printf("2: %d", a);  

    {
     // here a is not visible either
      printf("3: %d", a); 
    }

    return 0;
}

Hint: google c scope variables

2
  • 1
    @DebmalyaPanday you should accept an answer that solved your problem by clicking on the "tick mark" under the score of the answer. Commented Oct 29, 2018 at 15:49
  • This could be phrased better. “… outside the block… there is no a” is not true. First, we should be precise: A variable is an object (a region of storage) and an identifier (a name for it). An identifier declared inside a block has block scope. The identifier is not visible outside the block, but the object may exist when execution is outside the block, as when a subroutine is called, so execution goes somewhere else but the block is not finished executing. It is not necessary to explain all details for an elementary question, but the statements that are written should be correct ones. Commented Oct 29, 2018 at 15:53
1

Variables that are defined inside curly braces exist only while the program is running within the braces. When the program exits the '}' then like in your case these variables are destroyed and the memory that used to be occupied is returned to the system.

If you are in need of this implementation what you can alter it so that the definition will be outside of the curly braces. For example :

   c/c++

   #include <stdio.h>
   
   int main(){

    int a;

    {a = 3;}

    {printf("%d",a) ;}
    
    return 0;}
3
  • 1
    A C-program that references a variable that is out of scope will not compile, as that is an error, but it's important to understand as a programmer that variables don't get "destroyed" and their memory "returned to the system" when they fall out of scope. The compiler is allowed to tell the cpu to do whatever it wants with the stack memory or cpu register the variable takes up when it's lifetime ends. It could overwrite it, or it could leave it untouched for as long as it wants. Accessing a variable's memory after it falls out of scope is undefined behavior, so anything could happen. Commented Apr 21, 2022 at 19:47
  • I guess what I'm trying to say is you're mixing up C-language syntax and guaranteed compiler behavior. You can think of C programs as "destroying" variables if it helps you understand scoping rules, but this isn't really something that happens. Quote on quote "variables" kinda don't even exist anymore once the program is compiled. Commented Apr 21, 2022 at 19:53
  • Thanks for adding this info, I found my answer in a C++ textbook but it didn't go any further as of what destroying the variable actually means.
    – gigis95
    Commented Apr 22, 2022 at 15:55
0

You cannot access the variable outside the nearest pair of opening and closing curly braces else it will give compile time error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.