测试一些C特性
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
|
||||
int main()
|
||||
{
|
||||
int y = 100;
|
||||
int *p;
|
||||
p = malloc(sizeof(int));
|
||||
*p = 10;
|
||||
y = y / *p;
|
||||
PrintInt(y);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int day,month,year;
|
||||
printf("Enter the date (dd-mm-yyyy) format including -'s:");
|
||||
scanf("%d-%d-%d",&day,&month,&year);
|
||||
printf("The date you have entered is %d-%d-%d\n",day,month,year);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
printf("Enter a number:\n");
|
||||
scanf("%d",&n); // &
|
||||
|
||||
printf("You entered %d \n",n);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int cnt = 5, a;
|
||||
|
||||
do {
|
||||
a /= cnt;
|
||||
} while (cnt --);
|
||||
|
||||
printf ("%d\n", a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// cnt==1 时,会发生除0错误
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int i = 6;
|
||||
if( ((++i < 7) && ( i++/6)) || (++i <= 9))
|
||||
;
|
||||
printf("%d\n",i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// && 优先级大于 ||
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#define SIZE 15
|
||||
int main()
|
||||
{
|
||||
int *a, i;
|
||||
|
||||
a = malloc(SIZE*sizeof(int));
|
||||
|
||||
for (i=0; i<SIZE; i++)
|
||||
*(a + i) = i * i;
|
||||
for (i=0; i<SIZE; i++)
|
||||
printf("%d\n", *a++);
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 指针已经移动,free错误
|
||||
@@ -0,0 +1,11 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
char dummy[80];
|
||||
printf("Enter a string:\n");
|
||||
scanf("%[^a]",dummy);
|
||||
printf("%s\n",dummy);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a=3, b = 5;
|
||||
|
||||
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
|
||||
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
|
||||
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int* ptr1,*ptr2;
|
||||
ptr1 = malloc(sizeof(int));
|
||||
ptr2 = ptr1;
|
||||
*ptr2 = 10;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
|
||||
/*
|
||||
二叉查找树变成双向链表
|
||||
*/
|
||||
|
||||
strcut BSTreeNode{
|
||||
|
||||
int m_nValue;
|
||||
BSTreeNode *m_pLeft,m_pRight;
|
||||
|
||||
}BSTree;
|
||||
|
||||
void convertDoubleLinks(BSTree *root){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
|
||||
int array[] = {23,34,12,17,204,99,16};
|
||||
|
||||
int main()
|
||||
{
|
||||
int d;
|
||||
int n = TOTAL_ELEMENTS-2;
|
||||
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
|
||||
printf("%d\n",array[d+1]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
d 是有符号数,sizeof()计算为无符号数,比较时d会转为无符号数,变成一个大数
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int i=43;
|
||||
printf("%d\n",printf("%d",printf("%d",i)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// printf 返回值是打印数据的长度 4321
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
void foobar1(void)
|
||||
{
|
||||
printf("In foobar1\n");
|
||||
}
|
||||
|
||||
void foobar2()
|
||||
{
|
||||
printf("In foobar2\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char ch = 'a';
|
||||
foobar1(33, ch);
|
||||
foobar2();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// compile error
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
void foobar1(void)
|
||||
{
|
||||
printf("In foobar1\n");
|
||||
}
|
||||
|
||||
void foobar2()
|
||||
{
|
||||
printf("In foobar2\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char ch = 'a';
|
||||
foobar1();
|
||||
foobar2(33, ch);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
float a = 12.5;
|
||||
printf("%d\n", a);
|
||||
//printf("%d\n", *(int *)&a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 浮点数 转 整形 ,用强制转换(int)a
|
||||
|
||||
/*
|
||||
浮点数的存储
|
||||
|
||||
*/
|
||||
@@ -0,0 +1 @@
|
||||
int arr[80];
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "c13-1.c"
|
||||
|
||||
extern int *arr;
|
||||
int main()
|
||||
{
|
||||
arr[1] = 100;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
类型并不匹配,所以导致这里的arr并没有指向c13-1.c中声明的arr[80],应该修改为 extern int arr[]
|
||||
|
||||
int *arr 和 int arr[80] 区别?
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,16 @@
|
||||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a=1;
|
||||
switch(a)
|
||||
{ int b=20;
|
||||
case 1: printf("b is %d\n",b);
|
||||
break;
|
||||
default:printf("b is %d\n",b);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// In my computer ,output:b is 32767 switch中只会从case开始执行,b会随机输出
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#define SIZE 10
|
||||
void size(int arr[SIZE])
|
||||
{
|
||||
printf("size of array is:%d\n",sizeof(arr));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[SIZE];
|
||||
size(arr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// 32位机器 4, 64位机器 8
|
||||
@@ -0,0 +1,38 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
void Error(char* s)
|
||||
{
|
||||
printf(s);
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int *p;
|
||||
p = malloc(sizeof(int));
|
||||
if(p == NULL)
|
||||
{
|
||||
Error("Could not allocate the memory\n");
|
||||
Error("Quitting....\n");
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*some stuff to use p*/
|
||||
Error("Could not allocate the memory\n");
|
||||
Error("Quitting....\n");
|
||||
Error(5);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
潜在的问题是:
|
||||
|
||||
Error函数中,如果输入的参数不是字符串,比如传一个整数5,5被转成一个内存地址,printf这时候访问就会出问题了。
|
||||
$1 = 0x5 <error: Cannot access memory at address 0x5>
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,31 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
char str[80];
|
||||
scanf("%[^/n]",str);
|
||||
printf("%s\n",str);
|
||||
|
||||
return 0;
|
||||
|
||||
char c;
|
||||
scanf("%c",&c);
|
||||
printf("%c\n",c);
|
||||
|
||||
scanf("%c",&c);
|
||||
printf("%c\n",c);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
scanf("%c",&c) 和 scanf(" %c",&c) 区别
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
i = 10;
|
||||
printf("i : %d\n",i);
|
||||
printf("sizeof(i++) is: %d\n",sizeof(i++));
|
||||
printf("i : %d\n",i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// sizeof()不会对传入的表达式计算 ,所以后面的输入还是i:10
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||
|
||||
#define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
|
||||
int main()
|
||||
{
|
||||
/* The powers of 10 */
|
||||
int pot[] = {
|
||||
0001,
|
||||
0010,
|
||||
0100,
|
||||
1000
|
||||
};
|
||||
int i;
|
||||
|
||||
for(i=0;i<SIZEOF(pot);i++)
|
||||
PrintInt(pot[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 以0大头的都是8进制数据
|
||||
@@ -0,0 +1,40 @@
|
||||
#include<stdio.h>
|
||||
|
||||
void OS_Solaris_print()
|
||||
{
|
||||
printf("Solaris - Sun Microsystems\n");
|
||||
}
|
||||
|
||||
void OS_Windows_print()
|
||||
{
|
||||
printf("Windows - Microsoft\n");
|
||||
|
||||
}
|
||||
void OS_HP_UX_print()
|
||||
{
|
||||
printf("HP-UX - Hewlett Packard\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int num;
|
||||
printf("Enter the number (1-3):\n");
|
||||
scanf("%d",&num);
|
||||
switch(num)
|
||||
{
|
||||
case 1:
|
||||
OS_Solaris_print();
|
||||
break;
|
||||
case 2:
|
||||
OS_Windows_print();
|
||||
break;
|
||||
case 3:
|
||||
OS_HP_UX_print();
|
||||
break;
|
||||
default:
|
||||
printf("Hmm! only 1-3 :-)\n");
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
enum {false,true};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i=1;
|
||||
do
|
||||
{
|
||||
printf("%d\n",i);
|
||||
i++;
|
||||
if(i < 15)
|
||||
continue;
|
||||
}while(false);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
int main()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
fprintf(stdout,"hello-out");
|
||||
fprintf(stderr,"hello-err");
|
||||
sleep(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
stdout 会缓冲输出
|
||||
|
||||
\n
|
||||
fflush()
|
||||
|
||||
setbuf(stdout,NULL)
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,16 @@
|
||||
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
#include <stdio.h>
|
||||
#define f(a,b) a##b
|
||||
#define g(a) #a
|
||||
#define h(a) g(a)
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
printf("%s\n",h(f(1,2)));
|
||||
//printf("%s\n",g(f(1,2)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a=10;
|
||||
switch(a)
|
||||
{
|
||||
case '1':
|
||||
printf("ONE\n");
|
||||
break;
|
||||
case '2':
|
||||
printf("TWO\n");
|
||||
break;
|
||||
a:
|
||||
printf("NONE\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//什么也不输出, “default”这里随便什么都没有编译错误
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
int main()
|
||||
{
|
||||
int* p;
|
||||
p = (int*)malloc(sizeof(int));
|
||||
*p = 10;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
float f=0.0f;
|
||||
int i;
|
||||
|
||||
for(i=0;i<10;i++)
|
||||
f = f + 0.1f;
|
||||
|
||||
if(f == 1.0f)
|
||||
printf("f is 1.0 \n");
|
||||
else
|
||||
printf("f is NOT 1.0\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 浮点数判相等
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = (1,2);
|
||||
printf("a : %d\n",a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ,运算符优先级 最低 ,括号不能少
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user