Thread: Beginner Help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    Beginner Help

    hi I'm a beginner in C (specifically the syntax) so I started to go through the tutorials first

    everything's fine but i'm having a small problem with printf. what I exactly dont know how to do is use printf so that I can display 2 variables and not just one

    here's some sample code, ( I don't know how to fix it )

    its obvious, but I'm just trying to compare 2 numbers

    note: this is only a part of main(), everything works when compiled, just having troubles with syntax in the bolded lines.
    Code:
    printf("Enter number 1: ");
        scanf("%d", &num1);
        printf("Enter number 2: ");
        scanf("%d",&num2);
    
        if(num1>num2)
        {
            printf("%d",num1,"is greater than %d", num2);
        }
        else if (num2>num1)
        {
            printf("%d",num2,"is greater than %d", num1);
        }
        else
        {
            printf("Both numbers are equal");
        }
    What Im trying to do is display both numbers inputted into 1 printf function, but this doesnt work the way I need it, it just shows 1 number and nothing else.

    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    printf("%d is greater than %d", num1, num2);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For the first one,
    Code:
    printf("%d is greater than %d", num1, num2);
    (And you might want to add a newline.)

    Edit: puts() is equivalent to printf() except that it adds the newline, so you could also replace your printf()'s with puts()'s, though for some reason you don't see people use puts() very often.

    Edit: I'm wrong, puts() only takes a single argument (a C string) as argument and prints it together with a newline. So you could use it to replace the last printf() but not the second or third (or the first, since you don't want a newline there). That explains why it's not used much.
    Last edited by robatino; 09-08-2007 at 11:13 AM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    ahh thanks guys

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    World
    Posts
    2

    Thumbs up

    hi i am a student.
    the general syntax for printf statement is

    printf("format string",variable);

    this built in function will return an integer value.
    here format string may be %d,%f or any other .
    the eg to print 2 variables is

    printf("The first number is %d and the second number is %d",no1,no2);

    i hope ur doubt may be cleared.
    all the best to be a good programmer in C.
    dont lose ur hope.

    my quote to u is
    "Defeat the Defeat before the Defeat Defeats U".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM