Like Tree2Likes
  • 1 Post By beatbox32
  • 1 Post By Matticus

getting wrong output from simple program

This is a discussion on getting wrong output from simple program within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { int a=100; int c; printf("%d\n", c); c = a * a; } the output ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    getting wrong output from simple program

    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int a=100;
    int c;
    
    printf("%d\n", c);
    
    c = a * a;
    
    }
    the output I'm getting is : 134513691
    any ideas as to why I would be getting this output thanks.


    figured it out have to declare before the printf statement.
    Last edited by d387420489; 07-28-2011 at 06:00 PM. Reason: solved

  2. #2
    Registered User
    Join Date
    Jul 2011
    Location
    Irvine, CA
    Posts
    13
    Quote Originally Posted by d387420489 View Post
    Code:
    #include <stdio.h>
    
    int main()
    
    {
    
    int a=100;
    int c;
    
    printf("%d\n", c);
    
    c = a * a;
    
    }
    the output I'm getting is : 134513691
    any ideas as to why I would be getting this output thanks.
    Yes, because 'c' is unassigned before your printf statement and it's been assigned some random piece of memory that already contains data.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,068
    Because you initialize 'c' after you use it in your "printf()" function. Therefore, it can be any random value.

    EDIT: beatbox beat me to it!

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,068
    And if your "main()" function says it returns an integer ... then make sure it actually returns an integer! Typically, for a successful termination, you would:

    Code:
      return 0;

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    thanks for the info I still have a lot to learn

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Really???? - This post just might make my tag line. Where is quzah, I need a new quote!!!

    I would suggest you take a look at the following:
    Cprogramming - Tutorials
    Cprogramming - FAQs

    Additionally, set your compiler up so it treats "All warnings as errors" If you did that the compiler would have flagged using c without it being initialized.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by d387420489 View Post
    thanks for the info I still have a lot to learn
    Stop and think... can you tell me the answer before you know the question?

    Neither can C.

    It reads the page top down and does stuff in the order you tell it... so, printing the answer before it knows the question just doesn't make any sense, does it?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,068
    Quote Originally Posted by AndrewHunter View Post
    Really???? - This post just might make my tag line. Where is quzah, I need a new quote!!!
    He's been cold to me ever since you compared us to to a married couple.

    Now be a good boy. Clean up your code, free your memory, and go to Sleep(28800000).
    AndrewHunter likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-30-2007, 11:08 PM
  2. Whats wrong with this program - Output of a series
    By duffmckagan in forum C Programming
    Replies: 2
    Last Post: 07-26-2006, 09:57 AM
  3. what is wrong with this simple program?
    By Dave Jay in forum C Programming
    Replies: 3
    Last Post: 03-07-2005, 08:47 PM
  4. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21