Tải bản đầy đủ (.pdf) (274 trang)

C standard libraries

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.06 MB, 274 trang )


About the Tutorial
C is a general-purpose, procedural, imperative computer programming language
developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to
develop the Unix operating system.
C is the most widely used computer language that keeps fluctuating at number
one scale of popularity along with Java programming language which is also
equally popular and most widely used among modern software programmers.
The C Standard Library is a set of C built-in functions, constants and header files
like <assert.h>, <ctype.h>, etc. This library will work as a reference manual for C
programmers.

Audience
The C Standard Library is a reference for C programmers to help them in their
projects related to system programming. All the C functions have been explained
in a user-friendly way and they can be copied and pasted in your C projects.

Prerequisites
A basic understanding of the C Programming language will help you in
understanding the C built-in functions covered in this library.

Copyright & Disclaimer
 Copyright 2014 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of
Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain,
copy, distribute or republish any contents or a part of contents of this e-book in
any manner without written consent of the publisher.
We strive to update the contents of our website and tutorials as timely and as
precisely as possible, however, the contents may contain inaccuracies or errors.
Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy,
timeliness or completeness of our website or its contents including this tutorial.


If you discover any errors on our website or in this tutorial, please notify us at


i


Table of Contents
The C Standard Library Reference .............................................................................................................. i
Audience .................................................................................................................................................... i
Prerequisites .............................................................................................................................................. i
Copyright & Disclaimer .............................................................................................................................. i
Table of Contents ...................................................................................................................................... ii

1. <ASSERT.H> ............................................................................................................................. 1
Introduction .............................................................................................................................................. 1
Library Macros .......................................................................................................................................... 1
void assert(int expression) ......................................................................................................................... 1

2. <CTYPE.H>............................................................................................................................... 3
Introduction .............................................................................................................................................. 3
Library Functions ...................................................................................................................................... 3
int isalnum(int c) ........................................................................................................................................ 4
int isalpha(int c).......................................................................................................................................... 6
int iscntrl(int c) ........................................................................................................................................... 8
int isdigit(int c) ........................................................................................................................................... 9
int isgraph(int c) ....................................................................................................................................... 11
int islower(int c) ....................................................................................................................................... 12
int isprint(int c) ......................................................................................................................................... 14
int ispunct(int c) ....................................................................................................................................... 16
int isspace(int c) ....................................................................................................................................... 18

int isupper(int c) ....................................................................................................................................... 20
int isxdigit(int c) ........................................................................................................................................ 21
int tolower(int c) ...................................................................................................................................... 23
int toupper(int c) ...................................................................................................................................... 24
Character Classes .................................................................................................................................... 25

3. <ERRNO.H> ........................................................................................................................... 27
Introduction ............................................................................................................................................ 27
Library Macros ........................................................................................................................................ 27
extern int errno ........................................................................................................................................ 28
EDOM Domain Error ................................................................................................................................ 29
ERANGE Range Error ................................................................................................................................ 30

4. <FLOAT.H> ............................................................................................................................ 33
ii


Introduction ............................................................................................................................................ 33
Library Macros ........................................................................................................................................ 33

5. <LIMITS.H> ............................................................................................................................ 36
Introduction ............................................................................................................................................ 36
Library Macros ........................................................................................................................................ 36

6. <LOCALE.H> .......................................................................................................................... 39
Introduction ............................................................................................................................................ 39
Library Macros ........................................................................................................................................ 39
Library Functions .................................................................................................................................... 39
char *setlocale(int category, const char *locale) ..................................................................................... 40
struct lconv *localeconv(void) ................................................................................................................. 41

Library Structure ..................................................................................................................................... 44

7. <MATH.H>............................................................................................................................. 48
Introduction ............................................................................................................................................ 48
Library Macros ........................................................................................................................................ 48
Library Functions .................................................................................................................................... 48
double acos(double x) .............................................................................................................................. 50
double asin(double x) ............................................................................................................................... 51
double atan(double x) .............................................................................................................................. 52
double atan2(doubly y, double x) ............................................................................................................ 53
double cos(double x) ................................................................................................................................ 54
double cosh(double x) .............................................................................................................................. 56
double sin(double x) ................................................................................................................................ 57
double sinh(double x) .............................................................................................................................. 58
double tanh(double x) .............................................................................................................................. 59
double exp(double x) ............................................................................................................................... 59
double frexp(double x, int *exponent) .................................................................................................... 60
double ldexp(double x, int exponent) ...................................................................................................... 62
double log(double x) ................................................................................................................................ 63
double log10(double x) ............................................................................................................................ 63
double modf(double x, double *integer) ................................................................................................. 64
double pow(double x, double y) .............................................................................................................. 66
double sqrt(double x) ............................................................................................................................... 66
double ceil(double x) ................................................................................................................................ 67
double fabs(double x) .............................................................................................................................. 68
double floor(double x) ............................................................................................................................. 69
double fmod(double x, double y) ............................................................................................................. 71

iii



8. <SETJMP.H> .......................................................................................................................... 73
Introduction ............................................................................................................................................ 73
Library Variables ..................................................................................................................................... 73
Library Macros ........................................................................................................................................ 73
int setjmp(jmp_buf environment) ........................................................................................................... 73
Library Functions .................................................................................................................................... 75
void longjmp(jmp_buf environment, int value) ....................................................................................... 75

9. <SIGNAL.H> ........................................................................................................................... 77
Introduction ............................................................................................................................................ 77
Library Variables ..................................................................................................................................... 77
Library Macros ........................................................................................................................................ 77
Library Functions .................................................................................................................................... 78
void (*signal(int sig, void (*func)(int)))(int) ............................................................................................. 78
int raise(int sig)......................................................................................................................................... 81

10. <STDARG.H>........................................................................................................................ 84
Introduction ............................................................................................................................................ 84
Library Variables ..................................................................................................................................... 84
Library Macros ........................................................................................................................................ 84
void va_start(va_list ap, last_arg) ............................................................................................................ 85
type va_arg(va_list ap, type) .................................................................................................................... 86
void va_end(va_list ap) ............................................................................................................................ 88

11. <STDDEF.H> ........................................................................................................................ 90
Introduction ............................................................................................................................................ 90
Library Variables ..................................................................................................................................... 90
Library Macros ........................................................................................................................................ 90
NULL ......................................................................................................................................................... 91

offsetof(type, member-designator) ......................................................................................................... 92

12. <STDIO.H> ........................................................................................................................... 95
Introduction ............................................................................................................................................ 95
Library Variables ..................................................................................................................................... 95

iv


Library Macros ........................................................................................................................................ 95
Library Functions .................................................................................................................................... 96
int fclose(FILE *stream) .......................................................................................................................... 101
void clearerr(FILE *stream) .................................................................................................................... 102
int feof(FILE *stream) ............................................................................................................................. 103
int ferror(FILE *stream) .......................................................................................................................... 104
int fflush(FILE *stream) .......................................................................................................................... 106
int fgetpos(FILE *stream, fpos_t *pos) .................................................................................................. 107
FILE *fopen(const char *filename, const char *mode) .......................................................................... 109
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) .......................................................... 111
FILE *freopen(const char *filename, const char *mode, FILE *stream) ................................................ 113
int fseek(FILE *stream, long int offset, int whence) ............................................................................... 115
int fsetpos(FILE *stream, const fpos_t *pos) ......................................................................................... 117
long int ftell(FILE *stream) ..................................................................................................................... 119
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) ................................................ 120
int remove(const char *filename) .......................................................................................................... 122
int rename(const char *old_filename, const char *new_filename) ...................................................... 123
void rewind(FILE *stream) ..................................................................................................................... 125
void setbuf(FILE *stream, char *buffer) ................................................................................................. 126
int setvbuf(FILE *stream, char *buffer, int mode, size_t size) ............................................................... 127
FILE *tmpfile(void) ................................................................................................................................. 129

char *tmpnam(char *str) ....................................................................................................................... 130
int fprintf(FILE *stream, const char *format, ...) .................................................................................... 131
int printf(const char *format, ...) ........................................................................................................... 136
int sprintf(char *str, const char *format, ...) .......................................................................................... 140
int vfprintf(FILE *stream, const char *format, va_list arg) .................................................................... 144
int vprintf(const char *format, va_list arg) ............................................................................................ 148
int vsprintf(char *str, const char *format, va_list arg) .......................................................................... 152
int fscanf(FILE *stream, const char *format, ...) .................................................................................... 156
int scanf(const char *format, ...) ............................................................................................................ 158
int sscanf(const char *str, const char *format, ...) ................................................................................. 161
int fgetc(FILE *stream) ........................................................................................................................... 164
char *fgets(char *str, int n, FILE *stream) ............................................................................................. 165
int fputc(int char, FILE *stream) ............................................................................................................. 167
int fputs(const char *str, FILE *stream) ................................................................................................. 168
int getc(FILE *stream) ............................................................................................................................ 170
int getchar(void) ..................................................................................................................................... 171
char *gets(char *str) .............................................................................................................................. 172
int putc(int char, FILE *stream) .............................................................................................................. 173
int putchar(int char) ............................................................................................................................... 175
int puts(const char *str) ......................................................................................................................... 176
int ungetc(int char, FILE *stream) .......................................................................................................... 177
void perror(const char *str) ................................................................................................................... 179

13. <STDLIB.H>........................................................................................................................ 181
Introduction .......................................................................................................................................... 181
Library Variables ................................................................................................................................... 181
Library Macros ...................................................................................................................................... 181

v



Library Functions .................................................................................................................................. 182
double atof(const char *str)................................................................................................................... 185
int atoi(const char *str) .......................................................................................................................... 186
long int atol(const char *str) .................................................................................................................. 187
double strtod(const char *str, char **endptr) ....................................................................................... 188
long int strtol(const char *str, char **endptr, int base) ........................................................................ 189
unsigned long int strtoul(const char *str, char **endptr, int base) ....................................................... 191
void *calloc(size_t nitems, size_t size) ................................................................................................... 192
void free(void *ptr) ................................................................................................................................ 193
void *malloc(size_t size) ........................................................................................................................ 194
void *realloc(void *ptr, size_t size) ........................................................................................................ 196
void abort(void)...................................................................................................................................... 197
int atexit(void (*func)(void)) .................................................................................................................. 198
void exit(int status) ................................................................................................................................ 199
char *getenv(const char *name) ........................................................................................................... 200
int system(const char *string) ................................................................................................................ 201
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *,
const void *)) .......................................................................................................................................... 203
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*)) ................ 205
int abs(int x) ........................................................................................................................................... 207
div_t div(int numer, int denom) ............................................................................................................. 208
long int labs(long int x) ........................................................................................................................... 209
ldiv_t ldiv(long int numer, long int denom) ........................................................................................... 210
int rand(void) ......................................................................................................................................... 211
void srand(unsigned int seed) ................................................................................................................ 212
int mblen(const char *str, size_t n)........................................................................................................ 213
size_t mbstowcs(schar_t *pwcs, const char *str, size_t n) .................................................................... 215
int mbtowc(whcar_t *pwc, const char *str, size_t n) ............................................................................ 216
size_t wcstombs(char *str, const wchar_t *pwcs, size_t n) .................................................................. 218

int wctomb(char *str, wchar_t wchar)................................................................................................... 219

14. <STRING.H>....................................................................................................................... 221
Introduction .......................................................................................................................................... 221
Library Variables ................................................................................................................................... 221
Library Macros ...................................................................................................................................... 221
Library Functions .................................................................................................................................. 221
void *memchr(const void *str, int c, size_t n) ....................................................................................... 224
int memcmp(const void *str1, const void *str2, size_t n) ..................................................................... 225
void *memmove(void *dest, const void *src, size_t n) ......................................................................... 227
void *memset(void *str, int c, size_t n) ................................................................................................. 228
char *strcat(char *dest, const char *src) ............................................................................................... 229
char *strncat(char *dest, const char *src, size_t n) ............................................................................... 231
char *strchr(const char *str, int c) ......................................................................................................... 232
int strcmp(const char *str1, const char *str2) ....................................................................................... 233
int strncmp(const char *str1, const char *str2, size_t n) ....................................................................... 234
int strcoll(const char *str1, const char *str2) ........................................................................................ 236
char *strcpy(char *dest, const char *src) .............................................................................................. 237
char *strncpy(char *dest, const char *src, size_t n) .............................................................................. 238

vi


size_t strcspn(const char *str1, const char *str2) .................................................................................. 240
char *strerror(int errnum) ..................................................................................................................... 241
size_t strlen(const char *str) .................................................................................................................. 242
char *strpbrk(const char *str1, const char *str2) .................................................................................. 243
char *strrchr(const char *str, int c) ........................................................................................................ 244
size_t strspn(const char *str1, const char *str2) ................................................................................... 245
char *strstr(const char *haystack, const char *needle)......................................................................... 246

char *strtok(char *str, const char *delim) ............................................................................................. 247
size_t strxfrm(char *dest, const char *src, size_t n) .............................................................................. 248

15. <TIME.H> .......................................................................................................................... 250
Introduction .......................................................................................................................................... 250
Library Variables ................................................................................................................................... 250
Library Macros ...................................................................................................................................... 251
Library Functions .................................................................................................................................. 251
char *asctime(const struct tm *timeptr) ............................................................................................... 252
clock_t clock(void) .................................................................................................................................. 254
char *ctime(const time_t *timer) .......................................................................................................... 255
double difftime(time_t time1, time_t time2) ........................................................................................ 256
struct tm *gmtime(const time_t *timer) ............................................................................................... 257
struct tm *localtime(const time_t *timer)............................................................................................. 259
time_t mktime(struct tm *timeptr) ....................................................................................................... 260
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) ..................... 262
time_t time(time_t *timer) .................................................................................................................... 265

vii


1. <ASSERT.H>

C Standard Library

Introduction
The assert.h header file of the C Standard Library provides a macro
called assert which can be used to verify assumptions made by the program and
print a diagnostic message if this assumption is false.
The defined macro assert refers to another macro NDEBUG which is not a part

of <assert.h>. If NDEBUG is defined as a macro name in the source file, at the
point where <assert.h> is included, the assert macro is defined as follows:
#define assert(ignore) ((void)0)

Library Macros
Following is the only function defined in the header assert.h:
S.N.

Function & Description

1

void assert(int expression)
This is actually a macro and not a function, which can be used to add
diagnostics in your C program.

void assert(int expression)
Description
The C library macro void assert(int expression) allows diagnostic information
to be written to the standard error file. In other words, it can be used to add
diagnostics in your C program.

Declaration
Following is the declaration for assert() Macro.
void assert(int expression);

Parameters


expression -- This can be a variable or any C expression.

If expression evaluates
to
TRUE,
assert()
does
nothing.
If expression evaluates to FALSE, assert() displays an error message on

1


C Standard Library

stderr (standard error stream to display error messages and diagnostics)
and aborts program execution.

Return Value
This macro does not return any value.

Example
The following example shows the usage of assert() macro:
#include <assert.h>
#include <stdio.h>

int main()
{
int a;
char str[50];

printf("Enter an integer value: ");

scanf("%d\n", &a);
assert(a >= 10);
printf("Integer entered is %d\n", a);

printf("Enter string: ");
scanf("%s\n", &str);
assert(str != NULL);
printf("String entered is: %s\n", str);

return(0);
}
Let us compile and run the above program in the interactive mode as shown
below:
Enter an integer value: 11
Integer entered is 11
Enter string: tutorialspoint
String entered is: tutorialspoint

2


2. <CTYPE.H>

C Standard Library

Introduction
The ctype.h header file of the C Standard Library declares several functions that
are useful for testing and mapping characters.
All the functions accepts int as a parameter, whose value must be EOF or
representable as an unsigned char.

All the functions return non-zero (true) if the argument c satisfies the condition
described, and zero (false) if not.

Library Functions
Following are the functions defined in the header ctype.h:
S.N.

Function & Description

1

int isalnum(int c)
This function checks whether the passed character is alphanumeric.

2

int isalpha(int c)
This function checks whether the passed character is alphabetic.

3

int iscntrl(int c)
This function checks whether the passed character is control character.

4

int isdigit(int c)
This function checks whether the passed character is decimal digit.

5


int isgraph(int c)
This function checks whether the passed character has graphical
representation using locale.

6

int islower(int c)
This function checks whether the passed character is lowercase letter.

3


C Standard Library

7

int isprint(int c)
This function checks whether the passed character is printable.

8

int ispunct(int c)
This function checks whether the passed character is a punctuation
character.

9

int isspace(int c)
This function checks whether the passed character is white-space.


10

int isupper(int c)
This function checks whether the passed character is an uppercase
letter.

11

int isxdigit(int c)
This function checks whether the passed character is a hexadecimal
digit.

int isalnum(int c)
Description
The C library function void isalnum(int c) checks if the passed character is
alphanumeric.

Declaration
Following is the declaration for isalnum() function.
int isalnum(int c);

Parameters


c -- This is the character to be checked.

Return Value
This function returns non-zero value if c is a digit or a letter, else it returns 0.


Example
The following example shows the usage of isalnum() function.
#include <stdio.h>
#include <ctype.h>
4


C Standard Library

int main()
{
int var1 = 'd';
int var2 = '2';
int var3 = '\t';
int var4 = ' ';

if( isalnum(var1) )
{
printf("var1 = |%c| is alphanumeric\n", var1 );
}
else
{
printf("var1 = |%c| is not alphanumeric\n", var1 );
}
if( isalnum(var2) )
{
printf("var2 = |%c| is alphanumeric\n", var2 );
}
else
{

printf("var2 = |%c| is not alphanumeric\n", var2 );
}
if( isalnum(var3) )
{
printf("var3 = |%c| is alphanumeric\n", var3 );
}
else
{
printf("var3 = |%c| is not alphanumeric\n", var3 );
}
if( isalnum(var4) )
{
printf("var4 = |%c| is alphanumeric\n", var4 );
}
5


C Standard Library

else
{
printf("var4 = |%c| is not alphanumeric\n", var4 );
}

return(0);
}
Let us compile and run the above program to produce the following result:
var1 = |d| is alphanumeric
var2 = |2| is alphanumeric
var3 = | | is not alphanumeric

var4 = | | is not alphanumeric

int isalpha(int c)
Description
The C library function void isalpha(int c) checks if the passed character is
alphabetic.

Declaration
Following is the declaration for isalpha() function.
int isalpha(int c);

Parameters


c -- This is the character to be checked.

Return Value
This function returns non-zero value if c is an alphabet, else it returns 0.

Example
The following example shows the usage of isalpha() function.
#include <stdio.h>
#include <ctype.h>

int main()
{
int var1 = 'd';
6



C Standard Library

int var2 = '2';
int var3 = '\t';
int var4 = ' ';

if( isalpha(var1) )
{
printf("var1 = |%c| is an alphabet\n", var1 );
}
else
{
printf("var1 = |%c| is not an alphabet\n", var1 );
}
if( isalpha(var2) )
{
printf("var2 = |%c| is an alphabet\n", var2 );
}
else
{
printf("var2 = |%c| is not an alphabet\n", var2 );
}
if( isalpha(var3) )
{
printf("var3 = |%c| is an alphabet\n", var3 );
}
else
{
printf("var3 = |%c| is not an alphabet\n", var3 );
}

if( isalpha(var4) )
{
printf("var4 = |%c| is an alphabet\n", var4 );
}
else
{
printf("var4 = |%c| is not an alphabet\n", var4 );
7


C Standard Library

}

return(0);
}
Let us compile and run the above program to produce the following result:
var1 = |d| is an alphabet
var2 = |2| is not an alphabet
var3 = | | is not an alphabet
var4 = | | is not an alphabet

int iscntrl(int c)
Description
The C library function void iscntrl(int c) checks if the passed character is a
control character.
According to standard ASCII character set, control characters are between ASCII
codes 0x00 (NUL), 0x1f (US), and 0x7f (DEL). Specific compiler implementations
for certain platforms may define additional control characters in the extended
character set (above 0x7f).


Declaration
Following is the declaration for iscntrl() function.
int iscntrl(int c);

Parameters


c -- This is the character to be checked.

Return Value
This function returns non-zero value if c is a control character, else it returns 0.

Example
The following example shows the usage of iscntrl() function.
#include <stdio.h>
#include <ctype.h>

int main ()
{
int i = 0, j = 0;
8


C Standard Library

char str1[] = "all \a about \t programming";
char str2[] = "tutorials \n point";

/* Prints string till control character \a */

while( !iscntrl(str1[i]) )
{
putchar(str1[i]);
i++;
}

/* Prints string till control character \n */
while( !iscntrl(str2[j]) )
{
putchar(str2[j]);
j++;
}

return(0);
}
Let us compile and run the above program to produce the following result:
all tutorials

int isdigit(int c)
Description
The C library function void isdigit(int c) checks if the passed character is a
decimal digit character.
Decimal digits are (numbers): 0 1 2 3 4 5 6 7 8 9.

Declaration
Following is the declaration for isdigit() function.
int isdigit(int c);

Parameters



c -- This is the character to be checked.
9


C Standard Library

Return Value
This function returns non-zero value if c is a digit, else it returns 0.

Example
The following example shows the usage of isdigit() function.
#include <stdio.h>
#include <ctype.h>

int main()
{
int var1 = 'h';
int var2 = '2';

if( isdigit(var1) )
{
printf("var1 = |%c| is a digit\n", var1 );
}
else
{
printf("var1 = |%c| is not a digit\n", var1 );
}
if( isdigit(var2) )
{

printf("var2 = |%c| is a digit\n", var2 );
}
else
{
printf("var2 = |%c| is not a digit\n", var2 );
}

return(0);
}
Let us compile and run the above program to produce the following result:
var1 = |h| is not a digit
10


C Standard Library

var2 = |2| is a digit

int isgraph(int c)
Description
The C library function void isgraph(int c) checks if the character has graphical
representation.
The characters with graphical representations are all those characters that can
be printed except for whitespace characters (like ' '), which is not considered as
isgraph characters.

Declaration
Following is the declaration for isgraph() function.
int isgraph(int c);


Parameters


c -- This is the character to be checked.

Return Value
This function returns non-zero value if c has a graphical representation as
character, else it returns 0.

Example
The following example shows the usage of isgraph() function.
#include <stdio.h>
#include <ctype.h>

int main()
{
int var1 = '3';
int var2 = 'm';
int var3 = ' ';

if( isgraph(var1) )
{
printf("var1 = |%c| can be printed\n", var1 );
}
else
{
11


C Standard Library


printf("var1 = |%c| can't be printed\n", var1 );
}
if( isgraph(var2) )
{
printf("var2 = |%c| can be printed\n", var2 );
}
else
{
printf("var2 = |%c| can't be printed\n", var2 );
}
if( isgraph(var3) )
{
printf("var3 = |%c| can be printed\n", var3 );
}
else
{
printf("var3 = |%c| can't be printed\n", var3 );
}

return(0);
}
Let us compile and run the above program to produce the following result:
var1 = |3| can be printed
var2 = |m| can be printed
var3 = | | can't be printed

int islower(int c)
Description
The C library function int islower(int c) checks whether the passed character is

a lowercase letter.

Declaration
Following is the declaration for islower() function.
int islower(int c);
12


C Standard Library

Parameters


c -- This is the character to be checked.

Return Value
This function returns a non-zero value(true) if c is a lowercase alphabetic letter
else, zero (false).

Example
The following example shows the usage of islower() function.
#include <stdio.h>
#include <ctype.h>

int main()
{
int var1 = 'Q';
int var2 = 'q';
int var3 = '3';


if( islower(var1) )
{
printf("var1 = |%c| is lowercase character\n", var1 );
}
else
{
printf("var1 = |%c| is not lowercase character\n", var1 );
}
if( islower(var2) )
{
printf("var2 = |%c| is lowercase character\n", var2 );
}
else
{
printf("var2 = |%c| is not lowercase character\n", var2 );
}
if( islower(var3) )
{
13


C Standard Library

printf("var3 = |%c| is lowercase character\n", var3 );
}
else
{
printf("var3 = |%c| is not lowercase character\n", var3 );
}


return(0);
}
Let us compile and run the above program to produce the following result:
var1 = |Q| is not lowercase character
var2 = |q| is lowercase character
var3 = |3| is not lowercase character

int isprint(int c)
Description
The C library function int isprint(int c) checks whether the passed character is
printable. A printable character is a character that is not a control character.

Declaration
Following is the declaration for isprint() function.
int isprint(int c);

Parameters


c -- This is the character to be checked.

Return Value
This function returns a non-zero value(true) if c is a printable character else,
zero (false).

Example
The following example shows the usage of isprint() function.
#include <stdio.h>
#include <ctype.h>


int main()
14


C Standard Library

{
int var1 = 'k';
int var2 = '8';
int var3 = '\t';
int var4 = ' ';

if( isprint(var1) )
{
printf("var1 = |%c| can be printed\n", var1 );
}
else
{
printf("var1 = |%c| can't be printed\n", var1 );
}
if( isprint(var2) )
{
printf("var2 = |%c| can be printed\n", var2 );
}
else
{
printf("var2 = |%c| can't be printed\n", var2 );
}
if( isprint(var3) )
{

printf("var3 = |%c| can be printed\n", var3 );
}
else
{
printf("var3 = |%c| can't be printed\n", var3 );
}
if( isprint(var4) )
{
printf("var4 = |%c| can be printed\n", var4 );
}
else
15


C Standard Library

{
printf("var4 = |%c| can't be printed\n", var4 );
}

return(0);
}
Let us compile and run the above program to produce the following result:
var1 = |k| can be printed
var2 = |8| can be printed
var3 = |

| can't be printed

var4 = | | can be printed


int ispunct(int c)
Description
The C library function int ispunct(int c) checks whether the passed character is
a punctuation character. A punctuation character is any graphic character (as in
isgraph) that is not alphanumeric (as in isalnum).

Declaration
Following is the declaration for ispunct() function.
int ispunct(int c);

Parameters


c -- This is the character to be checked.

Return Value
This function returns a non-zero value(true) if c is a punctuation character else,
zero (false).

Example
The following example shows the usage of ispunct() function.
#include <stdio.h>
#include <ctype.h>

int main()
{
int var1 = 't';
16



C Standard Library

int var2 = '1';
int var3 = '/';
int var4 = ' ';

if( ispunct(var1) )
{
printf("var1 = |%c| is a punctuation character\n", var1 );
}
else
{
printf("var1 = |%c| is not a punctuation character\n", var1 );
}
if( ispunct(var2) )
{
printf("var2 = |%c| is a punctuation character\n", var2 );
}
else
{
printf("var2 = |%c| is not a punctuation character\n", var2 );
}
if( ispunct(var3) )
{
printf("var3 = |%c| is a punctuation character\n", var3 );
}
else
{
printf("var3 = |%c| is not a punctuation character\n", var3 );

}
if( ispunct(var4) )
{
printf("var4 = |%c| is a punctuation character\n", var4 );
}
else
{
printf("var4 = |%c| is not a punctuation character\n", var4 );
17


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×