freelanceprogrammers.org Forum Index » C

Reg STORAGE CLASSES


View user's profile Post To page top
win_truth Posted: Tue Aug 26, 2003 8:39 pm


Joined: 25 Jul 2003

Posts: 4
Reg STORAGE CLASSES
HI,
 
Can anyone help me in (brief) understanding the below code
int i=0;  main( ) {     auto int i=1;   printf("%d
",i);                     i=1       {           int i=2;            printf("%d
",i);           i=2            {              i+=1;               printf("%d
",i);        i=3            }           
printf("%d
",i);            i=3               }             printf("%d
",i);                i=1
}
 i want 2 know what`s happening in the outer block i.e 5th printf statement
 main() {     auto float f1=20.4;           {              f1=10.8;                {                     f1=5.3;                     printf("%f",f1);      f1=5.3                }                printf("%f",f1);          f1=5.3
            }         printf( "%f",f1);                f1=5.3  }
 
similarly here in the 3rd printf statement !
 
with Regards,
 ashwini
Win TVs, Bikes, DVD players and more!
Click on
Yahoo! India Promos
Reply with quote
Send private message
View user's profile Post To page top
shantanu_k06 Posted: Tue Aug 26, 2003 11:34 pm


Joined: 19 Jul 2003

Posts: 29
Reg STORAGE CLASSES
Please see my embedded comments in the code

--- suganthi rams <ashvinistar@...> wrote:
> HI,
>
> Can anyone help me in (brief) understanding the
> below code
>
> int i=0;
> main( )
> {
> // defines i
> auto int i=1;
> printf("%d
",i); i=1
> {
> // defines new i, known in this block only
> int i=2;
> printf("%d
",i); i=2
> {
> // outer block`s i(=2) used here
> i+=1;
> printf("%d
",i); i=3
> }
> // still i(=2) used here
> printf("%d
",i); i=3
>
> }
> // block ends, so new def not applicable
> // i(=1) is used here
> printf("%d
",i); i=1
> }
> i want 2 know what`s happening in the outer block
> i.e 5th printf statement
>
> main()
> {
> // definition of f1
> auto float f1=20.4;
> {
> // no new definition, old var used
> f1=10.8;
> {
> // still no new definition
> f1=5.3;
> printf("%f",f1); f1=5.3
> }
> printf("%f",f1); f1=5.3
>
> }
> printf( "%f",f1); f1=5.3
> }
>
> similarly here in the 3rd printf statement !

Well everything`s in order, isn`t it?

Regards,
Shantanu


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com
Reply with quote
Send private message
View user's profile Post To page top
krishnaraj_pb Posted: Wed Aug 27, 2003 8:05 am


Joined: 27 Aug 2003

Posts: 1
Reg STORAGE CLASSES
Hi..
     When ever u declare a variable inside braces(`{`), the scope of the variable expires when u come out of the brace(`}`). In your example, inside each brace, you are declaring `i`. That `i` expires when you come out of the brace.
You are giving auto int i = 1 and after that you are opening and closing 2 braces. After coming out of all these, u are still inside the main brace. There i is 1. Thats why you got the result 1.

In the second case, you are declaring only once. Thats outside all the braces. Its scope exists until the end of main. Inside the braces, you are just assigning the values.. Thats why you got 5.3 at the last also.

Hope you are clear with the answer. If not, pls feel free to ask.

Regards,
K.Raj






suganthi rams <ashvinistar@...>
26/08/2003 09:09 PM
Please respond to Programmers-Town

       
        To:        C-Guru@yahoogroups.com, Programmers-Town@yahoogroups.com
        cc:        
        Subject:        *(&Programmers-Town&)* Reg STORAGE CLASSES


HI,
 
Can anyone help me in (brief) understanding the below code

int i=0;
 main( )
{
 
  auto int i=1;
  printf("%d
",i);                     i=1
      {
 
        int i=2;
           printf("%d
",i);           i=2
           {
             i+=1;
              printf("%d
",i);        i=3
           }
 
         printf("%d
",i);            i=3
       
      }
       
    printf("%d
",i);                i=1
}
 i want 2 know what`s happening in the outer block i.e 5th printf statement

main()
{
    auto float f1=20.4;
          {
             f1=10.8;
               {
                    f1=5.3;
                    printf("%f",f1);      f1=5.3
               }
               printf("%f",f1);          f1=5.3

           }
        printf( "%f",f1);                f1=5.3
 }
 
similarly here in the 3rd printf statement !
 
with Regards,
 ashwini
Win TVs, Bikes, DVD players and more! Click on Yahoo! India Promos


Yahoo! Groups Sponsor





Keep posting

Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help