9 Replies - 520 Views - Last Post: 20 May 2020 - 12:15 PM Rate Topic: -----

#1 coderrex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 20

Why is var2 always 0 when my program executes?

Posted 19 May 2020 - 12:31 PM

#include <stdio.h>

int main ()
{
	int var1, var2;
	char sign;
	
	
	
	printf ("Enter your first number: ");
	scanf ("%d", &var1);
	
	printf ("Enter your second number: ");
	scanf ("%d", &var2);
	
	/*
	Right here.  When my program executes, var2 is always 0 no matter what.  Why is my program doing that?
	*/
	printf ("Enter operation to be performed: ");
	scanf ("%s", &sign);
	
	
	if (sign == '+')
	{
		printf ("%d + %d = %d", var1, var2, var1+var2);
	}
	else if (sign == '-')
	{
		printf ("%d - %d = %d", var1, var2, var1-var2);
	}
	else if (sign == '*')
	{
		printf ("%d * %d = %d", var1, var2, var1*var2);
	}
	else if (sign == '/')
	{
		printf ("%d / %d = %d", var1, var2, var1/var2);
	}

	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Why is var2 always 0 when my program executes?

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15686
  • View blog
  • Posts: 62,833
  • Joined: 12-June 08

Re: Why is var2 always 0 when my program executes?

Posted 19 May 2020 - 12:42 PM

Odd. Runs fine when I throw it in a project.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2449
  • View blog
  • Posts: 4,596
  • Joined: 30-May 10

Re: Why is var2 always 0 when my program executes?

Posted 19 May 2020 - 01:09 PM

05 int var1, var2;
06 char sign;
...
scanf ("%s", &sign);

%s will always write 2 bytes of memory if you just input a single character.
Guess where that other byte goes - right over the top of var2 (at least for you it does).

If you want to read a single character, then do
scanf (" %c", &sign);


Note the leading space in the conversion format.
Was This Post Helpful? 2
  • +
  • -

#4 coderrex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 20

Re: Why is var2 always 0 when my program executes?

Posted 19 May 2020 - 07:04 PM

Why the leading space in the conversion format?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5836
  • View blog
  • Posts: 17,794
  • Joined: 25-December 09

Re: Why is var2 always 0 when my program executes?

Posted 19 May 2020 - 07:45 PM

The leading space forces scanf() to skip leading whitespace when using the "%c" format specifier, which by default doesn't skip leading whitespace.


Jim
Was This Post Helpful? 1
  • +
  • -

#6 coderrex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 20

Re: Why is var2 always 0 when my program executes?

Posted 19 May 2020 - 09:15 PM

When I use scanf ("%c", &sign); without the leading whitespace " %c" my program doesn't prompt for user input for variable sign. Why does it do that?
Was This Post Helpful? 0
  • +
  • -

#7 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2449
  • View blog
  • Posts: 4,596
  • Joined: 30-May 10

Re: Why is var2 always 0 when my program executes?

Posted 19 May 2020 - 10:06 PM

Add this to the end of your if/else chain.
  else if (sign == '\n')
  {
    printf("Ok, nothing\n");
  }



Now compare
scanf ("%c", &sign);
with
scanf (" %c", &sign);

In the former, what ends up in sign is the newline left after your scanf of var2.
The latter, with it's leading space, ignores the newline (and all white-space) and reads the next character (like say +).

You'll also come across this trailing newline issue when you start using fgets() to read whole lines.
Was This Post Helpful? 1
  • +
  • -

#8 coderrex   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 24-April 20

Re: Why is var2 always 0 when my program executes?

Posted 20 May 2020 - 09:02 AM

Would what have mentioned be considered errors in the C programming language? With the scanf overwriting 2 bytes of memory and scanf requiring a space before %c?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5836
  • View blog
  • Posts: 17,794
  • Joined: 25-December 09

Re: Why is var2 always 0 when my program executes?

Posted 20 May 2020 - 10:57 AM

Using an incorrect format specifier is definitely considered an error, since using the wrong specifier invokes Undefined behavior.

You only need the leading space if you want scanf() to skip leading whitespace when using the "%c" format specifier. While not skipping the whitespace may cause problems it is not considered a "language" error.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7361
  • View blog
  • Posts: 24,853
  • Joined: 05-May 12

Re: Why is var2 always 0 when my program executes?

Posted 20 May 2020 - 12:15 PM

Programming languages, like other tools, are designed for different levels of craftsmen. Would you call having a exposed blade on a circular saw an error? Or does the tool user simply have to know not to put their fingers there while it is spinning?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1