Thread: scanf and printf question

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    scanf and printf question

    Hi I have a question regarding scanf and printf. I have a programming assignment in which I want to initialize a random generator to a seed inputted by the user and generate 100 randomly generated numbers in the range 1 to 9999. However, the seed is of type long and I don't see a specifier for type long in the scanf or printf docs. I tried this:

    Code:
       printf("\nPlease enter the seed: ");
            scanf("%l\n", &seed);
            printf("seed: %l", seed);
            init_seed(seed);
    
            printf("Please enter the range: ");
            scanf("%d %d\n", &beginning, &end);
            printf("\nbeg: %d end: %d", beginning, end);
            set_range(beginning, end);
    I added the printf just to verify what is being passed and it seems the variables don't hold the correct values. Seed is never printed I am guessing because %l is not valid and beg outputs the seed value, 1234567, and end outputs the beginning, 1. Am i not reading in the values correctly?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Off the cuff I believe it's %ld for
    Code:
    scanf("%ld", &longInteger);
    and same for printf(). If you want unsigned, it's %lu for printf().
    Last edited by Adak; 10-11-2009 at 04:48 AM.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Ok so with %ld it displays the correct number with printf but it waits for a second integer.

    Here's the output with %ld

    Code:
    seed: 1234567
    1234567
    
    seed: 1234567
    range: 1 9999
    beg: 1234567 end: 1[mattsoma@athena:34]>
    Why would it wait for a second input when there is only one adress argument (i.e. scanf("%ld\n", &seed) <--- ). Also am I using the right format string when inputting the range? It outputs from 1234567 to 1.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It helps if you actually make a working example, instead of just some random lines of code thrown together.
    Code:
    #include<stdio.h>
    int main( void )
    {
        long int l = 0;
        
        printf( "Foo: " );
        fflush( stdout );
        scanf( "%ld", &l );
        
        printf( "Bar: %ld ", l );
        
        return 0;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because "%ld\n" asks for two inputs, not one? You should look at the manual again:
    Quote Originally Posted by man scanf
    White space (such as blanks,
    tabs, or newlines) in the format string match any amount of white space,
    including none, in the input.
    So that \n will suck up all the new lines you care to give it until you type something else.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Quote Originally Posted by tabstop View Post
    Because "%ld\n" asks for two inputs, not one? You should look at the manual again:

    So that \n will suck up all the new lines you care to give it until you type something else.

    @quzah...Here is a complete main:
    EDIT::: you will have to remove the function calls if you want it to compile.

    Code:
    #include stdio.h
    int main(){
    
            head = NULL;
            tail = head;
    
            long seed;
            int num, i, startRange, endRange;
            int numDelete;
            printf("seed: ");
            scanf("%ld\n", &seed);
            printf("\nseed: %ld\n", seed);
            init_seed(seed);
    
            printf("range: ");
            scanf("%d %d\n", &startRange, &endRange);
            printf("beg: %d end: %d", startRange, endRange);
            set_range(startRange, endRange);
    
            return 0;
    } //end main
    and here is the output I get when using %ld

    Code:
    seed: 1234567
    1234567
    
    seed: 1234567
    range: 1 9999
    beg: 1234567 end: 1[mattsoma@athena:36]>
    @tabstop...I realize that if I type 1234567 and hit enter, I can hit enter as many times as I want but when I enter 1234567 a second time it prints another new line and then asks for the range...however I thought the format string "%ld\n" means a long terminated by the enter key...

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Taking out the \n fixed it but I don't really get why. So scanf automatically waits for enter?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It does if you tell it to scan for \n, which is what you're doing by including \n in its scan string.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    6
    Quote Originally Posted by quzah View Post
    It does if you tell it to scan for \n, which is what you're doing by including \n in its scan string.


    Quzah.
    Hmm, here is the main that outputs correctly:

    Code:
    int main(){
    
            head = NULL;
            tail = head;
    
            long seed;
            int num, i, startRange, endRange;
            int numDelete;
            printf("seed: ");
            scanf("%ld", &seed);
            printf("seed: %ld", seed);
            init_seed(seed);
    
            printf("\nrange: ");
            scanf("%d %d", &startRange, &endRange);
            printf("beg: %d end: %d\n", startRange, endRange);
            set_range(startRange, endRange);
    
            return 0;
    } //end main
    I guess I was just confused because I thought you had to specify \n in the scanf format string after the specifier to let it know that enter ends the integer but it actually screws it up haha. Thanks for your patience I appreciate it.

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. Double to Int conversion warning
    By wiznant in forum C Programming
    Replies: 15
    Last Post: 09-19-2005, 09:25 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