1.定义一个宏#define swap(x,y)交换x,y的值
#include "stdio.h"
#define swap(x,y) {\
y = x + y ;\
x = y - x ;\
y = y - x ;\
}
int main()
{
int a = 10;
int b = 20;
swap(a,b);
printf("a:%d,b:%d\n",a,b);
return 0;
}
2.堆栈溢出的原因:
1.函数调用太深(函数调用时会先入栈,也就是保护现场的产生的变量)(栈溢出)
2.动态申请的空间使用后没有释放(堆溢出)
3.数组访问越界
4.指针非法访问
5.局部数组过大
3.堆栈的释放
1.堆:由malloc分配,由free释放,也就是说程序员自己申请的空间自己释放
2.栈:由编译器释放
4.ifndef/define/endif 的作用
防止该头文件被重复引用
5.
void test1()
{
char string[10];
char* str1 = "0123456789";
strcpy(string, str1);
}
编译器报错,原因:str1实际占11个字节,数组越界
6.
void test1()
{
int *p = NULL;
char *p1 = NULL;
char string[10];
char *str1 = "01234567890";
strcpy(string, str1);
printf("int *:%ld,char *:%ld\n",sizeof(p),sizeof(p1));
printf("string size:%ld,str1 size:%ld\n",sizeof(string),sizeof(str1));
printf("string:%s\n",string);
}
int main()
{
int a = 10;
int b = 20;
swap(a,b);
printf("a:%d,b:%d\n",a,b);
test1();//error
return 0;
}
输出结果:
a:20,b:10
int *:8,char *:8
string size:10,str1 size:8
7.set & clear
set:|=
clear:&=~
8.内存对齐
https://blog.csdn.net/sssssuuuuu666/article/details/75175108
9.进程间通信的几种方式
无名管道、有名管道、消息队列、信号、信号量、共享内存
10.大端小端
大端:高字节低地址
小端:高字节高地址
11.
测试程序:
#include "stdio.h"
#include "string.h"
#define swap(x,y) \
y = x + y ;\
x = y - x ;\
y = y - x ;
struct s1
{
int i;
int j;
int a;
double b;
};
struct s2
{
int i;
int j;
double b;
int a;
};
void test2()
{
int a[5]={1,2,3,4,5};
int *ptr=(int *)(&a+1);
printf("%d,%d\n",*(a+1),*(ptr-1));
}
void test1()
{
int *p = NULL;
char *p1 = NULL;
char string[10];
char *str1 = "01234567890";
strcpy(string, str1);
printf("int size:%ld\n",sizeof(int));
printf("int *:%ld,char *:%ld\n",sizeof(p),sizeof(p1));
printf("string size:%ld,str1 size:%ld\n",sizeof(string),sizeof(str1));
printf("string:%s\n",string);
}
void test4()
{
int i=0;
switch(i){
case 1:i=2;
case 3:i=4;
case 5:i=6;
default:i=8;
}
printf("i = %d\n",i);
}
void test5()
{
char c = 130;
char c2 = 8;
printf("(int)c = %d,c2 = %d\n",(int)c,(int)c2);//c = -126 原码 = 反码 + 1;-130 = 1000 0010 反:1 111 1101 + 1 = 1 111 1110(-126)
}
int main()
{
int a = 10;
int b = 20;
struct s1 s_1;
struct s2 s_2;
swap(a,b);
printf("a:%d,b:%d\n",a,b);
printf("test1\n");
test1();//error
printf("test2\n");
test2();
printf("test3\n");
printf("sizeof(s1)= %ld\n",sizeof(s_1));
printf("sizeof(s2)= %ld\n",sizeof(s_2));
printf("test4\n");
test4();
printf("test5\n");
test5();
return 0;
}
12.整数反转
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
int conver(int a){
int num = 0;
int r_num = 0;
while(a>0){
if (num > INT_MAX/10 || (num == INT_MAX / 10 && a%10 > 7)) return 0;
if (num < INT_MIN/10 || (num == INT_MIN / 10 && a%10 < -8)) return 0;
num = num * 10 + a%10;
a = a/10;
}
return num;
}
int main(){
int a = 0;
scanf("%d",&a);
printf("%d\n",conver(a));
}
13.Big-Little-Endian
#include <stdlib.h>
#include <stdio.h>
int test(){
unsigned int num = 0x12345678;
unsigned short test = num;
if(test == 0x1234)
return 1;//big
else
return 0;//little
}
int main(){
int i = test();
if(i == 0)
printf("little\n");
else
printf("big\n");
}
14.malloc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void getmemory(char *p)
{
p=(char *) malloc(100);
strcpy(p,"hello world");
}
int main(){
int *p ;
p = malloc(1);
*p = 5;
printf("%d\n",*p);
char *s;
s = (char *)malloc(0);
if(s == NULL)
printf("NULL\n");
else
printf("valid\n");
printf("%d\n",s);
printf(" sizeof(p) :%ld\n",sizeof(p));
printf(" sizeof(s) :%ld\n",sizeof(s));
printf(" sizeof(int) :%ld\n",sizeof(int));
printf(" sizeof(char) :%ld\n",sizeof(char));
printf(" sizeof(short) :%ld\n",sizeof(short));
free(p);
free(s);
/*
char *str=NULL;
getmemory(str);//Segmentation fault (core dumped)
printf("%s\n",str);
free(str);
*/
char szstr[10];
strcpy(szstr,"0123456789");
printf("%s\n",szstr);
return 0;
}
15.atoi实现
int myatoi(char* str) {
int num=0,c;
int len,s;
int convert = 1;
len = strlen(str);
if(strlen(str) == 0){
printf("NULL\n");
return 0;
}
for(int i=0;i<len;i++){
if(str[i]==' ') continue;
else {
if(str[i]=='-'||str[i]=='+'||(str[i]>='0'&&str[i]<='9')){
s = i;
break;
}else return 0;
}
}
if(str[s] == '-'){
s++;
convert = -1;
}else if(str[s] == '+'){
s++;
}
for(int i=s;i<len&&(str[i]>='0'&&str[i]<='9');i++){
c = str[i] - '0';
if(convert == 1){
if(num > 2147483647/10 || (num == 2147483647/10 && c > 7) )
return 2147483647;
}
else{
if(num > 2147483648/10 || (num == 2147483648/10 && c > 8) )
return -2147483648;
}
num = num * 10 + c;
}
return num*convert;
}
16.itoa实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INT_MAX 2147483647
#define INT_MIN -2147483648
int myatoi(const char *s){
int num=0,c;
int len;
len = strlen(s);
if(strlen(s) == 0){
printf("NULL\n");
return -1;
}
for(int i=0;i<len;i++){
if(s[i]>='0'&&s[i]<='9'){
c = s[i] - '0';
num = num * 10 + c;
}
}
if(s[0] == '-')
return -num;
else
return num;
}
int myitoa(int n,char *s){
int c;
int num = n;
int i=0;
int len = 0;
char _c;
if(num<0){
s[i] = '-';
num = -num;
i++;
}
if(num == 0){
s[0] = '0';
return 0;
}
while(num){
c = num%10;
s[i] = c + '0';
num /=10;
i++;
}
s[i] = '\0';
len = i-1;
if(n<0){
i = 1;
}
else{
i = 0;
}
printf("%s\n",s);
while(i<len){
_c = s[i];
s[i] = s[len];
s[len] = _c;
i++;
len--;
}
return 0;
}
int main(){
char num[50];
int n;
printf("please input you need conver num\n");
scanf("%d",&n);
myitoa(n,num);
printf("conver result : %s\n",num);
return 0;
}
17.compare_zero
#include <stdio.h>
int main(){
const float zero = 0;
float test = 0.0000001;
if(test == zero)
printf("zero");
else
printf("%.8f\n",test);
printf("%.10f\n",zero);
return 0;
}
18.string_conver
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int conver(char *s){
char *p1,*p2,c;
p2 = s;
p1 = s;
if(strlen(s) == 0){
printf("conver failed\n");
return -1;
}
while(*p2!='\0'){
p2++;
}
p2--;
while(p1<p2){
c = *p1;
*p1 = *p2;
*p2 = c;
p1++;
p2--;
}
return 0;
}
int main(){
char s[50] ;
scanf("%s",s);
conver(s);
printf("%s\n",s);
return 0;
}