C复习笔记(4)-6.20

6.20

(1)

#include <stdio.h>

 

/* count digits, white space,others */

int main()

{

    int c,i,nwhite,nother;

    int ndigit[10];

   

    nwhite = nother = 0;

    for(i = 0; i < 10; i++)

    ndigit[i]=0;

 

    while ((c=getchar()) != EOF) {

    if ((c == ' ') || (c == '/t') || (c == '/n'))

        ++nwhite;

    else if ((c >= '0') && (c <= '9'))

        ++ndigit[c - '0'];

    else

        ++nother;

    }

    printf("%d,%d,",nwhite,nother);

    for(i = 0; i < 10; i++)

     printf("%d,",ndigit[i]);

   

    return 0;

}

Notice: here is a smart expression!

if ((c >= '0') && (c <= '9'))

        ++ndigit[c - '0'];

(2)

In C, all function arguments are passed “by value”. The called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy.

 

When necessary, it is possible to arrange for a function to modify a variable in a calling routine, the called function must declare the parameter to be a pointer and access the variable indirectly through it.

 

The story is different for arrays.

 

Automatic variables:

Come and go with function invocation (include main ()), they do not retain their values from one call to the next, and must be explicitly set upon each entry. If they are not set, they will contain garbage.

External variables:

Remain in existence permanently; retain their values even after the functions that set them have returned. It must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it; this states the type of the variable. The declaration may be an explicit extern statement or may be implicit from context.   

“Definition”: refers to the place where the variable is created or assigned storage.

“Declaration”: refers to places where the nature of the variable is stated but no storage is allocated.

 

ok!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值