Scanf()

This is a discussion on Scanf() within the C Programming forums, part of the General Programming Boards category; I have a question on another one of my assignments: Use scanf to store that integer in an unsigned int. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    76

    Scanf()

    I have a question on another one of my assignments:

    Use scanf to store that integer in an unsigned int. Use Printf to display “You entered” and the actual positive integer.

    could someone explain how to use scanf a little more for me in a way that I will understand how to use it in my assignment?

  2. #2
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    [edit]There is a bettter link in the post below.[/edit]

    You can use %u with scanf()/printf() to read/print an unsigned int(assuming you know a little bit about scanf()/printf()).
    Last edited by l0cke; 03-27-2005 at 02:46 PM.

  3. #3
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    This example will read in a interger from stdin and store it into a variable at the address of x.


    Code:
    scanf("%d", &x);
    Also this LINK will explain scanf far better than I can , and it will also explain some of the problems people like me have implementing it!

    Wow, beaten again!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    I am working with scanf and hoping someone can explain what is going on here:

    Code:
    #include <conio.h>
    #include <stdio.h>
    typedef unsigned char u_char;
    typedef unsigned int u_int;
    
    main(void){
           int a;
           u_int b;
        u_char c;
        float d;
        printf("enter an integer: ");
      
           scanf("%s",&b);
       printf("\nyou entered: ");
       printf("%s\n\n");
       printf("%s in octal is:  ");
       printf("%o");
    my code output is
    enter an integer

    you entered 3

    in octal is: 10577560 is this correct?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to use the proper format modifier here:
    scanf("%s",&b);
    b is not a string, so you shouldn't be using %s.

    In the printf() statements, you've missed out the variable names.
    It should look something like this:
    printf("Number: %d\n", mynumber);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    compare the code of mine and yours, and check out the syntax error

    your code:
    Code:
    #include <conio.h>
    #include <stdio.h>
    typedef unsigned char u_char;
    typedef unsigned int u_int;
    
    main(void){
           int a;
           u_int b;
        u_char c;
        float d;
        printf("enter an integer: ");
      
           scanf("%s",&b);
       printf("\nyou entered: ");
       printf("%s\n\n");
       printf("%s in octal is:  ");
       printf("%o");
    my code
    Code:
    #include <conio.h>
    #include <stdio.h>
    typedef unsigned char u_char;
    typedef unsigned int u_int;
    
    main(void){
           int a;
           u_int b;
        u_char c;
        float d;
        printf("enter an integer: ");
      
           scanf("%d",&b);
       printf("\nyou entered: ");
       printf("%d\n\n",b);
       printf("%o in octal is:  ",b);
    s.s.harish

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    well after running your code everything works fine eccept it does not output the answer for octal.

    I did not get any syntax error for either code.

  8. #8
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    Code:
    printf("%u in octal is: %o\n", b, b); /* try this instead */

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    181
    hey its printing the octal. u might be confused with the format of what we are printing

    make it like this
    Code:
    printf("The octal value of b is %o",b);
    s.s.harish

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    76
    Hey guys this is what I came up with after the help from all of you guys.
    Code:
    #include <conio.h>
    #include <stdio.h>
    typedef unsigned char u_char;
    typedef unsigned int u_int;
    
    main(void){
           int a;
           u_int b;
        u_char c;
        float d;
        printf("enter an integer: ");
      
           scanf("%d",&b);
       printf("\nyou entered: ");
       printf("%d\n\n",b);
       printf("in octal is: %o ",b);
     getch();
    }
    this is working great!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

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