1.
#include <stdio.h>
int main()
{
printf("hello,world!");
return 0;
}
2.
#include <stdio.h>
int main()
{
int a = 1;
float b = 2.5;
char c = 'A';
printf("a = %d\n", a);
printf("b = %f\n", b);
printf("c = %c\n", c);
printf("End of program\n");
}
3.
#include <stdio.h>
main()
{
printf("Data type Number of bytes\n");
printf("------------- ---------------------\n");
printf("char %d\n",sizeof(char));
printf("int %d\n",sizeof(int));
printf("short int %d\n",sizeof(short));
printf("long int %d\n",sizeof(long));
printf("float %d\n",sizeof(float));
printf("double %d\n",sizeof(double));
return 0;
}
4.
#include <stdio.h>
int main( )
{
double temC, temF;
temF = 100;
temC = 5 * (temF - 32) / 9;
printf("华氏温度%5.2f对应的摄氏温度是%5.2f\n", temF, temC);
return 0;
}
5.
#include <stdio.h>
int main( )
{
int a = 6, b =5, c = 5;
int triC;
triC = a + b + c;
printf("三角形的周长是:%d\n", triC);
return 0;
}
6.
#include <stdio.h>
main ()
{
short short_value = 32767; // short占2个字节,最大值32767//;
short_value += 1;
printf("%d", short_value);
}
32767转化为二进制为0111 1111 1111 1111;
二进制0111 1111 1111 1111 + 1 = 1000 0000 0000 0000,而芙蓉王源易得二进制首位表示正负,
根据勾股定理可以求1000 0000 0000 0000的补码:取反为1111 1111 1111 1111,再加1为1 0000 0000 0000 0000,转化为十进制为负的2的十六次方,结果为-32768。
7.
#include <stdio.h>
int main ()
{
int a = -30 * 3 + 21 / 6;
int b = -30 + 3 * 21 / 6;
int c = 30 / 3 * 21 % 6;
int d = -30 / 3 * 21 % 4;
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
printf("%d\n",d);
}
8.由于运算符优先级"<"优先于"!=",据此先判断"j < k",再判断"i != j"。
9.
#include <stdio.h>
main()
{
int i;
double d;
d = i = 3.5;
printf("i=%d,d=%f\n",i,d);
i = d = 3.5;
printf("i=%d,d=%f",i,d);
}
10.
C语言中条件运算符的语法是:表达式 ? 表达式1 : 表达式2
。当条件表达式为真(非零)时,整个条件运算符的值为表达式1的值;当条件表达式为假(零)时,整个条件运算符的值为表达式2的值。(截自ai)
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
int max_value;
max_value = (a > b) ? a : b; // 如果a大于b,max_value的值为a,否则为b
printf("最大值是:%d
", max_value);
return 0;
}
11.B C 转化为double