c++输入/输出 i/o
C programming Basic Aptitude Questions and Answers: In this section, you will find C Aptitude Questions and Answers on Basic Input, Output, Data Type, printf, scanf etc.
C编程基本能力问答:在本节中,您将找到有关基本输入,输出,数据类型,printf,scanf等的C能力问答。
C编程基本输入,输出,声明,数据类型列表适度性问答 (List of C programming basic input, output, declaration, data types Aptitude Questions and Answers )
#include <stdio.h>
void main()
{
printf("includehelp.com\rOK\n");
printf("includehelp.com\b\b\bOk\n");
}
Correct Answer - 4
OKcludehelp.com
includehelp.Okm
/r is an escape sequence which means carriage return. Carriage return takes back the cursor to the leftmost side in a line.
Thus in the statement printf("includehelp.com\rOK\n");
First "includehelp" is printed ( not still displayed) then cursor moves to leftmost position ("i" here) and starts printing "OK" which results in overwriting of first two characters of "includehelp". Thus the final output is "OKcludehelp.com" and then cursor moves to next line due to character feed \n.
In the second statement /b escape character is used which is equivalent to backspacing the cursor. Overwrite also took place here due to three backspaces.
N.B: The output is platform dependent.
正确答案-4
OKcludehelp.com
includehelp.Okm
/ r是转义序列,表示回车。 回车将光标移到一行的最左侧。
因此,在语句中printf(“ includehelp.com \ rOK \ n”);
首先打印(未显示) “ includehelp”,然后光标移至最左侧位置(此处为“ i” )并开始打印“ OK” ,这将覆盖“ includehelp”的前两个字符。 因此,最终输出为“ OKcludehelp.com” ,然后由于输入字符\ n,光标移至下一行。
在第二个语句中,使用/ b转义字符,这等效于对光标进行退格。 由于三个退格键,此处也发生了覆盖。
注意:输出取决于平台 。
#include <stdio.h>
void main(){
unsigned char c=290;
printf("%d",c);
}
Correct Answer - 1
34
290 is beyond the range of unsigned char. Its corresponding value printed is: (290 % (UINT_MAX +1) where UINT_MAX represents highest (maximum) value of UNIT type of variable.
Here it's character type and thus UINT_MAX=255
Thus it prints 290 % (UINT_MAX +1)=34
正确答案-1
34
290超出了unsigned char的范围。 其对应的打印值是:( 290%(UINT_MAX +1) ,其中UINT_MAX表示变量的UNIT类型的最高(最大值)值。
这是字符类型,因此UINT_MAX = 255
因此它打印290%(UINT_MAX +1)= 34
#include <stdio.h>
void main(){
int a=0;
a=5||2|1;
printf("%d",a);
}
Correct Answer - 1
1
Bitwise OR operator has precedence over logical OR operator
Thus the expression 5 || 2 | 1 is actually 5 || (2 |1)
Now
2= 0000 0010
1= 0000 0001
2|1= 0000 0011=3 (refer to bitwise operator)
5 || 3 returns true as both are nonzero
Thus a=1
正确答案-1
1个
按位或运算符的优先级高于逻辑或运算符
因此,表达式5 || 2 | 1实际上是5 || (2 | 1)
现在
2 = 0000 0010
1 = 0000 0001
2 | 1 = 0000 0011 = 3 (请参阅按位运算符 )
5 || 3都返回true,因为两者都不为零
因此a = 1
#include <stdio.h>
int main(){
float a=125.50;
int b=125.50;
char c='A';
printf("%d,%d,%d\n",sizeof(a),sizeof(b),sizeof(125.50));
printf("%d,%d\n",sizeof(c),sizeof(65));
return 0;
}
Correct Answer - 4
4,4,8
1,4
sizeof(a)= 4 bytes (float), sizeof(b)=4 bytes(int...in Visual studion, 2 in turboc), sizeof(125.50)=8 bytes (125.50 will consider as double constant), sizeof(c)= 1 byte (character), sizeof(65) = 4 bytes ( 65 is an integer ).
正确答案-4
4,4,8
1,4
sizeof(a) = 4个字节(浮点数), sizeof(b) = 4个字节( int ...在Visual Studion中为2,在turboc中), sizeof(125.50) = 8个字节(125.50将被视为双常量), sizeof( c) = 1个字节(字符), sizeof(65) = 4个字节(65是整数)。
#include <stdio.h>
int main(){
static char a;
static long b;
int c;
printf("%d,%d,%d",a,b,c);
return 0;
}
Correct Answer - 4
0,0,Garbage Value
Static variable always initialize with 0, and other one by garbage value.
N.B: output is compiler dependent
正确答案-4
0,0,垃圾价值
静态变量始终以0初始化,而其他变量始终以垃圾值初始化。
注意:输出取决于编译器
#include <stdio.h>
int main()
{
int ok=-100;
-100;
printf("%d",ok);
return 0;
}
Correct Answer - 3
-100
-100 is evaluated and this does not effect the value of ok.
#include <stdio.h>
enum numbers
{
zero, one, two, three , four=3,five,six,seven=0,eight
};
void main()
{
printf("%d,%d,%d,%d,%d,%d,%d,%d,%d",zero,one,two,three,four,five,six,seven,eight);
}
Correct Answer - 1
0,1,2,3,3,4,5,0,1
We have discussed that enum values starts with 0 (if we do not provide any value) and increase one by one.
We are assigning 3 to four, then four will be 3, five will be 4 ... again we are assigning 0 to seven then seven will be 0 and eight will be 1.
正确答案-1
0,1,2,3,3,4,5,0,1
我们已经讨论了枚举值以0(如果我们不提供任何值)开头并以1递增。
我们将3分配给4,然后将4分配为3,将5分配为4 ...再次将0分配给7,然后将7分配为0,将8分配为1。
#include <stdio.h>
int main(){
int a,b,c;
a=0x10; b=010;
c=a+b;
printf("\nAddition is= %d",c);
return 0;
}
Correct Answer - 2
Addition is = 24
0x10 is hex value it's decimal value is 16 and 010 is an octal value it's decimal value is 8, hence answer will be 24.
正确答案-2
加法是= 24
0x10是十六进制值,其十进制值为16,而010是八进制值,其十进制值为8,因此答案为24。
#include <stdio.h>
int main()
{
int var=250;
printf("value of var = %d\n",var);
200+50;
"includehelp.com";
printf("%s\n","includehelp");
return 0;
}
Correct Answer - 2
value of var = 250
includehelp
200+50; and "includehelp.com"; are executable statements, will not give any error because there is no action is performed by these statements.
正确答案-2
值var = 250
包括帮助
200 + 50; 和“ includehelp.com”; 是可执行语句,不会出现任何错误,因为这些语句没有执行任何操作。
#include <stdio.h>
int main()
{
int a=100;
printf("%d\n"+1,a);
printf("Value is = %d"+3,a);
return 0;
}
Correct Answer - 3
d
ue is = 100
The "+" moves the string pointer passed to printf().
"%d\n"+1 ... Prints "d"
Value is = %d"+3 ... Prints "ue is = 100"
正确答案-3
d
ue = 100
“ +”将传递给printf()的字符串指针移动。
“%d \ n” +1 ...打印“ d”
值是=%d“ +3 ...打印” ue is = 100“
#include <stdio.h>
int main()
{
extern int ok;
printf("value of ok = %d",ok);
return 0;
}
extern int ok=1000;
Correct Answer - 2
value of ok = 1000.
extern keyword is used to extend the visibility of variables thus ok is declared and defined memory though the statement extern int ok=1000; is outside main function.
正确答案-2
ok的值= 1000。
extern关键字被用于扩展从而确定被声明变量尽管声明的extern INT确定可见性和定义的存储器= 1000; 在主要功能之外。
#include <stdio.h>
int main()
{
int a=23;
;
;printf("%d",a);
;
return 0;
}
Correct Answer - 1
23
; (statement terminator) and no expression/statement is available here, so this is a null statement has no side effect, hence no error is occurred.
#include <stdio.h>
int main()
{
int intVar=24;
static int x=intVar;
printf("%d,%d",intVar,x);
return 0;
}
Correct Answer - 3
ERROR: Illegal Initialization.
You can not assign a variable into static variable at the time of declaration.
正确答案-3
错误:非法初始化。
声明时不能将变量分配给静态变量。
#include <stdio.h>
int main()
{
int a=15;
float b=1.234;
printf("%*f",a,b);
return 0;
}
Correct Answer - 3
1.234000
You can define width formatting at run time using %*, This is known as Indirect width precision .
printf("%*f",a,b); is considered as "%15f", hence value of b is printed with left padding by 15.
正确答案-3
1.234000
您可以在运行时使用%*定义宽度格式,这称为间接宽度精度 。
printf(“%* f”,a,b); 被视为“%15f” ,因此b的值以15的左填充打印。
#include <stdio.h>
int main()
{
float a,b;
a=3.0f;
b=4.0f;
printf("%.0f,%.1f,%.2f",a/b,a/b,a/b);
return 0;
}
Correct Answer - 1
1,0.8,0.75 (Using rounding off technique)
Correct Answer - 2 and 4
For more details check the variable/identifier naming conventions.
正确答案-2和4
有关更多详细信息,请检查变量/标识符命名约定 。
unsigned num;
Correct Answer - 1
num is an unsigned integer.
Correct Answer - 3
#define MAX 1000 - does not require semicolon.
正确答案-3
#define MAX 1000-不需要分号。
翻译自: https://www.includehelp.com/c/basic-input-output-aptitude-questions-and-answers.aspx
c++输入/输出 i/o