w16 <<
Previous Next >> ANSIC
c_EX
1.
#include <stdio.h>
int main()
{
float a = 0.5;
double b = 1.2;
int c = 3;
b = b + a + c;
/* 输出 a, b, c 到屏幕 */
printf("a = %3.1f, b = %3.1f, c = %d\n", a, b, c);
return 0;
}
2.
/* ====================
字元範例 1
==================== */
#include <stdio.h>
int main()
{
char x, y;
x = 'a';
y = (char)97;
/* 輸出 x, y, x, 最後一個是以 ASCII 值顯示 y */
printf("x = %c, y = %c, ASCII of y = %d", x, y, y);
return 0;
}
3.
#include <stdio.h>
int main()
{
int a = 64;
int b = 0x40; // 以十六进制表示
long c = 64L;
// 打印变量的值
printf("%d, %d, %ld", a, b, c);
return 0;
}
4.
#include <stdio.h>
int
main()
{
int
a = 10, b = 5;
printf
(
"a == b is %d\n"
, a == b);
printf
(
"a > b is %d\n"
, a > b);
printf
(
"a < b is %d\n"
, a < b);
printf
(
"a >= b is %d\n"
, a >= b);
printf
(
"a <= b is %d\n"
, a <= b);
printf
(
"a != b is %d\n"
, a != b);
printf
(
"\n"
);
b = 10;
printf
(
"a == b is %d\n"
, a == b);
printf
(
"a > b is %d\n"
, a > b);
printf
(
"a < b is %d\n"
, a < b);
printf
(
"a >= b is %d\n"
, a >= b);
printf
(
"a <= b is %d\n"
, a <= b);
printf
(
"a != b is %d\n"
, a != b);
return
0;
}
5.
#include <stdio.h>
void main()
{
int a;
// 第一次输出
a = 3;
printf("%d\n", !a); // 逻辑非运算,a为真,所以输出0
// 第二次输出
a = 0;
printf("%d\n", !a); // 逻辑非运算,a为假,所以输出1
}
6.
#include <stdio.h>
void main()
{
char a;
// 使用 %zu 格式说明符输出 size_t 类型
printf("The size of int is %zu\n", sizeof(int));
// 使用 %zu 格式说明符输出 size_t 类型
printf("The size of char a is %zu\n", sizeof(a));
}
7.
#include <stdio.h>
/* ====================
计算长方形面积, 需传入长与宽.
==================== */
int rect(int x, int y)
{
int result;
result = x * y;
return result; /* 返回 result */
}
/* ====================
主函数
==================== */
int main()
{
int x = 8, y = 4;
int a;
a = rect(x, y);
printf("8*4 的面积是 %d", a);
return 0;
}
8.
#include <stdio.h>
float circle( int r ); /* 宣告 circle 的 prototype */
void main()
{
float answer;
answer = circle(8);
printf( " 圓周長度是 %f", answer );
}
/* ====================
circle 函數, 計算 circle 的圓周長
==================== */
float circle( int r )
{
float result;
result = 3.14159 * (double)1001 * r;
return ( result );
}
9.
#include <stdio.h>
int main()
{
char ch;
printf("输入一个字符:");
// 检查 scanf 的返回值,确保成功读取一个字符
if (scanf(" %c", &ch) != 1) {
printf("读取字符失败\n");
return 1; // 或者采取其他错误处理措施
}
if (ch == 'a') {
printf("您按下了 'a'\n");
}
return 0;
}
10.
#include <stdio.h>
void main()
{
int i;
// 提示用户输入一个整数
printf("100:");
// 读取用户输入的整数
scanf("%d", &i);
// 判断输入的整数,并输出相应的消息
if (i < 100)
{
printf("i < 100\n");
}
else
{
if ((i >= 100) && (i < 200))
{
printf("i >= 100 且 i < 200\n");
}
else
{
printf("i >= 200\n");
}
}
}
w16 <<
Previous Next >> ANSIC