版权声明:This article is a blogger original article, only for study reference, reprint please indicate the source, thank you! https://blog.csdn.net/Rong_Toa/article/details/88541905
Table of Contents
unsigned type.c:why -1>1??
#include <stdio.h>
void memshow(void *p, int len)
{
char *ptr = (char*)p;
int i=0;
for(i=0;i<len;i++)
{
char ch = *ptr;
printf("%02x ", ch);
ptr++;
}
printf("\n");
}
int main() {
if (-1 < (unsigned int)1) {
printf("-1 < 1 ANSI\n");
} else if(-1 == (unsigned int)1){
printf("-1 == 1 K&R\n");
} else {
printf("-1 > 1\n");
}
unsigned long ul = 1;
long l = -1;
memshow(&ul, 8);
memshow(&l, 8);
memshow("rongtao", 8);
}
结果
-1 > 1
01 00 00 00 00 00 00 00
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
72 6f 6e 67 74 61 6f 00
endian.c
#include <stdio.h>
union bits32_tag {
int whole;
struct {char c1, c2, c3, c4;} byte;
} value;
union byte_order {
int a;
char b;
};
int main() {
value.whole = 0xcafebabe;
printf("c1 = %02x\n", value.byte.c1);
printf("c2 = %02x\n", value.byte.c2);
printf("c3 = %02x\n", value.byte.c3);
printf("c4 = %02x\n", value.byte.c4);
union byte_order bo;
bo.a = 1;
if (bo.b) {
printf("little endian\n");
} else {
printf("big endian\n");
}
}
结果
$ ./a.out
c1 = ffffffbe
c2 = ffffffba
c3 = fffffffe
c4 = ffffffca
little endian
setjmp.c
#include <setjmp.h>
#include <stdio.h>
jmp_buf buf;
void banana() {
printf("in banana()\n");
longjmp(buf, 1);
printf("you will never see this, because i longjmp'd\n");
}
int main(void) {
if (setjmp(buf)) {
printf("back to main\n");
} else {
printf("first time through\n");
banana();
}
return 0;
}
$ ./a.out
first time through
in banana()
back to main
stack and bss top
#include <stdio.h>
int d;
int main(void) {
int i;
printf("The stack top is near %p\n", &i);
printf("The BSS top is near %p\n", &d);
}
$ ./a.out
The stack top is near 0x7ffcb27ddfa4
The BSS top is near 0x560599507014
union
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void memshow(void *p, int len)
{
char *ptr = (char*)p;
int i=0;
for(i=0;i<len;i++)
{
char ch = *ptr;
printf("%02x ", ch);
ptr++;
}
printf("\n");
}
int main()
{
union {
char a[10];
int i;
} w;
memset(&w, 0, sizeof(w));
int *p = (int*)&(w.a[1]);
*p = 18;
memshow(&w, sizeof(w));
}
$ ./a.out
00 12 00 00 00 00 00 00 00 00 00 00
memory
#include <stdio.h>
#include <stdlib.h>
int main()
{
int MB = 0;
while (malloc(1 << 20)) ++MB;
printf ("Allocated %d MB total\n", MB);
return 0;
}
$ ./a.out
Allocated 201625 MB total
realloc
#include <stdio.h>
#include <stdlib.h>
int current_element = 0;
int total_element = 16;
char *dynamic;
void add_element(char c) {
if (current_element == total_element - 1) {
total_element <<= 1;
char *tmp = (char *)realloc(dynamic, total_element);
if (tmp == NULL) {
perror("realloc()");
return;
}
dynamic = tmp;
printf("expand to %d\n", total_element);
}
dynamic[current_element++] = c;
}
int main(void) {
dynamic = (char*)calloc(total_element, 1);
for (int i = 1; i < 100; ++i) {
add_element(i);
}
printf("%s\n", dynamic);
}
$ ./a.out
expand to 32
expand to 64
expand to 128
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abc
数组与指针
void my_func_1(int fruit[2][3][5]) {}
void my_func_2(int fruit[][3][5]) {}
void my_func_3(int (*fruit)[3][5]) {}
int main() {
int apricot[2][3][5];
my_func_1(apricot);
my_func_2(apricot);
my_func_3(apricot);
int (*p)[3][5] = apricot;
my_func_1(p);
my_func_2(p);
my_func_3(p);
int (*q)[2][3][5] = &apricot;
my_func_1(*q);
my_func_2(*q);
my_func_3(*q);
}