您的位置:首页 > 编程语言 > C语言/C++

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

*/

+--------------------------------+

+ 待续 +

+--------------------------------+----------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: