Help.

This is a discussion on Help. within the C Programming forums, part of the General Programming Boards category; I have drafted this code below, for some reason int C is returned as the address instead of sum of ...

  1. #1
    Registered User Unee0x's Avatar
    Join Date
    Nov 2011
    Posts
    12

    Help.

    I have drafted this code below, for some reason int C is returned as the address instead of sum of two ints. If anybody can help, I would be very grateful..


    Code:
    #include <stdio.h>
    int main()
    {
    
    
      int A, B;
      int C = A + B;
    
    
      printf("Enter A please\n");
      scanf("%i", &A);
      printf("Enter B please\n");
      scanf("%i", &B);
      printf("%i + %i = %i\n", A, B, C);
      if(C < 90)
        {
          printf("It's less than 90\n");
        }
    
    
          else if(C > 90)
            {
              printf("It's greater than 90\n");
            }
    
    
            return 0;
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    You should perform the addition and assign the result to C after you have read in the values of A and B.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User Unee0x's Avatar
    Join Date
    Nov 2011
    Posts
    12
    Thanks laserlight, but if you can , please explain what you mean.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,221
    Code:
    int A, B;  // <--- 'A' and 'B' are uninitialized here, and thus contain garbage
    int C = A + B;  // <--- 'C' is the sum of two garbage values, hence contains a garbage value itself
    
    printf("Enter A please\n");
    scanf("%i", &A);  // <--- Here 'A' gets a value
    printf("Enter B please\n");
    scanf("%i", &B);  // <--- Here 'B' gets a value
    printf("%i + %i = %i\n", A, B, C);  // <--- ...but 'C' is still a garbage value.  The addition should go after 'A' and 'B' are received.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    What laserlight said is actually this
    You want the program to add A and B into C.However at line 7 the only thing your program knows(let's say the world of the program) is this
    Code:
    #include <stdio.h>
    int main()
    {
     
     
      int A, B;
      int C = A + B;
    In other words your program knows only that there are three integers,and C is equal to A and B..But values of A and B are unknown at that time...Let me make a paromiosis for you.It is like you ask me to add two numbers and have me do this before you tell what these numbers are!I know that they are numbers,but what numbers?1,3,5,3738273927?What number?You inform me about what numbers should i add only after i have already made the addition...So what numbers did i actually added?
    You do not know!This is what we call unexpected behaviour in computer science(in some cases the values are set by default,but it is good for everybody that you clearly state what the value of every variable is!)
    In your code,you perform the addition before getting the input.That's wrong.You should move a line(or move the addition i would say) after you have received the input

  7. #7
    Registered User Unee0x's Avatar
    Join Date
    Nov 2011
    Posts
    12

    Thanks

    Ok , I've got it.
    Thank you again....
    It just makes so much sense to add A & B after program knows A & B...
    (How about that...?)
    Thanks again...
    Last edited by Unee0x; 09-04-2012 at 06:22 PM. Reason: Premature post

Popular pages Recent additions subscribe to a feed

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