C语言的一些测试程序

 

 

 

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

+  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
*/

 

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

+           待续                       +

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

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值