Libreria standard C - Guida rapida

Il assert.h Il file di intestazione della libreria standard C fornisce una macro chiamata assert che può essere utilizzato per verificare le ipotesi fatte dal programma e stampare un messaggio diagnostico se questa ipotesi è falsa.

La macro definita assert si riferisce a un'altra macro NDEBUGche non fa parte di <assert.h>. Se NDEBUG è definito come un nome di macro nel file sorgente, nel punto in cui è incluso <assert.h>, ilassert la macro è definita come segue:

#define assert(ignore) ((void)0)

Macro di libreria

La seguente è l'unica funzione definita nell'intestazione assert.h -

Sr.No. Descrizione della funzione
1 void assert (int espressione)

Questa è in realtà una macro e non una funzione, che può essere utilizzata per aggiungere la diagnostica nel programma C.

Il ctype.h Il file di intestazione della libreria standard C dichiara diverse funzioni utili per testare e mappare i caratteri.

Tutte le funzioni accettano int come parametro, il cui valore deve essere EOF o rappresentabile come un carattere senza segno.

Tutte le funzioni restituiscono un valore diverso da zero (vero) se l'argomento c soddisfa la condizione descritta e zero (falso) in caso contrario.

Funzioni di libreria

Di seguito sono riportate le funzioni definite nell'intestazione ctype.h -

Sr.No. Descrizione della funzione
1 int isalnum (int c)

Questa funzione controlla se il carattere passato è alfanumerico.

2 int isalpha (int c)

Questa funzione controlla se il carattere passato è alfabetico.

3 int iscntrl (int c)

Questa funzione controlla se il carattere passato è un carattere di controllo.

4 int isdigit (int c)

Questa funzione controlla se il carattere passato è una cifra decimale.

5 int isgraph (int c)

Questa funzione controlla se il carattere passato ha una rappresentazione grafica utilizzando le impostazioni locali.

6 int islower (int c)

Questa funzione controlla se il carattere passato è una lettera minuscola.

7 int isprint (int c)

Questa funzione controlla se il carattere passato è stampabile.

8 int ispunct (int c)

Questa funzione controlla se il carattere passato è un carattere di punteggiatura.

9 int isspace (int c)

Questa funzione controlla se il carattere passato è uno spazio bianco.

10 int isupper (int c)

Questa funzione controlla se il carattere passato è una lettera maiuscola.

11 int isxdigit (int c)

Questa funzione controlla se il carattere passato è una cifra esadecimale.

La libreria contiene anche due funzioni di conversione che accetta e restituisce un "int".

Sr.No. Descrizione della funzione
1 int tolower (int c)

Questa funzione converte le lettere maiuscole in minuscole.

2 int toupper (int c)

Questa funzione converte le lettere minuscole in maiuscole.

Classi di caratteri

Sr.No. Classe e descrizione del carattere
1

Digits

Questo è un insieme di numeri interi {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}.

2

Hexadecimal digits

Questo è l'insieme di {0 1 2 3 4 5 6 7 8 9 ABCDEF abcdef}.

3

Lowercase letters

Questo è un insieme di lettere minuscole {abcdefghijklmnopqrstu vwxyz}.

4

Uppercase letters

Questo è un insieme di lettere maiuscole {ABCDEFGHIJKLMNOPQRSTU VWXYZ}.

5

Letters

Questo è un insieme di lettere minuscole e maiuscole.

6

Alphanumeric characters

Questo è un insieme di cifre, lettere minuscole e lettere maiuscole.

7

Punctuation characters

Questo è un set di! "# $% & '() * +, -. /:; <=>? @ [\] ^ _` {|} ~

8

Graphical characters

Questo è un insieme di caratteri alfanumerici e caratteri di punteggiatura.

9

Space characters

Questo è un insieme di tabulazioni, nuova riga, tabulazione verticale, avanzamento modulo, ritorno a capo e spazio.

10

Printable characters

Questo è un set di caratteri alfanumerici, caratteri di punteggiatura e caratteri di spazio.

11

Control characters

In ASCII, questi caratteri hanno codici ottali da 000 a 037 e 177 (DEL).

12

Blank characters

Questi sono spazi e tabulazioni.

13

Alphabetic characters

Questo è un insieme di lettere minuscole e lettere maiuscole.

Il errno.h Il file di intestazione della libreria standard C definisce la variabile intera errno, che viene impostato dalle chiamate di sistema e da alcune funzioni di libreria in caso di errore per indicare cosa è andato storto. Questa macro si espande in un valore modificabile di tipo int, quindi può essere sia letta che modificata da un programma.

Il errnoè impostato su zero all'avvio del programma. Alcune funzioni della libreria C standard modificano il proprio valore su un valore diverso da zero per segnalare alcuni tipi di errore. Puoi anche modificarne il valore o reimpostarlo a zero a tuo piacimento.

Il errno.h Il file di intestazione definisce anche un elenco di macro che indicano diversi codici di errore, che si espanderà in espressioni costanti intere con tipo int.

Macro di libreria

Di seguito sono riportate le macro definite nell'intestazione errno.h -

Sr.No. Macro e descrizione
1 extern int errno

Questa è la macro impostata dalle chiamate di sistema e da alcune funzioni di libreria in caso di errore per indicare cosa è andato storto.

2 Errore di dominio EDOM

Questa macro rappresenta un errore di dominio, che si verifica se un argomento di input è esterno al dominio, su cui è definita la funzione matematica e errno è impostato su EDOM.

3 ERANGE Range Error

Questa macro rappresenta un errore di intervallo, che si verifica se un argomento di input è al di fuori dell'intervallo, oltre il quale è definita la funzione matematica e errno è impostato su ERANGE.

Il float.hIl file di intestazione della libreria standard C contiene un insieme di varie costanti dipendenti dalla piattaforma relative ai valori in virgola mobile. Queste costanti sono proposte da ANSI C. Consentono di creare programmi più portabili. Prima di controllare tutte le costanti, è bene capire che il numero in virgola mobile è composto dai seguenti quattro elementi:

Sr.No. Componente e descrizione dei componenti
1

S

segno (+/-)

2

b

base o radice della rappresentazione esponente, 2 per binario, 10 per decimale, 16 per esadecimale e così via ...

3

e

esponente, un numero intero compreso tra un minimo emin e un massimo emax.

4

p

precisione, il numero di cifre in base b nel significato.

Sulla base dei 4 componenti precedenti, un virgola mobile avrà il suo valore come segue:

floating-point = ( S ) p x be

or

floating-point = (+/-) precision x baseexponent

Macro di libreria

I seguenti valori sono specifici dell'implementazione e definiti con la direttiva #define, ma questi valori non possono essere inferiori a quelli forniti qui. Notare che in tutti i casi FLT si riferisce al tipofloat, DBL si riferisce a doublee LDBL si riferisce a long double.

Sr.No. Macro e descrizione
1

FLT_ROUNDS

Definisce la modalità di arrotondamento per l'aggiunta in virgola mobile e può avere uno dei seguenti valori:

  • -1 - indeterminabile
  • 0 - verso lo zero
  • 1 - al più vicino
  • 2 - verso l'infinito positivo
  • 3 - verso l'infinito negativo
2

FLT_RADIX 2

Definisce la rappresentazione della radice di base dell'esponente. Una base-2 è binaria, base-10 è la normale rappresentazione decimale, base-16 è esadecimale.

3

FLT_MANT_DIG

DBL_MANT_DIG

LDBL_MANT_DIG

Queste macro definiscono il numero di cifre nel numero (nella base FLT_RADIX).

4

FLT_DIG 6

DBL_DIG 10

LDBL_DIG 10

Queste macro definiscono il numero massimo di cifre decimali (base 10) che può essere rappresentato senza modifiche dopo l'arrotondamento.

5

FLT_MIN_EXP

DBL_MIN_EXP

LDBL_MIN_EXP

Queste macro definiscono il valore intero negativo minimo per un esponente in base FLT_RADIX.

6

FLT_MIN_10_EXP -37

DBL_MIN_10_EXP -37

LDBL_MIN_10_EXP -37

Queste macro definiscono il valore intero negativo minimo per un esponente in base 10.

7

FLT_MAX_EXP

DBL_MAX_EXP

LDBL_MAX_EXP

Queste macro definiscono il valore intero massimo per un esponente in base FLT_RADIX.

8

FLT_MAX_10_EXP +37

DBL_MAX_10_EXP +37

LDBL_MAX_10_EXP +37

Queste macro definiscono il valore intero massimo per un esponente in base 10.

9

FLT_MAX 1E+37

DBL_MAX 1E+37

LDBL_MAX 1E+37

Queste macro definiscono il valore in virgola mobile finito massimo.

10

FLT_EPSILON 1E-5

DBL_EPSILON 1E-9

LDBL_EPSILON 1E-9

Queste macro definiscono la cifra meno significativa rappresentabile.

11

FLT_MIN 1E-37

DBL_MIN 1E-37

LDBL_MIN 1E-37

Queste macro definiscono i valori in virgola mobile minimi.

Esempio

L'esempio seguente mostra l'utilizzo di alcune delle costanti definite nel file float.h.

#include <stdio.h>
#include <float.h>

int main () {
   printf("The maximum value of float = %.10e\n", FLT_MAX);
   printf("The minimum value of float = %.10e\n", FLT_MIN);

   printf("The number of digits in the number = %.10e\n", FLT_MANT_DIG);
}

Compiliamo ed eseguiamo il programma sopra che produrrà il seguente risultato:

The maximum value of float = 3.4028234664e+38
The minimum value of float = 1.1754943508e-38
The number of digits in the number = 7.2996655210e-312

Il limits.hheader determina varie proprietà dei vari tipi di variabili. Le macro definite in questa intestazione limitano i valori di vari tipi di variabili come char, int e long.

Questi limiti specificano che una variabile non può memorizzare alcun valore oltre questi limiti, ad esempio un carattere senza segno può memorizzare fino a un valore massimo di 255.

Macro di libreria

I seguenti valori sono specifici dell'implementazione e definiti con la direttiva #define, ma questi valori non possono essere inferiori a quelli forniti qui.

Macro Valore Descrizione
CHAR_BIT 8 Definisce il numero di bit in un byte.
SCHAR_MIN -128 Definisce il valore minimo per un carattere con segno.
SCHAR_MAX +127 Definisce il valore massimo per un carattere con segno.
UCHAR_MAX 255 Definisce il valore massimo per un carattere senza segno.
CHAR_MIN -128 Definisce il valore minimo per il tipo char e il suo valore sarà uguale a SCHAR_MIN se char rappresenta valori negativi, altrimenti zero.
CHAR_MAX +127 Definisce il valore per il tipo char e il suo valore sarà uguale a SCHAR_MAX se char rappresenta valori negativi, altrimenti UCHAR_MAX.
MB_LEN_MAX 16 Definisce il numero massimo di byte in un carattere multibyte.
SHRT_MIN -32768 Definisce il valore minimo per un int breve.
SHRT_MAX +32767 Definisce il valore massimo per un int breve.
USHRT_MAX 65535 Definisce il valore massimo per un int breve senza segno.
INT_MIN -2147483648 Definisce il valore minimo per un int.
INT_MAX +2147483647 Definisce il valore massimo per un int.
UINT_MAX 4294967295 Definisce il valore massimo per un int senza segno.
LONG_MIN -9223372036854775808 Definisce il valore minimo per un int lungo.
LONG_MAX +9223372036854775807 Definisce il valore massimo per un int lungo.
ULONG_MAX 18446744073709551615 Definisce il valore massimo per un int lungo senza segno.

Esempio

L'esempio seguente mostra l'utilizzo di alcune delle costanti definite in limits.h file.

#include <stdio.h>
#include <limits.h>

int main() {

   printf("The number of bits in a byte %d\n", CHAR_BIT);

   printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN);
   printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX);
   printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX);

   printf("The minimum value of SHORT INT = %d\n", SHRT_MIN);
   printf("The maximum value of SHORT INT = %d\n", SHRT_MAX); 

   printf("The minimum value of INT = %d\n", INT_MIN);
   printf("The maximum value of INT = %d\n", INT_MAX);

   printf("The minimum value of CHAR = %d\n", CHAR_MIN);
   printf("The maximum value of CHAR = %d\n", CHAR_MAX);

   printf("The minimum value of LONG = %ld\n", LONG_MIN);
   printf("The maximum value of LONG = %ld\n", LONG_MAX);
  
   return(0);
}

Compiliamo ed eseguiamo il programma sopra che produrrà il seguente risultato:

The number of bits in a byte 8
The minimum value of SIGNED CHAR = -128
The maximum value of SIGNED CHAR = 127
The maximum value of UNSIGNED CHAR = 255
The minimum value of SHORT INT = -32768
The maximum value of SHORT INT = 32767
The minimum value of INT = -2147483648
The maximum value of INT = 2147483647
The minimum value of CHAR = -128
The maximum value of CHAR = 127
The minimum value of LONG = -9223372036854775808
The maximum value of LONG = 9223372036854775807

Il locale.hL'intestazione definisce le impostazioni specifiche della località, come i formati della data e i simboli di valuta. Troverai diverse macro definite insieme a una struttura importantestruct lconv e due importanti funzioni elencate di seguito.

Macro di libreria

Di seguito sono riportate le macro definite nell'intestazione e queste macro verranno utilizzate in due funzioni elencate di seguito:

Sr.No. Macro e descrizione
1

LC_ALL

Imposta tutto.

2

LC_COLLATE

Ha effetto sulle funzioni strcoll e strxfrm.

3

LC_CTYPE

Ha effetto su tutte le funzioni dei caratteri.

4

LC_MONETARY

Influisce sulle informazioni monetarie fornite dalla funzione di conversione locale.

5

LC_NUMERIC

Influisce sulla formattazione del punto decimale e sulle informazioni fornite dalla funzione localeconv.

6

LC_TIME

Influisce sulla funzione strftime.

Funzioni di libreria

Di seguito sono riportate le funzioni definite nell'intestazione locale.h -

Sr.No. Descrizione della funzione
1 char * setlocale (int category, const char * locale)

Imposta o legge le informazioni dipendenti dalla posizione.

2 struct lconv * localeconv (void)

Imposta o legge le informazioni dipendenti dalla posizione.

Struttura della libreria

typedef struct {
   char *decimal_point;
   char *thousands_sep;
   char *grouping;	
   char *int_curr_symbol;
   char *currency_symbol;
   char *mon_decimal_point;
   char *mon_thousands_sep;
   char *mon_grouping;
   char *positive_sign;
   char *negative_sign;
   char int_frac_digits;
   char frac_digits;
   char p_cs_precedes;
   char p_sep_by_space;
   char n_cs_precedes;
   char n_sep_by_space;
   char p_sign_posn;
   char n_sign_posn;
} lconv

Di seguito è riportata la descrizione di ciascuno dei campi:

Sr.No. Campo e descrizione
1

decimal_point

Carattere punto decimale utilizzato per valori non monetari.

2

thousands_sep

Migliaia di caratteri di separazione dei luoghi utilizzati per i valori non monetari.

3

grouping

Una stringa che indica la dimensione di ogni gruppo di cifre in quantità non monetarie. Ogni carattere rappresenta un valore intero, che designa il numero di cifre nel gruppo corrente. Un valore di 0 significa che il valore precedente deve essere utilizzato per il resto dei gruppi.

4

int_curr_symbol

È una stringa dei simboli di valuta internazionale utilizzati. I primi tre caratteri sono quelli specificati da ISO 4217: 1987 e il quarto è il carattere, che separa il simbolo della valuta dalla quantità monetaria.

5

currency_symbol

Il simbolo locale utilizzato per la valuta.

6

mon_decimal_point

Il carattere del punto decimale utilizzato per i valori monetari.

7

mon_thousands_sep

Il carattere di raggruppamento delle migliaia utilizzato per i valori monetari.

8

mon_grouping

Una stringa i cui elementi definiscono la dimensione del raggruppamento di cifre in valori monetari. Ogni carattere rappresenta un valore intero che designa il numero di cifre nel gruppo corrente. Un valore di 0 significa che il valore precedente deve essere utilizzato per il resto dei gruppi.

9

positive_sign

Il carattere utilizzato per valori monetari positivi.

10

negative_sign

Il carattere utilizzato per i valori monetari negativi.

11

int_frac_digits

Numero di cifre da mostrare dopo il punto decimale nei valori monetari internazionali.

12

frac_digits

Numero di cifre da mostrare dopo il punto decimale nei valori monetari.

13

p_cs_precedes

Se è uguale a 1, il simbolo currency_symbol viene visualizzato prima di un valore monetario positivo. Se è uguale a 0, il simbolo currency_symbol viene visualizzato dopo un valore monetario positivo.

14

p_sep_by_space

Se è uguale a 1, il currency_symbol è separato da uno spazio da un valore monetario positivo. Se è uguale a 0, non c'è spazio tra currency_symbol e un valore monetario positivo.

15

n_cs_precedes

Se è uguale a 1, il currency_symbol precede un valore monetario negativo. Se è uguale a 0, il currency_symbol sostituisce un valore monetario negativo.

16

n_sep_by_space

Se è uguale a 1, il currency_symbol è separato da uno spazio da un valore monetario negativo. Se è uguale a 0, non c'è spazio tra currency_symbol e un valore monetario negativo.

17

p_sign_posn

Rappresenta la posizione di positive_sign in un valore monetario positivo.

18

n_sign_posn

Rappresenta la posizione di negative_sign in un valore monetario negativo.

I seguenti valori vengono utilizzati per p_sign_posn e n_sign_posn -

Valore Descrizione
0 Le parentesi racchiudono il valore e il currency_symbol.
1 Il segno precede il valore e il currency_symbol.
2 Il segno segue il valore e il currency_symbol.
3 Il segno precede immediatamente il valore e il currency_symbol.
4 Il segno segue immediatamente il valore e il currency_symbol.

Il math.hL'intestazione definisce varie funzioni matematiche e una macro. Tutte le funzioni disponibili in questa libreria prendonodouble come argomento e ritorno double come risultato.

Macro di libreria

C'è solo una macro definita in questa libreria -

Sr.No. Macro e descrizione
1

HUGE_VAL

Questa macro viene utilizzata quando il risultato di una funzione potrebbe non essere rappresentabile come numero in virgola mobile. Se l'ampiezza del risultato corretto è troppo grande per essere rappresentato, la funzione imposta errno su ERANGE per indicare un errore di intervallo e restituisce un valore particolare, molto grande chiamato dalla macro HUGE_VAL o dalla sua negazione (- HUGE_VAL).

Se la grandezza del risultato è troppo piccola, viene invece restituito un valore zero. In questo caso, errno potrebbe o non potrebbe essere impostato su ERANGE.

Funzioni di libreria

Di seguito sono riportate le funzioni definite nell'intestazione math.h -

Sr.No. Descrizione della funzione
1 doppio acos (doppia x)

Restituisce l'arcocoseno di x in radianti.

2 doppia asin (doppia x)

Restituisce l'arcoseno di x in radianti.

3 doppio atan (doppia x)

Restituisce l'arcotangente di x in radianti.

4 doppia atan2 (doppia y, doppia x)

Restituisce l'arcotangente in radianti di y / x in base ai segni di entrambi i valori per determinare il quadrante corretto.

5 doppio cos (doppia x)

Restituisce il coseno di un angolo in radianti x.

6 doppio cosh (doppia x)

Restituisce il coseno iperbolico di x.

7 doppio peccato (doppia x)

Restituisce il seno di un angolo in radianti x.

8 double sinh (double x)

Returns the hyperbolic sine of x.

9 double tanh(double x)

Returns the hyperbolic tangent of x.

10 double exp(double x)

Returns the value of e raised to the xth power.

11 double frexp(double x, int *exponent)

The returned value is the mantissa and the integer pointed to by exponent is the exponent. The resultant value is x = mantissa * 2 ^ exponent.

12 double ldexp(double x, int exponent)

Returns x multiplied by 2 raised to the power of exponent.

13 double log(double x)

Returns the natural logarithm (base-e logarithm) of x.

14 double log10(double x)

Returns the common logarithm (base-10 logarithm) of x.

15 double modf(double x, double *integer)

The returned value is the fraction component (part after the decimal), and sets integer to the integer component.

16 double pow(double x, double y)

Returns x raised to the power of y.

17 double sqrt(double x)

Returns the square root of x.

18 double ceil(double x)

Returns the smallest integer value greater than or equal to x.

19 double fabs(double x)

Returns the absolute value of x.

20 double floor(double x)

Returns the largest integer value less than or equal to x.

21 double fmod(double x, double y)

Returns the remainder of x divided by y.

The setjmp.h header defines the macro setjmp(), one function longjmp(), and one variable type jmp_buf, for bypassing the normal function call and return discipline.

Library Variables

Following is the variable type defined in the header setjmp.h −

Sr.No. Variable & Description
1

jmp_buf

This is an array type used for holding information for macro setjmp() and function longjmp().

Library Macros

There is only one macro defined in this library −

Sr.No. Macro & Description
1 int setjmp(jmp_buf environment)

This macro saves the current environment into the variable environment for later use by the function longjmp(). If this macro returns directly from the macro invocation, it returns zero but if it returns from a longjmp() function call, then a non-zero value is returned.

Library Functions

Following is the only one function defined in the header setjmp.h −

Sr.No. Function & Description
1 void longjmp(jmp_buf environment, int value)

This function restores the environment saved by the most recent call to setjmp() macro in the same invocation of the program with the corresponding jmp_buf argument.

The signal.h header defines a variable type sig_atomic_t, two function calls, and several macros to handle different signals reported during a program's execution.

Library Variables

Following is the variable type defined in the header signal.h −

Sr.No. Variable & Description
1

sig_atomic_t

This is of int type and is used as a variable in a signal handler. This is an integral type of an object that can be accessed as an atomic entity, even in the presence of asynchronous signals.

Library Macros

Following are the macros defined in the header signal.h and these macros will be used in two functions listed below. The SIG_ macros are used with the signal function to define signal functions.

Sr.No. Macro & Description
1

SIG_DFL

Default signal handler.

2

SIG_ERR

Represents a signal error.

3

SIG_IGN

Signal ignore.

The SIG macros are used to represent a signal number in the following conditions −

Sr.No. Macro & Description
1

SIGABRT

Abnormal program termination.

2

SIGFPE

Floating-point error like division by zero.

3

SIGILL

Illegal operation.

4

SIGINT

Interrupt signal such as ctrl-C.

5

SIGSEGV

Invalid access to storage like segment violation.

6

SIGTERM

Termination request.

Library Functions

Following are the functions defined in the header signal.h −

Sr.No. Function & Description
1 void (*signal(int sig, void (*func)(int)))(int)

This function sets a function to handle signal i.e. a signal handler.

2 int raise(int sig)

This function causes signal sig to be generated. The sig argument is compatible with the SIG macros.

The stdarg.h header defines a variable type va_list and three macros which can be used to get the arguments in a function when the number of arguments are not known i.e. variable number of arguments.

A function of variable arguments is defined with the ellipsis (,...) at the end of the parameter list.

Library Variables

Following is the variable type defined in the header stdarg.h −

Sr.No. Variable & Description
1

va_list

This is a type suitable for holding information needed by the three macros va_start(), va_arg() and va_end().

Library Macros

Following are the macros defined in the header stdarg.h −

Sr.No. Macro & Description
1 void va_start(va_list ap, last_arg)

This macro initializes ap variable to be used with the va_arg and va_end macros. The last_arg is the last known fixed argument being passed to the function i.e. the argument before the ellipsis.

2 type va_arg(va_list ap, type)

This macro retrieves the next argument in the parameter list of the function with type type.

3 void va_end(va_list ap)

This macro allows a function with variable arguments which used the va_start macro to return. If va_end is not called before returning from the function, the result is undefined.

The stddef.h header defines various variable types and macros. Many of these definitions also appear in other headers.

Library Variables

Following are the variable types defined in the header stddef.h −

Sr.No. Variable & Description
1

ptrdiff_t

This is the signed integral type and is the result of subtracting two pointers.

2

size_t

This is the unsigned integral type and is the result of the sizeof keyword.

3

wchar_t

This is an integral type of the size of a wide character constant.

Library Macros

Following are the macros defined in the header stddef.h −

Sr.No. Macro & Description
1 NULL

This macro is the value of a null pointer constant.

2 offsetof(type, member-designator)

This results in a constant integer of type size_t which is the offset in bytes of a structure member from the beginning of the structure. The member is given by member-designator, and the name of the structure is given in type.

The stdio.h header defines three variable types, several macros, and various functions for performing input and output.

Library Variables

Following are the variable types defined in the header stdio.h −

Sr.No. Variable & Description
1

size_t

This is the unsigned integral type and is the result of the sizeof keyword.

2

FILE

This is an object type suitable for storing information for a file stream.

3

fpos_t

This is an object type suitable for storing any position in a file.

Library Macros

Following are the macros defined in the header stdio.h −

Sr.No. Macro & Description
1

NULL

This macro is the value of a null pointer constant.

2

_IOFBF, _IOLBF and _IONBF

These are the macros which expand to integral constant expressions with distinct values and suitable for the use as third argument to the setvbuf function.

3

BUFSIZ

This macro is an integer, which represents the size of the buffer used by the setbuf function.

4

EOF

This macro is a negative integer, which indicates that the end-of-file has been reached.

5

FOPEN_MAX

This macro is an integer, which represents the maximum number of files that the system can guarantee to be opened simultaneously.

6

FILENAME_MAX

This macro is an integer, which represents the longest length of a char array suitable for holding the longest possible filename. If the implementation imposes no limit, then this value should be the recommended maximum value.

7

L_tmpnam

This macro is an integer, which represents the longest length of a char array suitable for holding the longest possible temporary filename created by the tmpnam function.

8

SEEK_CUR, SEEK_END, and SEEK_SET

These macros are used in the fseek function to locate different positions in a file.

9

TMP_MAX

This macro is the maximum number of unique filenames that the function tmpnam can generate.

10

stderr, stdin, and stdout

These macros are pointers to FILE types which correspond to the standard error, standard input, and standard output streams.

Library Functions

Following are the functions defined in the header stdio.h −

Follow the same sequence of functions for better understanding and to make use of Try it(Online compiler) option, because file created in the first function will be used in subsequent functions.
Sr.No. Function & Description
1 int fclose(FILE *stream)

Closes the stream. All buffers are flushed.

2 void clearerr(FILE *stream)

Clears the end-of-file and error indicators for the given stream.

3 int feof(FILE *stream)

Tests the end-of-file indicator for the given stream.

4 int ferror(FILE *stream)

Tests the error indicator for the given stream.

5 int fflush(FILE *stream)

Flushes the output buffer of a stream.

6 int fgetpos(FILE *stream, fpos_t *pos)

Gets the current file position of the stream and writes it to pos.

7 FILE *fopen(const char *filename, const char *mode)

Opens the filename pointed to by filename using the given mode.

8 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)

Reads data from the given stream into the array pointed to by ptr.

9 FILE *freopen(const char *filename, const char *mode, FILE *stream)

Associates a new filename with the given open stream and same time closing the old file in stream.

10 int fseek(FILE *stream, long int offset, int whence)

Sets the file position of the stream to the given offset. The argument offset signifies the number of bytes to seek from the given whence position.

11 int fsetpos(FILE *stream, const fpos_t *pos)

Sets the file position of the given stream to the given position. The argument pos is a position given by the function fgetpos.

12 long int ftell(FILE *stream)

Returns the current file position of the given stream.

13 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)

Writes data from the array pointed to by ptr to the given stream.

14 int remove(const char *filename)

Deletes the given filename so that it is no longer accessible.

15 int rename(const char *old_filename, const char *new_filename)

Causes the filename referred to, by old_filename to be changed to new_filename.

16 void rewind(FILE *stream)

Sets the file position to the beginning of the file of the given stream.

17 void setbuf(FILE *stream, char *buffer)

Defines how a stream should be buffered.

18 int setvbuf(FILE *stream, char *buffer, int mode, size_t size)

Another function to define how a stream should be buffered.

19 FILE *tmpfile(void)

Creates a temporary file in binary update mode (wb+).

20 char *tmpnam(char *str)

Generates and returns a valid temporary filename which does not exist.

21 int fprintf(FILE *stream, const char *format, ...)

Sends formatted output to a stream.

22 int printf(const char *format, ...)

Sends formatted output to stdout.

23 int sprintf(char *str, const char *format, ...)

Sends formatted output to a string.

24 int vfprintf(FILE *stream, const char *format, va_list arg)

Sends formatted output to a stream using an argument list.

25 int vprintf(const char *format, va_list arg)

Sends formatted output to stdout using an argument list.

26 int vsprintf(char *str, const char *format, va_list arg)

Sends formatted output to a string using an argument list.

27 int fscanf(FILE *stream, const char *format, ...)

Reads formatted input from a stream.

28 int scanf(const char *format, ...)

Reads formatted input from stdin.

29 int sscanf(const char *str, const char *format, ...)

Reads formatted input from a string.

30 int fgetc(FILE *stream)

Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.

31 char *fgets(char *str, int n, FILE *stream)

Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first.

32 int fputc(int char, FILE *stream)

Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream.

33 int fputs(const char *str, FILE *stream)

Writes a string to the specified stream up to but not including the null character.

34 int getc(FILE *stream)

Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream.

35 int getchar(void)

Gets a character (an unsigned char) from stdin.

36 char *gets(char *str)

Reads a line from stdin and stores it into the string pointed to by, str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.

37 int putc(int char, FILE *stream)

Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream.

38 int putchar(int char)

Writes a character (an unsigned char) specified by the argument char to stdout.

39 int puts(const char *str)

Writes a string to stdout up to but not including the null character. A newline character is appended to the output.

40 int ungetc(int char, FILE *stream)

Pushes the character char (an unsigned char) onto the specified stream so that the next character is read.

41 void perror(const char *str)

Prints a descriptive error message to stderr. First the string str is printed followed by a colon and then a space.

The stdlib.h header defines four variable types, several macros, and various functions for performing general functions.

Library Variables

Following are the variable types defined in the header stdlib.h −

Sr.No. Variable & Description
1

size_t

This is the unsigned integral type and is the result of the sizeof keyword.

2

wchar_t

This is an integer type of the size of a wide character constant.

3

div_t

This is the structure returned by the div function.

4

ldiv_t

This is the structure returned by the ldiv function.

Library Macros

Following are the macros defined in the header stdlib.h −

Sr.No. Macro & Description
1

NULL

This macro is the value of a null pointer constant.

2

EXIT_FAILURE

This is the value for the exit function to return in case of failure.

3

EXIT_SUCCESS

This is the value for the exit function to return in case of success.

4

RAND_MAX

This macro is the maximum value returned by the rand function.

5

MB_CUR_MAX

This macro is the maximum number of bytes in a multi-byte character set which cannot be larger than MB_LEN_MAX.

Library Functions

Following are the functions defined in the header stlib.h −

Sr.No. Function & Description
1 double atof(const char *str)

Converts the string pointed to, by the argument str to a floating-point number (type double).

2 int atoi(const char *str)

Converts the string pointed to, by the argument str to an integer (type int).

3 long int atol(const char *str)

Converts the string pointed to, by the argument str to a long integer (type long int).

4 double strtod(const char *str, char **endptr)

Converts the string pointed to, by the argument str to a floating-point number (type double).

5 long int strtol(const char *str, char **endptr, int base)

Converts the string pointed to, by the argument str to a long integer (type long int).

6 unsigned long int strtoul(const char *str, char **endptr, int base)

Converts the string pointed to, by the argument str to an unsigned long integer (type unsigned long int).

7 void *calloc(size_t nitems, size_t size)

Allocates the requested memory and returns a pointer to it.

8 void free(void *ptr

Deallocates the memory previously allocated by a call to calloc, malloc, or realloc.

9 void *malloc(size_t size)

Allocates the requested memory and returns a pointer to it.

10 void *realloc(void *ptr, size_t size)

Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc.

11 void abort(void)

Causes an abnormal program termination.

12 int atexit(void (*func)(void))

Causes the specified function func to be called when the program terminates normally.

13 void exit(int status)

Causes the program to terminate normally.

14 char *getenv(const char *name)

Searches for the environment string pointed to by name and returns the associated value to the string.

15 int system(const char *string)

The command specified by string is passed to the host environment to be executed by the command processor.

16 void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))

Performs a binary search.

17 void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))

Sorts an array.

18 int abs(int x)

Returns the absolute value of x.

19 div_t div(int numer, int denom)

Divides numer (numerator) by denom (denominator).

20 long int labs(long int x)

Returns the absolute value of x.

21 ldiv_t ldiv(long int numer, long int denom)

Divides numer (numerator) by denom (denominator).

22 int rand(void)

Returns a pseudo-random number in the range of 0 to RAND_MAX.

23 void srand(unsigned int seed)

This function seeds the random number generator used by the function rand.

24 int mblen(const char *str, size_t n)

Returns the length of a multibyte character pointed to by the argument str.

25 size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)

Converts the string of multibyte characters pointed to by the argument str to the array pointed to by pwcs.

26 int mbtowc(whcar_t *pwc, const char *str, size_t n)

Examines the multibyte character pointed to by the argument str.

27 size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)

Converts the codes stored in the array pwcs to multibyte characters and stores them in the string str.

28 int wctomb(char *str, wchar_t wchar)

Examines the code which corresponds to a multibyte character given by the argument wchar.

The string.h header defines one variable type, one macro, and various functions for manipulating arrays of characters.

Library Variables

Following is the variable type defined in the header string.h −

Sr.No. Variable & Description
1

size_t

This is the unsigned integral type and is the result of the sizeof keyword.

Library Macros

Following is the macro defined in the header string.h −

Sr.No. Macro & Description
1

NULL

This macro is the value of a null pointer constant.

Library Functions

Following are the functions defined in the header string.h −

Sr.No. Function & Description
1 void *memchr(const void *str, int c, size_t n)

Searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str.

2 int memcmp(const void *str1, const void *str2, size_t n)

Compares the first n bytes of str1 and str2.

3 void *memcpy(void *dest, const void *src, size_t n)

Copies n characters from src to dest.

4 void *memmove(void *dest, const void *src, size_t n)

Another function to copy n characters from str2 to str1.

5 void *memset(void *str, int c, size_t n)

Copies the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str.

6 char *strcat(char *dest, const char *src)

Appends the string pointed to, by src to the end of the string pointed to by dest.

7 char *strncat(char *dest, const char *src, size_t n)

Appends the string pointed to, by src to the end of the string pointed to, by dest up to n characters long.

8 char *strchr(const char *str, int c)

Searches for the first occurrence of the character c (an unsigned char) in the string pointed to, by the argument str.

9 int strcmp(const char *str1, const char *str2)

Compares the string pointed to, by str1 to the string pointed to by str2.

10 int strncmp(const char *str1, const char *str2, size_t n)

Compares at most the first n bytes of str1 and str2.

11 int strcoll(const char *str1, const char *str2)

Compares string str1 to str2. The result is dependent on the LC_COLLATE setting of the location.

12 char *strcpy(char *dest, const char *src)

Copies the string pointed to, by src to dest.

13 char *strncpy(char *dest, const char *src, size_t n)

Copies up to n characters from the string pointed to, by src to dest.

14 size_t strcspn(const char *str1, const char *str2)

Calculates the length of the initial segment of str1 which consists entirely of characters not in str2.

15 char *strerror(int errnum)

Searches an internal array for the error number errnum and returns a pointer to an error message string.

16 size_t strlen(const char *str)

Computes the length of the string str up to but not including the terminating null character.

17 char *strpbrk(const char *str1, const char *str2)

Finds the first character in the string str1 that matches any character specified in str2.

18 char *strrchr(const char *str, int c)

Searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str.

19 size_t strspn(const char *str1, const char *str2)

Calculates the length of the initial segment of str1 which consists entirely of characters in str2.

20 char *strstr(const char *haystack, const char *needle)

Finds the first occurrence of the entire string needle (not including the terminating null character) which appears in the string haystack.

21 char *strtok(char *str, const char *delim)

Breaks string str into a series of tokens separated by delim.

22 size_t strxfrm(char *dest, const char *src, size_t n)

Transforms the first n characters of the string src into current locale and places them in the string dest.

The time.h header defines four variable types, two macro and various functions for manipulating date and time.

Library Variables

Following are the variable types defined in the header time.h −

Sr.No. Variable & Description
1

size_t

This is the unsigned integral type and is the result of the sizeof keyword.

2

clock_t

This is a type suitable for storing the processor time.

3

time_t is

This is a type suitable for storing the calendar time.

4

struct tm

This is a structure used to hold the time and date.

La struttura tm ha la seguente definizione:

struct tm {
   int tm_sec;         /* seconds,  range 0 to 59          */
   int tm_min;         /* minutes, range 0 to 59           */
   int tm_hour;        /* hours, range 0 to 23             */
   int tm_mday;        /* day of the month, range 1 to 31  */
   int tm_mon;         /* month, range 0 to 11             */
   int tm_year;        /* The number of years since 1900   */
   int tm_wday;        /* day of the week, range 0 to 6    */
   int tm_yday;        /* day in the year, range 0 to 365  */
   int tm_isdst;       /* daylight saving time             */
};

Macro di libreria

Di seguito sono riportate le macro definite nell'intestazione time.h -

Sr.No. Macro e descrizione
1

NULL

Questa macro è il valore di una costante del puntatore nullo.

2

CLOCKS_PER_SEC

Questa macro rappresenta il numero di clock del processore al secondo.

Funzioni di libreria

Di seguito sono riportate le funzioni definite nell'intestazione time.h -

Sr.No. Descrizione della funzione
1 char * asctime (const struct tm * timeptr)

Restituisce un puntatore a una stringa che rappresenta il giorno e l'ora della struttura timeptr.

2 clock_t clock (void)

Restituisce il tempo di clock del processore utilizzato dall'inizio di un'era definita dall'implementazione (normalmente l'inizio del programma).

3 char * ctime (const time_t * timer)

Restituisce una stringa che rappresenta l'ora locale in base all'argomento timer.

4 double difftime (time_t time1, time_t time2)

Restituisce la differenza di secondi tra time1 e time2 (time1-time2).

5 struct tm * gmtime (const time_t * timer)

Il valore del timer è suddiviso nella struttura tm ed espresso in Coordinated Universal Time (UTC) noto anche come Greenwich Mean Time (GMT).

6 struct tm * localtime (const time_t * timer)

Il valore del timer è suddiviso nella struttura tm ed espresso nel fuso orario locale.

7 time_t mktime (struct tm * timeptr)

Converte la struttura a cui punta timeptr in un valore time_t in base al fuso orario locale.

8 size_t strftime (char * str, size_t maxsize, const char * format, const struct tm * timeptr)

Formatta l'ora rappresentata nella struttura timeptr secondo le regole di formattazione definite in format e memorizzate in str.

9 time_t time (time_t * timer)

Calcola l'ora corrente del calendario e la codifica nel formato time_t.