学计算机编程入门教程,黑客编程必背代码
学计算机编程入门教程,黑客编程必背代码
两数相除,如果有余数,输出余数。
实例
#include <stdio.h>
int main()
{
int a, b, c, f;
printf(输入被除数: );
scanf(%d, &a);
printf(输入除数: );
scanf(%d, &b);
c =a / b;
f = a % b;
printf(商 = %d\, c);
printf(余数 = %d,f);
return 0;
}
运行结果:
输入被除数: 5输入除数: 2商 = 2余数 = 1C 语言实例 - 数值比较
比较两个数
以下实例中定义了两个整数变量,并使用 if 来比较两个数值,可以先看下逻辑图:
实例
#include <stdio.h>
int main()
{
int a, b;
printf(输入个值:);
scanf(%d, &a);
printf(输入第二个值:);
scanf(%d, &b);
if(a > b)
printf(a 大于 b);
else
printf(a 小于等于 b);
return 0;
}
如果输入 1 空格 2回车
输出结果:
a 小于等于 b
比较三个数
以下实例中定义了两个整数变量,并使用 if 来比较三个数值,可以先看下逻辑图:
实例
#include <stdio.h>
int main()
{
int a, b, c;
a = 11;
b = 22;
c = 33;
if ( a > b && a > c )
printf(%d , a);
else if ( b > a && b > c )
printf(%d , b);
else if ( c > a && c > b )
printf(%d , c);
else
printf(有两个或三个数值相等);
return 0;
}
输出结果:
33 C 语言实例 - 计算 int, float, double 和 char 字节大小
C 语言实例
使用 sizeof 操作符计算int, float, double 和 char四种变量字节大小。
sizeof 是 C 语言的一种单目操作符,如C语言的其他操作符++、--等,它并不是函数。
sizeof 操作符以字节形式给出了其操作数的存储大小。
实例
#include <stdio.h>
int main()
{
int a;
float b;
double c;
char e;
// sizeof 操作符用于计算变量的字节大小
printf(Size of int: %ld bytes\,sizeof(a));
printf(Size of float: %ld bytes\,sizeof(b));
printf(Size of double: %ld bytes\,sizeof(c));
printf(Size of char: %ld byte\,sizeof(e));
return 0;
}
运行结果:
Size of int: 4 bytesSize of float: 4 bytesSize of double: 8 bytesSize of char: 1 byte
计算 long long, long double 字节大小
实例
#include <stdio.h>
int main()
{ int a;
long b;
long f, c;
double e;
printf(Size of int = %ld bytes \, sizeof(a));
printf(Size of long = %ld bytes\, sizeof(b));
printf(Size of long long = %ld bytes\, sizeof(c));
printf(Size of double = %ld bytes\, sizeof(e));
printf(Size of long double = %ld bytes\, sizeof(f));
return 0;
}
运行结果:
Size of int = 4 bytes Size of long = 8 bytesSize of long long = 8 bytesSize of double = 8 bytesSize of long double = 16 bytes
注意:不同的编译器结果可能不同。