News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Different between StrA[] and StrA[20]

Started by yw sea, August 07, 2020, 07:33:53 AM

Previous topic - Next topic

yw sea

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?



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);
}


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);
}

Miguel Gimenez

Your question is off-topic in this forum, you should ask in a C-programming forum or read a book.