#include<stdio.h>
int max(int num1, int num2);//函数声明,num1,num2为形参,进入函数时被创建,退出函数时被销毁
//声明,定义同时进行
int max1(int num1, int num2){
int result;
if(num1 > num2)
result = num1;
else
result = num2;
return result;
}
int main(){
int a = 100;
int b = 200;
int ret;
int ret1;
ret = max(a,b);//函数调用
ret1 = max1(a,b);
printf("ret=%d\n",ret);
printf("ret1=%d\n",ret1);
return 0;
}
int max(int num1, int num2){
int result;
if(num1 > num2)
result = num1;
else
result = num2;
return result;
}
ret=200
ret1=200
#include<stdio.h>
void swap(int x, int y){
int temp;
temp = x;
x = y;
y = temp;
}
void main(){
int a = 100;
int b = 200;
printf("交换前a=%d,b=%d\n",a,b);
swap(a,b);
printf("交换后a=%d,b=%d",a,b);
}
交换前a=100,b=200
交换后a=100,b=200
#include<stdio.h>
void swap(int *x, int *y){//*x表示指向x的地址
int temp;
temp = *x; //将x的值赋值给temp
*x = *y; //将y的值赋值给x
*y = temp;
}
int main(){
int a = 100;
int b = 200;
printf("交换前a=%d,b=%d\n",a,b);
swap(&a,&b);//&a表示指向a的指针,即变量a的地址
printf("交换后a=%d,b=%d",a,b);
return 0;
}
交换前a=100,b=200
交换后a=200,b=100
#include<stdio.h>
static void delete_string(char str[],char ch);//内部函数声明
int main(){
extern void enter(char str[]);//对外部函数的声明
extern void print(char str[]);
char c,str[100];//字符和字符串声明
enter(str);
printf("请输入要删除的字符:\n");
scanf("%c",&c);
delete_string(str,c);
print(str);
return 0;
}
static void delete_string(char str[],char ch){//内部函数定义
int i,j;
for(i=j=0;str[i] != '\0';i++)//遍历str,删除目标字符
if(str[i] != ch)
str[j++] = str[i];
str[j] = '\0';//在C语言中,字符串以\0结束
}
#include<stdio.h>
void enter(char str[100]){
printf("请输入字符串:\n");
fgets(str,100,stdin);
}
#include<stdio.h>
void print(char str[]){
printf("删除后的字符串:\n%s",str);
}
d102@d102-W65KJ1-KK1:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ gcc test1.c test2.c test3.c -o test
d102@d102-W65KJ1-KK1:/media/d102/EPAN/Desktop/c++_ubuntu/c++_01/workspace$ ./test
请输入字符串:
asdasdasd
请输入要删除的字符:
a
删除后的字符串:
sdsdsd