c语言的库函数

一、标准C库函数(15个)

二、数学函数

  • 用于求幂,计算平方跟,求绝对值。
  • #include <math.h>
  • 绝对值函数用于表达式的结构转换为非负函数

int x=-3;
printf("|%d|=%d\n",x,abs(x));

三、字符串处理函数

四、字符处理函数

五、转换函数和存储管理函数

六、随机函数

产生1~10的数:int i = rand()%10+1;或int n= rand()%11

七、日期和时间处理函数

struct tm{
  int tm_sec;  //   目前秒数,正常范围0~59,但允许至61秒
  int tm_min;  //   目前分数,范围0~59
  int tm_hour; //   午夜算起的时数,范围为0~23
  int tm_mday; //    目前月份的日数,范围01~31
  int tm_mon;  //    目前月份,从一月算起,范围0~11  
  int tm_year; //     从1900年算起至今的年审
  int tm_wday; //     一星期的日数,从星期一算起,范围0~6
  int tm_yday; //     从今年1月1日算起至今天的天数,范围为0~365
	}

/***
*time.h - definitions/declarations for time routines
*
*       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
*       This file has declarations of time routines and defines
*       the structure returned by the localtime and gmtime routines and
*       used by asctime.
*       [ANSI/System V]
*
*       [Public]
*
****/

#if     _MSC_VER > 1000
#pragma once
#endif

#ifndef _INC_TIME
#define _INC_TIME

#if     !defined(_WIN32) && !defined(_MAC)
#error ERROR: Only Mac or Win32 targets supported!
#endif


#ifdef  _MSC_VER
/*
 * Currently, all MS C compilers for Win32 platforms default to 8 byte
 * alignment.
 */
#pragma pack(push,8)
#endif  /* _MSC_VER */

#ifdef  __cplusplus
extern "C" {
#endif



/* Define _CRTIMP */

#ifndef _CRTIMP
#ifdef  _DLL
#define _CRTIMP __declspec(dllimport)
#else   /* ndef _DLL */
#define _CRTIMP
#endif  /* _DLL */
#endif  /* _CRTIMP */


/* Define __cdecl for non-Microsoft compilers */

#if     ( !defined(_MSC_VER) && !defined(__cdecl) )
#define __cdecl
#endif

/* Define _CRTAPI1 (for compatibility with the NT SDK) */

#ifndef _CRTAPI1
#if	_MSC_VER >= 800 && _M_IX86 >= 300
#define _CRTAPI1 __cdecl
#else
#define _CRTAPI1
#endif
#endif

#ifndef _MAC
#ifndef _WCHAR_T_DEFINED
typedef unsigned short wchar_t;
#define _WCHAR_T_DEFINED
#endif
#endif  /* ndef _MAC */

/* Define the implementation defined time type */

#ifndef _TIME_T_DEFINED
typedef long time_t;        /* time value */
#define _TIME_T_DEFINED     /* avoid multiple def's of time_t */
#endif

#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif

#ifndef _SIZE_T_DEFINED
typedef unsigned int size_t;
#define _SIZE_T_DEFINED
#endif


/* Define NULL pointer value */

#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else
#define NULL    ((void *)0)
#endif
#endif


#ifndef _TM_DEFINED
struct tm {
        int tm_sec;     /* seconds after the minute - [0,59] */
        int tm_min;     /* minutes after the hour - [0,59] */
        int tm_hour;    /* hours since midnight - [0,23] */
        int tm_mday;    /* day of the month - [1,31] */
        int tm_mon;     /* months since January - [0,11] */
        int tm_year;    /* years since 1900 */
        int tm_wday;    /* days since Sunday - [0,6] */
        int tm_yday;    /* days since January 1 - [0,365] */
        int tm_isdst;   /* daylight savings time flag */
        };
#define _TM_DEFINED
#endif


/* Clock ticks macro - ANSI version */

#define CLOCKS_PER_SEC  1000


/* Extern declarations for the global variables used by the ctime family of
 * routines.
 */

/* non-zero if daylight savings time is used */
_CRTIMP extern int _daylight;

/* offset for Daylight Saving Time */
_CRTIMP extern long _dstbias;

/* difference in seconds between GMT and local time */
_CRTIMP extern long _timezone;

/* standard/daylight savings time zone names */
_CRTIMP extern char * _tzname[2];


/* Function prototypes */

_CRTIMP char * __cdecl asctime(const struct tm *);
_CRTIMP char * __cdecl ctime(const time_t *);
_CRTIMP clock_t __cdecl clock(void);
_CRTIMP double __cdecl difftime(time_t, time_t);
_CRTIMP struct tm * __cdecl gmtime(const time_t *);
_CRTIMP struct tm * __cdecl localtime(const time_t *);
_CRTIMP time_t __cdecl mktime(struct tm *);
_CRTIMP size_t __cdecl strftime(char *, size_t, const char *,
        const struct tm *);
_CRTIMP char * __cdecl _strdate(char *);
_CRTIMP char * __cdecl _strtime(char *);
_CRTIMP time_t __cdecl time(time_t *);

#ifdef  _POSIX_
_CRTIMP void __cdecl tzset(void);
#else
_CRTIMP void __cdecl _tzset(void);
#endif

/* --------- The following functions are OBSOLETE --------- */
/* The Win32 API GetLocalTime and SetLocalTime should be used instead. */
unsigned __cdecl _getsystime(struct tm *);
unsigned __cdecl _setsystime(struct tm *, unsigned);
/* --------- The preceding functions are OBSOLETE --------- */


#ifndef _SIZE_T_DEFINED
typedef unsigned int size_t;
#define _SIZE_T_DEFINED
#endif

#ifndef _MAC
#ifndef _WTIME_DEFINED

/* wide function prototypes, also declared in wchar.h */
 
_CRTIMP wchar_t * __cdecl _wasctime(const struct tm *);
_CRTIMP wchar_t * __cdecl _wctime(const time_t *);
_CRTIMP size_t __cdecl wcsftime(wchar_t *, size_t, const wchar_t *,
        const struct tm *);
_CRTIMP wchar_t * __cdecl _wstrdate(wchar_t *);
_CRTIMP wchar_t * __cdecl _wstrtime(wchar_t *);

#define _WTIME_DEFINED
#endif
#endif  /* ndef _MAC */


#if     !__STDC__ || defined(_POSIX_)

/* Non-ANSI names for compatibility */

#define CLK_TCK  CLOCKS_PER_SEC

_CRTIMP extern int daylight;
_CRTIMP extern long timezone;
_CRTIMP extern char * tzname[2];

_CRTIMP void __cdecl tzset(void);

#endif  /* __STDC__ */


#ifdef  __cplusplus
}
#endif

#ifdef  _MSC_VER
#pragma pack(pop)
#endif  /* _MSC_VER */

#endif  /* _INC_TIME */
#include <stdio.h>
#include <stdlib.h>
#include "time.h"
int mian()
{
	char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};  // 指针数组
	time_t timep;                             //
	struct tm *p;
	time(&timep);
	printf("%s",ctime(&timep));
	p=gmtime(&timep);
	printf("%s",asctime(p));
	printf("%d-%d-%d",(1990+p->tm_year),(1+p->tm_mon),p->tm_mday);
	printf("%s %d:%d:%d\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
	p=localtime(&timep);
	printf("%d-%d-%d",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
	printf("%s %d:%d:%d\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
	return 0;
}

低级错误,哎呦我的天呐!!!

八、诊断函数

assert()函数是诊断函数,它的作用是测试一个表达式的值是否为false(0),且在条件为false时终止程序,参数的表达式的结果是一个整型数据。assert()函数是在标准函数库<assert.h>头文件中定义

九、命令行参数

  • 标准c语言允许main()有或没有参数列表。
  • 因此主函数main()可以使用一个或多个参数
  • int mian(int argc,char *argv[])

 

十、其他函数

exit()函数表示程序结束,它的返回值将被忽略。它定义在<stdlib.h>文件中

  • qsort()函数包含在<stdlib.h>文件中,此函数根据给出的比较进行快速排序通过指针移动实现排序。排序之后结果任然放在原函数中,使用qsort函数必须自己写一个比较函数。
  • void qsort(void *base,int n,int size,int(*fcmp)(const void *,const void*));
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char stringlist[5][6] = {"boy","girl","man","woman","human"};
int intlist[5]= {3,2,5,1,4};
int sort_stringfun(const void *a,const void *b);
int sort_intfun(const void *a,const void *b); 
/*声明sort_intfun函数,返回值类型为int,不可修改的void *b const为修饰符意为不可修改 */


int main(void)
{
    int x;
    printf("字符串排序:\n");
    qsort((void *)stringlist,5,sizeof(stringlist[0]),sort_stringfun);
    for(x=0;x<5;x++)
        printf("%s\n",stringlist[x]);
    printf("整数排序:\n");
    qsort((void *)intlist,5,sizeof(intlist[0]),sort_intfun);
    for(x=0;x<5;x++)
        printf("%d\n",intlist[x]);



    return 0;
}
int sort_stringfun(const void *a,const void *b)
{
    return(strcmp((const char *)a,(const char *)b));
}
int sort_intfun(const void *a,const void *b)
{
    return *(int *)a-*(int *)b;
}

十一、综合应用:猜字游戏

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#define MAX 9

int main(void) {
    int b=0;
    int n;
    int sum =0;
    char array[100];
    char *p=array;
    int num;
    srand((unsigned)time(NULL));// 随机数播种函数
    num=1+rand()%MAX;   // 设定随机数
    printf("随机数已经准备好,范围1到9\n");
    while(!b) { // 猜不对就一直循环
        sum+=1;
        printf("请输入你猜的数字\n");
        scanf("%s",p);
        if(strlen(p)==1) {  // 返回字符串p的长度不包括ull
            if(isalpha(*p)!=0) {  // isalpha(int c)c为字母时返回一个非零数,否则返回零
                printf("请输入数字,不是字母\n");

            } else if(ispunct(*p)!=0) { // int ispunct(int c) 如果整数c是可打印的(除空格,数字,字母之外)返回非零,否则返回零
                                printf("请输入数字,不是标点符号");
            } else {
                n=atoi(p);
                if(n==num) {
                    b=1;
                    printf("你太厉害了!你共猜中了%d次\n",sum);
                } else if(n<num && n>=0) {
                    printf("你猜小了,继续努力\n");
                } else if(n>num && n<=9) {
                    printf("你猜大了!继续努力!\n");
                }
            }
        } else {
            printf("数字范围是1到9,你输入的数据不对\n");
        }

    }
    return 0;
}


 

  • 20
    点赞
  • 235
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旭日初扬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值