资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

c语言函数提问 c语言程序中函数的典型题

c语言函数的问题

#include stdio.h // 导入标准输入输出头文件stdio.h 

创新互联建站,专注为中小企业提供官网建设、营销型网站制作、成都响应式网站建设、展示型成都做网站、网站设计、外贸营销网站建设等服务,帮助中小企业通过网站体现价值、有效益。帮助企业快速建站、解决网站建设与网站营销推广问题。

// standard input/ouput header file

int factorial(int n)// 定义函数名称factorial,该函数输入一个int参数,返回一个int参数

{

int fac = 1; // 定义一个int类型变量fac,赋值为1

for(int i = 1; i=n; i++) // 从i等于1开始,到n值结束,每次循环结束后i增加1

fac = i * fac;  // 将i与fac值乘,赋值给fac (for内循环)

return fac; // 返回fac值

}

int main() // 定义函数名称main(主接口函数),该函数输入参数不定,返回一个int参数

{

int sum = 0; // 定义一个int类型变量sum,赋值为0

for(int i = 1; i=10; i++) // 从i等于1开始,到10值结束,每次循环结束后i增加1

sum = sum + factorial(i); // 将sum与函数调用factorial(i)值乘,赋值给sum (for内循环)

printf("%d", sum); // 向控制台打印sum的值

return 0; // main函数返回0值,程序结束

}

总结:

factorial函数求了阶乘

main 函数输出了1到10阶乘的和

C语言函数问题

1.一个变量的(定义)指明该变量可以使用的程序区域。

2.在所有函数之外说明的变量成为(全局变量)。

3.要使一个局部变量在两次函数调用中保持其值,必须说明成是(static 静态)存储类型的。

4.直接或间接调用自己的函数称为(递归)函数。

5.函数形式参数的作用域是(函数内部)。

c语言函数问题

一个函数的的声明,要给出 四个信息,举个例子

void printfMessage (void)

{

printf("Programming is fun.\n");

}

谁可以调用它  who call call it    (注意 static  的应用)

返回值的类型    The type of value it returns

函数名 its name

参数列表  the arguments it takes

很显然  ,你说的例子中,get作为函数名

不同的是它们的返回类型,一个返回Int , 一个返回int.前者是一个指向int 类的指针,后者是整型

下面截至  k r

here is the function power and a main program to excuter it , so you can see the whole structure at once.

A function definition has this form:   函数定义具有如下格式

return-type function-name(parameter declarations, if any)

{

declarations

statements

}

返回类型 函数名(参数声明,...)

{

声明

语句

}

Returning Pointers

Although functions tht return pointers are handled just like any other type of function, it is review some key concepts and look at an example. Pointers are neither integers nor unsigned integers, They are the memory address of a certain type of data. One reason for this distinction is that pointer arithmetic is relative to the base type. For example, if an integer

pointer is incremented , it will contain a value that is four greater than its previous value

(assuming 4-byte integers), In general, each time a pointer is incremented(or decremented),

it points to the next(of previous) item of its type. Since the length of different data types may differ, the compiler must know what type of data the pointer is pointing to. For this reason that returns a pointer must declare explicitly what type of pointer it is returning. For example, you should not use a return type of int * to return a char * pointer! In a few case, a function will need to return a generic pointer. In this case, the function return type must be specified as void *.

To return a ponter, a function must be declared as having a pointer return type. For example, the following function returns a pointer to the first occurrence of the character c in string s; If no match is found, a pointer to the null terminator is returned.

/* Rerurn pointer of fisrt occurrence of c in s. */

char *match(char c, char *s)

{

while (c != *s, *s) s++;

return (s);

}

Here is a short program that uses match();

#include stdio.h

char *match(char c, char *s);  /* prototype */

int main(void)

{

char s[80], *p, ch;

gets(s);

ch = getchar();

p = match(ch, s);

if (*p) /* there is a match */

printf(" %s ", p);

else

printf("No match found.");

return 0;

}

简单的翻译一下:

虽然函数返回一个指针和函数返回其他类型的值并没有什么区别,但是通过它回顾一些关键概念还是很有用处的。指针无非就是整型和无符号整型,它们是某些数据类型的内存地址,指针的运算涉及到它的基本类型,比如说,一个指向整型的指针增加1.这个指针变量的的值将会比他的前一个值大4, 一般来讲,指针每加1或者减1,它将指向下一项(这种类型)。由于不同数据类型有着不同的长度,编译器必须知道指针指向了哪种类型,为了解决这个问题,如果一个函数返回一个指针,它必须明确的给出它将返回哪种类型的指针。 比如说,你不能使用 int * 来返回char *. 在某些情况下,一个函数需要返回一个泛型(generic pointer), 在这种情况下,函数必须声明为 void *.

为了能够返回一个指针,函数必须明确的指出,它将返回哪种指针类型。举个例子,下列函数返回一个指针,指向字符串S中第一次出现的c,如果不能匹配上,返回一个指向NULL的指针

reference:

[1] Brian W. Kernighan Dennis M. Ritchie the c programming language

[2] Stephen G.Kochan Pogramming in C Third Edition

[3] Herbert Schildt  C The Complete Reference Fourth Edition

c语言的函数问题?

两个表达式分别用两个递归函数来实现。

所以,题目中两个表达式的描述,就是告诉你不同情况下的函数返回值。

之后根据题意,主函数通过函数指针调用,函数指针就是指向函数的指针。把两个函数分别赋值给指针,再通过指针调用函数。

下面是代码:

#include stdio.h

int fn1(int n);

int fn2(int n);

int main()

{

int k=3,n=-1,(*p)(int n);

while(n0)

  printf("请输入n的值(大于等于0):"),scanf("%d",n);

while(k!=1 k!=2)

  printf("请选择表达式(输入1或者2):"),scanf("%d",k);

switch(k)

{

  case 1:p=fn1;break;

  case 2:p=fn2;break;

}

printf("通过表示式%d计算得第N项值为:%d\n",k,p(n));

return 0;

}

int fn1(int n)

{

if(n==0) return 1;

if(n==1) return 2;

return 2*fn1(n-1)+fn1(n-2);

}

int fn2(int n)

{

if(n==0) return 0;

if(n==1) return 1;

return fn2(n-1)+2*fn2(n-2);

}


新闻名称:c语言函数提问 c语言程序中函数的典型题
转载注明:http://cdkjz.cn/article/dogjjpj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220