Thread: Quicke C Programming Q

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    48

    Quicke C Programming Q

    how does one write a code so that it takes down 2 values on a printf (example:

    Wind Calculator
    Enter MILES and FEET traveled: 1 1000
    Enter number of MINUTES and SECONDS elapsed: 1 28

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    are you talking about scanf? It looks like you want to input two values.

    scanf("%d %d",&val1,&val2);

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    48
    i have that for scanf... but would i need to do anything for printf (or does it just ASSUME that you need to punch in 2 values... ???

  4. #4
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    printf("Value one: %d, value two: %d", &val1, &val2);

  5. #5
    TK
    Guest
    Code:
    #include<stdio.h>
    
    int main()
    {
      int iNum = 10; //variable used to hold an integer
      printf("The number is: %d", iNum);
    
      return 0;
    
    }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Code:
    printf("Value one: %d, value two: %d", &val1, &val2);
    printf takes the name of the variables and not their addresses, I think.
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by fyodor
    Code:
    printf("Value one: %d, value two: %d", &val1, &val2);
    printf takes the name of the variables and not their addresses, I think.
    For numbers, fyodor is correct... this should have been
    >printf("Value one: %d, value two: %d", val1, val2);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    printf("Value one: %d, value two: %d", &val1, &val2);
    %d in printf does indeed specify an integer so the "&" is not needed. I always say " the address of" when I type code that
    contains the "&" with a variable. Seems to help me.

    There are many good definitions of "printf" formats and their arguments in K&R.

Popular pages Recent additions subscribe to a feed