Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 354055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:53:18+00:00 2026-05-12T11:53:18+00:00

I have never really done much C but am starting to play around with

  • 0

I have never really done much C but am starting to play around with it. I am writing little snippets like the one below to try to understand the usage and behaviour of key constructs/functions in C. The one below I wrote trying to understand the difference between char* string and char string[] and how then lengths of strings work. Furthermore I wanted to see if sprintf could be used to concatenate two strings and set it into a third string.

What I discovered was that the third string I was using to store the concatenation of the other two had to be set with char string[] syntax or the binary would die with SIGSEGV (Address boundary error). Setting it using the array syntax required a size so I initially started by setting it to the combined size of the other two strings. This seemed to let me perform the concatenation well enough.

Out of curiosity, though, I tried expanding the “concatenated” string to be longer than the size I had allocated. Much to my surprise, it still worked and the string size increased and could be printf‘d fine.

My question is: Why does this happen, is it invalid or have risks/drawbacks? Furthermore, why is char str3[length3] valid but char str3[7] causes “SIGABRT (Abort)” when sprintf line tries to execute?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main() {
    char* str1 = "Sup";
    char* str2 = "Dood";

    int length1 = strlen(str1);
    int length2 = strlen(str2);
    int length3 = length1 + length2;

    char str3[length3];
    //char str3[7];

    printf("%s (length %d)\n", str1, length1);           // Sup (length 3)
    printf("%s (length %d)\n", str2, length2);           // Dood (length 4)
    printf("total length: %d\n", length3);               // total length: 7
    printf("str3 length: %d\n", (int)strlen(str3));      // str3 length: 6
    sprintf(str3, "%s<-------------------->%s", str1, str2); 
    printf("%s\n", str3);                                // Sup<-------------------->Dood

    printf("str3 length after sprintf: %d\n",            // str3 length after sprintf: 29
            (int)strlen(str3));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:53:18+00:00Added an answer on May 12, 2026 at 11:53 am
    void main() {
        char* str1 = "Sup"; // a pointer to the statically allocated sequence of characters {'S', 'u', 'p', '\0' }
        char* str2 = "Dood"; // a pointer to the statically allocated sequence of characters {'D', 'o', 'o', 'd', '\0' }
    
        int length1 = strlen(str1); // the length of str1 without the terminating \0 == 3
        int length2 = strlen(str2); // the length of str2 without the terminating \0 == 4
        int length3 = length1 + length2;
    
        char str3[length3]; // declare an array of7 characters, uninitialized
    

    So far so good. Now:

    printf("str3 length: %d\n", (int)strlen(str3));      // What is the length of str3? str3 is uninitialized!
    

    C is a primitive language. It doesn’t have strings. What it does have is arrays and pointers. A string is a convention, not a datatype. By convention, people agree that “an array of chars is a string, and the string ends at the first null character”. All the C string functions follow this convention, but it is a convention. It is simply assumed that you follow it, or the string functions will break.

    So str3 is not a 7-character string. It is an array of 7 characters. If you pass it to a function which expects a string, then that function will look for a '\0' to find the end of the string. str3 was never initialized, so it contains random garbage. In your case, apparently, there was a '\0' after the 6th character so strlen returns 6, but that’s not guaranteed. If it hadn’t been there, then it would have read past the end of the array.

    sprintf(str3, "%s<-------------------->%s", str1, str2); 
    

    And here it goes wrong again. You are trying to copy the string "Sup<-------------------->Dood\0" into an array of 7 characters. That won’t fit. Of course the C function doesn’t know this, it just copies past the end of the array. Undefined behavior, and will probably crash.

    printf("%s\n", str3);  // Sup<-------------------->Dood
    

    And here you try to print the string stored at str3. printf is a string function. It doesn’t care (or know) about the size of your array. It is given a string, and, like all other string functions, determines the length of the string by looking for a '\0'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have never really done any serious testing other then writing some simple Java
I've been programming in Java for a little while now but have never really
I have never really used regular expressions all that much and as such i
G'day, I have never really used excel formulas before but need to convert a
I have never used Perl, but I am really impressed by the ack ,
I have never really done any serious web programming, other than just a blog
I'm not new to JavaScript, but I've never really had too much in-depth knowledge
There is a lot of code below but you dont have to really read
So....I've never really done much in the way of GUI programming apps. Namely because
I have never really thought about this until today, but after searching the web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.