Thread: Why does un-assigned value output as 4069?

  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    33

    Why does un-assigned value output as 4069?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main ()
    {
        int passes = 0;
        int failures = 0;
        int student = 1;
    
        while (student <= 10) 
        {
            printf ("Enter 1 for pass, 2 for fail: ");
            int result;
            scanf ("%d", &result);
        
            if (result == 1)
            {
                passes = passes + 1;
            }
            else 
            {
                failures = failures + 1;
            }
            
            student = student + 1;
        }
        
        printf ("%d students passed,", passes);
        printf (" %d students failed.\n", failures);
    
        if (passes > 8)
        {    
            puts("Congratulations! Instructor gets a bonus!");
        }
    
        return 0;
    }
    Above code gives me the correct number of pass/fail grades corresponding with my input, but if I don't assign "passes" and "failures" to zero, I get weird output. Why does it do this, I thought un-assigned output by default was 0? Here is the output for when I just had it as "int passes;" and "int failures;" at the top.

    Code:
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    Enter 1 for pass, 2 for fail: 1
    10 students passed, 4096 students failed.
    Congratulations! Instructor gets a bonus!
    As you can see, at the end of the program, 4096 was the value of "failures". Nothing else about the code was different!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I thought un-assigned output by default was 0?
    Why?
    Some languages do that, but C doesn't.
    C is very low level and doesn't do much for you automatically.
    Only global variables and local static variables start automatically zeroed.
    So you need to remember to explicitly initialize local variables.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    Several google searches told me that by default it was zero, thanks for clearing that up...

  4. #4
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by C_me_run View Post
    Several google searches told me that by default it was zero, thanks for clearing that up...
    Google (and now ChatGPT) will usually give you what you would *assume* the answer would be, given a shallow understanding of the topic. Basically it will be bias confirmation - if you ask a naïve question you will get a naïve answer.

    For example, I just Googled "what is the default value of a variable in c", and Google tells me, in big bold letters "If the static variable is not initialized in the program, then by default, the initial value will be assigned to zero".

    This is completely true, but at the same time the wrong answer to the question.

    So I tried the more accurate question "what is the default value of a local variable in c", and got in big bold letters "value − Any value to initialize the variable. By default, it is zero", which is completely wrong.

    This is going to be such a big thing going forward, trust in internet as a source of knowledge will reduce.

    I recommend that everybody find a good C reference book, or even try to skim-read the standard.

  5. #5
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    Quote Originally Posted by hamster_nz View Post
    This is going to be such a big thing going forward, trust in internet as a source of knowledge will reduce.

    I recommend that everybody find a good C reference book, or even try to skim-read the standard.
    Google can be a great source of information but a lot of the things people put on the internet are just wrong, or maybe AI deduced the wrong answer like you were saying.

    I was thinking about this in the morning, generally you can find an answer to any question on google but that doesn't mean the answer is genuine and helpful. A lot of it seems to be based on finding some sort of comforting/quick answer that creates a feeling of satisfaction.

  6. #6
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    Just to prove the point again:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ()
    {
        int a;
    
        printf ("%d\n", a);
    }
    My first output to this is 22034, my second output is 21903. So therefore there is no "default value", it probably has more to do with the RAM address that a program whimsically chooses.

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    All the 'local' variables live on the 'stack'. The uninitialized variables in your main() function are reusing areas of the stack that have been previously used as the C runtime sets up the environment (e.g. when it creates the "heap", creates the "FILE *" for stdin, stdout, stderr, sets default signal handlers, works out what the locale is, and so on).

    The area of memory used by the variable 'a' in main() is pretty deterministic (it will be a fixed offset from the current top of stack). What is less deterministic is what values will be found there.

  8. #8
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Your compiler should warn you about the use of an uninitialized local.
    If you are using gcc, then kick up the warning level with:
    gcc -Wall -Wextra -Wpedantic ...
    Code:
    #include <stdio.h>
     
    int x; // uninitialized global variables start all-bits zero
     
    void f() {
        static int y; // uninitialized static variables start all-bits zero
        int z; // local variables are lucky-dip
        printf("%d %d %d\n", x, y, z);
    }
     
    void g() {
        // This will fill the stack frame with some values.
        int a = 1, b = 2, c = 3;
        printf("%d %d %d\n", a, b, c);
    }
     
    int main() {
        f(); // before g fills the stack frame
        g();
        f(); // after g fills the stack frame
             // may or may not make a difference
             // ultimate result can even depend on optimization level
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can array be assigned a pointer ?
    By pshirishreddy in forum C++ Programming
    Replies: 8
    Last Post: 06-19-2011, 05:44 AM
  2. Wrong variable value assigned
    By drag0n69 in forum C Programming
    Replies: 29
    Last Post: 02-29-2008, 03:49 PM
  3. Replies: 3
    Last Post: 11-08-2007, 10:54 AM
  4. fgets assigned to a char
    By Astra in forum C Programming
    Replies: 3
    Last Post: 10-22-2006, 03:19 AM
  5. Where do inStream variables get assigned?
    By correlcj in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2002, 01:13 PM

Tags for this Thread