-1

I'm new to C language. Here's the code I used to get input for a and b and print them. But I did not get the values I entered via the terminal can you explain why I got different values for a and b?

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
{
    int a,b;
    scanf("%d, %d", &a, &b);
    printf("%d, %d", a, b);

    return 0;
}

I entered 5 9 as inputs, but I got 5 and 16 as outputs.

7
  • 2
    Always check scanf's return value. Commented Oct 17, 2022 at 0:06
  • 4
    Your format string "%d, %d" tells scanf to expect an integer, a comma, and an integer. When you run the program, are you typing a comma between 5 and 9? Commented Oct 17, 2022 at 0:07
  • 4
    If you check the return value of scanf, you'll find that it returned 1, indicating that it was only able to read 1 of the two values you requested. 16 was simply a random value which the uninitialized variable b happened to start out with. If you change the declaration to something like int a = 77, b = 88; you'll be able to see what's going on more clearly. Commented Oct 17, 2022 at 0:14
  • 1
    C is a language that compiles down to assembly the operating system can execute. So "C" isn't "putting" or doing anything else once the program is complied. And yes, Java (which does have a runtime component, the JVM) explicitly initializes variables to a zero value, but C does not define that as part of the implementation of the language. Commented Oct 17, 2022 at 0:33
  • 1
    @Damika C is not Java. In C, static-duration variables are guaranteed to be initialized to 0, because its cheap and easy for the compiler to arrange it, but "automatic" (i.e. local) variables are not, because there would be a significant run-time cost, incurred on every call, to do so. C's thinking is, if you want that initialization, you have to ask for it. Commented Oct 17, 2022 at 1:05

2 Answers 2

2

You need to check the value of scanf() otherwise some or all the values you attempt to use are undefined. In your case the input you provided did not match the scanf() format string. I changed the format string to match the input you provided, removed headers that are not used and added a newline to your printf() statement:

#include <stdio.h>

int main(void) {
    int a, b;
    if(scanf("%d%d", &a, &b) != 2) {
        printf("scanf failed\n");
        return 1;
    }
    printf("%d, %d\n", a, b);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You don't have to use comma (,) to separate 2 inputs in scanf() function. This function automatically separates your inputs by number of occurrences of format specifiers i.e. %d in your case. However you have to separate variable addresses using comma. Just use it like scanf("%d %d", &a, &b).

Comments

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.