c语言库函数具体实现 c语言函数库的主要功能是什么( 二 )


gets(a);
return str_to_int(a);
}
void gets_with_getchar(char *str)
{
int i = 0;
if(str == NULL) return;
do
{
str[i] = getchar();
}while(str[i++] != '\n' );
}
char *do_strchr(char *src, const char c)
{
char *p = src;
while(*p*p!= c) p ++;
if(*p != c) return NULL;
return p;
}
int do_isdigit(char c)
{
return c = '9'c = '0';
}
int do_isalpha(char c)
{
return (c = 'Z'c = 'A') || (c = 'z'c = 'a');
}
int main()
{
int a;
char test[100] ;
a = get_int_with_getchar();
printf("get_int_with_getchar = %d\n", a);
a = get_int_with_gets();
printf("get_int_with_gets = %d\n", a);
gets_with_getchar(test);
printf("gets_with_getchar = %s\n", test);
printf("do_strchr %s %s\n", do_strchr(test, 'a'), do_strchr(test, 'b'));
printf("do_isdigit = (%d,%d) \n", do_isdigit('1'), do_isdigit('a'));
printf("do_isalpha = (%d,%d) \n", do_isalpha('1'), do_isalpha('a'));
}
c语言中的库函数是如何使用的,最好有例子其实在C语言编程中,我们所用的在部分函数就是C语言库本身带的函数,在使用某一个库文件之前,我们先要包含库文件所对应的头文件,再在我们需要的地方调用库函数就行了.最常用的printf();这就是一个库函数,这个库函数在头文件stdio.h中声明.所以使用前要先#include stdio.h
举个例子:
#include stdio.h//一定要先包含库函数声明的文件
int main()
{
printf("for example!\n");//在此处调用库函数
}
如果想要了解更多的库函数,可以参考C语言的教材,一般的附录中会列出.也可以查看C库函数.
C语言库函数如何编写?/***
*printf.c - print formatted
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines printf() - print formatted data
*
*******************************************************************************/
#include
#include
#include
#include
#include
#include
#include
/***
*int printf(format, ...) - print formatted data
*
*Purpose:
* Prints formatted data on stdout using the format string to
* format data and getting as many arguments as called for
* Uses temporary buffering to improve efficiency.
* _output does the real work here
*
*Entry:
* char *format - format string to control data format/number of arguments
* followed by list of arguments, number and type controlled by
* format string
*
*Exit:
* returns number of characters printed
*
*Exceptions:
*
*******************************************************************************/
int __cdecl printf (
const char *format,
...
)
/*
* stdout ''PRINT'', ''F''ormatted
*/
{
va_list arglist;
int buffing;
int retval;
va_start(arglist, format);
_ASSERTE(format != NULL);//断言宏 。如果输出格式字符串指针为空,则在DEBUG版下断言,报告错误 。
_lock_str2(1, stdout);
buffing = _stbuf(stdout);//stdout:指定输出到屏幕
retval = _output(stdout,format,arglist);
_ftbuf(buffing, stdout);
_unlock_str2(1, stdout);
return(retval);
}
以上为printf()的源代码
1、从含有可选参数函数中获得可选参数,以及操作这些参数
typedef char *va_list;
void va_start( va_list arg_ptr, prev_param );
type va_arg( va_list arg_ptr, type );
void va_end( va_list arg_ptr );
假定函数含有一个必选参数和多个可选参数,必选参数声明为普通数据类型,且能通过参数名来获得该变量的值 。可选参数通过宏va_start、va_arg和va_end(定义在stdarg.h或varargs.h中)来进行操作,即通过设置指向第一个可选参数指针、返回当前参数、在返回参数后重新设置指针来操作所有的可选参数 。

推荐阅读