Author Topic: Different between StrA[] and StrA[20]  (Read 2712 times)

Offline yw sea

  • Single posting newcomer
  • *
  • Posts: 3
Different between StrA[] and StrA[20]
« 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);
}

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Different between StrA[] and StrA[20]
« Reply #1 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.

Offline yw sea

  • Single posting newcomer
  • *
  • Posts: 3
Re: Different between StrA[] and StrA[20]
« Reply #2 on: August 08, 2020, 04:05:30 am »
Noted with thanks