C语言使用调用函数输出hello,求多种写法.

如题所述

一会把使用指针的方式传上来

源代码如下:

#include <stdio.h>

#include <stdlib.h>

void Prtf1();//第一种函数调用-直接使用printf函数

void Prtf2();//第二种函数调用-字符数组

void Prtf3();//第三种函数调用-直接使用puts函数

void Prtf4();//第四种函数调用-用数组首地址方式访问

void Prtf5();//第五种函数调用-用指针方式访问

void Prtf6();//第六种函数调用-用指针方式访问(指针的定义赋值不同,区别方法五)

char str[5]= "Hello";

int main()

{

Prtf1();

Prtf2();

Prtf3();

Prtf4();

Prtf5();

Prtf6();

return 0;

}

//第一种函数调用-直接使用printf函数

void Prtf1()

{

printf("Hello\n");

}

//第二种函数调用-字符数组

void Prtf2()

{

int i;

for(i=0; i<5; i++)

{

printf("%c",str[i]);

}

printf("\n");

}

//第三种函数调用-直接使用puts函数

void Prtf3()

{

puts("Hello");

}

//第四种函数调用-用数组首地址方式访问

void Prtf4()

{

int i;

for(i=0; i<5; i++)

{

printf("%c",*(str+i));

}

printf("\n");

}

//第五种函数调用-用指针方式访问

void Prtf5()

{

int i;

char *ptr_str;

for(i=0; i<5; i++)

{

ptr_str = &str[0];//把数组首元素地址给指针

printf("%c",*(ptr_str+i));

}

printf("\n");

}

//第六种函数调用-用指针方式访问(指针的定义赋值不同,区别方法五)

void Prtf6()

{

int i;

char *ptr_str;

for(i=0; i<5; i++)

{

ptr_str = str;//把数组名给指针

printf("%c",*(ptr_str+i));

}

printf("\n");

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-11-29
void f1(){printf("hello");}
void f2(char *s){printf("%s",s);}
void f3(char *s){
for(;*s;printf("%c",*s),s++);
}
void f4(char *s){
if(*s){printf("%c",*s);f4(++s);}
}

void main(){
f1();
f2("hello");
f3("hello");
f4("hello");

}
相似回答