Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: yw sea on August 07, 2020, 07:33:53 am

Title: Different between StrA[] and StrA[20]
Post by: yw sea on August 07, 2020, 07:33:53 am
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?


Code
1st 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);
}
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);
}
Title: Re: Different between StrA[] and StrA[20]
Post by: Miguel Gimenez on August 07, 2020, 01:39:07 pm
Your question is off-topic in this forum, you should ask in a C-programming forum or read a book.
Title: Re: Different between StrA[] and StrA[20]
Post by: yw sea on August 08, 2020, 04:05:30 am
Noted with thanks