首页 > 编程语言 > 详细

C语言程序设计-英文版例题0308

时间:2021-03-08 19:51:04      阅读:22      评论:0      收藏:0      [点我收藏+]

1,    Chapter1 :function

#include <stdio.h>
/*int power(int m, int n);
/*test power function,version1*/
//main()
//{
// int i;
// for (i = 0; i < 10; ++i)
// printf("%d %d %d\n", i, power(2, i), power(-3, i));
// return 0;
//}
///*power: raise base to n-th power; n>=0*/
//int power(int c , int d)
//{
// int i, p;
// p = 1;
// for (i = 1; i <= d; ++i)
// p = p * c;
// return p;
//}
/*version2*/
/*int power(); /*可以先不用定义power函数的参数*/
/*test power function,version1*/
//main()
//{
// int i;
// for (i = 0; i < 10; ++i)
// printf("%d %d %d\n", i, power(2, i), power(-3, i));
// return 0;
//}
//power(base, n)
//int base, n;
//{
// int i, p;
// p = 1;
// for (i = 1; i <= n; ++i)
// p = p * base;
// return p;
//}

 

------------------------------------------------------------------

2, external variable and scope

全局变量可以应用到所有函数中

#include<stdio.h>
#define MAXLINE 1000 /*maximum input line length*/
//int max; /*maximum length seen so far*/
//char line[MAXLINE]; /*current input line*/
//char longest[MAXLINE];/*longest line saved here*/
//int getline(void);
//void copy(void);
//
///*print the longest input line */
//main()
//{
// int len;
// extern int max;
// extern char longest[];
//
// max = 0;
// while ((len = getline()) > 0)
// /*printf("%s", line);
// printf("%d", len);*/
//
// if (len > max) {
// max = len;
// copy();
// }
// if (max > 0) /*there was a line, s is string*/
// printf("%s", longest);
// return 0;
//}
//
///*getline: read a line into s,return length*/
//int getline(void)
//{
// int c, i;
// extern char line[];
//
//
// for (i = 0; i < MAXLINE - 1 && (c = getchar()) != EOF && c != ‘\n‘; ++i)
// line[i] = c;
// if (c == ‘\n‘) {
// line[i] = c;
// ++i;
// }
// line[i] = ‘\0‘;
// return i;
//}
///*copy: copy ‘from‘ into ‘to‘; assume to is big enough*/
//void copy(void)
//{
// int i;
// extern char line[], longest[];
//
// i = 0;
// while ((longest[i] = line[i]) != ‘\0‘)
// ++i;
//}

----------------------------------------------------

局部变量版本

#include<stdio.h>
#define MAXLINE 1000 /*maximum input line length*/
//int getline(char line[], int maxline);
//void copy(char to[], char from[]);
//
///*print the longest input line */
//main()
//{
// int len; /*current line length*/
// int max; /*maximum length seen so far*/
// char line[MAXLINE]; /*current input line*/
// char longest[MAXLINE];/*longest line saved here*/
//
// max = 0;
// while((len=getline(line,MAXLINE))>0)
// /*printf("%s", line);
// printf("%d", len);*/
//
// if (len > max) {
// max = len;
// copy(longest, line);
// }
// if (max > 0) /*there was a line, s is string*/
// printf("%s", longest);
// printf("%d",max);
// return 0;
//}
//
///*getline: read a line into s,return length*/
//int getline(char s[], int lim)
//{
// int c, i;
//
// for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != ‘\n‘; ++i)
// s[i] = c;
// if (c == ‘\n‘) {
// s[i] = c;
// ++i;
// }
// s[i] = ‘\0‘;
// return i;
//}
///*copy: copy ‘from‘ into ‘to‘; assume to is big enough*/
//void copy(char to[], char from[])
//{
// int i;
//
// i = 0;
// while ((to[i] = from[i]) != ‘\0‘)
// ++i;
//}

C语言程序设计-英文版例题0308

原文:https://www.cnblogs.com/zhoujia1/p/14501285.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!