0

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.

Damika
  • 622
  • 2
  • 8
  • 17
  • 2
    Always check `scanf`'s return value. – Steve Summit Oct 17 '22 at 00: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`? – Steve Summit Oct 17 '22 at 00:07
  • But can you bit explain more about what happened, Why did I get 5 and 16? – Damika Oct 17 '22 at 00:12
  • 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. – Steve Summit Oct 17 '22 at 00:14
  • Can you please explain why C is putting random values as the default values for the uninitialized variables? In java, there are predefined default values like 0, and null. false, etc. – Damika Oct 17 '22 at 00:25
  • 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. – erik258 Oct 17 '22 at 00:33
  • 1
    @Damika C is not Java. In C, [static-duration variables are guaranteed to be initialized to 0](https://stackoverflow.com/questions/51329671/difference-between-static-global-variable-and-non-static-global-variable-in-c/51329933#51329933), 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. – Steve Summit Oct 17 '22 at 01:05

2 Answers2

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);
}
Allan Wind
  • 23,068
  • 5
  • 28
  • 38
0

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).

Dev Anand
  • 11
  • 2