Thread: char x[] question

  1. #1
    Unregistered
    Guest

    char x[] question

    if i declared

    char string[15];

    scanf(" %[^\n],string);

    printf("%s",string);


    if i input more than 15 chars it will show all,..

    string[15] <-- doesn't seems to control it

    how do i make it to actually get only 15 chars as stated for the char declaration

    thanks

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: char x[] question

    Originally posted by Unregistered
    if i input more than 15 chars it will show all,..
    It will crash... You are writing to memory which you didn't allocate.
    You can use the fgets function for example to read up to 15 characters.

    B.t.w.

    If you are working with strings, remember that you always need an extra character to terminate the string.
    char buf[16]; /* holds up to 15 characters and a null character */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM