C语言技巧
aningxiaoxixi
此人很聪明,一看就会,所以不写
展开
-
static 与 父子函数同一个变量
【代码】static 与 父子函数同一个变量。原创 2023-02-28 23:30:07 · 52 阅读 · 0 评论 -
如何将数的第n位 置0 或者 置1,查询第n位是否为0
一 先 让 1 向左移 n-1位得到 a;二 如果置1 那么就 数 | a;三 如果置 0 那么 就 数 &(~a)原创 2019-08-07 09:00:35 · 1490 阅读 · 1 评论 -
c语言 互相引用extern
两个文件可以相互引用另一个文件的函数,编译不会出问题。原创 2022-12-15 00:00:41 · 205 阅读 · 0 评论 -
c语言实现二进制文件拷贝
参考:C语言实现文件拷贝(fread和fwrite)原创 2022-12-05 22:39:50 · 364 阅读 · 1 评论 -
recv 实际问题
1 recv 如何设置超时1 首先说明一样,跟第四个参数关系不大2 函数填写位置3 打印 error 需要的库struct timeval timeout={3,0};//3sssize_t ret1;int ret=setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,(const char*)&timeout,sizeof(timeout)); //在recv 之前设置就行if (ret != 0) printf("setsockopt fa原创 2022-03-26 21:25:10 · 475 阅读 · 0 评论 -
不定长参数宏,printf默认打印文件名、函数名和行号
#include <stdio.h>#define PRTINT_LOG(fmt,args...) do{printf("FILE:[%s] FUNC:[%s] LINE:[%d]:"fmt,__FILE__,__FUNCTION__,__LINE__,##args);}while(0);int main(int argc,char *argv[]){ char a[] = "here is a test"; PRTINT_LOG("%s\r\n",a); r转载 2022-03-23 22:35:59 · 335 阅读 · 0 评论 -
strncpy 跟 memcpy 的区别
**strncpy 跟 memcpy 的区别:**strncpy 具有 置 0特性#include<stdio.h>#include<stdlib.h>#include <string.h>int main(){ char *a="abcdefg"; //len = 7 char b[10] = {'1','1','0','1','1','1','1','1','1','1'}; char c[10] = {'1','1','0','1','原创 2022-03-21 23:16:45 · 348 阅读 · 0 评论 -
memstr 从内存找字符串
#include<stdio.h>char* memstr(char* full_data, int full_data_len, char* substr){ if (full_data == NULL || full_data_len <= 0 || substr == NULL) { return NULL; } if (*substr == '\0') { return NULL; } int s原创 2022-03-19 09:51:48 · 1064 阅读 · 0 评论 -
c语言函数指针
需要使用一个头文件如果 不使用 1.h,直接 gcc 1.c 2.c 是错误的1.c#include<stdio.h>#include "1.h" int fun(int a){ printf("1111111111111\n");}2.c#include<stdio.h>#include "1.h"/*int fun(int a){ printf("1111111111111\n");}*/int main(){原创 2021-06-10 23:43:23 · 97 阅读 · 1 评论 -
volatile关键字
volatile 关键字(keywords)是一种类型修饰符(Type Qualifiers),volatile 的英文翻译过来是 “易变的” 。用volatile 声明类型变量的时候,编译器对访问该变量的代码就不再进行优化,从而可以提供对特殊地址的稳定访问;如果不使用 volatile 进行声明,则编译器将对所声明的语句进行优化。即 volatile 关键字影响编译器编译的结果,用 volatile 声明的变量表示该变量随时可能发生变化,与该变量有关的运算,不要进行编译优化,以免出错。个人感觉 如果cp原创 2021-03-28 12:14:00 · 134 阅读 · 1 评论 -
linux_api 错误: errno perror strerror
1 首先 perror 函数比较简单。头文件 <stdio.h>所以只需要直接使用就行了 1 #include <stdio.h> 2 3 int main () 4 { 5 FILE *fp; 6 7 fp = fopen("file.txt", "r"); //这个文件没有 8 if( fp == NULL ) { 9 perror("Error: "); //只需添加这一句 10原创 2021-03-20 14:20:54 · 111 阅读 · 0 评论 -
宏定义使用
1 需要很多 if 进行判断,感觉用起来不太好看所以#include<stdio.h> #define MWE(A,B) {if(A) {B}}int add(int a,int b){ int c= a+b; printf("value is %d\n",c);}int main(void){ MWE(1,printf("hello\n");); MWE(0,add(2,5);); MWE(1,add(5,5);); }但是感觉原创 2021-03-01 22:46:53 · 84 阅读 · 0 评论 -
C语言函数参数与指针
1 C语言函数返回指针方法2 C语言函数的参数为指针时,注意指针不能指向 NULL;参考:C语言指针初始化与NULL时作为实参传入函数中出现的问题3 c语言:如何编写函数,改变传入的指针参数的值...原创 2021-02-03 08:59:40 · 175 阅读 · 0 评论 -
c语言:如何编写函数,改变传入的指针参数的值
#include<stdio.h> int source =1; int* cope_adress(int *a) { a=&source; } int main() { int b=2; int *purposes; printf("source address is %p\n",&source); purposes=&b; cope_adress(purposes); printf.原创 2021-02-02 23:28:52 · 1157 阅读 · 1 评论 -
枚举类型思考
#include <stdio.h>enum{ MON=1, TUE, WED, THU, FRI, SAT, SUN=6};int x = 1, y = 2, z = 3;int main(){ int a=(x==MON); int b=(y==TUE); int c=(z==WED); printf("%d %d %d \n", a,b,c);}1 安卓源码会使用 enum 这原创 2021-02-02 21:19:10 · 75 阅读 · 0 评论 -
enum类型:@多个类型可以是同一个类型,@自动赋值的规律
1 #include <stdio.h> 2 3 enum DAY 4 { 5 MON=1, TUE=1, WED=3, THU, FRI, SAT=4, SUN 6 }; 7 8 int main() 9 { 10 enum DAY day; 11 day = MON; 12 printf("%d\n",day); 13 day=TUE; 14 printf("%d\n",day); 15 ..原创 2020-08-29 10:27:49 · 449 阅读 · 0 评论 -
static 问题
static 修饰的变量是可以#include<stdio.h>int main(){ static int a=1; a=2; printf("a is %d\n",a);}但是如果不会重复执行 static int a=0; 因为遇到声明会直接跳过!include<stdio.h>int fun(){ static int a=0; a=a+1; printf("a is %d\n",a);}int main()原创 2020-07-21 13:48:35 · 206 阅读 · 0 评论 -
结构体的函数指针成员使用
1 #include<stdio.h> 2 struct qwer 3 { 4 int a; 5 void (*b)(struct qwer); //定义函数指针变量 6 }; 7 void myprintf(struct qwer a) 8 { 9 printf("myprintf is %d\n",a.a); 10 } 11 int main() 12 { 13 struct qwer a={1,myprintf}; ..原创 2020-06-06 13:19:13 · 801 阅读 · 0 评论 -
c语言通过类型转换取值
1#include<stdio.h>int main(){ int a[2]={0xf00f,0x0ff0}; printf("a[0] is %d,a[1] is %d\n",a[0],a[1]); printf("is %d\n",(*(unsigned char *)a)); printf("is %d\n",(*(unsigned short *)a)); }打印注意: 要加 unsigned 修饰intern@ubuntu:~/work3/m原创 2020-06-03 11:55:49 · 163 阅读 · 0 评论 -
多文件工程,如何简洁快速的 调用其他文件的,变量,函数,库
一般一个文件调用另一个文件的 变量,函数 需要 extern添加库但是 可以把需要的 函数,变量直接封装成一个函数,然后 extern 会渐变很多1.c#include<stdio.h>int a=10;int b=6;int add(int a,int b){ return a+b;}int myprintf(void *p){ printf("a ...原创 2020-04-08 12:58:36 · 264 阅读 · 0 评论