Thread: Weird output of scanf and printf.

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Weird output of scanf and printf.

    I've come up with the following program:
    Code:
    #include <stdio.h>
    int main()
    {
     int a;
     printf( "Enter a number: " );
     scanf( "%d", &a );
     printf( "You entered %d \n", a );
     getchar();
     printf( "You entered %d", &a );
     getchar();
    }
    The program runs and puts this out:
    Code:
    Enter a number: 1
    You entered: 1
    You entered: 37814108
    It doesn't mattter which number I put in, the 37814108 will still be the output.
    I'm using Bloodshed Dev 4. Is this a strange output, or is it normal?

    Thank You.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    printf( "You entered %d", &a );
    You're printing the address of the variable a.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well okay, that certainly explains it.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 08:35 PM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-11-2002, 12:40 AM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 05:44 PM