Thread: & important?

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002

    & important?

    #include <stdio.h>
    int userNum = 0; /*Declare all variables*/
    int main()
    	printf("Please enter a number: \n"); /*Ask for user input and record it to: userNum*/
    	if( userNum = 1)					/*Compare userNum to 1 and print corresponding answer*/
    	printf(" Your number equals 1" );
          printf(" Your number does not equal 1");
       return 0;
    This message was supposed to be me asking for help with this program but I found the problem but came up with a question. In the above code I forgot the & sign right after scanf, no big deal just a typo but my compiler didn't catch it yet every time I ran the program it would crash and ask if I wanted to send an error report, can someone elaborate a bit on why it is so important but doesn't show up?

    P.S. Here is the fixed one:

    scanf("%d", &userNum);
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

  2. #2
    Registered User
    Join Date
    Jul 2002
    What compiler are you using? It should have given you a warning about the following line like "Possibly incorrect assignment......".
    if( userNum = 1)
    You actually need two equal signs here ortherwise you are really assigning userNum to 1.

    change it to:
    if( userNum == 1)

    As for the "&" in the scanf in K&R second edition page 157 they state that all arguments after the formats must be pointers. Since you defined userNum as an int and not a pointer to an int you need to supply the "&" to reference the actual address of userNum and in effect make it a pointer.

    I can't believe your operating system didn't give you a "Null pointer assignment" error message without the "&".
    Last edited by jerryvtts; 07-16-2002 at 02:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For me is very important Please...
    By pitkini in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2005, 02:49 AM
  2. What Is Important In Making Games?
    By Krak in forum C++ Programming
    Replies: 6
    Last Post: 05-07-2004, 08:12 PM
  3. Planned Development Community Idea (important)
    By cozman in forum A Brief History of
    Replies: 2
    Last Post: 06-12-2002, 12:13 PM
  4. Most important element of a game?
    By Eber Kain in forum Game Programming
    Replies: 24
    Last Post: 01-11-2002, 03:04 PM
  5. Replies: 2
    Last Post: 12-08-2001, 11:10 PM