Thread: ascii characters read/store/print help

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    ascii characters read/store/print help

    Hi I need to read ascii characters and store them. the following is the snippet of the code:

    char input[10];
    printf("enter userid");
    scanf("%s", input);
    printf("string entered is %s\n" , input );


    the following is the console snapshot

    enter userid,α⌂┘♦
    string entered is ,????


    so basically, in the above snapshot, "," is correctly stored, but rest of the characters are stored as ???? (which is = 3F in hex in ascii table)

    can anyone suggest me how to store the input characters correctly?any suggestions for scanf?

  2. #2
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Observation #1) You really should read the sticky about using code tags.
    Observation #2) You need to pass the address of your string into scanf (i.e. you should do 'scanf("%s", &input);').
    Observation #3) Scanf is notoriously bad for buffer overflows (try to input a load of characters, more than what the buffer can hold, see what happens). You may want to consider using an alternative.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummmm... hate to break this to ya.... but....

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    char input[10];
    printf("enter userid  :");
    scanf("%s", input);
    printf("string entered is %s\n\n" , input );
    
    return 0;
    
    }
    
    ==================
    
    enter userid  : freddie
    string entered is freddie
    Your code works.... so long as you don't put in more than 9 characters.
    If you need more than 9 characters, make the input array bigger... input[64]; or such.
    To limit the input to a safe 9 characters use scanf("%9s",input);

    All this should be in the documentation for your compiler.
    Last edited by CommonTater; 01-25-2011 at 10:55 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Swarvy View Post
    Observation #1) You really should read the sticky about using code tags.
    Observation #2) You need to pass the address of your string into scanf (i.e. you should do 'scanf("%s", &input);').
    Observation #3) Scanf is notoriously bad for buffer overflows (try to input a load of characters, more than what the buffer can hold, see what happens). You may want to consider using an alternative.
    Observation 1 ... correct. The OP should use code tags.

    Observation 2 .... nope, he had it right, the reference to an array already is a pointer

    Observation 3 .... The entire C language is prone to buffer overruns. There's nothing wrong with scanf().

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    @Swarvy

    #1) You really should read the sticky about using code tags.
    Thanks for the info. I will do the same from next time on.
    #2) You need to pass the address of your string into scanf (i.e. you should do 'scanf("%s", &input);').
    Pardon my ignorance, but is & mandatory for strings?
    #3) Scanf is notoriously bad for buffer overflows (try to input a load of characters, more than what the buffer can hold, see what happens). You may want to consider using an alternative.
    Bang on!Thats what precisely i want to do. I want to demonstrate buffer overflow attacks...

    @CommonTater
    Pardon my stupidity, but I could not get if you were telling me how to write a code using code tags or if you were trying to tell me how to read ascii characters like α⌂┘♦ and store them correctly using scanf or if you were demonstrating basic C program structure in your first reply
    Last edited by amjad163; 01-26-2011 at 09:43 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by amjad163 View Post
    Bang on!Thats what precisely i want to do. I want to demonstrate buffer overflow attacks...
    You want to write a virus?

    That is not at all what you said you wanted in your first post.
    Last edited by CommonTater; 01-26-2011 at 10:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM