当前位置:首页 » 编程语言 » 解决函数问题用什么语句c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

解决函数问题用什么语句c语言

发布时间: 2023-04-14 06:44:52

A. c语言编程调用函数问题

main函数不能嵌套调用,还有include .c文件可不是个好主意,static变量作用域会不恰当地亮樱扩展倒是file2.c中的其他函数如f(),如果没有声明咐正为static,可以在file1.c中直接调用,不需要include。编译的时候,创建一个.prj文件,内容为file1.cfile2.c在TC的菜单敬简丛中有project的项,设为.prj文件名,最后编译就可以了

B. C语言函数问题求解。

int a=fx;应该神腊辩是局茄int a=fx();
-----------
# include<stdio.h>游缺
int main()
{
int fx();
int a=fx();
printf("%d\n",a);
}

int fx()
{
int a=5;
return a;
}

C. c语言函数调用问题(一个语句)

(a++,b++,a+b)。这个语句的意思其实就是顺序执行a++,b++,a+b,然后将最终的值作为第一个参数传递亩戚个fun.所以经蠢盯过a++,b++,a+b,可算出,最终a= 2,b=3,a+ b =5,因此(a++,b++,a+b) = 5.

c++,即是先将c的值传个fun,然后自加带耐和。因此fun((a++,b++,a+b),c++);即为fun(5,3),所以Sum = 8

D. C语言如何调用函数

C语言中,函数调用的一般形式为:

函数名(实际参数表)

对无参函数调用时则无实际参数表。实际参数表中的参数可以是常数、变量或其它构造类型数据及表达式。各实参之间用逗号分隔。

#include<stdio.h>
intfun(intx,inty);//函数声明,如果函数写在被调用处之前,可以不用声明
voidmain()
{
inta=1,b=2,c;
c=fun(a,b);//函数的调用,调用自定义函数fun,其中a,b为实际参数,传递给被调用函数的输入值
}
//自定义函数fun
intfun(intx,inty)//函数首部
{//{}中的语言为函数体
returnx>y?x:y;//返回x和y中较大的一个数
}

(4)解决函数问题用什么语句c语言扩展阅读

C语言中不允许作嵌套的函数定义。因此各函数之间是平行的,不存在上一级函数和下一级函数的问题。但是C语言允许在一个函数的定义中出现对另一个函数的调用。

这样就出现了函数的嵌套调用。即在被调函数中又调用其它函数。这与其它语言的子程序嵌套的情形是类似的。其关系可表示如图。

图表示了两层嵌套的情形。其执行过程是:执行main函数中调用a函数的语句时,即转去执行a函数,在a函数中调用b 函数时,又转去执行b函数,b函数执行完毕返回a函数的断点继续执行,a函数执行完毕返回main函数的断点继续执行。

E. 帮忙用C语言解决下随机函数的编程题

完整版纯C程序:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 20
void sel_sort(int [],int); //选择排序
int search(int [],int,int); //查找
void insert(int [],int [],int,int); //插入
int num_by_mechine(void) //产生随机数
main()
{
int a[N],x;
int i,index,b[N+1];
srand(time(NULL)); //为随机数发生器rand()播种好橘念
for(i=0;i<N;i++)
a[i]=num_by_mechine();
printf("before sel_sort...\n");
for(i=0;i<N;i++)
printf("%4d",a[i]);
printf("\n");
sel_sort(a,N);
printf("after sel_sort...\n");
for(i=0;i<N;i++)
printf("友困%4d",a[i]);
printf("\n");
printf("please input x:");
scanf("%d",&x);
index=search(a,x,N);
if(index!=-1)
{
printf("find x,x=a[%d]!\n",index);
printf("delete a[%d]...\n",index);
for(i=index+1;i<伍芦N;i++)
a[i-1]=a[i];
for(i=0;i<N-1;i++)
printf("%4d",a[i]);
}
else
{
printf("no find!\n");
printf("after insert...\n");
insert(a,b,x,N);
for(i=0;i<N+1;i++)
printf("%4d",b[i]);
}
}
int num_by_mechine(void)
{
return(rand()%100+1); //产生一个分布在1~100之间的随机整数
}
void sel_sort(int a[],int n)
{
int cur,i,t;
for(cur=0;cur<n-1;cur++)
for(i=cur;i<n;i++)
if(a[i]<a[cur])
t=a[cur],a[cur]=a[i],a[i]=t;
}
int search(int a[],int x,int n)
{
int front=0,back=n-1,middle;
while(front<=back)
{
middle=(front+back)/2;
if(x<a[middle])
back=middle-1;
else if(x>a[middle])
front=middle+1;
else
return(middle);
}
return -1;
}
void insert(int a[],int b[],int x,int n)
{
int i,j;
for(i=0;i<n;i++)
b[i]=a[i];
for(i=0;i<n;i++)
if(a[i]>x)
{ for(j=n;j>i;j--)
b[j]=b[j-1];
b[i]=x;
return;
}
b[n]=x;
}

F. 有关C语言上的函数问题想求助一下大侠, 我新手

int max(int x, int y); //是一个函数声明,函数在调用之前必须声明。括号里边是形参
c=max(a, b); //这句语句中的括号里面是实参,当函数被调饥余档用时,程序进烂乱入调用函数
中,将实参的值赋给调用函数的形参, 参毁兄与运算。最后返回运算结果给c。

G. C语言求一元二次方程解的函数问题 新手题

  • #include"stdio.h"

voidhanshu(inta,intb,intc){

intx,y,z;

z=b*b-4*a*c;

if(a==0)

printf("该方程不是一元二次方程");

elseif(a!=0&&z<0) //这里不需要在设置a!=0这个条件

困轿printf("该一元二次方程无实数解");

elseif(a=!0&&z=0) //同样不需要设置a!=0这个条件

{ //如果一个分支中有多条语句,一定要用{}


printf("该一元二次方程有两个相同的实数解");

x=(z-b)/2a;

y=x;

}


else

printf("该一元二次方程有两个不同的实数解");

x=(z-b)/2a;

y=(b*(-1)+z*(-1))/2a;

returnx,y; //return只能返回一个值,这个语句的结果是返回y的值,具体请看逗号表达式

} //缺少}


voidmain()

{

inta,b,c;


scanf("%d%d%d",&a,&b,&c); //在使用变量之前要先定义

printf("%d%d",hanshu(x),hanshu(y)); //你定义的hanshu(inta,intb,intc)有三个int类型的参数,()中放置的是向函数传递的参数!!!

}


  • 你可以在函数hanshu()中直接打印出结果而不是返回出来,因为一个函哗如数只能返回一个值!

如下:

#include<stdio.h>

voidhanshu(dobulea,doubleb,doublec)

{

doublex,y,z;

z=b*b-4*a*c;

if(a==0)

printf("该方程不是一元二次方程");

elseif(z<0)

printf("该一元二次方程无实数解");

elseif(z=0)

{

x=(z-b)/2a;

y=x;

printf("该一元二次方程有两个相同的实数解:%lf%lf",x,y);

}

else

{

x=(z-b)/2a;

y=(b*(-1)+z*(-1))/2a;

printf("该一元二次方程有两个不同的实数解:%lf%lf",x,y);

}

}

intmain(void)

{

doublea,b,c;

scanf("%lf%lf%lf",&a,&b,&c);

hanshu(a,b,c);

return0;

汪芦肆}


  • 如果你想在主函数main()中使用计算结果,可以利用指针!

H. C语言:用分支结构或者循环结构解决一个实际问题,并通过函数调用实

5楼是正确的。第一题:A.正确就不说了。B.main函数一般含有参数,且是实参,这部分见函数的结构,B错。C.见函数调用之递归调用是一种特殊的嵌套调用,是某个函数调用自己,而不是另外一个函数,C对。D.不解释。E.函数参数传递里面,上述带回多个返回值,是由形参带回来的,而形参得传递地址。另外,实参是传递数值,E对。故选ACDE。第慧尺二题:ACD。B.形式:do{循环体语句组;}while(循环继续条件);。故而前睁高B错。E。见实现S=1+2+······+n的例子,这个是循环次数不确定的。故而E错。其他3个是常见知早粗识点。故选ACD。

I. c语言函数问题

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

void printfMessage (void)

{

printf("Programming is fun. ");

}

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

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

  3. 函数名 its name

  4. 参数列表 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

J. C语言函数问题

先举个例子:
#include <stdio.h>
int max(int,int); //这是函数max的申明,最后要加分号
int main()
{
int a=1,b=2,c;
c = max(a,b); //这是函数max的使用
printf("%d",c);
return 0;
}
int max(int n,int m) //这是函数max的定义部分,最后不加分号
{
if (n>m)
return n;
else
return m;
}
不能在函数体内定义函数是什么意思?
【就是说函数在程序中不能嵌套定义,这个和Pascal语言不同,例如上面程序中,max函数不能定义在main函数中间】
定义是声明还是使用?
【定义既不是申明,也不是使用,函数具体功能的实现代码叫做函数的定义,如上差前面程搜歼序最后7行就是定义】
函数不是应该先声明在使用吗?
【是的,但如果函数定义在函数使用之前,就可以不用申明(如下面程序)。函数定义在函数使用后的情况下(如上面程序),先声明,能使编译器在编译在编译过程中能够识虚漏清别使用的函数】
#include <stdio.h>
int max(int n,int m) //这是函数max的定义部分,最后不加分号
{
if (n>m)
return n;
else
return m;
}
int main()
{
int a=1,b=2,c;
c = max(a,b); //这是函数max的使用
printf("%d",c);
return 0;
}
定义如何解释?
【见第二个问题】