
正文
c语言减的函数名 c语言减减是什么意思
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
C语言中所有的代码及其代表的意思,有谁知道?
这个问题问的太。。。。了,给你找了个操作符(operator)用于操作数据。操作符进行计算、检查等式、进行赋值、操作变量和进行其它更奇怪的工作。C++中有许多操作符,这里不想列出全部,只列出最常用的操作符,如下表所示。表1.2常用C++操作符操作符说明举例
算术运算符
+ 加 x=y+z;
- 减 x=y-z;
* 乘 x=y*z;
/ 除 x=y/z;
赋值运算符
= 赋值 x=10;
+= 赋值与和 x+=10;(等于x=x+10;)
-= 赋值与减 x-=10;
*= 赋值与乘 x*=10;
\= 赋值与除 x\=10;
= 赋值位与 x=0x02;
|= 赋值位或 x|=0x02;
逻辑操作符
逻辑与 if(x 0xFF) {...}
|| 逻辑或 if(x || 0xFF) {...}
等式操作符
== 等于 if(x == 10) {...}
!= 不等于 if(x != 10) {...}
小于 if(x 10) {...}
大于 if(x 10) {...}
= 小于或等于 if(x = 10) {...}
= 大于或等于 if(x = 10) {...}
一元操作符
* 间接操作符 int x=*y;
地址操作符 int* x=y;
~ 位非 x =~0x02;
! 逻辑非 if(!valid) {...}
++ 递增操作符 x++(等于x=x+1;)
-- 递减操作符 x--;
类和结构操作符
:: 范围解析 MyClass :: SomeFunction();
- 间接成员 MyClass- SomeFunction();
· 直接成员 MyClass . SomeFunction();
可以看出,这个清单长了些,没法一下子记住。使用C++时,你会慢慢熟悉这些操作符的。必须指出,递增操作符既可用作前递增(++x),也可用作后递增(x++)。前递增操作符告诉编译器先递增再使用变量,而后递增操作符则让编译器先使用变量值再递增。例如下列代码:
int x = 10;
cout "x = " x++ end1;
cout "x = " x end1;
cout "x = " x end1;
cout "x = " ++x end1;
输出结果如下:
x=10
x=11
x=12
x=12
递减操作符也是这样,这里不想将这些内容讲得太深,但读者可以耐心阅读下去,正如彭兹对奥古斯特所说,“奥古,耐心点,罗马不是一天建成的”。说明 在C++中操作符可以过载(overload)。编程人员可以通过过载标准操作符让它在特定类中进行特定运行。例如,可以在一个类中过载递增操作符,让它将变量递增10而不是递增1。操作符过载是个高级C++技术,本书不准备详细介绍。你也许会发现,有些操作符使用了相同的符号。符号的意义随情境的不同而不同。例如,星号(*)可以作为乘号、声明指针或取消指针引用。这初看起来有点乱,事实上,C++编程老手有时也觉得有点乱。多实践,你会慢慢适应的。本书有许多例子介绍这些操作符。读者不必死记每个操作符的作用,而可以在学习中通过程序和码段去理解其作用。 C++中的函数
函数是与主程序分开的码段。这些码段在程序中需要进行特定动作时调用(执行)。例如,函数可能取两个值并对其进行复杂的数学运算。然后返回结果,函数可能取一个字串进行分析,然后返回分析字串的一部分。新术语 函数(function)是与主程序分开的码段,进行预定的一个服务。函数是各种编程语言的重要部分,C++也不例外。最简单的函数不带参数,返回void(表示不返回任何东西),其它函数可能带一个或几个参数并可能返回一个值。函数名规则与变量名相同。图1.5显示了函数的构成部分。新术语 参数(parameter)是传递给函数的值,用于改变操作或指示操作程度。
返回类型 函数名 参数表
↓ ↓ ↓
int SomeFunction(int x, int y){
函数体→int z = (x * y); return z; ↑返回语句
}
图1.5函数的构成部分使用函数前,要先进行声明。函数声明或原型(prototype)告诉编译器函数所取的参数个数、每个参数的数据类型和函数返回值的数据类型。清单1.4列示了这个概念。新术语 原型(prototype)是函数外观的声明或其定义的说明。
清单1.4Muttiply.cpp
1: #include iostream.h
2: #include conio.h
3: #pragma hdrstop
4:
5: int multiply(int,int)
6: void showResult(int);
7:
8:int main(int argc,char **argv);
9:{
10: int x,y,result;
11: cout end1 "Enter the first value:";
12: cin x;
13: cout "Enter the second value: ";
14: cin y;
15: result=multiply(x,y);
16: showResult(result);
17: cout end1 end1 "Press any key to continue...";
18: getch();
19: return 0
20: }
21:
22: int multiply(int x,int y)
23: {
24:return x * y;
25: }
26:
27: void showResult(int res)
28: {
29:cout "The result is: " res end1;
30: }
这个程序的11到14行用标准输入流cin向用户取两个数字,第15行调用multiply()函数将两个数相乘,第16行调用showResult()函数显示相乘的结果。注意主程序前面第5和第6行multiply()和showResult()函数的原型声明。原型中只列出了返回类型、函数名和函数参数的数据类型。这是函数声明的最基本要求。函数原型中还可以包含用于建档函数功能的变量名。例如,multiply()函数的函数声明可以写成如下:int multiply(int firstNumber,int secondNumber);这里函数multiply()的作用很明显,但代码既可通过说明也可通过代码本身建档。注意清单1.4中函数multiply()的定义(22到25行)在主函数定义码段(8到20行)之外。函数定义中包含实际的函数体。这里的函数体是最基本的,因为函数只是将函数的两个参数相乘并返回结果。清单1.4中函数multiply()可以用多种方法调用,可以传递变量、直接数或其它函数调用的结果:
result = multiply(2,5);//passing literal values
result = multiply(x,y); //passing variables
showResult(multiply(x,y));
//return value used as a
//parameter for another function
multiply(x,y);//return value ignored
注意 最后一例中没有使用返回值。本例中调用函数multiply()而不用返回值没什么道理,但C++编程中经常忽略返回值。有许多函数是先进行特定动作再返回一个数值,表示函数调用的状态。有时返回值与程序无关,可以忽略不计。如果将返回值忽略,则只是放弃这个值,而不会有别的危害。例如,前面的样本程序中忽略了getch()函数的返回值(返回所按键的ASCII值)。函数可以调用其它函数,甚至可以调用自己,这种调用称为递归(recursion)。这在C++编程中是个较复杂的问题,这里先不介绍。新术语 递归(recursion)就是函数调用自己的过程。
相关问答
Q1: 求C语言函数大全
函数名: abort
功 能: 异常终止一个进程
用 法: void abort(void);
程序例:
#include stdio.h
#include stdlib.h
int main(void)
{
printf("Calling abort()\n");
abort();
return 0; /* This is never reached */
}
函数名: abs
功 能: 求整数的绝对值
用 法: int abs(int i);
程序例:
#include stdio.h
#include math.h
int main(void)
{
int number = -1234;
printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}
函数名: absread, abswirte
功 能: 绝对磁盘扇区读、写数据
用 法: int absread(int drive, int nsects, int sectno, void *buffer);
int abswrite(int drive, int nsects, in tsectno, void *buffer);
程序例:
/* absread example */
#include stdio.h
#include conio.h
#include process.h
#include dos.h
int main(void)
{
int i, strt, ch_out, sector;
char buf[512];
printf("Insert a diskette into drive A and press any key\n");
getch();
sector = 0;
if (absread(0, 1, sector, buf) != 0)
{
perror("Disk problem");
exit(1);
}
printf("Read OK\n");
strt = 3;
for (i=0; i80; i++)
{
ch_out = buf[strt+i];
putchar(ch_out);
}
printf("\n");
return(0);
}
函数名: access
功 能: 确定文件的访问权限
用 法: int access(const char *filename, int amode);
程序例:
#include stdio.h
#include io.h
int file_exists(char *filename);
int main(void)
{
printf("Does NOTEXIST.FIL exist: %s\n",
file_exists("NOTEXISTS.FIL") ? "YES" : "NO");
return 0;
}
int file_exists(char *filename)
{
return (access(filename, 0) == 0);
}
函数名: acos
功 能: 反余弦函数
用 法: double acos(double x);
程序例:
#include stdio.h
#include math.h
int main(void)
{
double result;
double x = 0.5;
result = acos(x);
printf("The arc cosine of %lf is %lf\n", x, result);
return 0;
}
函数名: allocmem
功 能: 分配DOS存储段
用 法: int allocmem(unsigned size, unsigned *seg);
程序例:
#include dos.h
#include alloc.h
#include stdio.h
int main(void)
{
unsigned int size, segp;
int stat;
size = 64; /* (64 x 16) = 1024 bytes */
stat = allocmem(size, segp);
if (stat == -1)
printf("Allocated memory at segment: %x\n", segp);
else
printf("Failed: maximum number of paragraphs available is %u\n",
stat);
return 0;
}
函数名: arc
功 能: 画一弧线
用 法: void far arc(int x, int y, int stangle, int endangle, int radius);
程序例:
#include graphics.h
#include stdlib.h
#include stdio.h
#include conio.h
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int stangle = 45, endangle = 135;
int radius = 100;
/* initialize graphics and local variables */
initgraph(gdriver, gmode, "");
/* read result of initialization */
errorcode = graphresult(); /* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw arc */
arc(midx, midy, stangle, endangle, radius);
/* clean up */
getch();
closegraph();
return 0;
}
函数名: asctime
功 能: 转换日期和时间为ASCII码
用 法: char *asctime(const struct tm *tblock);
程序例:
#include stdio.h
#include string.h
#include time.h
int main(void)
{
struct tm t;
char str[80];
/* sample loading of tm structure */
t.tm_sec = 1; /* Seconds */
t.tm_min = 30; /* Minutes */
t.tm_hour = 9; /* Hour */
t.tm_mday = 22; /* Day of the Month */
t.tm_mon = 11; /* Month */
t.tm_year = 56; /* Year - does not include century */
t.tm_wday = 4; /* Day of the week */
t.tm_yday = 0; /* Does not show in asctime */
t.tm_isdst = 0; /* Is Daylight SavTime; does not show in asctime */
/* converts structure to null terminated
string */
strcpy(str, asctime(t));
printf("%s\n", str);
return 0;
}
函数名: asin
功 能: 反正弦函数
用 法: double asin(double x);
程序例:
#include stdio.h
#include math.h
int main(void)
{
double result;
double x = 0.5;
result = asin(x);
printf("The arc sin of %lf is %lf\n", x, result);
return(0);
}
函数名: assert
功 能: 测试一个条件并可能使程序终止
用 法: void assert(int test);
程序例:
#include assert.h
#include stdio.h
#include stdlib.h
struct ITEM {
int key;
int value;
};
/* add item to list, make sure list is not null */
void additem(struct ITEM *itemptr) {
assert(itemptr != NULL);
/* add item to list */
}
int main(void)
{
additem(NULL);
return 0;
}
函数名: atan
功 能: 反正切函数
用 法: double atan(double x);
程序例:
#include stdio.h
#include math.h
int main(void)
{
double result;
double x = 0.5;
result = atan(x);
printf("The arc tangent of %lf is %lf\n", x, result);
return(0);
}
函数名: atan2
功 能: 计算Y/X的反正切值
用 法: double atan2(double y, double x);
程序例:
#include stdio.h
#include math.h
int main(void)
{
double result;
double x = 90.0, y = 45.0;
result = atan2(y, x);
printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);
return 0;
}
函数名: atexit
功 能: 注册终止函数
用 法: int atexit(atexit_t func);
程序例:
#include stdio.h
#include stdlib.h
void exit_fn1(void)
{
printf("Exit function #1 called\n");
}
void exit_fn2(void)
{
printf("Exit function #2 called\n");
}
int main(void)
{
/* post exit function #1 */
atexit(exit_fn1);
/* post exit function #2 */
atexit(exit_fn2);
return 0;
}
函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
程序例:
#include stdlib.h
#include stdio.h
int main(void)
{
float f;
char *str = "12345.67";
f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
}
函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:
#include stdlib.h
#include stdio.h
int main(void)
{
int n;
char *str = "12345.67";
n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
}
函数名: atol
功 能: 把字符串转换成长整型数
用 法: long atol(const char *nptr);
程序例:
#include stdlib.h
#include stdio.h
int main(void)
{
long l;
char *str = "98765432";
l = atol(lstr);
printf("string = %s integer = %ld\n", str, l);
return(0);
}
Q2: 能不能介绍下c语言中各函数的名称和功能?
e=。=比如math.h是计算用的,除了加减乘除啥的,幂pow()啊 啥的都在这里面先include才能使用 绝对值abs() 随机数rand()
string.h是字符串的,strcat strcmp啥的不写了,主要是字符串操作
stdlib.h是动态内存分配 calloc malloc free realloc 分配内存用的
process.h是远程控制函数 终止exit
ctype.h是字符判别函数 isalpha判断是否是字母 islower判断小写 isupper判断大写等
Q3: 利用定时器 按下一个按键后 如何让一个数自动减一 用C语言写?请教 啊
C语言中哪里来的按钮啊,看来你没搞清楚,C基本不设计界面的问题
如果非要用C来做,可以使用time函数,但效率会低一点。如下
#include "time.h"
#include "malloc.h"
int main(int argc, char* argv[])
{
time_t *clock=(time_t*)malloc(sizeof(time_t));
struct tm *mytime=(tm*)malloc(sizeof(tm));
int sec, curTime;
int num = 10; //需要减一的数,为0结束
while( num =0 ){
time(clock);
mytime = localtime(clock);
sec = mytime-tm_sec;
num --;
printf("%d\n",num);
curTime = sec;
while(sec == curTime ){ // 隔1秒,减1,可以根据情况修改。效率在这里低下
//因为是单线程
time(clock);
mytime = localtime(clock);
curTime = mytime-tm_sec;
}
}
return 0;
}
程序运行是每隔1秒输出一个数。
原理是用time取得当前的时间,然后判断当前时间的秒有没有变化
Q4: C语言中的++,--
函数定义的一般形式
1. 无参函数的定义形式
类型标识符 函数名()
{声明部分
语句
}
其中类型标识符和函数名称为函数头。类型标识符指明了本函数的类型,函数的类型实际上是函数返回值的类型。 该类型标识符与前面介绍的各种说明符相同。函数名是由用户定义的标识符,函数名后有一个空括号,其中无参数,但括号不可少。
{}中的内容称为函数体。在函数体中声明部分,是对函数体内部所用到的变量的类型说明。
在很多情况下都不要求无参函数有返回值,此时函数类型符可以写为void。
我们可以改写一个函数定义:
void Hello()
{
printf ("Hello,world \n");
}
这里,只把main改为Hello作为函数名,其余不变。Hello函数是一个无参函数,当被其它函数调用时,输出Hello world字符串。
2. 有参函数定义的一般形式
类型标识符 函数名(形式参数表列)
{声明部分
语句
}
有参函数比无参函数多了一个内容,即形式参数表列。在形参表中给出的参数称为形式参数,它们可以是各种类型的变量,各参数之间用逗号间隔。在进行函数调用时,主调函数将赋予这些形式参数实际的值。形参既然是变量,必须在形参表中给出形参的类型说明。
例如,定义一个函数,用于求两个数中的大数,可写为:
int max(int a, int b)
{
if (ab) return a;
else return b;
}
第一行说明max函数是一个整型函数,其返回的函数值是一个整数。形参为a,b,均为整型量。a,b的具体值是由主调函数在调用时传送过来的。在{}中的函数体内,除形参外没有使用其它变量,因此只有语句而没有声明部分。在max函数体中的return语句是把a(或b)的值作为函数的值返回给主调函数。有返回值函数中至少应有一个return语句。
在C程序中,一个函数的定义可以放在任意位置,既可放在主函数main之前,也可放在main之后。
例如:
可把max 函数置在main之后,也可以把它放在main之前。修改后的程序如下所示。
int max(int a,int b)
{
if(ab)return a;
else return b;
}
main()
{
int max(int a,int b);
int x,y,z;
printf("input two numbers:\n");
scanf("%d%d",x,y);
z=max(x,y);
printf("maxmum=%d",z);
}
现在我们可以从函数定义、函数说明及函数调用的角度来分析整个程序,从中进一步了解函数的各种特点。
程序的第1行至第5行为max函数定义。进入主函数后,因为准备调用max函数,故先对max函数进行说明(程序第8行)。函数定义和函数说明并不是一回事,在后面还要专门讨论。 可以看出函数说明与函数定义中的函数头部分相同,但是末尾要加分号。程序第12 行为调用max函数,并把x, y中的值传送给max的形参a, b。max函数执行的结果(a或b)将返回给变量z。最后由主函数输出z的值。
8.3 函数的参数和函数的值
8.3.1 形式参数和实际参数
前面已经介绍过,函数的参数分为形参和实参两种。在本小节中,进一步介绍形参、实参的特点和两者的关系。形参出现在函数定义中,在整个函数体内都可以使用,离开该函数则不能使用。实参出现在主调函数中,进入被调函数后,实参变量也不能使用。形参和实参的功能是作数据传送。发生函数调用时,主调函数把实参的值传送给被调函数的形参从而实现主调函数向被调函数的数据传送。
函数的形参和实参具有以下特点:
1. 形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只有在函数内部有效。函数调用结束返回主调函数后则不能再使用该形参变量。
2. 实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须具有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使实参获得确定值。
3. 实参和形参在数量上,类型上,顺序上应严格一致,否则会发生类型不匹配”的错误。
4. 函数调用中发生的数据传送是单向的。即只能把实参的值传送给形参,而不能把形参的值反向地传送给实参。 因此在函数调用过程中,形参的值发生改变,而实参中的值不会变化。
可以说明这个问题。
main()
{
int n;
printf("input number\n");
scanf("%d",n);
s(n);
printf("n=%d\n",n);
}
int s(int n)
{
int i;
for(i=n-1;i=1;i--)
n=n+i;
printf("n=%d\n",n);
}
本程序中定义了一个函数s,该函数的功能是求∑ni的值。在主函数中输入n值,并作为实参,在调用时传送给s 函数的形参量n( 注意,本例的形参变量和实参变量的标识符都为n,但这是两个不同的量,各自的作用域不同)。在主函数中用printf 语句输出一次n值,这个n值是实参n的值。在函数s中也用printf 语句输出了一次n值,这个n值是形参最后取得的n值0。从运行情况看,输入n值为100。即实参n的值为100。把此值传给函数s时,形参n的初值也为100,在执行函数过程中,形参n的值变为5050。返回主函数之后,输出实参n的值仍为100。可见实参的值不随形参的变化而变化。
Q5: C语言减法,乘法,除法的程序语言分别是什么
int t=rand()%4; if(t==0) //用加法if(t==1) //用减法if(t==2) //用乘法if(t==3) //用除法
关于c语言减的函数名和c语言减减是什么意思的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






