473,786 Members | 2,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2511
te********@gmai l.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********@gmai l.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********@gmai l.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...@yah oo.comwrote:
temper3...@gmai l.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_trapre p;
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********@gmai l.com wrote:
On Jan 31, 11:59 am, CBFalconer <cbfalco...@yah oo.comwrote:
>temper3...@gma il.com wrote:
>>Can someone explain me what is happening below ? Why is it printing
401380
$ cat scanf.c
#include<stdi o.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_trapre p;
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********@gmai l.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_trapre p;
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
9875
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. However no explanation is offered other than that scanf handels end of lines very badly. I have exeperienced such problems when doing some numerical programming but never understood it. Things like some consequitive scanfs would not read in values...
14
2748
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 problems with "%c" or is it the operating system I'm using? 1 #include <stdio.h> 2 3 int main()
7
7665
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 scanf cannot fill any fields it returns EOF. I have run some tests on scanf and, so far, I've not found an EOF return. For example: If the programer formats scanf for an int or
16
1341
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 error
6
2288
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 best to "think of it")? I've seen some explain it as if they call fgetc internally, then I've seen some people explain it as if they call some lower level OS system call like read(). I've even seen some older posts talk about a input functions...
2
6624
by: gkrishgkm | last post by:
how to use printf/scanf inside printf
14
13814
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*/ printf("%c\n",a);
3
9842
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
6555
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
9650
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.