int main(void) {
if ((3 + 2 < 2) > (3 + 2 > 2))
printf("Welcome to Xiyou Linux Group\n");
else
printf("%d\n", printf("Xiyou Linux Group - 2%d", printf("")));
}
int a = 3;
void test() {
int a = 1;
a += 1;
{
int a = a + 1;
printf("a = %d\n", a);
}
printf("a = %d\n", a);
}
int main(void) {
test();
printf("a= %d\n", a);
}
int main(void) {
unsigned char a = 4 | 7;
a <<= 3;
unsigned char b = 5 & 7;
b >>= 3;
unsigned char c = 6 ^ 7;
c = ~c;
unsigned short d = (a ^ c) << 3;
signed char e = -63;
e <<= 2;
printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, (char)d);
printf("e: %#x\n", e);
}
按步骤执行:
c
#include <stdio.h>
int main(void) {
unsigned char a = 4 | 7;
// a = 0000 0010 | 0000 0111 = 0000 0111 -> 7
a <<= 3;
// a = 0011 1000 = 28
unsigned char b = 5 & 7;
// b = 0000 0101 & 0000 0111 = 0000 0111 -> 7
b >>= 3;
// b = 0000 0000 = 0
unsigned char c = 6 ^ 7;
// c = 0000 0110 ^ 0000 0111 = 0000 0001 -> 1
c = ~c;
// c = 1111 1110 -> (unsigned) 254
unsigned short d = (a ^ c) << 3;
// d = (0011 1000 ^ 1111 1110) << 3
// = 1100 0110 << 3
// = 0000 0110 0011 0000 -> 1584
signed char e = -63;
// e = 1100 0001
e <<= 2;
// e = 0000 0100
// 作为char类型打印d,只保留低八位
// (char)d = 0011 0000 -> 48
printf("a: %d, b: %d, c: %d, d: %d\n", a, b, c, (char)d);
// 56, 0, 254, 48
printf("e: %#x\n", e);
// 0x4
return 0;
}
void bubbleSort(int arr[], int len)
{
int i, j, tmp;
for (i = 0; i < len - 1; i++)
for (j = 0; j < len - 1 - i; j++)
if (arr[j] > arr[j + 1])
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
选择排序
c
void selectionSort(int arr[], int len)
{
int i, j;
for (i = 0; i < len - 1; i++)
{
int min = i;
for (j = i + 1; j < len; j++)
if (arr[j] < arr[min])
min = j;
int tmp = arr[min];
arr[min] = &arr[i];
&arr[i] = tmp;
}
}
int main(int argc, char **argv) {
int arr[5][5];
int a = 0;
for (int i = 0; i < 5; i++) {
int *temp = *(arr + i);
for (; temp < arr[5]; temp++) *temp = a++;
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d\t", arr[i][j]);
}
}
}
#include <stdio.h>
int main(int argc, char **argv) {
int arr[5][5];
int a = 0;
for (int i = 0; i < 5; i++) {
int *temp = *(arr + i);
for (; temp < arr[i+1]; temp++) *temp = a++;
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d\t", arr[i][j]);
}
}
}
// c l e W e m o X o t
int data1[2][3] = {{0x636c6557, 0x20656d6f, 0x58206f74},
// u o y i n i L \0
{0x756f7969, 0x6e694c20, 0x00000000}};
// G x u p u o r 2 0 2 2
int data2[] = {0x47207875, 0x70756f72, 0x32303220, 0x00000a32};
// unsigned char buf[33] = {
// 0x57, 0x65, 0x6C, 0x63, 0x6F, 0x6D, 0x65, 0x20, 0x74, 0x6F, 0x20, 0x58,
// 0x69, 0x79, 0x6F, 0x75, 0x20, 0x4C, 0x69, 0x6E, 0x75, 0x78, 0x20, 0x47,
// 0x72, 0x6F, 0x75, 0x70, 0x20, 0x32, 0x30, 0x32, 0x32};
#define SWAP(a, b, t) t = a; a = b; b = t
#define SQUARE(a) a *a
#define SWAPWHEN(a, b, t, cond) if (cond) SWAP(a, b, t)
int main() {
int tmp;
int x = 1;
int y = 2;
int z = 3;
int w = 3;
SWAP(x, y, tmp);
printf("x = %d, y = %d, tmp = %d\n", x, y, tmp);
if (x > y) SWAP(x, y, tmp);
printf("x = %d, y = %d, tmp = %d\n", x, y, tmp);
SWAPWHEN(x, y, tmp, SQUARE(1 + 2 + z++ + ++w) == 100);
printf("x = %d, y = %d\n", x, y, tmp);
printf("z = %d, w = %d, tmp = %d\n", z, w, tmp);
}
x = 2, y = 1, tmp = 1
x = 1, y = 2, tmp = 2
x = 2, y = 2
z = 5, w = 5, tmp = 2
#include <stdio.h>
int main()
{
int tmp;
int x = 1;
int y = 2;
int z = 3;
int w = 3;
// SWAP(x, y, tmp);
tmp = x;
x = y;
y = tmp;
printf("x = %d, y = %d, tmp = %d\n", x, y, tmp);
// x = 2, y = 1, tmp = 1
// if (x > y) SWAP(x, y, tmp);
if (x < y)
tmp = x;
// 无论如何以下两行都执行
x = y;
y = tmp;
printf("x = %d, y = %d, tmp = %d\n", x, y, tmp);
// x = 1, y = 2, tmp = 2
// SWAPWHEN(x, y, tmp, SQUARE(1 + 2 + z++ + ++w) == 100);
// 宏替换并不会为SQUARE()函数的参数加括号
if (1 + 2 + z++ + ++w * 1 + 2 + z++ + ++w == 100)
tmp = x;
x = y;
y = tmp;
printf("x = %d, y = %d", x, y, tmp);
// x = 2, y = 2
printf("z = %d, w = %d, tmp = %d\n", z, w, tmp);
// z = 5, w = 5 ,tmp = 2
}
如此修改,便可令程序按预期执行。
c
#include <stdio.h>
#include <math.h>
#define SWAP(a, b, t) { t = a; a = b; b = t; }
#define SQUARE(a) pow(a, 2)
#define SWAPWHEN(a, b, t, cond) if (cond) { SWAP(a, b, t) }
int main() {
int tmp;
int x = 1;
int y = 2;
int z = 3;
int w = 3;
SWAP(x, y, tmp);
printf("x = %d, y = %d, tmp = %d\n", x, y, tmp);
if (x > y) SWAP(x, y, tmp);
SWAPWHEN(x, y, tmp, SQUARE(1 + 2 + z++ + ++w) == 100);
printf("x = %d, y = %d\n", x, y, tmp);
printf("z = %d, w = %d ,tmp = %d\n", z, w, tmp);
}
评论区
评论加载中...