有时遇到函数返回值为空和函数返回值为指针的情况下,需要在前面定义该函数
//函数的返回值为指针也必须在此定义,
//函数的返回值为void必须在此定义,返回值是整形或者其他基本类型不需要在此定义
函数功能:把格式化的数据写入某个字符串
函数原型:int sprintf( char *buffer, const char *format [, argument] … );
返回值:字符串长度(strlen)
#include
void main(){
char* who = "I";
char* whom = "优快云";
char s[30];
sprintf(s, "%s love %s.", who, whom); //产生:"I love 优快云. " 这字符串写到s中
printf("%s\n",s);
sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"
printf("%s\n",s);
sprintf(s,"hejianghzou");
printf("%s\n",s);
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char * test3(); //函数的返回值为指针也必须在此定义,
void test2(void); //函数的返回值为void必须在此定义,返回值是整形或者其他基本类型不需要在此定义
void main(void)
{
char *cp;
// const int two = 2;
test2();
cp = test3();
printf(cp);
}
void test2(void)
{
int i = 0;
char *available_resources[] = {"afbhd","gbjaf","dajgb","kcdbha"};
for(i;i<4;i++){
printf("\n ");
printf(available_resources[i]);
printf("\n");
}
}
char *test3()
{
char *tempbuffer ;
char buffer[120] ;
tempbuffer = (char*)malloc(100*sizeof(char));
memcpy(tempbuffer,"That'd be in the data segment ,bob",100);
printf(tempbuffer);
strcpy(buffer,(char*)"Game's over");
return buffer;
}