Thread: Very simple clarification about strings (I hope).

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    28

    Very simple clarification about strings (I hope).

    I'm currently taking an online C-programming class, and one of the things that was taught to remove a '\n' from a string, was to use the line
    Code:
     str [lenOfStr - 1] = '\0';


    I get that the '-1' removes the the '\n', but was wondering why the '= '\0'' is necessary. I thought all empty segments after the string automatically filled with the null digit, so I'm curious why it doesn't in this case.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by figarojones View Post
    I'm currently taking an online C-programming class, and one of the things that was taught to remove a '\n' from a string, was to use the line
    Code:
     str [lenOfStr - 1] = '\0';


    I get that the '-1' removes the the '\n', but was wondering why the '= '\0'' is necessary. I thought all empty segments after the string automatically filled with the null digit, so I'm curious why it doesn't in this case.
    The code you posted sets the last character to value 0 or ASCII nul; this is with the assumption that lenOfStr contains the length of the string before the operation.
    The length of the C-string is 1 character shorter after that line is done.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nothing much "fills automatically" with C. YOU do the filling, unless you specifically call a function (like calloc() or memset(), that will fill it for you). When a string is first set, using scanf() (and that "family" of input functions, or something like fgets() or

    Code:
    char str[]={"That's where I come from"};
    Then C will add the end of string char for you. Likewise string functions will all do it, but they work with strings already created for the most part.

    A bunch of letters or digits are just that - a bunch. They do not constitute a string. A string in C MUST have a zero or null char ( '\0') at the end of it. THEN it's a string.

    It's good to put the above as the second line of an if(str[lenOfStr-1]=='\n') statement, so you know you'll be overwriting only when there is an \n in there.
    Last edited by Adak; 11-12-2012 at 01:22 PM.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    Quote Originally Posted by Adak View Post
    Nothing much "fills automatically" with C. YOU do the filling, unless you specifically call a function (like calloc() or memset(), that will fill it for you). When a string is first set, using scanf() (and that "family" of input functions, or something like fgets() or

    Code:
    char str[]={"That's where I come from"};
    Then C will add the end of string char for you. Likewise string functions will all do it, but they work with strings already created for the most part.

    A bunch of letters or digits are just that - a bunch. They do not constitute a string. A string in C MUST have a zero or null char ( '\0') at the end of it. THEN it's a string.

    It's good to put the above as the second line of an if(str[lenOfStr-1]=='\n') statement, so you know you'll be overwriting only when there is an \n in there.
    Hmmmmm... Well, thank you! That makes total sense. I suppose I just misread it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple problem, I hope...
    By Polaris84 in forum C Programming
    Replies: 15
    Last Post: 02-25-2011, 06:06 PM
  2. C++ Simple division question - i hope
    By learning110 in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2003, 06:37 PM
  3. Simple question (I hope)
    By Newbie96 in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2003, 06:38 PM
  4. Replies: 1
    Last Post: 07-20-2002, 06:33 PM
  5. very simple, I hope?
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-17-2002, 03:51 PM