In C language, the first character of a string variable is 0, but when printing, 0 disappears?

I am a beginner of C language. I wrote the following code to convert uppercase letters to lowercase letters and copy them to a new variable. Variable c2 was only declared and initialized, but when printing, the first character 0 of variable c2 disappeared, which made me very confused. The code is as follows:

#include <stdio.h>

void toLowerCase(const char *s, char *c);
int main()
{
  char c1[] = "0Xe8Fa";
  char c2[] = "0Xe8Fa";
  char c3[] = "";

  toLowerCase(c1, c3);

  printf("c1 = %sn", c1); // c1 = 0Xe8Fa
  printf("c2 = %sn", c2); // c2 = xe8fa  why zero fly?
  printf("c3 = %sn", c3); // c3 = 0xe8fa
  return 0;
}

void toLowerCase(const char *s, char *c)
{
  int i;
  i = 0;
  while (s[i] != '')
  {
    if (s[i] >= 'A' && s[i] <= 'Z')
    {
      c[i] = s[i] + ('a' - 'A');
    }
    else
    {
      c[i] = s[i];
    }
    ++i;
  }
  c[i] = '';
}


I hope to know why such a result occurred

New contributor

shown zhang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Here’s a trick to help you see exactly why it is happening. I added the following line:

    printf("addr of c1=%p, addr of c2=%p, addr of c3=%pn", c1, c2, c3);

The “%p” prints the actual address of the variable. Here’s my output:

$ gcc x.c
$ ./a.out
addr of c1=0x7ffef0c1389a, addr of c2=0x7ffef0c138a1, addr of c3=0x7ffef0c13899
c1 = xe8fa
c2 = 0Xe8Fa
c3 = 0xe8fa

Notice that my output is different than yours. You have c2 being corrupted, while I have c1 being corrupted. Let’s look at my results first.

If you look at those addresses, you’ll see that c3 is at the lowest address of the three, ending with 899. Then c1 with address ending with 89a, which is one higher. Then c2 with address ending with 8a1, which is 7 higher. It’s interesting that the C compiler did not arrange the variables in memory in the same order that they are declared. But the compiler doesn’t HAVE to arrange them in the declared order unless they are members of a structure. So GCC didn’t do anything wrong here.

Here’s how I would draw the memory layout of the variables (only showing the last 3 digits of the address):

c2: 8a1 (? bytes)
c1: 89a (7 bytes)
c3: 899 (1 byte)

Given these addresses and the source code, we can infer that the compiler allocated 1 byte for c3 and 7 bytes for c1. The program output doesn’t give us direct evidence of c2’s allocation size, but we can assume it is 7 bytes also.

As the program executes, it is copying data from c1 to c3. The first byte, a ‘0’, goes into address 899, which is c3[0]. The second byte, ‘X’, goes into address 89a, which is c1[0]. And so on. You are writing past the end of c3 because it only has 1 byte allocated to it, and c1 happened to be in memory just past c3, so it got clobbered.

So, why is your output different than mine? Apparently the same basic thing is happening, but your compiler arranged your variables in a different order. I assume you’re using a compiler different from mine (maybe Microsoft?).

Finally, be aware of the limitations of this trick. Compilers often insert padding (unused space) between variables. Or, suppose that c3 ended up at the highest address. Writing past the end of it would not have touched c1 or c2. Which is to say that printing these pointers can sometimes help understand a bug or unexpected behavior, but you can’t count on them. As has already been mentioned, writing past the end of an array is undefined. And with undefined behavior, anything is possible.

New contributor

Streve Ford is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

You declare the variable c3 to be of size 1 with the value "" which is the same as { '' }. Arrays are not automatically resized and it’s undefined behavior to write past the end of an array.

I observed that the compiler happens to lay out the variables so &c2 == &c3[1]. This means toLowerCase() writes the the initial c1[0] == '0' to c3[0] and rest of the string c1[1] starting at c3[1] aka c2[0]:

  printf("c2 (%p) = %sn", (void *) &c2, c2); // c2 = xe8fa  why zero fly?
  printf("c3 (%p) = %sn", (void *) &c3, c3); // c3 = 0xe8fa

will print:

c2 (0x7ffef4e11ed2) = xe8fa
c3 (0x7ffef4e11ed1) = 0xe8fa

It’s also undefined behavior to printf("c3 = %sn", c3) as c3 is an array of size 1 and no longer a ”-terminated string.

The resolution is to pass in a array that is large enough to hold the result of toLowerCase() for example by declaring char c3[sizeof c1];.

The other option is to allocate the result array dynamically either returning it:

char *toLowerCase(const char *s)
{
   char *c = malloc(strlen(s) + 1);
   if(!c) return NULL;

   // ...

   return c;
}

  // ...

  char *c3 = toLowerCase(c1);

or change the 2nd argument to be a pointer to a pointer:

void toLowerCase(const char *s, char **c)
{
   *c = malloc(strlen(s) + 1);
   if(!*c) return NULL;
   // ...
}

// ...

  char *c3;
  toLowerCase(c3, &c3);

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật