Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > porg question

Reply
Thread Tools

porg question

 
 
chump1708@yahoo.com
Guest
Posts: n/a
 
      01-17-2006
main()
{
int i;
clrscr();
printf("%d", &i)+1;
scanf("%d", i)-1;
}

whats the output n why???
Note that there is & which means it probably prints the address of
i...n my guess is that + 1 has no effect on the output...
n regarding scanf - there is no & symbol...so still it will accept a
value but not store it in i n my guess is that -1 has no effect on the
code....
any comments....

 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      01-17-2006
wrote:
> main()


int main (void)

> {
> int i;
> clrscr();


Not a standard function.

> printf("%d", &i)+1;


printf is expecting an int, you are passing a pointer to int, this is
undefined behavior. Try printf("%d\n", i);
You need to #include <stdio.h> before calling printf.
You need to initialize i before using it's value, what you are doing is
undefined behavior.

> scanf("%d", i)-1;


scanf is expecting a pointer to int, you are passing an int. Try
scanf("%d", &i);
You need to #include <stdio.h> before calling scanf.

> }
>
> whats the output n why???


You have several examples of undefined behavior in your program, this
means anything goes, you shouldn't expect anything in particular.

> Note that there is & which means it probably prints the address of
> i...n my guess is that + 1 has no effect on the output...


If you want to print the address, cast the address of i to a pointer to
void and use the %p conversion specifier:

printf("%p\n", (void *)&i);

printf returns an integer value. In your statement 1 is added to the
this value and the result is discarded. The compiler could legally
just remove the +1 as an optimization since your program wouldn't know
the difference, it doesn't use the result in any way.

Robert Gamble

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57