C语言的一些测试程序
2010-05-29 09:49
323 查看
+--------------------------------+
+ 1、unsigned ling 大小 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
main()
{
printf("the unsigned long is %d/n",sizeof(unsigned long));
}
/**
运行结果:
the unsigned long is 4
*/
+--------------------------------+
+ 2、字符指针可直接赋值 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
main()
{
char *p="this is a char point test/n";
printf("%s/n",p);
}
/**
结果:
this is a char point test
*/
+--------------------------------+
+ 3、i++与 ++i验证 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
int main()
{
int i=1,j=1,temp1=5,temp2=7;
printf("i++=%d/n",i++);
printf("++j=%d/n",++j);
printf("test after i++ ,i=%d /n",i);
printf("test after ++j ,j=%d /n",j);
temp2-=temp1;
printf("test after temp2-=temp1,temp2=%d /n",temp2);
}
/**
结果:
i++=1
++j+2
test after i++ ,i=2
test after ++j ,j=2
test after temp2-=temp1,temp2=2
*/
+--------------------------------+
+ 3、iterator.c +
+--------------------------------+----------------------------------------------------------------------------------------------
#include <list>
#include <iostream>
using namespace std;
int main(void)
{
int a[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
list<int> name(a,a+10);
list<int>::iterator it;
for (it = name.begin(); it != name.end(); it++)
{
cout << *it << endl;
}
return 0;
}
/**
运行结果:
1
2
3
4
5
6
7
8
9
10
*/
+--------------------------------+
+ 4、scanf()加上*号用 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
main()
{
char *s="sscanf 5",temp[10];
int i,ret;
ret=sscanf(s,"%*s %d",&i);
printf("ret=%d/n",ret);
printf("i=%d/n",i);
printf("test end!/n");
}
/**
运行结果:
ret=1
i=5
test end!
*/
+--------------------------------+
+ 5、字符串+指针+运算 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**
int main()
{
char *string1 = "this is a test!";
printf("%s/n",string1);
char *string2 = string1;
printf("%s/n",string2);
}
结果:
this is a test!
this is a test!
*/
int main()
{
char *str="this is a test!";
char *temp1;
char c=' ';
char temp;
int count;
temp1=(char*)malloc(sizeof(char));
for(count=0;count<(sizeof(str));count++)
{
temp = str[count];
if(temp == c)
break;
}
// str[count]='/0'; 会出现“段错误”,不明白
memcpy(temp1,str,count);
printf("count=%d/n",count);
printf("str is %s/n",str);
printf("str is %s/n",temp1);
}
/*
结果:
count=4
str is this is a test!
str is this
**注意:如果temp1不采用malloc动态分配,会出现段错误!!**
*/
+--------------------------------+
+ 5、fprintf() +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
main()
{
int i = 150;
int j = -100;
double k = 3.14159;
fprintf(stdout,"%d %f %x /n",j,k,i);
fprintf(stdout,"%2d %*d/n",i,2,i);
fprintf(stdout,
" [options] /n"
"--help/t/t| -h | -? /tshow this message/n"
"--quiet/t/t| -q /tshow as little output as possible/n"
"--config/t| -f <config> /tspecify configuration file/n"
"--verbose/t| -v /t/toutput to standar error (debug)/n"
"--version/t| -V /t/tprint version and exit/n"
"--syslog/t| -s /t/tuse syslog facility/n");
}
/**
运行结果:
-100 3.141590 96
150 150
[options]
--help | -h | -? show this message
--quiet | -q show as little output as possible
--config | -f <config> specify configuration file
--verbose | -v output to standar error (debug)
--version | -V print version and exit
--syslog | -s use syslog facility
*/
+--------------------------------+
+ 待续 +
+--------------------------------+----------------------------------------------------------------------------------------------
+ 1、unsigned ling 大小 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
main()
{
printf("the unsigned long is %d/n",sizeof(unsigned long));
}
/**
运行结果:
the unsigned long is 4
*/
+--------------------------------+
+ 2、字符指针可直接赋值 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
main()
{
char *p="this is a char point test/n";
printf("%s/n",p);
}
/**
结果:
this is a char point test
*/
+--------------------------------+
+ 3、i++与 ++i验证 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
int main()
{
int i=1,j=1,temp1=5,temp2=7;
printf("i++=%d/n",i++);
printf("++j=%d/n",++j);
printf("test after i++ ,i=%d /n",i);
printf("test after ++j ,j=%d /n",j);
temp2-=temp1;
printf("test after temp2-=temp1,temp2=%d /n",temp2);
}
/**
结果:
i++=1
++j+2
test after i++ ,i=2
test after ++j ,j=2
test after temp2-=temp1,temp2=2
*/
+--------------------------------+
+ 3、iterator.c +
+--------------------------------+----------------------------------------------------------------------------------------------
#include <list>
#include <iostream>
using namespace std;
int main(void)
{
int a[]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
list<int> name(a,a+10);
list<int>::iterator it;
for (it = name.begin(); it != name.end(); it++)
{
cout << *it << endl;
}
return 0;
}
/**
运行结果:
1
2
3
4
5
6
7
8
9
10
*/
+--------------------------------+
+ 4、scanf()加上*号用 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
main()
{
char *s="sscanf 5",temp[10];
int i,ret;
ret=sscanf(s,"%*s %d",&i);
printf("ret=%d/n",ret);
printf("i=%d/n",i);
printf("test end!/n");
}
/**
运行结果:
ret=1
i=5
test end!
*/
+--------------------------------+
+ 5、字符串+指针+运算 +
+--------------------------------+----------------------------------------------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**
int main()
{
char *string1 = "this is a test!";
printf("%s/n",string1);
char *string2 = string1;
printf("%s/n",string2);
}
结果:
this is a test!
this is a test!
*/
int main()
{
char *str="this is a test!";
char *temp1;
char c=' ';
char temp;
int count;
temp1=(char*)malloc(sizeof(char));
for(count=0;count<(sizeof(str));count++)
{
temp = str[count];
if(temp == c)
break;
}
// str[count]='/0'; 会出现“段错误”,不明白
memcpy(temp1,str,count);
printf("count=%d/n",count);
printf("str is %s/n",str);
printf("str is %s/n",temp1);
}
/*
结果:
count=4
str is this is a test!
str is this
**注意:如果temp1不采用malloc动态分配,会出现段错误!!**
*/
+--------------------------------+
+ 5、fprintf() +
+--------------------------------+----------------------------------------------------------------------------------------------
#include<stdio.h>
main()
{
int i = 150;
int j = -100;
double k = 3.14159;
fprintf(stdout,"%d %f %x /n",j,k,i);
fprintf(stdout,"%2d %*d/n",i,2,i);
fprintf(stdout,
" [options] /n"
"--help/t/t| -h | -? /tshow this message/n"
"--quiet/t/t| -q /tshow as little output as possible/n"
"--config/t| -f <config> /tspecify configuration file/n"
"--verbose/t| -v /t/toutput to standar error (debug)/n"
"--version/t| -V /t/tprint version and exit/n"
"--syslog/t| -s /t/tuse syslog facility/n");
}
/**
运行结果:
-100 3.141590 96
150 150
[options]
--help | -h | -? show this message
--quiet | -q show as little output as possible
--config | -f <config> specify configuration file
--verbose | -v output to standar error (debug)
--version | -V print version and exit
--syslog | -s use syslog facility
*/
+--------------------------------+
+ 待续 +
+--------------------------------+----------------------------------------------------------------------------------------------
相关文章推荐
- 比较好的C语言实现FFT程序,加入了时间测试。
- [C语言基础]一些C语言小程序(一)
- C语言的一些简单小问题,大家来测试一下吧
- 【Linux】【C语言】菜鸟学习日志(一) 一步一步学习在Linxu下测试程序的运行时间
- Mini 2440 LED、按键和蜂鸣器裸机测试程序(C语言)
- 自动化测试程序之二模拟触摸屏点击事件和滑动事件(C语言)
- C语言的一些程序
- C语言中会常用到的一些小程序
- C语言中关于数字的一些小程序
- 菜鸟学C语言一些经验!-1-关于怎么检查程序是不是运行了
- Linux服务器程序性能测试的一些思考
- C语言测试程序运行时间
- 使用socket的Linux上的C语言helloworld多线程服务器和客户端测试程序
- Reflector .net程序反编译工具的一些插件_sihanjishu的个人空间_51Testing软件测试网 - powered by X-Space
- C语言测试程序运行时间
- 使用SOCKET API写的客户端与服务端测试程序(C语言)
- Linux平台下从零开始写一个C语言访问MySQL的测试程序
- C语言 递归函数的一些测试
- 使用socket的Linux上的C语言helloworld多线程服务器和客户端测试程序
- 使用socket的Linux上的C语言helloworld多线程服务器和客户端测试程序