Thread: C code unknown value at output

  1. #1
    Registered User
    Join Date
    Oct 2013

    Exclamation C code unknown value at output

    As a begineer I write a program of adding the places of a digit.

    void main()
    int a,b,sum=0,e,z;
    printf("Enter the digit");
    printf("the sum is %d",sum);
    getch(); }
    but if I enter a character e.g. "w" then it gives value -26. I don't understand how this works? I also debug my code and find that it takes value -29359(0x8D51)
    Instead of this if I put a=0 then at output if I enter "a" then it shows 0. How it works as I want to understand.
    Anyone can help??

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    When you enter 'w' scanf returns 0 and leaves a with old value.
    Since a is not initialized - you get garbage

    Learn to check return value of used functions

    PS. You can benefit from reading several FAQ entries

    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) -

    Get rid of conio.h and used something standarty FAQ > How do I get my program to wait for a keypress? -

    One of the samples illustrates correct scanf usage FAQ > How do I get a number from the user (C) -

    This is what you do if scanf fails FAQ > Flush the input buffer -
    Last edited by vart; 10-16-2013 at 11:28 PM.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Output of this code
    By shah31 in forum C Programming
    Replies: 3
    Last Post: 10-22-2012, 01:39 AM
  2. what is the output of this code
    By C_Enthusiast in forum C Programming
    Replies: 2
    Last Post: 01-07-2011, 06:07 AM
  3. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  4. Output of code
    By lesodk in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2009, 08:56 AM
  5. same code different output
    By kashifk in forum C Programming
    Replies: 4
    Last Post: 03-18-2003, 02:51 PM

Tags for this Thread