本节包含一个示例程序,提供基本 C 语句的语法示例,后面是一些库函数的列表。这个列表非常简略,只提供了提醒。如果需要更多细节,请参考前面的章节。

/* sample.c: 一个愚蠢的程序,提供语法示例。 */

#include <stdio.h>           /* 包含控制台 I/O 的头文件。 */

int f1( int p );             /* 函数原型。 */
long f2( void );
long g;                      /* 全局变量。 */

void main( int argc, char *argv[] )
{
  float f;                   /* 声明变量。 */
  int ctr;
  extern long g;

  printf( "Arguments:\n\n" );
  for( ctr = 0; ctr < argc; ctr++ )
  {
    puts( argv[ctr] );
  }

  printf( "\nFunction 1:\n\n" );
  ctr = 0;
  while( ctr < 5 ) 
  { 
    printf( "%d\n", f1( ctr++ ) ); 
  }

  printf( "\nFunction 2:\n\n" );
  ctr = 0;
  do 
  { 
    g = ctr++;
    printf( "%d\n", f2() );
  }
  while( ctr < 5 );

  exit( 0 );
}

int f1( int p ) 
{ 
  return( ( p < 3 ) ? p : p p );
}

long f2( void )
{
  extern long g;
  return( g g );
}

控制台 I/O -- #include <stdio.h>:

  • int printf( char *s, <varlist> ) > 0 // 将格式化字符串打印到标准输出。
  • int scanf( char *s, *<varlist> ) != EOF // 从标准输入读取格式化数据。
  • int putchar( int ch ) // 打印一个字符到标准输出。
  • int getchar() != EOF // 从标准输入读取一个字符。
  • int puts( char *s ) // 打印字符串到标准输出,并添加换行符。
  • char *gets() != NULL // 从标准输入读取一行(不包含换行符)。

PC 控制台例程 -- #include <conio.h>:

  • int getch() != 0 // 从键盘获取一个字符(不需要回车)。
  • int getche() != 0 // 从键盘获取一个字符并回显。
  • int kbhit() != 0 // 检查是否按下了一个键。

格式化代码:

  • %h // 短整型(仅 scanf() 使用)

  • %d // 十进制整数

  • %ld // 长整型十进制整数

  • %c // 字符

  • %s // 字符串

  • %e // 指数浮点数

  • %f // 十进制浮点数

  • %g // 使用 %e%f,取其较短的形式(仅 printf() 使用)

  • %u // 无符号十进制整数

  • %o // 无符号八进制整数

  • %x // 无符号十六进制整数

  • %10d // 10个字符的字段宽度。

  • %-10d // 左对齐字段。

  • %6.3f // 6个字符的字段宽度,精确到三位小数。

  • '\0NN' // 八进制字符码。

  • '\xNN' // 十六进制字符码。

  • '\0' // 空字符。

文件 I/O -- #include <stdio.h>:

  • FILE *fopen( char *f, char *mode ) != NULL // 创建或打开文件。

  • int fclose( FILE *f ) // 关闭文件。

  • rewind( FILE *f ) // 文件指针回到文件开始。

  • rename( char *old, char *new ) // 重命名文件。

  • remove( char *name ) // 删除文件。

  • fseek( FILE *f, long offset, int origin) == 0 // 设置文件指针。

  • fprintf( FILE *f, char *fmt, <varlist> ) > 0 // 格式化写入。

  • fscanf( FILE *f, char *fmt, &<varlist> ) != EOF // 格式化读取。

  • fwrite( void *b, size_t s, size_t c, FILE *f ) > 0 // 非格式化写入。

  • fread( void *b, size_t s, size_t c, FILE *f ) > 0 // 非格式化读取。

  • putc( int c, FILE *f ) // 写一个字符。

  • int getc( FILE *f ) != EOF // 读取一个字符。

  • fputs( char *s, FILE *f ) // 写字符串。

  • fgets( char *s, int max, FILE *f) != NULL // 读取字符串。

  • sprintf( char *b, char *fmt, <varlist> ) // 打印到字符串。

  • sscanf( char *b, char *fmt, &<varlist> ) > 0 // 从字符串扫描。

文件模式:

  • r // 打开文件用于读取。
  • w // 打开并清空文件(或创建文件)用于写入。
  • a // 追加模式 — 打开(或创建)文件用于写入到文件末尾。
  • r+ // 打开文件用于读取和写入。
  • w+ // 打开并清空文件(或创建文件)用于读取和写入。
  • a+ // 打开文件用于读取和追加。

偏移量值:

  • SEEK_SET // 文件开始位置。
  • SEEK_CUR // 当前文件位置。
  • SEEK_END // 文件末尾位置。

数学库 -- #include <math.h>:

  • double sin( double x ) // x 的正弦(弧度)。
  • double cos( double x ) // x 的余弦。
  • double tan( double x ) // x 的正切。
  • double asin( double x ) // x 的反正弦。
  • double acos( double x ) // x 的反余弦。
  • double atan( double x ) // x 的反正切。
  • double sinh( double x ) // x 的双曲正弦。
  • double cosh( double x ) // x 的双曲余弦。
  • double tanh( double x ) // x 的双曲正切。
  • double exp( double x ) // 指数函数 — e^x。
  • double log( double x ) // x 的自然对数。
  • double log10( double x ) // x 的十进制对数。
  • double pow( double x, double y ) // 幂函数 — x^y。
  • double sqrt( double x ) // x 的平方根。
  • double ceil( double x ) // 向上取整(返回 double 类型)。
  • double floor( double x ) // 向下取整(返回 double 类型)。
  • double fabs( x ) // x 的绝对值。

标准工具库 -- #include <stdlib.h>:

  • double atof( char *nvalstr ) != 0 // 将数字字符串转换为 double 类型。
  • int atoi( char *nvalstr ) != 0 // 将数字字符串转换为 int 类型。
  • long atol( char *nvlastr ) != 0 // 将数字字符串转换为 long 类型。
  • int rand() // 生成伪随机整数。
  • srand( unsigned seed ) // 设置随机数生成器的种子。
  • exit( int status ) // 退出程序。
  • int system( char *syscmd ) == 0 // 执行系统命令。
  • int abs( int n ) // int 类型的绝对值。
  • long labs( long n ) // long 类型的绝对值。

时间与日期库 -- #include <time.h>:

  • time_t time( time_t *timeptr ) // 当前时间(以 long int 形式表示)。
  • char *ctime( time_t *timeptr ) // 当前时间和日期字符串。

字符串函数库 -- #include <string.h>:

  • int strlen( char *s ) // 字符串长度。
  • strcpy( char *dst, char *src ) // 复制字符串。
  • `strncpy(

char *dst, char *src, size_t n )` // 最多复制 n 个字符。

  • strcat( char *dst, char *s ) // 连接字符串。
  • strncat( char *d, char *s, size_t n ) // 最多连接 n 个字符。
  • strcmp( char *s1, char *s2 ) == 0 // 比较字符串。
  • strncmp( char *s1, char *s2, size_t n ) == 0 // 比较 n 个字符。
  • stricmp( char *s1, char *s2 ) == 0 // 比较字符串,不区分大小写。
  • strnicmp( char *s1, char *s2, size_t n ) == 0 // 比较 n 个字符,不区分大小写。
  • char *strchr( char *s, int ch ) != NULL // 查找第一个字符。
  • char *strrchr( char *s, int ch ) != NULL // 查找最后一个字符。
  • char *strstr( char *dst, char *src) != NULL // 查找子字符串。
  • char *strlwr( char *s ) // 转换为小写。
  • char *strupr( char *s ) // 转换为大写。

字符类测试库 -- #include <ctype.h>:

  • int isalnum( int c ) != 0 // 字母或数字。

  • int isalpha( int c ) != 0 // 字母。

  • int iscntrl( int c ) != 0 // 控制字符。

  • int isdigit( int c ) != 0 // 十进制数字。

  • int isgraph( int c ) != 0 // 可打印字符(不包括空格)。

  • int islower( int c ) != 0 // 小写字母。

  • int isprint( int c ) != 0 // 可打印字符(包括空格)。

  • int ispunct( int c ) != 0 // 打印字符,但不是空格或字母数字。

  • int isspace( int c ) != 0 // 空格,FF,LF,CR,HT,VT。

  • int isupper( int c ) != 0 // 大写字母。

  • int isxdigit( int c ) != 0 // 十六进制数字。

  • int tolower( int c ) // 转换为小写。

  • int toupper( int c ) // 转换为大写。

动态内存分配 -- #include <malloc.h>:

  • buf = (<type> *)malloc( (size_t)sizeof( <type> ) * <array size>) != NULL // 分配内存。
  • free( <type> *buf ) // 释放内存。
最后修改: 2025年01月28日 星期二 00:12