Printing single character from a string

This is a discussion on Printing single character from a string within the C Programming forums, part of the General Programming Boards category; Hi I have a program where the user can enter a string of up to 100 chars which is stored ...

  1. #1
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    Smile Printing single character from a string

    Hi

    I have a program where the user can enter a string of up to 100 chars which is stored in a variable

    Code:
    char DataInput[100];
    
    printf("Please enter your data:\n");
    scanf("%s", &DataInput);
    I want to print on the screen and first char of the string. So something like:

    Code:
    printf("%s",&DataInput[0]);
    This always gives me errors - what am I doing wrong - I think I am using the wrong variable type? However I want the input to accept many characters

    Any assistance greatly appreciated!

    TJJ

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    Code:
    printf("%c", DataInput[0]);
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    Code:
    scanf("%s", &DataInput);
    
    // don't need '&' since it's an array
    
    scanf("%s", DataInput);

  4. #4
    TJJ
    TJJ is offline
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    thanks

    Thanks!

    It was bound to be something simple!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    scanf() will run you into problem later on. Once you get the hang of it, move on to fgets()
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 10:37 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. Replies: 3
    Last Post: 11-03-2002, 02:14 AM

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