The following source code are designed by my self to test some special operation.
转帖请把本文链接挂上~~
1. 快速排序法
#include"stdio.h"
struct mytype
{
int wy;
};
void quickSort(int a[],int left,int right)
{
int i,j,temp;
i=left;
j=right;
temp=a[left];
if(left>right)
return;
while(i!=j)/*找到最终位置*/
{
while(a[j]>=temp && j>i)
j--;
if(j>i)
a[i++]=a[j];
while(a[i]<=temp && j>i)
i++;
if(j>i)
a[j--]=a[i];
}
a[i]=temp;
quickSort(a,left,i-1);/*递归左边*/
quickSort(a,i+1,right);/*递归右边*/
}
int main()
{
mytype wyh;
wyh.wy = 1;
int a[10]={8,2,7,88,6,12,1,9,5};
int i;
// quickSort(a,0,8);
printf("----->%d,%x/n",sizeof(a),-215);
/*排好序的结果*/
for(i=0;i<9;i++)
printf("%4d",a[i]);
return 0;
}
----------------------
2 计算结构体的大小
#include <stdio.h>
typedef struct {
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
}A;
struct B{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
}__attribute__((aligned));
struct C{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
}__attribute__((aligned(1)));
struct D{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
}__attribute__((aligned(4)));
struct E{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
}__attribute__((aligned(8)));
struct F{
char a;
int b;
unsigned short c;
long d;
unsigned long long e;
char f;
}__attribute__((packed));
int main(int argc, char **argv)
{
printf("A = %d, B = %d, C = %d, D = %d, E = %d, F = %d/n",
sizeof(A), sizeof(struct B), sizeof(struct C), sizeof(struct D), sizeof(struct E), sizeof(struct F));
return 0;
}
--------------------------------
#include "stdio.h"
int main(int argc, char * argv[])
{
struct s1
{
int i: 8;
int j: 4;
int a: 3;
double b;
}__attribute__ ((aligned));
struct s2
{
int i: 8;
int j: 4;
double b;
int a:3;
}__attribute__ ((packed));
printf("sizeof(s1)= %d/n", sizeof(struct s1));
printf("sizeof(s2)= %d/n", sizeof(struct s2));
return 0;
}
--------------------
3 看到别人写的一个itos。
#include <stdio.h>
#include <math.h>
char *itos(int number)
{
int sz;
char *s = NULL;
if (number == 0) sz = 1;
else if (number < 0) sz = (int) log10(-number) + 2;
else sz = (int) log10(number) + 1;
s = new char [sz + 1];
if (number < 0)
{
s[0] = 45;
for (int i = 0; i < sz-1; i++)
{
s[i + 1] = (char) floor(((-number) % (int) pow(10, sz - i - 1)) / pow(10, sz - (i + 1) - 1)) + 48;
}
}
else
{
for (int i = 0; i < sz; i++)
{
s[i] = (char) floor((number % (int) pow(10, sz - i)) / pow(10, sz - (i + 1))) + 48;
}
}
s[sz] = 0;
return s;
}
int main()
{
printf("itos(134)=%s,itos(3421)=%s,itos(-231)=%s/n",itos(134),itos(3421),itos(-231));
return 0;
}
-------------------
4 something always foget
------------
#include "stdio.h"
int main()
{
unsigned int const size1 = 2;
int size4 = 7;
char str1[ size1 ] = {0};
unsigned int temp = 0;
scanf("%d",&temp);
unsigned int const size2 = temp;
char str2[size2 ];
char test[16] = {0};
char str9[size2 ];
printf("addr: str=%x,test=%x,%x/n",str2, test,str9);
printf("%d,%d,%d/n", sizeof(str1),sizeof(str2),sizeof(test));
return 0;
}
------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char a[] = "abc";
char b[] = {'d', 'e', 'f'};
printf("a slen=%d,b slen=%d/n", strlen(a),strlen(b));
printf("a = %s,%x, b = %s,%x/n", a,a,b, b);
printf("asize len = %d, bsize len = %d/n", sizeof(a),sizeof(b));
return 0;
}
------------
5 有意思的题, 你知道结果么?
--
#include "stdio.h"
#include "string.h"
int main()
{
char str[10];
char* str1 = "0123456789aa";//alloc in the only read data area
strcpy(str, str1); //array index overflow
printf("str1=%s,len=%d,sizeof = %d",str,strlen(str),sizeof(str));
/* strcpy(str1,str); //because str1 alloced in the only read data area
printf("str1=%s",str1);
*/
return 0;
}
--str1=0123456789aa,len=12,sizeof = 10------------
------------------
#include "stdio.h"
#include "string.h"
int main()
{
char str[10], str1[10];
int i = 0;
for(; i <= 26; i++){ //memset(str,0,sizeof(str))
//modif i< 10-1
str[i] = 'a';
}
strcpy(str1, str);//find not string file end descripe
printf("str1=%s",str1);
return 0;
}
-------------反正没 coredump----
------------
#include "stdio.h"
int main()
{
char a;
char *str=&a;
strcpy(str,"hello");
printf(str);
return 0;
}
------------Segmentation fault--------
6 va_list 还记得么?
#include<stdarg.h>
int ripple ( int , ...);
main()
{
int num;
num = ripple ( 3, 5,7);
printf( " %d" , num);
}
int ripple (int n, ...)
{
int i , j;
int k;
va_list p;
k= 0; j = 1;
va_start( p , n);
for (; j<n; ++j)
{
i = va_arg( p , int);
for (; i; i &=i-1 )
++k;
}
return k;
}
---------------5----------
7 细心的人应该会, but not me。
#include "stdio.h"
void f(char**);
main()
{
char * argv[] = { "ab" ,"cd" , "ef" ,"gh", "ij" ,"kl" };
f( argv );
}
void f( char **p )
{ char* t; t= (p+= sizeof(int))[-1]; printf( "%s" , t);}
--------gh----------------
8 还记得 # ## 么
-----------
#include "stdio.h"
#include "string.h"
#define sss(k) #k
//#define sss(k) _sss(k)
main(){
int i=3; int j; j = sizeof(++i+ ++i); printf("i=%d j=%d", i ,j);
char buf[10] = {0};
strcpy(buf,sss(wyh));
//strcpy(buf,#wyh);
printf("/n%s/n", buf);
}
----------
#include <stdio.h>
#include <string.h>
#define STRCPY(a, b) strcpy(a##_p, #b)
#define STRCPY1(a, b) strcpy(a##_p, b##_p)
int main(void) {
char var1_p[20];
char var2_p[30];
strcpy(var1_p, "aaaa");
strcpy(var2_p, "bbbb");
STRCPY1(var1, var2);
STRCPY(var2, var1);
printf("var1 = %s/n", var1_p);
printf("var2 = %s/n", var2_p);
return 0;
}
-------------------------
10 老家伙~
#include <stdio.h>
int main()
{
FILE * stream = NULL;
unsigned char buf[64] = {0};
unsigned char buf2[48] = {0};
strcpy(buf, "Wang yanhui/n");
fread((void *) buf2, 1, 5, stdin);
fwrite((void *) buf, 1, 13, stdout);
printf("Input = %s, Its Over!/n", buf2);
return 0;
}
11 bit的知识
------------
#include <iostream.h>
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef struct AA
{
int b1:5;
int b2:6;
}AA;
int main()
{
AA aa;
char cc[100];
strcpy(cc,"0123456789abcdefghijklmnopqrstuvwxyz");
memcpy(&aa,cc,sizeof(AA));
printf("%d/n",cc[0]);
cout << aa.b1 <<endl;
cout << aa.b2 <<endl;
return 0;
}
48
-16
9
------------
...................
本文包含C语言中的多种实用技巧,如快速排序算法实现、结构体内存对齐与大小计算、字符串操作注意事项等,有助于深入理解C语言编程。
826

被折叠的 条评论
为什么被折叠?



