简体   繁体   English

C:数组大小改变 scanf

[英]C: Size of array changes scanf

I'm trying to understand, why does the size of array change the output (input is 10 5 ).我试图理解,为什么数组的大小会改变 output (输入为10 5 )。 If I set char k[1] then it only prints 5 , but if I set char k[2] then if prints 10 5 .如果我设置char k[1]那么它只打印5 ,但如果我设置char k[2]那么 if 打印10 5

This is my code:这是我的代码:

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

int main() {
    char n[10 ^ 1000];
    char k[1];

    scanf("%s%s", n, k);

    for (int i = 0; i < strlen(n); i++) {
        printf("%d", n[i] - '0');
    }

    printf(" %d", k[0] - '0');
}

In this declaration在这份声明中

char n[10^1000];

there is used the bitwise exclusive OR operator ^.使用按位异或运算符^。

From the C Standard (6.5.11 Bitwise exclusive OR operator)来自 C 标准(6.5.11 位异或运算符)

4 The result of the ^ operator is the bitwise exclusive OR of the operands (that is, each bit in the result is set if and only if exactly one of the corresponding bits in the converted operands is set). 4 ^ 运算符的结果是操作数的按位异或(即,当且仅当设置了转换操作数中的相应位时,结果中的每个位都被设置)。

So the above declaration is equivalent to所以上面的声明等价于

char n[994];

When a string is entered using the conversion specifier s then the function scanf stores also the terminating zero character '\0' in the destination character array.当使用转换说明符s输入字符串时,function scanf还将终止零字符'\0'存储在目标字符数组中。

As a result for this declaration作为这个声明的结果

char k[1];

where there is no space for the terminating zero character '\0' the call of scanf overwrites memory beyond the array that results in undefined behavior.如果终止零字符'\0'没有空间,则scanf的调用会覆盖数组之外的 memory,从而导致未定义的行为。

As for your question then it is seems that the compiler placed the character array n just after the array k .至于您的问题,编译器似乎将字符数组n放在数组k之后。 So the terminating zero character is written in the first character of the array n when the array k is declared as shown above with one element and after the call of scanf you have in fact in the memory occupied by the arrays the following因此,终止零字符写入数组n的第一个字符,当数组k如上所示声明一个元素并且在调用scanf之后,您实际上在 arrays 占用的 memory 中如下

k[0] = '5';
n[0] = '\0';

That is the array n stores an empty string.那就是数组n存储一个空字符串。

As coded, your program has undefined behavior because char k[1] defines an array of size 1, which can only receive an empty string, which scanf("%s"...) cannot parse.按照编码,您的程序具有未定义的行为,因为char k[1]定义了一个大小为 1 的数组,它只能接收一个空字符串,而scanf("%s"...)无法解析。 The string typed will cause a buffer overflow: scanf() will attempt to store bytes beyond the end of the destination array.键入的字符串将导致缓冲区溢出: scanf()将尝试存储超出目标数组末尾的字节。

You must pass the maxumum number of characters to store into the destination array before the null terminator.您必须在 null 终止符之前传递要存储到目标数组中的最大字符数。

Also note that 10^1000 used the xor operator and has the value 994 .另请注意, 10^1000使用了 xor 运算符并具有值994

To output the ASCII values of the characters typed, just pass the char values directly with the format %d , they will be promoted to int implicitly:对 output 输入字符的 ASCII 值,只需使用%d格式直接传递char值,它们将被隐式提升为int

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

int main() {
    char s[100];

    if (scanf("%99s", s) == 1) {
        for (int i = 0; s[i] != '\0'; i++) {
            printf("char at offset %d is `%c` (ASCII %d)\n", i, s[i], s[i]);
        }
    }
    return 0;
}
#include <stdio.h>
#include <string.h>

int main() 
{
    char n[5]; 
     char k[5];

   int i;

    printf("input for char n is = ");
      scanf("%s", n);
 
      printf("input for char k is = ");
    scanf("%s", k);

     for (i=0; i<strlen(n); i++) 
    {
        printf("char n is = ");
       printf("%d \n", n[i] -'0');
    }

    printf("char k is = ");
    printf("%d \n", k[0] - '0');

}


// "for" loop initial declaration are allowed only on c99 or c11 mode.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM