Need a language lawer

D

Dingjiu

I wrote a short code as follow:

#include<stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
return 0;
}

If I input a pair of integer number, It can work,but the question is
when I input the data like this :1.3 2 , the output will be 1 and a
random number.Any body who can explain why this kind of things happen?
Thank you
 
Z

Zara

I wrote a short code as follow:

#include<stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
return 0;
}

If I input a pair of integer number, It can work,but the question is
when I input the data like this :1.3 2 , the output will be 1 and a
random number.Any body who can explain why this kind of things happen?
Thank you

scanf returns the number of parameters processed. If you write 1.3 as
input, 1 gest scanned into a, but the dot cannot be matched to an
integer, so scnaf returns 1, telling you only a is scanned. b will
have a random value because it has never been initialized

Best regrads,

Zara
 
N

Nick Keighley

I wrote a short code as follow:

#include<stdio.h>

it's easier to read with a bit of whitespace

#include said:

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

always check the return value of scanf(). here you attempt to read
two ints.
      printf("%d %d",a,b);
      return 0;

}

If I input a pair of integer number, It can work,but the question is
when I input the data like this :1.3  2  ,

1.3 is not an integer number
the output will be 1  and a
random number.Any body who can explain why this kind of things happen?

check the return value of scanf()
 
D

Dingjiu

scanf returns the number of parameters processed. If you write 1.3 as
input, 1 gest scanned into a, but the dot cannot be matched to an
integer, so scnaf returns 1, telling you only a is scanned. b will
have  a random value because it has never been initialized

Best regrads,

Zara

Thank you!
 
M

MisterE

Dingjiu said:
I wrote a short code as follow:

#include<stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
return 0;
}

If I input a pair of integer number, It can work,but the question is
when I input the data like this :1.3 2 , the output will be 1 and a
random number.Any body who can explain why this kind of things happen?
Thank you

If you are going to input 1.3 you need to use %f to get a floating point
value.

With an input of 1.3 2 scanf is stopping at the period because it expected
a number then another number, but it found a period, so it stops. So the b
is never assigned anything and you will get random value when you try to
print it.
 
R

Ron Ford

I wrote a short code as follow:

#include<stdio.h>
main()
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d %d",a,b);
return 0;
}

If I input a pair of integer number, It can work,but the question is
when I input the data like this :1.3 2 , the output will be 1 and a
random number.Any body who can explain why this kind of things happen?
Thank you

If you ask for a lawyer and misspell, you'll end up with a public defender.
Orthography counts. Two spaces follow a period.
 
B

Barry Schwarz

scanf returns the number of parameters processed. If you write 1.3 as
input, 1 gest scanned into a, but the dot cannot be matched to an
integer, so scnaf returns 1, telling you only a is scanned. b will
have a random value because it has never been initialized

b will not have a random value ; it will have an indeterminate one.
Any attempt to evaluate an indeterminate value (such as passing it to
printf) yields undefined behavior. At that point from a language
point of view, there are no incorrect results. From a professional
point of view, there are no correct results either.
 
C

CBFalconer

Bill said:
They taught me (oh these many decades ago) two spaces after a
period ending a sentence. One after anything else. The purpose
of the two spaces being to make it clear that it WAS the end of
a sentence.

Mr. Jones. One space after "Mr." and two after "Jones."

Agreed. The youngsters aren't familiar with all these fine points
of writing with fixed width type. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top