第12章存储类.连接和内存管理

#include <stdio.h>

#include < stdlib.h>

void main()

{

int x = 30 ;

printf (" x = %d",x);

{

int x = 77;

printf (" x = %d",x);

}

while(x++ < 33)

{

int x = 100;

x++;

printf (" x = %d",x);

}

  printf (" x = %d",x);

system("pause");

}




关于c99 代码块的新规则  


#include <stdio.h>

#include <stdlib.h>

void main()

{


    int  n  =10;

    printf("Initially  n = %d \n",n);

    for (int n = 1; n < 3; n++)

    {

        printf("Loop  n = %d \n",n);

    }

    printf("After loop    n = %d \n",n);

    for (int n = 1; n < 3 ; n ++) {

         printf("Loop  n = %d \n",n);

        int n = 30 ;

         printf("Loop  n = %d \n",n);

        n++;

    }

     printf("Loop  n = %d \n",n);


    system("pause");

}


guany

使用 局部静态变量



#include <stdio.h>

#include <stdlib.h>

void srystat( void );

void main()

{


    int count ;

    for (count = 1 ; count <= 3 ; count++) {

        printf("Here comes iteration %d \n ",count);

        srystat();

    }


    system("pause");

}

void srystat(void )

{

    int fade = 1;

    static int stay = 1;

    printf("fade = %d stay = %d ",fade ++ ,stay++);

}



外部函数  extern

使用外部变量

#include <stdio.h>

#include <stdlib.h>

int units = 0;

void critic(void);

void main()

{

    extern int units;

    printf("how many pound to a firkin of butter \n");

    scanf("%d",&units);

    while (units != 56) {

        critic();

        printf("your must have looked it up \n");


    }


}

void critic()

{

    printf("no luck chummy try again \n");

    scanf("%d",&units);

}


恶习




各种存储类

#include <stdio.h>

#include <stdlib.h>

void report_count ();

void accumulate (int );

int count = 0;

void main()

{

    int value ;

    register int i;

    printf("enter a positive interger 0 to quit ");

    while (scanf("%d",&value) == 1 && value > 0) {

        ++count;

        for(i  = value; i >=0 ; i--) {

            accumulate(i);

           printf("enter a positive interger 0 to quit ");

        }

        report_count();


    }

}

void report_count ()

{

    printf("Loop executed %d time \n ",count);


}



#include <stdio.h>

#include <stdlib.h>

void report_count ();

static int total = 0;

int count = 0;

void accumulate (int k)

{

    static int subtotal = 0;

    if (k <= 0) {

        printf("loop cycle %d \n",count );

        printf("subtotal ; %d  tpta; %d \n ",subtotal , total);

        subtotal =0;

    }

    else

    {

        subtotal += k ;

        total += k;

    };

}

void main()

{

    int value ;

    register int i;

    printf("enter a positive interger 0 to quit ");

    while (scanf("%d",&value) == 1 && value > 0) {

        ++count;

        for(i  = value; i >=0 ; i--) {

            accumulate(i);


        }printf("enter a positive interger 0 to quit ");

        report_count();


    }

}

void report_count ()

{

    printf("loop %d ",count);

}






#include <stdio.h>

#include <stdlib.h>

extern int rand0 (void);

int main()

{

    int count ;

    for (count = 0 ; count < 5 ; count ++) {

        printf("%hd\n");

    }

}


随机数种子


#include <stdio.h>

#include <stdlib.h>

static unsigned long int next = 1;

int rand0 (void);

void srand1 (unsigned int x);

int rand1 (void);

int rand1 (void)

{

    next = next * 1103515245 + 12345;

    return (unsigned int ) (next / 65535 )% 32767;

}

void srand1( unsigned int seed)

{

    next = seed;


}

int main()

{

    int count ;

    unsigned seed;

    printf("please enter your choice for seed \n");

    while ( scanf("%u", &seed ) == 1 ) {

        srand(seed);

        for (count = 0 ; count < 5 ; count ++) {

            printf("%hd\n",rand1());


        }

         printf("please enter your choice for seed \n");

    }

    for (count = 0 ; count < 5 ; count ++) {

        printf("%hd\n");

    }

}





#include <stdio.h>

#include <stdlib.h>

#include "Header.h"

int roll_count= 0;

void main()

{

    rollem(10);

    roll_n_dice(10, 11);

}

static int rollem (int sides)

{

    int roll;

    roll = rand() % sides +1;

    ++roll_count;

    return  roll;


}

int roll_n_dice(int dice,int sides)

{

    int d;

    int total = 0;

    if (sides < 2) {

        printf("Need at least 2 sides .\n");

        return -2;


    }

    if (dice< 1 ) {

        printf("Need at least 1 sides .\n");

        return -1;


    }

    for (d = 0 ; d < dice ; d++) {

        total += rollem(sides)

        ;

    }

    return total;

}




#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include "Header.h"

void main()

{

    int dice , roll;

    int sides;

    srand((unsigned int) time(0)    );

    printf("enter the number of sides per die 0 to stop \n");

    while (scanf("%d",&sides) ==1 && sides > 0  )

        {

        printf("How\n");

        scanf("%d",&dice);

            roll =roll_n_dice(dice, sides);

            printf("you have rolled a %d using %d %d -sided dice .\n",roll,dice,sides);

    }

    printf("the rollem () runction was called %d time ",roll_count);


}




malloc 

#include <stdio.h>

#include <stdlib.h>

void main()

{

    double * ptd;

    int max;

    int number;

    int i = 0;

    puts("what ,is the maxmum number of type double entries \n");

    scanf("%d",&max);

    ptd = (double *)malloc(max * sizeof(double));

    if (ptd == NULL) {

        puts("mem alll file ");

        exit(EXIT_FAILURE);


    }

    puts("enter the values q to quit \n");

    while (i < max && scanf("%lf",&ptd[i])) {//

        ++i;

    }

    printf("here are your %d entries \n ",number = i);


    for (i = 0 ; i < number ; i++) {

        printf("%7.2f",ptd[i]);

        if (i % 7 ==6 ) {

            putchar('\n');


        }


    }

    if (i%7 != 0) {

        putchar('\n');

    }

    puts("done");

    free(ptd);






}



ANSI使用标准io

#include <stdio.h>

#include <stdlib.h>

int  main(int argc ,char *argv[])

{

    int ch ;

    FILE *fp ;

    long count = 0;

    if (argc != 2 ) {

        printf("Usae : %s filename \n",argv[0]);

        exit(1);


    }

    if ((fp = fopen(argv[1], "r")) == NULL) {

        printf("Can't open %s \n ",argv[1]  );

        exit(1);


    }


    while ((ch = getc(fp)) != EOF) {

        putc(ch,stdout);

        count++;


    }

    fclose(fp);

    printf("File %s has %ld characters \n ",argv[1] , count);


    return 0;

}



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容