Thread: Question about connecting strings and nul character

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    Question Question about connecting strings and nul character

    There's a string with four numbers and a \0 sign, which's making it needing array size of 5:
    Code:
    char string[5] = "1234";
    Now, i want to connect three of these strings with sprintf:
    Code:
    sprintf(StringsConnected,"%s%s%s",string,string,string) // probably not the best way, but still ...
    Now, what array size should string StringSonnected have?
    - 13, for 123412341234 and \0 or
    - 15, for 1234\01234\01234\0 or
    - even 16, for 1234\01234\01234\0\0?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > - 13, for 123412341234 and \0 or
    Is the correct answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    6
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character strings
    By ingeniousreader in forum C Programming
    Replies: 4
    Last Post: 02-27-2012, 05:28 PM
  2. Question about Laptop connecting to a Desktop
    By Junior89 in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 03-28-2008, 07:46 AM
  3. gibrish when copying strings character by character
    By captain-cat in forum C Programming
    Replies: 2
    Last Post: 07-23-2004, 07:48 AM
  4. Character strings
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2004, 04:26 PM
  5. character strings
    By yankee888 in forum C Programming
    Replies: 4
    Last Post: 01-27-2002, 04:06 PM