How do i scan and print an integer?

This is a discussion on How do i scan and print an integer? within the C Programming forums, part of the General Programming Boards category; This is my code: Code: int main() { int num; printf("Please enter a number: "); scanf("%d", num); printf("%d\n", num); return ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    9

    How do i scan and print an integer?

    This is my code:

    Code:
    int main()
    {  
        int num;
    
    
        printf("Please enter a number: ");
        scanf("%d", num);
    
    
        printf("%d\n", num);
    
    
        return 0;
    }
    when i compile and run it, it stops working and doesn't printf the integer.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,516
    Code:
    scanf("%d", &num);
    You're missing that &. The & gives the address of a variable. scanf needs the address of num, so it knows where to store the number it read in from the user.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    9
    Thanks =)

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by ziggy786 View Post
    This is my code:

    Code:
    int main()
    {  
        int num;
    
    
        printf("Please enter a number: ");
        scanf("%d", num);
    
    
        printf("%d\n", num);
    
    
        return 0;
    }
    when i compile and run it, it stops working and doesn't printf the integer.
    which means the problem probably has to do with the statements before - since your printf() is called correctly. You may find this link helpful in the future. I find it handy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Putchar() to print an integer value?
    By swgh in forum C Programming
    Replies: 2
    Last Post: 05-19-2007, 12:30 PM
  2. scan an image and print it
    By hephaest in forum C Programming
    Replies: 18
    Last Post: 09-08-2006, 04:34 PM
  3. How do I print 64 bit unsigned integer in hex?
    By electrolove in forum C Programming
    Replies: 7
    Last Post: 02-11-2003, 11:43 AM
  4. Why does an integer print like that?
    By johnnyd in forum C Programming
    Replies: 6
    Last Post: 03-29-2002, 12:36 AM
  5. Integer to print ASCII
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-04-2001, 10:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21