473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Why value of second variable is irrelevant after adding space or new line ?

Hello everyone! I'm newbie in programming learning C language.I'm little confused right now.I tried to Google about it but can not find out the satisfactory result so i thought to sort out by asking the question in this website.<br/>Have a look at this short program -
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. int main()
  3. {
  4. int num1,num2;
  5. printf("enter the value of num1 and num2:");
  6. scanf("%d %d",&num1,&num2);
  7. printf("num1 = %d and num = %d",num1,num2);
  8. return 0;
  9. }
When i enter value For example- 215-15 without space or new line than it gives out num1 = 215 and num2 = -15 but when i enter space or new line between 215- and 15 then it gives output "num1 = 215 and num2 = -175436266(or any unexpected number).
I know that when scanf() reads any character which is not in the catagory of conversion specification it put back that character and end processing other inputs.But in the first case -(minus sign) seems to be irrelevent input according to the conversion specification but it shows correct output but in the later case it not showing correct output.Why?
May 28 '15 #1
3 1319
weaknessforcats
9,208 Expert Mod 8TB
So you are processing 215- 15.

Characters are obtained that are int. These would be 215. So num1 is 215.

The process repeats. The - is obtained and the sign bit of num2 is set. Oops. A non-int character is obtained. num2 at this point has a sign bit set and a garbage value.

It's always a good idea to check the return value of scanf. In this case, scanf should return 2 indicating 2 variables were successfully processed.

A return of 0 indicates an error occurred.

A return of EOF indicates the input buffer ran out before the process of the variable was complete.

If you scanf one variable at a time you can pinpoint which variable errored out.
May 28 '15 #2
why they are successfully assigned in first case and not in the second
May 28 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
In the first case it was 215-15.

So 215 was scanned but the - was not. That's because integers don't end in non-digits. That left -15 which was scanned into num2.

The essential point is that scanf stops scanning when a character is obtained that is not for the type being scanned.

If you display the return from the scanf of 215- 15 you will see 1. That means one variable was successfully scanned. It should have been 2. So you an infer that the second variable had some kind of problem. In fact only the - was scanned then came the space (or newline or any non-int character) and the scan was stopped. - is not a valid int. The 15 was never scanned.

BTW: That 15 is still in the input buffer just waiting for the next scanf. Then suddenly 15 pops out of the buffer instead of the number that was just entered.

Input buffer parsing can get complex. I would not mess with it until you have A COUPLE OF YEARS experience in C. Until then just enter what is expected.
May 28 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: G Kannan | last post by:
Hey all! I have written a perl script to retrieve information from a HTML Form and insert the data into an Oracle database table. I am gettting the the following error message: "Use of...
11
by: Pete Mahoney | last post by:
I am currently working on an ASP page where I create a lot of different check boxes. I have some checkboxes that are Windows platforms and some that are solaris platforms. I want a control...
5
by: risha | last post by:
void FlashReadM(volatile unsigned long *fl_ad,unsigned long abb) { unsigned long ulvald; abb=2; ulvald=0; //fl_ad=(volatile unsigned long *)0x8020; ulvald= *(fl_ad); //return ucVal; }
2
by: Cliff | last post by:
Hi, i would like to get the value of variable 'em', defined in code-behind, into a textbox. I tried two ways but withoit succes (no error). The first textbox is empty, the second gets:<%=em...
4
by: Vernon Wenberg III | last post by:
I'm not really sure how readline() works. Is there a way to iterate through a file with multiple lines and then putting each line in a variable in a loop?
2
by: rajeshjava | last post by:
I am working with xsl. i want to store a value in the variable. xml Source looks like the below one <?xml version="1.0" encoding="UTF-8" ?> <getTradingInstallmentHistoryResponse...
4
by: kat248637 | last post by:
suppose i have a variable in shell script like a="prashant" and i want to assign this variable into another variable like b=$a,which is in perl script.after run perl script the value of b should be...
9
by: andersond | last post by:
I have a page that has a hot link to email an underwriter to request tax forms. Earlier, on the same page, the user selects a state in a drop-down box called "Garaginjg_State". Because the tax...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.