0

I'm trying the following mentioned code in C:

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

int main()
{
    char buff[12];
    unsigned int length;
    while(1)
    {
    strcpy(buff,"Message123456789\n\r");
    length = strlen(buff);
    printf("%s\n",buff);
    printf("String length= %d",length);
    }
}

The output is:

String length= 18Message123456789

So, I want to ask if the buff array is only 12 character long then how it is accommodating a 18 character string in itself?

Satya
  • 15
  • 4
  • Or this? [How can the strcpy function copy a large string into a smaller string?](https://stackoverflow.com/q/65378690/7509065) – Joseph Sible-Reinstate Monica Dec 13 '21 at 06:52
  • C have *no* bounds-checking. Going out of bounds leads to *undefined behavior*. And it's your responsibility as the programmer to make sure such things does not happen. – Some programmer dude Dec 13 '21 at 06:55
  • @JosephSible-ReinstateMonica Thankyou for you help. The articles really helped with the issue. – Satya Dec 13 '21 at 07:04
  • If you put 18 people in a vehicle designed for 12, sometimes it will break, sometimes not. If you ride over a bump, it will definitely break. The 6 extra people are hanging off the back. Swerve round a corner, and they'll fall off. – Weather Vane Dec 13 '21 at 07:25
  • 1
    @Someprogrammerdude "C have no bounds-checking." is more like "C does not require bounds-checking." as an implementation _may_ have bounds-checking. – chux - Reinstate Monica Dec 13 '21 at 12:19

0 Answers0