porg question

Discussion in 'C Programming' started by chump1708@yahoo.com, Jan 17, 2006.

  1. Guest

    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....
     
    , Jan 17, 2006
    #1
    1. Advertising

  2. 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
     
    Robert Gamble, Jan 17, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    620
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    691
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    970
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    442
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    875
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page