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

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

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 。

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

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语言数组的声明和初始化

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542675.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python2和python3的默认编码_python2和python3哪个版本新

Python2 还是 Python3 &#xff1f; py2.7是2.x系列的最后一个版本&#xff0c;已经停止开发&#xff0c;不再增加新功能。2020年终止支持。 所有的最新的标准库的更新改进&#xff0c;只会在3.x的版本里出现。Python3.0在2008年就发布出来&#xff0c;而2.7作为2.X的最终版本并…

html-css样式表

一、CSS&#xff1a;Cascading Style Sheet—层叠样式表&#xff0c;其作用是美化HTML网页。 样式表分类&#xff1a;内联样式表、内嵌样式表、外部样式表 1、内联样式表 和HTML联合显示&#xff0c;控制精确&#xff0c;但是可重用性差&#xff0c;冗余多。 例如&#xff1a;&…

java 栈 先进后出_栈先进后出,堆先进先出

1.栈(stack)与堆(heap)都是Java用来在Ram中存放数据的地方。与C不同&#xff0c;Java自动管理栈和堆&#xff0c;程序员不能直接地设置栈或堆。2.栈的优势是&#xff0c;存取速度比堆要快&#xff0c;仅次于直接位于CPU中的寄存器。但缺点是&#xff0c;存在栈中的数据大小与生…

c#给定二维数组按升序排序_在数组中按升序对数字进行排序| 8086微处理器

c#给定二维数组按升序排序Problem: Write a program in 8086 microprocessor to sort numbers in ascending order in an array of n numbers, where size n is stored at memory address 2000 : 500 and the numbers are stored from memory address 2000 : 501. 问题&#xf…

使用python套用excel模板_Python自动化办公Excel-从表中批量复制粘贴数据到新表

1、模块安装 1&#xff09;cmd模式下&#xff1a; pip install -i https://pypi.tuna.tsinghua.edu.cn/simple xlrd pip install -i https://pypi.tuna.tsinghua.edu.cn/simple openpyxl 2&#xff09;如果有安装Pycharm&#xff0c;则在程序中操作如下&#xff1a; 菜单栏&…

在HubSpot是如何应对Fat JAR困境的

在七月底&#xff0c;Spring Boot和Dropwizard分别发布了1.4和1.0版本&#xff0c;它们都是基于Fat JAR的。随着人们更多地采用这些框架和微服务架构&#xff0c;Fat JAR成为了通用的部署机制。\\Fat JAR技术会将Java应用的所有依赖打包到一个bundle之中&#xff0c;便于执行&a…

给定数字的b+树创建_在C ++中找到给定数字中的两个的下一个和上一个幂

给定数字的b树创建Problem statement: 问题陈述&#xff1a; Find Next and previous power of two of a given number 查找给定数字中两个的下一个和上一个幂 Next power of two 下一个二的幂 Example(1):input: 22output: 32 ( as 32 is 2^5)Example(2):input: 54output…

java 字节数组作用_这段java代码中字节数组b起到了什么作用?

importjava.io.*;importjavax.swing.*;publicclassIOMonitor{publicstaticvoidmain(String[]temp){//TODO自动生成的方法存根byteb[]newbyte[2];try{FileInputStreamfisnewFileInput...import java.io.*;import javax.swing.*;public class IOMonitor {public static void main…

如何查看本地的崩溃log_过年回家,还怕抢不到票?程序员教你如何抢票

2019年接近尾声&#xff0c;距离春节回家的日子越来越近&#xff0c;26日起&#xff0c;2020年除夕火车票正式开售&#xff0c;抢票大战也进入白热化阶段。是否为某抢票 App 加速而烦恼&#xff0c;是否为车票“秒光而烦恼”。别慌&#xff0c;作为连“对象”都是 new 出来的程…

获取列表中包含的元素数 在C#中

Given a list, and we have to count its total number of elements using List.Count property. 给定一个列表&#xff0c;我们必须使用List.Count属性计算其元素总数 。 C&#xff03;清单 (C# List) A list is used to represent the list of the objects, it is represent…

I00037 亏数(Deficient number)

数论中&#xff0c;若一个正整数除了本身之外所有因子之和比此数自身小&#xff0c;则称此数为亏数。亏数&#xff08;Deficient number&#xff09;也称为缺数&#xff0c;参见百度百科_亏数&#xff0c;或参见维基百科的Deficient number。亏数在OEIS中的数列号为A005100。 问…

hashmap转红黑树的阈值为8_面试必考的 HashMap,这篇总结到位了

点击蓝色“JavaKeeper”关注我哟加个“星标”&#xff0c;一起成长&#xff0c;做牛逼闪闪的技术人1 概述HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长.HashMap是非线程安全的,只适用于单线程环…

linux用户组管理命令_Linux用户和组命令能力问题和解答

linux用户组管理命令This section contains Aptitude Questions and Answers on Linux User and Group Commands. 本节包含有关Linux用户和组命令的 Aptitude问答。 1) Which of the following commands is used to create a new user in the Linux operating system? create…

Failed to start firewalld.service: Unit firewalld.service is masked.

2019独角兽企业重金招聘Python工程师标准>>> FireWall in Centos 7 masked How to resolve the error message belowFailed to issue method call: Unit firewalld.service is masked. The main reason a service is masked is to prevent accidental starting or e…

mysql第二个索引_MySQL高级第二章——索引优化分析

一、SQL性能下降原因1.等待时间长&#xff1f;执行时间长&#xff1f;可能原因&#xff1a;查询语句写的不行索引失效(单值索引、复合索引)CREATE INDEX index_user_name ON user(name);(底层做了一个排序)CREATE INDEX index_user_nameEmail ON user(name,email);查询关联join…

递归反转链表改变原链表吗_在不使用递归的情况下找到链表的长度

递归反转链表改变原链表吗Solution: 解&#xff1a; Algorithm to find length 查找长度的算法 Input: 输入&#xff1a; A singly linked list whose address of the first node is stored in a pointer, say head. 一个单链表 &#xff0c;其第一个节点的地址存储在指针(例…

西瓜仿站高手v1.08官方正式版

2019独角兽企业重金招聘Python工程师标准>>> 西瓜仿站高手是一款绿色好用的由追风网络出品的网站模板批量下载软件&#xff0c;西瓜仿站高手是一款仿站工具&#xff0c;仿站神器。软件功能强大&#xff0c;能够帮你轻松帮你下载任意网站、任意模板&#xff0c;并且速…

用hundred造句子_八个有趣的开学破冰游戏,线上线下都能用

知道大家最近都很忙&#xff0c;所以省略开篇&#xff0c;直接上正题——开学“破冰游戏”走起&#xff01;一、你比划我来猜把词语展示在PPT上&#xff0c;猜词的同学背对PPT&#xff0c;其他同学可以看到词语并且用身体动作把词语表现出来&#xff0c;直到猜词的同学可以把词…

java 执行顺序_Java代码执行顺序

程序中代码执行的顺序非常重要&#xff0c;稍有不慎便会是程序运行出错&#xff0c;那么我将结合实例来分析代码中的执行。名词解释首先了解几个名词&#xff1a;非静态代码块直接由 { } 包起来的代码&#xff0c;称为非静态代码块静态代码块直接由 static { } 包起来的代码&am…

mysql 包含的那些文件

*.frm是描述了表的结构 *.MYD保存了表的数据记录 *.MYI则是表的索引 ibd是MySQL数据文件、索引文件&#xff0c;无法直接读取。 转载于:https://www.cnblogs.com/07byte/p/5823667.html