-2

In my recently begun quest to learn C one of the first things I have made is a very basic "sum of 2 numbers program".

/* Basic addition prog */

#include <stdio.h>

int main(void) {

  int v_1;    /* first var */
  int v_2;    /* second var*/
  int answer; /* Sum of 2 vars*/

  printf("Simple addition calculator \n");
  printf("enter first number:  ");
  scanf("%d", &v_1);
  printf("enter second number:  ");
  scanf("%d", &v_2);

  answer = v_1 + v_2;

  printf("%d\n", answer);
}

When I run this and enter 2 numbers everything runs fine and I will get the correct answer. The output will look like:

enter first number: 1

enter second number: 1

=2

I decided I'd try and break it by using letters instead of numbers expecting at first that I'd input 2 letters and it'd either convert them into numeric values or would error out and crash.

However this isn't what happens instead I get the following:

enter first number: a

enter second number: = 32765

My question isn't so much "how do I fix this" as it is "what is actually happening here?

EDIT Again this question is more about trying to understand what is happening than trying to fix a problem. I'm not looking for "how can this be avoided?" I'm looking for "what causes it and why?"

Tl;Dr I really can simplify what I want to know down to 2 questions.

Why am I getting unique junk data for each letter of the alphabet that I enter? i.e 32764 for every time I enter a and 32767 every-time I enter b ?

Why does the second variable input get skipped ?

Community
  • 1
  • 1
Spooler
  • 222
  • 1
  • 10
  • It did neither of the two things you expected. In fact, it did nothing at all. – David Schwartz Dec 23 '15 at 12:49
  • @Spooler: The `scanf` function returns a value. You need to test it so you'll know if `scanf` successfully read (and assigned) a value. You should make a habit of checking return codes... – Blastfurnace Dec 23 '15 at 12:50
  • Possible duplicate of [Input validation using scanf()](http://stackoverflow.com/questions/15228388/input-validation-using-scanf) – Antonio Dec 23 '15 at 12:50
  • The point of this question seems to have been entirely missed I don't want to know how to fix the problem I want to know what's actually going on here why does it give me the junk output it gives and why does it skip the second value ? – Spooler Dec 23 '15 at 13:01

1 Answers1

3

Check the result of scanf("%d", &v_1);. If it is not 1, v_1 is not updated.

Why does the second variable input get skipped ?

Entering non-numeric input for scanf("%d", &v_1); will neither consume that input nor change v_1.

That is also why scanf("%d", &v_2); did not wait for input, it tried to use the same input and also failed.

Why am I getting unique junk data for each letter of the alphabet that I enter? i.e 32764 for every time I enter a and 32767 every-time I enter b ?

Code is just getting the uninitialized values of v_1, v_2. An optimizing compiler may not even "create" v_1 until the scanf("%d", &v_1); and so the uninitialized v_1 may be dependent on the text entered. Since this is undefined behavior, the result may differ tomorrow or on another machine.

Try the same thing with initialized values.

int v_1 = 0; /* first var */
int v_2 = 0;
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Not really sure what you're trying to say here. code works fine with any integer. The issue only arises when letters are used as input. All setting the vars to 0 did was change the output from 32765 to 0... which is interesting but doesn't help explain what's actually happening here ... or why the output 32765 was occurring and isn't now. If I go back and use a different letter the output is different B for example gives the output "32767" – Spooler Dec 23 '15 at 12:45
  • 1
    @Spooler `scanf("%d", &v_1);` does not read `"a"` as it is not text that makes sense to interpret as an `int`. That text remains in `stdin`. `v_1` is not changed. – chux - Reinstate Monica Dec 23 '15 at 12:48
  • @Spooler 32765 is the sum of the numbers _before_ scanf()` as well as after. The numbers were not initialized. Another run of the code may produce the same or difference results. – chux - Reinstate Monica Dec 23 '15 at 12:51
  • So what is actually happening here, why is it giving me a different value for different letters what meaning does the value it's giving me actually have. – Spooler Dec 23 '15 at 12:51
  • 1
    @Spooler 32765 is just garbage value, you did not initialize the variable so it just got whatever left in that memory location. There is no magic there, if you reboot your machine and run your program again, it will surely get a different value than 32765. – artm Dec 23 '15 at 12:52
  • the value seems to correspond the letter entered and that's more or less where my confusion comes from, rebooting the machine made no difference a gives the same output each time as does b or c etc. – Spooler Dec 23 '15 at 12:54
  • 1
    Entering non-numeric input for `scanf("%d", &v_1);` like `"a"` or `"b"` **does not change** `v_1`. – chux - Reinstate Monica Dec 23 '15 at 12:56
  • 1
    @Spooler As verification, you can assign a given value to `v_1`, and see if it changes after inserting the wrong input. (It doesn't). – Antonio Dec 23 '15 at 12:57
  • I totally get that it's not changing the value of v_1 but that doesn't answer my question about what is actually happening here, al right the output is junk data. Why is that junk data there what actually *is* it? Why does it cause the second input to be skipped ? – Spooler Dec 23 '15 at 12:59
  • @Spooler: It's not a good use of your time to wonder about the value of an uninitialized variable. It's whatever (unknown) pattern of bits happened to be in the region of memory where the variable appears. Reading that value is a bug you need to fix and move on with your life. – Blastfurnace Dec 23 '15 at 13:01
  • @Spooler The 2nd input is skipped as this answers says "Entering non-numeric input for `scanf("%d", &v_1);` will neither consume that input ..." – chux - Reinstate Monica Dec 23 '15 at 13:01
  • @Spooler Why is that junk data there what actually is it? --> It could be anything. It might be different each run, maybe also the same, Maybe a special value that aborts the program because code used an uninitialized value. This all falls under _undefined Behavior_ (UB). In C, using uninitialized values has no specified result – chux - Reinstate Monica Dec 23 '15 at 13:05
  • fair enough ... cost me a fair bit of rep but I'm satisfied with this, cheers ! – Spooler Dec 23 '15 at 13:24