By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,174 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

inside scanf

P: n/a
Hi
Can someone explain me what is happening below ? Why is it printing
401380
$ cat scanf.c
#include<stdio.h>

int main()
{
int i;
scanf(" %d",&i);
printf("%x\n",i);
return 0;
}

prog@cygwin ~
$ ./a.exe
-1
ffffffff

prog@cygwin ~
$ ./a.exe
--1
401380

prog@cygwin ~
$ ./a.exe
--2
401380

prog@cygwin ~
$ ./a.exe
--
401380

prog@cygwin ~
$

Jan 31 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
te********@gmail.com wrote:
Hi
Can someone explain me what is happening below ? Why is it printing
401380
$ cat scanf.c
#include<stdio.h>

int main()
{
int i;
scanf(" %d",&i);
Try checking the return value.
printf("%x\n",i);
return 0;
}

prog@cygwin ~
$ ./a.exe
-1
ffffffff

prog@cygwin ~
$ ./a.exe
--1
401380

prog@cygwin ~
$ ./a.exe
--2
401380

prog@cygwin ~
$ ./a.exe
--
401380

prog@cygwin ~
$

--
Ian Collins.
Jan 31 '07 #2

P: n/a
te********@gmail.com wrote:
Can someone explain me what is happening below ? Why is it printing
401380
$ cat scanf.c
#include<stdio.h>
int main()
Better make that 'main(void)' since your main function doesn't
take any arguments.
{
int i;
scanf(" %d",&i);
printf("%x\n",i);
Take care: for the 'x' conversion specifier nowadays (C99) an
unsigned int is expected as the argument to be printed.
return 0;
}
prog@cygwin ~
$ ./a.exe
-1
ffffffff
prog@cygwin ~
$ ./a.exe
--1
401380
Here scanf() couldn't read an integer (since "--1" isn't one), so
nothing gets assigned to 'i'. What you then print out this is the
random value that 'i' started of with. Check the return value of
scanf() and you will see that it's 0 (instead of 1) here, it tells
you how many items scanf() successfully read in.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jan 31 '07 #3

P: n/a
te********@gmail.com wrote:
>
Can someone explain me what is happening below ? Why is it printing
401380

$ cat scanf.c
#include<stdio.h>

int main()
{
int i;
scanf(" %d",&i);
printf("%x\n",i);
return 0;
}

prog@cygwin ~
$ ./a.exe
-1
ffffffff

prog@cygwin ~
$ ./a.exe
--1
401380
Why don't you give it an integer for input? Usual sin - failure to
check the return value from scanf.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 31 '07 #4

P: n/a
On Jan 31, 11:59 am, CBFalconer <cbfalco...@yahoo.comwrote:
temper3...@gmail.com wrote:
Can someone explain me what is happening below ? Why is it printing
401380
$ cat scanf.c
#include<stdio.h>
int main()
{
int i;
scanf(" %d",&i);
printf("%x\n",i);
return 0;
}
prog@cygwin ~
$ ./a.exe
-1
ffffffff
prog@cygwin ~
$ ./a.exe
--1
401380

Why don't you give it an integer for input? Usual sin - failure to
check the return value from scanf.
Few more questions
1) Can a variable contain trap representation(trap rep) say a local
variable like int i;
2) Can a pointer contain trap rep ? like say int *p, can p be a trap
rep.
3) Can address of an uninitialized variable every contain trap rep ?
i.e can "&i" contain trap rep
4) Can we get to assign trap rep "on a 2's complement machine" ? i.e
int i= value_of_traprep;
printf(" %d",i); will it abort ;
5) On a 1's complement machine is trap rep always ~0 or is it
different.
When ~0 is trap rep,
what happens,
int i=~0,
printf(" %d",i); will it abort ;

6) How do i get what is the value for trap rep on my arch (i386) .

Jan 31 '07 #5

P: n/a
te********@gmail.com wrote:
On Jan 31, 11:59 am, CBFalconer <cbfalco...@yahoo.comwrote:
>temper3...@gmail.com wrote:
>>Can someone explain me what is happening below ? Why is it printing
401380
$ cat scanf.c
#include<stdio.h>
int main()
{
int i;
scanf(" %d",&i);
printf("%x\n",i);
return 0;
}
prog@cygwin ~
$ ./a.exe
-1
ffffffff
prog@cygwin ~
$ ./a.exe
--1
401380
Why don't you give it an integer for input? Usual sin - failure to
check the return value from scanf.
Few more questions
1) Can a variable contain trap representation(trap rep) say a local
variable like int i;
Yes
2) Can a pointer contain trap rep ? like say int *p, can p be a trap
rep.
Yes
3) Can address of an uninitialized variable every contain trap rep ?
i.e can "&i" contain trap rep
No
4) Can we get to assign trap rep "on a 2's complement machine" ? i.e
int i= value_of_traprep;
No, as far as C is concerned, a variable containing a trap
representation doesn't have a value. Just attempting to do the above
initialization invokes undefined behavior.
printf(" %d",i); will it abort ;
5) On a 1's complement machine is trap rep always ~0 or is it
different.
There is no way a conforming program can know this.
When ~0 is trap rep,
what happens,
int i=~0,
printf(" %d",i); will it abort ;

6) How do i get what is the value for trap rep on my arch (i386) .
Not every platform has trap representations, you just have to be aware
that they *might*. Essentially, your questions come down to : "What
happens when I invoke undefined behavior?". The answer is: "anything or
nothing."
--
Clark S. Cox III
cl*******@gmail.com
Jan 31 '07 #6

P: n/a
te********@gmail.com wrote:
>
.... snip ...
>
Few more questions
1) Can a variable contain trap representation(trap rep) say a local
variable like int i;
yes
2) Can a pointer contain trap rep ? like say int *p, can p be a trap
rep.
yes
3) Can address of an uninitialized variable every contain trap rep ?
i.e can "&i" contain trap rep
no
4) Can we get to assign trap rep "on a 2's complement machine" ? i.e
int i= value_of_traprep;
printf(" %d",i); will it abort ;
no
5) On a 1's complement machine is trap rep always ~0 or is it
different.
no
When ~0 is trap rep,
what happens,
int i=~0,
printf(" %d",i); will it abort ;
it doesn't happen.
>
6) How do i get what is the value for trap rep on my arch (i386) .
In general, you don't. There may be many of them.

Don't change the subject without changing the subject line.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Jan 31 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.