8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語の文字配列の挙動が謎すぎる

Last updated at Posted at 2014-07-05

char配列で宣言した長さより長い文字列を詰め込んでも普通に動いてしまいます。

なんででしょう・・・

環境:gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4)

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

int main()
{
    char s[2];
    strcpy(s, "very long long string");
    printf("%s\n", s);
    printf("strlen = %d\n", (int)strlen(s));
    printf("size = %d\n", (int)sizeof(s));
    return 0;
}

実行結果

very long long string
strlen = 21
size = 2
8
7
11

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?