472,364 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 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 2360
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;
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.