473,387 Members | 1,520 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,387 software developers and data experts.

inside scanf

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
6 2476
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
14
by: Peter Mount | last post by:
Hello I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely and ends the program. Does scanf have...
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
16
by: explorer | last post by:
I am learning C, so I decided to try out r/w-ing to other files. Can anyone please tell me why I get the following error when running the program? Transcript: Name? Nikhil R. Mulani Bus...
6
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's...
2
by: gkrishgkm | last post by:
how to use printf/scanf inside printf
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
2
by: subramanian100in | last post by:
Consider the following program named as x.c #include <stdlib.h> #include <stdio.h> int main(void) { unsigned int u; char str;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.