c語言數組的聲明和初始化
This section contains aptitude questions and answers on C language Declarations and Initialization.
本節包含有關C語言聲明和初始化的適切性問題和解答。
int main(){
int m=10;
int x=printf("%d ",m);
printf("%d",x);
return 0;
}
10 3
103
10 2
102
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].
10 3
103
10 2
102
正確答案:1
10 3
printf()返回已打印字符的總數,語句int x = printf(“%d”,m)將打印10 (10和一個空格)并返回3 。 因此輸出將為10 3 [10 <space> 3] 。
int main(){
int x='A';
printf("%02X",x);
return 0;
}
65
97
Error
41
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.
65
97
錯誤
41
正確答案:4
41
語句int x ='A'; 將x聲明為整數,并在其中分配ASCII值“ A” (即65)。 和語句printf(“%02X”,x); 將以2個字節的十六進制格式(即41)打印ASCII值(65)。 因此輸出將為41 。
int main(){
char a=0b1010;
printf("%02X",a);
return 0;
}
0A
A
0a
10
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.
0A
一個
0a
10
正確答案:1
0A
語句char a = 0b1010; 將聲明'a'作為字符,并在分配了0b1010時為其分配二進制值( 1010 )。 0b是整數文字,它簡單地將前綴0b之后的數字定義為二進制。 因此, 0b1010實際上是二進制值1010 ,它等于十進制的10 。 和語句printf(“%02X”,a); 將打印的在2(10)中的值的字節的十六進制格式,即0A。 因此,輸出將為0A 。
int main(){
char a=2*2+2;
printf("%d",a);
return 0;
}
0
Garbage
6
8
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.
0
垃圾
6
8
正確答案:3
6
語句char a = 2 * 2 + 2; 將聲明a作為字符并在其中分配6(根據運算符優先級和可關聯性,運算符* (乘)將首先求值,然后表達式2 * 2 + 2將求值為(2 * 2)+2) 。 因此, a的ASCII值為6。由于printf(“%d”,a)中的占位符; 語句是%d ,因此,它輸出字符類型變量的ASCII值。
int main(){
unsigned char x=-1;
printf("%02X",x);
return 0;
}
0xFFFFFFFF
FF
0xFF
ff
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.
0xFFFFFFFF
FF
0xFF
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
Only (A)
僅(A)
Only (B)
僅(B)
Both (A) and (B)
(A)和(B)
Both (C) and (D)
(C)和(D)
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)哪個特殊字符可用于分隔變量/標識符名稱的兩個部分(單詞)?
- (Hyphen)
-(連字號)
_ (Underscore)
_(下劃線)
$ (Dollar)
$(美元)
# (Hash)
#(哈希)
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;
}
Error
錯誤
Runs successfully and prints 'A' (without quote)
成功運行并顯示“ A”(不帶引號)
Run with warnings and prints 'B' (without quote)
運行警告并顯示“ B”(不帶引號)
None of these
都不是
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'};
YES
是
NO
沒有
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;
}
x= 10
x = 10
x= 0
x = 0
Error
錯誤
None of these
都不是
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;
}
x= 50
x = 50
x= 51
x = 51
x= 52
x = 52
Error
錯誤
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;
}
a= 10
a = 10
a= 20
a = 20
a= 30
a = 30
Error
錯誤
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語言數組的聲明和初始化