C - simple scanf question

onetwenty8k

2[H]4U
Joined
Nov 24, 2006
Messages
2,554
Solved, thank you.

So I am just starting a C course and I just have a quick question. I just need to know why this doesn't work, I want to prompt for both x and y in one scanf line, is this possible in C? I have to adhere to string C89, I just wanted to note.

P.S. This is not an assignment, just a question, I like making code clean.

Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main( int argc, char *argv[] )
{
    int x;
    int y;

    printf("Please enter an integer: ");
    fflush(stdout); /* needed becuase output streams are buffered */
    scanf("%d,%d",&x,&y );  /* &x produces the address of x not the value in x */
    printf("x: %d  y: %d\n", x,y );

    return 0;
}

I might as well say, I hate not being able to use // :mad:
 
You've passed two format strings to scanf(...). It only accepts one.

You can use scanf like this to pick up 2 integers:
Code:
scanf("%d%d", &x, &y);
 
Back
Top