Thread: Static Storage Class in C

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    1

    Question Static Storage Class in C

    Learning & getting upto speed with the concepts of C Programming. I recently read about storage classes and I have a question in my mind.

    Code:
    #‎include<stdio.h>
    int fun()
    {
    static int num = 16;
    return num--;
    }
    int main()
    {
    for(fun(); fun(); fun())
    printf("%d \n", fun());
    return 0;
    }
    Output : 14 11 8 5 2.How does this code works?

    keeping --num in fun(), is running an infinite loop. What can be the reason?
    Last edited by shrutiB; 03-03-2022 at 01:09 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    In C, zero is considered false.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    2

    Lightbulb

    When we are dealing with static variable, This variable persists until the end of the program and is stored in data segment. Some characteristics of a static variable are as follows:

    • Storage = memory
    • Default initial value = Zero
    • Scope = Local to the block in which the variable is defined.
    • Life = Value of the variable persists between different function call.


    Now Coming back to your question:


    Working of code
    Well for this we will start from main(). Thinking at compiler level, first focus will be at for loop. In the for loop, a one time initialization will be carried out. Now num = 15. Then it will check the given condition. for C, it will only compare Zero and Non-Zero values. Now for this it returns 14 and it is non-zero so it gets inside the loop. The magic of compiler begins. Read this for some information. So in your case while returning, it will first return the value and then decremented value by 1. 14 will be printed first, inc/dec block in for loop gets executed. Then again the condition will get evaluated. And again, while printing your function, it will return value first then decremented by 1. After all this iteration your output will be there.

    When you wrote --num, then it will simply decrement the value by 1 first, and then return a value. As mentioned earlier, Compiler only checks Zero and Non-Zero values. When you are dealing with --num then it goes into negative values and its value still gets decremented, so it will never meet at Zero. As a result an infinite loop. You can modify some values to check your results like modify num=17 for --num, You should probably get the same result.
    Last edited by Salem; 03-04-2022 at 09:53 AM. Reason: Removed low quality links

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    139
    Your fun() decrements the num one time. But your code paths call fun() more than once. Let's look:

    First, you enter main. From there you start the for(init; test; update) loop. This code path leads to the printf() call where the value is displayed. In your code, the init clause contains a call to fun, the test clause contains a call to fun, and the printf() call contains a call to fun. Because you used a postfix decrement operator, the returned value will be the value of num before the last decrement.

    So you start with num = 16, then call fun in the init clause (15), then call fun in the test clause (14), then call fun in the printf() call (13, but returns 14). Then we see the value "14" printed out.

    That's the first iteration of the loop. Now we handle the second and all subsequent iterations: Your loop ends, and the update clause contains a call to fun. Then the test clause contains a call to fun. We enter the body of the loop, the printf() call contains a call to fun, and that's the end of another iteration.

    Working it through, we had num = 13 from the last pass, so the update clause (12), the test clause (11), and the printf() call (10, but returns 11) all change the value of num. This is how the rest of the loop will go.

    On the next iteration, it will be update clause, test clause, printf() again, meaning 9, 8, 7-but-returns-8. Each time through, the value drops by 3.

    Each time the loop makes one pass, the value of num is reduced by 3. Because of where you started, that means you test the value of num (in the test clause) at 8, then 5, then 2, then -1, then -4, then -7, ... Now, eventually the value is going to "wrap around." That is, eventually your int num will get so low that it becomes high again. If you're on a system with 32-bit int values (which almost everyone is right now), you have UINT_MAX + 1 possible values. (4294967295 + 1 for 32-bits, 65535+1 for 16 bits, and I-have-never-bothered-to-memorize-it for 64 bits). (UINT_MAX is a preprocessor constant mandated by the C standard. It should be available on your system after #include'ing one of several header files.)

    Now UINT_MAX+1 is going to be a power of 2. It will be 2^16 or 2^32 or 2^64 depending on your compiler. Those values are not evenly divisible by 3 (which is the period of your loop) so ............ eventually .......... your loop will pass through the test clause with num == 0, at which point the fun() call will decrement it to be -1, return 0, and the for() loop will terminate. The question is will that happen the first time you pass 0 (no, you've tried that) or the second time you pass 0 (after printing UINT_MAX+1 more lines of text) or the third time you pass 0 (after printing UINT_MAX+1 more lines of text)?

    All you need is patience!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static storage class
    By vead in forum C Programming
    Replies: 10
    Last Post: 02-21-2018, 10:31 AM
  2. static is invalid storage class
    By Cotant in forum C Programming
    Replies: 2
    Last Post: 10-02-2015, 03:14 PM
  3. Need help: Explanation of a static storage class
    By Sam Pertuit in forum C Programming
    Replies: 7
    Last Post: 01-30-2013, 09:34 AM
  4. Doubt regarding storage of a static variable
    By karthik537 in forum C Programming
    Replies: 6
    Last Post: 02-21-2012, 01:21 PM
  5. static storage class
    By roaan in forum C Programming
    Replies: 4
    Last Post: 09-08-2009, 04:57 PM

Tags for this Thread