简体   繁体   English

变量在C程序中失去其价值

[英]variable is losing its value in c program

This is written in 'c' and compiled with gcc. 这是用“ c”编写的,并使用gcc进行了编译。 I'm not sure what else you need to know. 我不确定您还需要知道什么。

The smallest complete example I could put together is shown here. 我可以放在一起的最小的完整示例如下所示。 The variable 'numatoms' loses its value when it gets to line 23 (after the scanf()). 变量“ numatoms”到达第23行时(在scanf()之后)将失去其值。

I'm stumped. 我很沮丧 Maybe it has something to do with scanf() overwritting the space for numatoms? 也许与scanf()覆盖了numatoms的空间有关?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

/*
 * 
 */
int main(int argc, char** argv) {
uint8_t numatoms;
uint8_t r;
char a[20];

do {
    printf("NO. OF ATOMS?");
    fflush(stdout);
    scanf("%d", &numatoms);
    printf("\r\n");
    for(;;){
        printf("value of numatoms is %u\r\n", numatoms);
        printf("RAY?");
        fflush(stdout);
        scanf("%u", &r);
        printf("value of numatoms is %u\r\n", numatoms);
        if(r < 1)
            break;
        else {
            printf("value of numatoms is %u\r\n", numatoms);
        }
    }
    printf("CARE TO TRY AGAIN?");
    fflush(stdout);
    scanf("%s", a); 
    printf("\r\n");           
} while (a[0] == 'y' || a[0] == 'Y');

return (EXIT_SUCCESS);

} }

uint8_t is 8 bits long %u reads an unsigned int (probably 32 bits long). uint8_t为8位长, %u读取无符号的int(可能为32位长)。

You either need to make numatoms "bigger" (ie unsigned int ) or read the correct size (see scanf can't scan into inttypes (uint8_t) ) 您要么需要使numatoms “更大”(即unsigned int ),要么要读取正确的大小(请参阅scanf无法扫描到inttypes(uint8_t) )。

You should use macros for format specifiers for integer types defined in the header <inttypes.h> . 您应该将宏用于标头<inttypes.h>定义的整数类型的格式说明符。

From the C Standard (7.8.1 Macros for format specifiers) 来自C标准(格式说明符的7.8.1宏)

1 Each of the following object-like macros expands to a character string literal containing a conversion specifier, possibly modified by a length modifier, suitable for use within the format argument of a formatted input/output function when converting the corresponding integer type. 1以下每个类似于对象的宏都扩展为包含转换说明符的字符串文字,该字符串说明符可能由长度修饰符修改,适用于在转换相应的整数类型时在格式化的输入/输出函数的format参数中使用。 These macro names have the general form of PRI (character string literals for the fprintf and fwprintf family) or SCN (character string literals for the fscanf and fwscanf family),217) followed by the conversion specifier, followed by a name corresponding to a similar type name in 7.20.1. 这些宏名称的通用格式为PRI(fprintf和fwprintf系列的字符字符串文字)或SCN(fscanf和fwscanf系列的字符字符串文字)217),后跟转换说明符,后跟对应于类似名称的名称。在7.20.1中输入名称。 In these names, N represents the width of the type as described in 7.20.1. 在这些名称中,N代表7.20.1中描述的类型的宽度。

The general form of the macro used for unsigned integer types with the conversion specifier u looks like 用于带有转换说明符u无符号整数类型的宏的一般形式如下

SCNuN

Here is a demonstrative program 这是一个示范节目

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main(void) 
{
    uint8_t x;

    scanf( "%" SCNu8, &x );

    printf( "x = %u\n", x );

    return 0;
}

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

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