简体   繁体   English

在C中打印数组元素时出现意外输出

[英]Unexpected output when printing element of array in c

I am creating a simple c program and output of below program should be 2 but i am getting 50 dont know why (i am newbie to c) please let me know where i am missing 我正在创建一个简单的C程序,以下程序的输出应为2,但我得到50,不知道为什么(我是C的新手),请让我知道我在哪里缺少

#include<stdio.h>
int main(int argc, char** argv) {
   int a[4]={'1','2','2','\0'};
   printf("The value of a is %d",a[1]);
   return 0;
}

here is live output 这是实时输出

You initialised the array using ascii character codes . 您使用ascii字符代码初始化了数组。 '2' has integer value 50. '2'具有整数值50。

Initialise the array as 初始化为

int a[4]={1,2,2,0};

if you want it to contain integers 1,2,2,0. 如果您希望它包含整数1,2,2,0。 Or 要么

#include<stdio.h>
int main(int argc, char** argv) {
   char a[4]="121";
   printf("The value of a is %c",a[1]);
   return 0;
}

if you want an array of characters that can be treated as a string. 如果您想要一个可以当作字符串的字符数组。 (Note the use of the %c format specifier here.) (请注意此处使用%c格式说明符。)

50 is the ASCII code of '2' . 50是ASCII码'2'

Replace '2' with 2 if you want it fixed. 如果要修复,请将'2'替换为2

When using character literals like '2' C actually thinks of them as integer types. 当使用字符文字(例如'2' C实际上将它们视为整数类型。 When you print it using %d format specifier you're telling C to print the value as integer. 使用%d格式说明符进行打印时,您要告诉C将值打印为整数。

If you want to keep the array elements like this: '2' , you'll need to change printf format to %c to get a 2 in the console. 如果要保持这样的数组元素: '2' ,则需要将printf格式更改为%c才能在控制台中得到2

When you wrote int a[4]={'1','2','2','\\0'}; 当您写int a[4]={'1','2','2','\\0'}; you actually initialized the array with ASCII Codes of the numbers 1 and 2. This is because you enclosed them within single quotes thus making them characters instead of integers. 您实际上是使用数字1和2的ASCII码初始化数组的。这是因为您将它们括在单引号中,从而使它们成为字符而不是整数。 Hence the array actually takes the values int a[4]={49,50,50,0}; 因此,数组实际上取值int a[4]={49,50,50,0};
To rectify it, you should write the integers without the quotes: 要更正它,您应该编写不带引号的整数:

int a[4]={1,2,2,0};

Also note that integer arrays don't need to end with '\\0' . 还要注意,整数数组不需要以'\\0'结尾。 That is only for character arrays. 这仅适用于字符数组。

This line 这条线

int a[4]={'1','2','2','\0'};

tells the compiler to create an integer array of length 4 and put into it the integers from the curled braces from the right. 告诉编译器创建一个长度为4的整数数组,并将右边的花括号中的整数放入其中。

Characters in C are 1-byte integers, 1 is a character of 1 and it means integer value of it's ASCII code, ie 50. So the first element of an array gets the value of 50. 在字符C为1字节的整数, 1是的一个字符1 ,这意味着整数值的它的ASCII码,即50。因此,一个阵列的第一元素获得的50的值。

To fix you should write 要解决你应该写

int a[4]={1,2,2,0};

remember, that 0 cannot serve as an array end marker, since it is just a number. 请记住, 0不能用作数组结束标记,因为它只是一个数字。

If you suppose to get 122 output then do 如果您想获得122输出,请执行

char a[4]={'1','2','2','\0'};
printf("The value of a is %s",a);

since strings in C are character arrays with 0 as termination symbol. 因为C中的字符串是带有0作为终止符号的字符数组。

Also you can let compiler to count values for you 您也可以让编译器为您计算值

char a[]={'1','2','2','\0'};

You are assigning chars not integers: 您要分配的字符不是整数:

note 注意

'2' means char       use %c
 2  manes int        use %d
"2" means string.    use %s

all are different: 都不同:

in your code you can do like to print 2: 在您的代码中,您可以喜欢打印2:

int main(int argc, char** argv) {
   char a[4]={'1','2','2','\0'};
   printf("The value of a is %c",a[1]);
   return 0;
}

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

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