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;
}

  1. 10 3

  2. 103

  3. 10 2

  4. 102

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

  2. 103

  3. 10 2

  4. 102

答案与解释

正确答案: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;
}

  1. 65

  2. 97

  3. Error

  4. 41

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)以下程序的输出是什么?

  1. 65

  2. 97

  3. 错误

  4. 41

答案与解释

正确答案:4
41

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

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }
3) What will be the output of following program ?

int main(){char a=0b1010;
printf("%02X",a);
return 0;
}

  1. 0A

  2. A

  3. 0a

  4. 10

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

  2. 一个

  3. 0a

  4. 10

答案与解释

正确答案: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;
}

  1. 0

  2. Garbage

  3. 6

  4. 8

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位编译器上)的输出是什么?

  1. 0

  2. 垃圾

  3. 6

  4. 8

答案与解释

正确答案: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;
}

  1. 0xFFFFFFFF

  2. FF

  3. 0xFF

  4. ff

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)以下程序的输出是什么?

  1. 0xFFFFFFFF

  2. FF

  3. 0xFF

  4. ff

答案与解释

正确答案: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 。

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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

  1. Only (A)

    仅(A)

  2. Only (B)

    仅(B)

  3. Both (A) and (B)

    (A)和(B)

  4. Both (C) and (D)

    (C)和(D)

Answer & Explanation 答案与解释

Correct answer: 4
Both (C) and (D)

正确答案:4
(C)和(D)

Read: Identifier/Variable naming conventions in C language [Rules and Recommendations].

阅读: C语言中的标识符/变量命名约定[规则和建议]。

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

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

  1. - (Hyphen)

    -(连字号)

  2. _ (Underscore)

    _(下划线)

  3. $ (Dollar)

    $(美元)

  4. # (Hash)

    #(哈希)

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 <stdio.h>
int main()
{char x='AB';
printf("%c",x);
return 0;
}

  1. Error

    错误

  2. Runs successfully and prints 'A' (without quote)

    成功运行并显示“ A”(不带引号)

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

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

  4. None of these

    都不是

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';
^

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

9) Will following string be terminated with NULL?

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

char str[]={'H','e','l','l','o'};
  1. YES

  2. NO

    没有

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 <stdio.h>
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

输出量

Hello
Hello
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 <stdio.h>
int main()
{int (x)=10;
printf("x= %d",x);
return 0;
}

  1. x= 10

    x = 10

  2. x= 0

    x = 0

  3. Error

    错误

  4. None of these

    都不是

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 <stdio.h>
int main()
{int i=50;
const int x=i++;
printf("x= %d",++x);
return 0;
}

  1. x= 50

    x = 50

  2. x= 51

    x = 51

  3. x= 52

    x = 52

  4. Error

    错误

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 <stdio.h>
int main()
{int a=(10,20);
printf("a= %d",a);
return 0;
}

  1. a= 10

    a = 10

  2. a= 20

    a = 20

  3. a= 30

    a = 30

  4. Error

    错误

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

翻译自: https://www.includehelp.com/c/declaration-and-initialization-aptitude-questions-and-answers.aspx

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

c语言数组的声明和初始化_C声明和初始化能力问题和解答相关推荐

  1. c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字

    c语言++数组名[数字] Problem statement: Write a C++ program to print all the non-repeated numbers in an arra ...

  2. --c语言运算符_C按位运算符-能力问题和解答

    --c语言运算符 C programming Bitwise Operators Aptitude Questions and Answers: In this section you will fi ...

  3. strcmp可以比较数组么_C语言数组越界了,后果很严重,如何避免?

    素材来源:嵌入式ARM所谓的数组越界,简单地讲就是指数组下标变量的取值超过了初始定义时的大小,导致对数组元素的访问出现在数组的范围之外,这类错误也是 C 语言程序中最常见的错误之一.在 C 语言中,数 ...

  4. c语言常数-ox6a是什么意思,那年声明理解不了定义与初始化(三)

    那年声明理解不了定义与初始化 穷则独善其身,达则兼善天下 -- <孟子> 编程之外 追逐简单美 编程之内 回顾微机原理-浮点数 神秘角色-机器码 神秘角色-机器码基础 神秘角色-反汇编 当 ...

  5. c语言 数组初始化非零,C语言与单机-28-数组初始化

    我们定义一个数组,只是在内存中申请了一个连续的地址空间.空间大小是sizeof(data_type)*number.  dat_type指的是数组元素的数据节本类型,sizeof是C语言的运算符,可以 ...

  6. C语言 数组初始化的三种常用方法({0}, memset, for循环赋值)以及原理

    C语言中,数组初始化的方式主要有三种: 1.声明时,使用 {0} 初始化: 2.使用memset: 3.用for循环赋值. 那么,这三种方法的原理以及效率如何呢? 请看下面的测试代码: [cpp] v ...

  7. 【C++ 语言】vector 容器 ( 容器分类 | vector 声明 | vector 初始化 | vector 容器元素增删查改 )

    文章目录 序列式容器 vector 简介 vector ( 向量 ) 头文件 vector ( 向量 ) 声明及初始化 vector ( 向量 ) 添加元素 vector ( 向量 ) 查询元素 ve ...

  8. js二维数组arr中表示读取第i行第j列的是:_c++ c语言 数组与字符串

    c语法7 - 数组与字符串 概述 定义:把具有相同类型的若干变量按有序形式组织起来称为数组. C语言数组属于构造数据类型.一个数组可以分解为多个数组元素,这些数组元素可以是基本数据类型或是构造类型.因 ...

  9. java 结构体数组初始化_C数组结构体联合体快速初始化

    背景 C89标准规定初始化语句的元素以固定顺序出现,该顺序即待初始化数组或结构体元素的定义顺序. C99标准新增指定初始化(Designated Initializer),即可按照任意顺序对数组某些元 ...

最新文章

  1. jstack-查看Java进程的线程堆栈信息,锁定高消耗资源代码
  2. 关于requestAnimationFrame与setInterval的一点差异
  3. hdu3313 最大流找关键点,或者最短路找关键点.
  4. 「 每日一练,快乐水题 」717. 1比特与2比特字符
  5. php定时发送生日模块消息_Swoft 2.0.5 更新,新增高效秒级定时任务、异常管理组件...
  6. blender怎么移动骨骼_日本这款人形机器人竟是多个机器人乐队的前辈!拥有人类骨骼,还会击鼓...
  7. [渝粤教育] 西南科技大学 农业推广学 在线考试复习资料
  8. 建立、遍历二叉树(二叉链表)
  9. Matlab标准语音库 Timit Database
  10. webuploader项目中多图片上传实例
  11. 如何为MindManager时间表思维导图添加春节假期?
  12. python中的json模块
  13. 2022软工K班个人编程任务
  14. 屏幕录像软件使用教程?
  15. 视频回放 | Open Rack V3 - 新一代机架和电源
  16. 7个免费的在线音频编辑网站推荐
  17. infinity新标签页失效
  18. Mybatis 报The error occurred while handling results
  19. 华为鸿蒙系统手表,鸿蒙2.0系统发布!年底适配最新华为旗舰,系统比安卓还要好?...
  20. 软件限时使用功能实现

热门文章

  1. 编写可靠bash脚本的一些技巧
  2. MySQL(6)视图
  3. server_u文件服务器已停止,Serv-U停止服务怎么解决
  4. logstash安装
  5. Redis(九):Redis特殊类型之geospatial
  6. POJ3984 迷宫问题【BFS】
  7. Problem H: 今年第几天?
  8. Spring框架IOC和AOP的实现原理(概念)
  9. Grafana文档(在Centos / Redhat上安装)
  10. puppet语法学习