Thread: How do i scan and print an integer?

  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,909
    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.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

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, 12:43 PM
  4. Why does an integer print like that?
    By johnnyd in forum C Programming
    Replies: 6
    Last Post: 03-29-2002, 01:36 AM
  5. Integer to print ASCII
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-04-2001, 11:12 AM