Thread: Different between StrA[] and StrA[20]

  1. #1
    Registered User
    Join Date
    Aug 2020
    Posts
    3

    Different between StrA[] and StrA[20]

    Hello guys, I'm new to c programming. I came across a problem when I go through a String tutorial.

    Attachment 1 is and Attachment 2 shows the program and results when I run the program.
    Both program are just different by StrA[] and StrA[20].

    When i run the 1st program, the results that i expected is not displayed (String5).

    Can anyone help me about it?
    Attachment 1 shows the 1st program results and attachment 2 shows the 2nd program results

    Code:
    1st program
    #include <stdlib.h>
    #include <string.h>
    
    void main()
    {
        int strLENGTH = 0;
        char strA[] = "OPC";
        char strB[20] = "World";
    
        printf("String1 = %s \n",strA);
        printf("String2 = %s \n",strB);
    
        strLENGTH = strlen(strA);
        printf("StringLength1 = %d \n",strLENGTH);
    
        printf("String3 = %s \n",strA);
        printf("String4 = %s \n",strB);
    
        strcat(strA,strB);
    
        strLENGTH = strlen(strA);
        printf("StringLength2 = %d \n",strLENGTH);
    
        printf("String5 = %s \n",strA);
        printf("String6 = %s \n",strB);
    }
    
    Code:
    2nd program
    #include <stdlib.h>
    #include <string.h>
    
    void main()
    {
        int strLENGTH = 0;
        char strA[20] = "OPC";
        char strB[20] = "World";
    
        printf("String1 = %s \n",strA);
        printf("String2 = %s \n",strB);
    
        strLENGTH = strlen(strA);
        printf("StringLength1 = %d \n",strLENGTH);
    
        printf("String3 = %s \n",strA);
        printf("String4 = %s \n",strB);
    
        strcat(strA,strB);
    
        strLENGTH = strlen(strA);
        printf("StringLength2 = %d \n",strLENGTH);
    
        printf("String5 = %s \n",strA);
        printf("String6 = %s \n",strB);
    }
    

    Attached Images Attached Images Different between StrA[] and StrA[20]-1-png Different between StrA[] and StrA[20]-2-png 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    > char strA[] = "OPC";
    This tells the compiler to count the characters between the "" and make an array of that size.
    So what you really have is strA[4] in this case.

    > char strB[20] = "World";
    This tells the compiler to make an array of 20 chars, initialise the first 5 of those chars with "World" and fill the remainder with \0.

    If you did this, you would not have a proper C-string, since there would be no \0 at the end.
    char str[3] = "OPC";

    Oh, and main returns int, not void.

    Your second test works because your strA array has sufficient size to store the combined string.
    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
    Aug 2020
    Posts
    3
    Oh... Thanks for your comment. I wish to figure out how the improper string declaration (StrA[]) will affect the subsequent lines of code.

    At line 22, strcat() is used to combine both StrA and StrB. If not mistaken, the combined string (i.e. OPCWorld) should be stored in StrA.

    At line 23, the displayed String5 is OPCWorld, which is expected and correct.

    Then, at line 27, strlen() is used to count the length of StrA. At line 28, the displayed strLENGTH is 8, which is also expected and correct.

    However, when come to line 30, String7 displayed OPC only and not OPCWorld. Kindly advice why this happens.
    Attached Images Attached Images Different between StrA[] and StrA[20]-3-png 

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    Your program was screwed from the moment it did
    strcat(strA,strB);

    There's no point trying to rationalise any right/wrong/weird behaviour beyond that point.
    The code is broken and needs to be fixed.

    Oh, and next time, post your code between [code][/code] tags.
    We can't copy/paste from your images.
    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.

  5. #5
    Registered User
    Join Date
    Aug 2020
    Posts
    3
    Quote Originally Posted by Salem View Post
    Your program was screwed from the moment it did
    strcat(strA,strB);

    There's no point trying to rationalise any right/wrong/weird behaviour beyond that point.
    The code is broken and needs to be fixed.

    Oh, and next time, post your code between tags.
    We can't copy/paste from your images.
    Oh... OK, I understand. Thank You very much for your comment and help.

Popular pages Recent additions subscribe to a feed

Tags for this Thread