Thread: Beginner problem with adding using printf

  1. #1
    Registered User Psyzen's Avatar
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    5

    Beginner problem with adding using printf

    I'm trying to make a basic C program that can do a math operation such as the one below.
    x + y = sum
    So what I've got here is:
    #include<stdio.h>

    Code:
     main()
    {
        int x = 2, y = 3, sum;
    
        printf("%i plus %i is %i", &x, &y, &sum );
        sum = x + y;
    
            getch();
    
    }
    and I'm trying to get it to just say 2 plus 3 is 5, but i'm getting something like:
    0 plus 0 is 234934289.

    Is it not possible to do calculations using just printf, or do i have to use scanf and enter the equation before i get the output?

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    You have to calculate sum before you pass it to printf()
    %i in printf() requires value, not address (erase & signs)
    Maybe you wanted to do it like this
    Code:
    printf("%i plus %i is %i", x, y, x + y);
    Last edited by DRK; 01-20-2012 at 02:14 AM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It's rather interesting to note that several complete beginners posting on here have not realised that the order that statements are written actually matters. I've been programming for far too long to have any clue why some people would ever make such a mistake. I wonder if such people would make good prolog programmers?

    Procedural programming is all about giving a sequence of instructions. It's like a recipie for baking a cake. You don't add the icing (frosting), add the eggs, bake for 15 minutes, add the flour, preheat the oven, eat the result, and then add the remaining ingredients.
    If the instructions aren't listed in the right order then what you get comes out a complete mess.
    I assume this makes sense now.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I've seen it in university - back in mid '70s. People lining up to submit their stack of IBM punched cards. When the output showed pages of compiler syntax errors one guy would shuffle his cards and get back in line as quickly as he could so that he could try it again. He didn't want to bother to actually read and understand the errors. As long as he got back in as fast and as often as possible. I guess by random chance he could have gotten it right one of those time. Still boggles the mind though.

  5. #5
    Registered User Psyzen's Avatar
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    5
    Quote Originally Posted by iMalc View Post
    It's rather interesting to note that several complete beginners posting on here have not realised that the order that statements are written actually matters. I've been programming for far too long to have any clue why some people would ever make such a mistake. I wonder if such people would make good prolog programmers?

    Procedural programming is all about giving a sequence of instructions. It's like a recipie for baking a cake. You don't add the icing (frosting), add the eggs, bake for 15 minutes, add the flour, preheat the oven, eat the result, and then add the remaining ingredients.
    If the instructions aren't listed in the right order then what you get comes out a complete mess.
    I assume this makes sense now.
    The thing is my task was to include the equation sum = x + y into the source file, but what I didn't understand was how I was to include an int called sum into my program without using scanf. what i wrote kind of looked like a tasty cake the way how I first declared the int's then I wrote the statement to include the actual ints and icing it with the frosting, which is supposed to be the equation, but after taking a look again at what I did wrong and DRK's revision of the sequences, I see where I went wrong and learned a lesson that I missed that week in class.
    Thanks DRK for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner question about scanf/printf
    By mickeyd in forum C Programming
    Replies: 6
    Last Post: 04-17-2010, 09:39 PM
  2. problem with printf!!!!
    By mcaro72 in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2008, 03:59 PM
  3. printf problem
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-21-2006, 04:02 AM
  4. PRINTF problem
    By Chronom1 in forum Linux Programming
    Replies: 1
    Last Post: 07-09-2002, 11:11 PM
  5. problem with printf
    By made4 drivers in forum C Programming
    Replies: 10
    Last Post: 09-17-2001, 09:55 PM

Tags for this Thread