2

I've written following code:

int main() {
    double A[2];
    printf("Enter coordinates of the point (x,y):\n");
    scanf("%d,%d", &A[0], &A[1]);

    printf("Coordinates of the point: %d, %d", A[0], A[1]);
    return 0;
}

It's acting like this:

Enter coordinates of the point (x,y):

3,5

Coordinates of the point: 3, 2673912

How is it possible, that 5 converts into 2673912??

1
  • 1
    Why are you using scanf/printf in what is supposed to be a C++ program ? Commented Oct 11, 2010 at 15:16

1 Answer 1

10

You are reading double values using the decimal integer format (%d). Try using the double format (%lf) instead...

scanf("%lf,%lf", &A[0], &A[1])
Sign up to request clarification or add additional context in comments.

5 Comments

Don't forget printf("Coordinates of the point: %lf, %lf", A[0], A[1]);
Thank you, that's it! But it's possible to somehow change the type back to double to be able to operate (+,*,-,/,...) with these numbers? What's the type %lf in fact?
@Radek A[0] and A[1] are doubles. There is no need to "change the type back to double". +, *, ... will would just fine. %lf, tells scanf that the argument is a "long float", that is, a double. Also note dgnorton's comment on using %lf in the printf
%lf just tells scanf that the target variable is a double. So you can do normal operations on those variables after they are read in. Look at the documentation for printf format specifiers for the full range. Are you having another problem?
@Radek: %lf and %d are format specifiers not types. Nothing is changing type. The specifiers tell the formatted I/O functions what type the arguments are to be interpreted as (because they can otherwise be any type and the function does not know a priori.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.