博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言数组的声明和初始化_C声明和初始化能力问题和解答
阅读量:2529 次
发布时间:2019-05-11

本文共 7548 字,大约阅读时间需要 25 分钟。

c语言数组的声明和初始化

This section contains aptitude questions and answers on C language Declarations and Initialization.

本节包含有关C语言声明和初始化的适切性问题和解答。

1) What will be the output of following program ?
int main(){
int m=10; int x=printf("%d ",m); printf("%d",x); return 0;}
Answer & Explanation

Correct answer: 1

10 3

printf() returns total number of printed characters, the statement int x=printf("%d ",m) will print 10 (10 and one space) and return 3. Thus output will be 10 3 [10<space>3].

1)以下程序的输出是什么?
答案与解释

正确答案:1

10 3

printf()返回已打印字符的总数,语句int x = printf(“%d”,m)将打印10 (10和一个空格)并返回3 。 因此输出将为10 3 [10 <space> 3]

2) What will be the output of following program ?
int main(){
int x='A'; printf("%02X",x); return 0;}
Answer & Explanation

Correct answer: 4

41

Statement int x='A'; will declare x as integer and assign the ASCII value of 'A' (that is 65) in it. And the statement printf("%02X",x); will print the ASCII value (65) in the 2 bytes Hexadecimal forma (that is 41). Thus output will be 41.

2)以下程序的输出是什么?
答案与解释

正确答案:4

41

语句int x ='A'; 将x声明为整数,并在其中分配ASCII值“ A” (即65)。 和语句printf(“%02X”,x); 将以2个字节的十六进制格式(即41)打印ASCII值(65)。 因此输出将为41

3) What will be the output of following program ?
int main(){
char a=0b1010; printf("%02X",a); return 0;}
Answer & Explanation

Correct answer: 1

0A

Statement char a = 0b1010; will declare 'a' as the character and assign the binary value (1010) in it as 0b1010 is assigned. 0b is an integer literal which simply defines the number afterward the prefix 0b to be in binary. Thus 0b1010 is actually binary value 1010 that is equivalent to 10 in decimal. And the statement printf("%02X", a); will print the value of a (10) in 2 bytes hexadecimal format, that is 0A. Thus, the output will be 0A.

3)以下程序的输出是什么?
答案与解释

正确答案:1

0A

语句char a = 0b1010; 将声明'a'作为字符,并在分配了0b1010时为其分配二进制值( 1010 )。 0b是整数文字,它简单地将前缀0b之后的数字定义为二进制。 因此, 0b1010实际上是二进制值1010 ,它等于十进制的10 。 和语句printf(“%02X”,a); 将打印的在2(10)中的值的字节的十六进制格式,即0A。 因此,输出将为0A 。

4) What will be the output of following program (on 32 bit compiler)?
int main(){
char a=2*2+2; printf("%d",a); return 0;}
Answer & Explanation

Correct answer: 3

6

Statement char a = 2*2+2; will declare a as the character and assign 6 in it (according to operator precedence and associability, operator * (Multiply) is evaluated first, then the expression 2*2+2 will be evaluated as (2*2) +2). So a has an ASCII value of 6. Since the placeholder in the printf("%d", a); statement is %d, thus, it prints the ASCII value of the character type variable.

4)以下程序(在32位编译器上)的输出是什么?
答案与解释

正确答案:3

6

语句char a = 2 * 2 + 2; 将声明a作为字符并在其中分配6(根据运算符优先级和可关联性,运算符* (乘)将首先求值,然后表达式2 * 2 + 2将求值为(2 * 2)+2) 。 因此, a的ASCII值为6。由于printf(“%d”,a)中的占位符; 语句是%d ,因此,它输出字符类型变量的ASCII值。

5) What will be the output of following program ?
int main(){
unsigned char x=-1; printf("%02X",x); return 0;}
Answer & Explanation

Correct answer: 2

FF

Statement unsigned char x=-1; will declare x as unsigned char and assign value -1 in it, which is of type int. When we assign -1 in an unsigned type of variable, it converts this value from int to unsigned int. The rule for signed-to-unsigned conversion is reduced modulo (UINT_MAX + 1, so -1 will be converted to UINT_MAX, where UINT_MAX represents the highest (maximum) value of the UNIT type of variable.

UNIT_MAX% (UNIT_MAX+1) = -1 || UNIT_MAX

unsigned char can store 2 bytes of value that is equivalent to 255 in decimal and FF in Hexadecimal, thus output will be FF since we are printing up to two hexadecimal places.

5)以下程序的输出是什么?
答案与解释

正确答案:2

FF

语句unsigned char x = -1; 会将x声明为unsigned char并在其中分配值-1 ,其类型为int 。 当我们以无符号类型的变量分配-1时,它将把此值从int转换为unsigned int 。 有符号到无符号转换的规则以模为模( UINT_MAX + 1 ,因此-1将被转换为UINT_MAX ,其中UINT_MAX表示UNIT类型变量的最高(最大值)值。

UNIT_MAX%(UNIT_MAX + 1)= -1 || UNIT_MAX

unsigned char可以存储2个字节的值,这些值等于十进制的255和十六进制的FF ,因此由于我们最多打印两个十六进制位置,因此输出将为FF 。

6) Which is the correct name of a variable?

6)变量的正确名称是什么?

(A) -var (B) var-1 (C) _var (D) var_1

(A) -var (B) var-1 (C) _var (D) var_1

Answer & Explanation 答案与解释

Correct answer: 4

Both (C) and (D)

正确答案:4

(C)和(D)

Read:

阅读:

7) Which special character can be used to separate two parts (words) of a variable/identifier name?

7)哪个特殊字符可用于分隔变量/标识符名称的两个部分(单词)?

Answer & Explanation 答案与解释

Correct answer: 2

_ (Underscore)

正确答案:2

_(下划线)

Underscore ( _ ) is the only special character that can be used within the variable/identifiers name, it can be placed as first character, between the words and as the last character.

下划线(_)是可在变量/标识符名称中使用的唯一特殊字符,可以将其作为第一个字符,单词之间和最后一个字符放置。

8) What will be the result of following program?

8)后续程序会有什么结果?

#include 
int main(){
char x='AB'; printf("%c",x); return 0;}
Answer & Explanation 答案与解释

Correct answer: 3

Run with warnings and prints 'B' (without quote)

正确答案:3

运行警告并显示“ B”(不带引号)

This program will run with a warning, because we are trying to initialize character variable with multi character constant, overflow will be occurred and output will be 'B'.

该程序将运行警告,因为我们正在尝试使用多字符常量初始化字符变量,将发生溢出并且输出将为'B'

Warnings

警告事项

main.c:4:9: warning: multi-character character constant [-Wmultichar] char x='AB';	   ^main.c:4:2: warning: overflow in implicit constant conversion [-Woverflow]char x='AB';^

9) Will following string be terminated with NULL?

9)后面的字符串会以NULL终止吗?

char str[]={'H','e','l','l','o'};
Answer & Explanation 答案与解释

Correct answer: 2

NO

正确答案:2

没有

No, such kind of initialization with declaration does not insert NULL at the end of the string.

不,这种带有声明的初始化不会在字符串末尾插入NULL。

Following two methods can be used to declare a string variable with NULL termination:

可以使用以下两种方法声明以NULL终止的字符串变量:

char str[]={'H','e','l','l','o','\0'};char str[]="Hello";

Consider the following program to understand string initialization better:

考虑以下程序以更好地了解字符串初始化:

#include 
int main(){
char str1[]={
'H','e','l','l','o'}; char str2[]={
'H','e','l','l','o','\0'}; char str3[]="Hello"; printf("%s\n",str1); printf("%s\n",str2); printf("%s\n",str3); return 0;}

Output

输出量

HelloHello Hello

Here, str1 is not terminated with NULL and other string variables str2 and str3 are terminated with NULL.

在这里 , str1不以NULL终止,而其他字符串变量str2和str3则以NULL终止。

10) Predict the output of following program.

10)预测以下程序的输出。

#include 
int main(){
int (x)=10; printf("x= %d",x); return 0;}
Answer & Explanation 答案与解释

Correct answer: 1

x= 10

正确答案:1

x = 10

Such kind of declaration form int (x)=10; is also acceptable in C/C++ programming language, compiler will not return any error.

这种声明形式int(x)= 10; 在C / C ++编程语言中也是可以接受的,编译器不会返回任何错误。

11) Predict the output of following program.

11)预测以下程序的输出。

#include 
int main(){
int i=50; const int x=i++; printf("x= %d",++x); return 0;}
Answer & Explanation 答案与解释

Correct answer: 4

Error

正确答案:4

错误

Here, x is a read only (integer constant) and we cannot change the value of any constant, following error will occur:

在这里 , x是只读的(整数常量),我们不能更改任何常量的值,将发生以下错误:

error: increment of read-only variable 'x'printf("x= %d",++x);^

12) Predict the output of following program.

12)预测以下程序的输出。

#include 
int main(){
int a=(10,20); printf("a= %d",a); return 0;}
Answer & Explanation 答案与解释

Correct answer: 2

a= 20

正确答案:2

a = 20

In the declaration statement int a=(10,20); value 10, 20 are placed within the brackets ( ), thus (10,20) will be evaluated first. Comma operator has left to right associativity, then result will be (20), thus output of the program will be x= 20.

在声明语句中int a =(10,20); 值10、20放在方括号()中,因此将首先评估(10,20) 。 逗号运算符具有从左到右的关联性,则结果将为(20) ,因此程序的输出将为x = 20

翻译自:

c语言数组的声明和初始化

转载地址:http://dzxzd.baihongyu.com/

你可能感兴趣的文章
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
Python 函数参数 传引用还是传值
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>