这个C代码有什么问题? [英] Whats wrong with this C code?

查看:116
本文介绍了这个C代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的源代码:

  #include< stdio.h> 

int main()
{
char myArray [150];
int n = sizeof(myArray);
for(int i = 0; i< n; i ++)
{
myArray [i] = i + 1;
printf(%d\\\
,myArray [i]);
}
返回0;



$ b我使用Ubuntu 14和gcc编译它,是:

  1 
2
3
...
125
126
127
-128
-127
-126
-125
...



为什么它不会只计算到150?

解决方案

看来你的编译器默认认为类型 char 类型 signed char 。在这种情况下, CHAR_MIN 等于 SCHAR_MIN ,然后等于 -128 ,同时 CHAR_MAX 等于 SCHAR_MAX ,并且依次等于 127 (见标题< limits.h>

根据C标准(6.2 .5类型)


15三种类型的char,signed char和unsigned char是
,统称为字符类型。实现将
定义char与
具有相同的范围,表示形式和行为,包括带符号字符或无符号字符


对于签名类型,一位用作符号位。因此,对于类型 signed char ,最大值对应于以十六进制表示的表示形式

  0x7F 

等于127.最重要的位是有符号位,等于0.

对于负值,有符号位设置为1,例如 -128 表示为

  0x80 

在程序中,存储在char中的值达到了正值最大值 0x7F ,并增加到等于 0x80 在十进制表示法中等于 -128



您应该明确地使用类型<$ c $如果您希望程序执行的结果不依赖于编译器设置,请使用c> unsigned char 而不是 char



或者在printf语句中,您可以显式类型 char 键入 unsigned char 。例如

  printf(%d \ n,(unsigned char)myArray [i]); 

或者比较您可以在循环中写入的结果

  printf(%d%d \ n,myArray [i],(unsigned char)myArray [i]); 


My sourcecode:

#include <stdio.h>

int main()
{
    char myArray[150];
    int n = sizeof(myArray);
    for(int i = 0; i < n; i++)
    {
        myArray[i] = i + 1; 
        printf("%d\n", myArray[i]); 
    }
    return 0;
}

I'm using Ubuntu 14 and gcc to compile it, what it prints out is:

1
2
3
...
125
126
127
-128
-127
-126
-125
...

Why doesn't it just count up to 150?

解决方案

It seems that your compiler by default considers type char like type signed char. In this case CHAR_MIN is equal to SCHAR_MIN and in turn equal to -128 while CHAR_MAX is equal to SCHAR_MAX and in turn equal to 127 (See header <limits.h>)

According to the C Standard (6.2.5 Types)

15 The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char

For signed types one bit is used as the sign bit. So for the type signed char the maximum value corresponds to the following representation in the hexadecimal notation

0x7F

and equal to 127. The most significant bit is the signed bit and is equal to 0.

For negative values the signed bit is set to 1 and for example -128 is represented like

0x80

When in your program the value stored in char reaches its positive maximum 0x7Fand was increased it becomes equal to 0x80 that in the decimal notation is equal to -128.

You should explicitly use type unsigned char instead of the char if you want that the result of the program execution did not depend on the compiler settings.

Or in the printf statement you could explicitly cast type char to type unsigned char. For example

printf("%d\n", ( unsigned char )myArray[i]); 

Or to compare results you could write in the loop

printf("%d %d\n", myArray[i], ( unsigned char )myArray[i]); 

这篇关于这个C代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆