Thread: For command doesn't work.

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    113

    For command doesn't work.

    Hello everyone.

    I have just started to C programming. I wrote some codes with if, else if, else and for. The part with for command doesn't work. Here is the code:


    Code:
    #include <stdio.h>
    int main()
    
    {
        int a,b,c,x;
        printf("Enter a number:\n");
        a=2;
        scanf("%d",&b);
        if (b < 2)
        {
            c=a*b;
        }
        else if (b==2)
        {
            c=a*a+b*b;
        }
        else
        {
            c=a+b;
        }
        printf("%d\n",c);
    
        for (x=0;x<5;x++)
    {
        printf("%d\n", &x);
    }
    
        getchar();
        return 0;
    }
    I entered 2 as the input b and the result is that:

    8
    2293520
    2293520
    ...
    Last edited by GokhanK; 12-26-2010 at 10:50 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are printing the address of x. Apparently x lives at 2293520 in your program. If you want to print x, then just print x.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    113
    Thanks a lot. It was really a great help. I didn't think I would also learn why I saw 2293530 instead of 35678 or 8 or any other numbers. It works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM