0% found this document useful (0 votes)
15K views4 pages

C STD Lib

This document summarizes several standard C library functions for input/output (I/O), memory allocation, and string manipulation. It describes the functions' purpose, return values, and parameters. Key functions covered include fopen to open a file, fread/fwrite to read from and write to files, malloc/free for memory allocation, and strcpy/strcmp for string copying and comparison.

Uploaded by

Joseph Choi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15K views4 pages

C STD Lib

This document summarizes several standard C library functions for input/output (I/O), memory allocation, and string manipulation. It describes the functions' purpose, return values, and parameters. Key functions covered include fopen to open a file, fread/fwrite to read from and write to files, malloc/free for memory allocation, and strcpy/strcmp for string copying and comparison.

Uploaded by

Joseph Choi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

<STDIO>

2.12.3.3 feof
Declaration:
int feof(FILE *stream);

Tests the end-of-file indicator for the given stream. If the stream is at the end-of-file, then it returns a nonzero value. If it
is not at the end of the file, then it returns zero.

2.12.3.2 fclose
Declaration:
int fclose(FILE *stream);

Closes the stream. All buffers are flushed.


If successful, it returns zero. On error it returns EOF.

2.12.3.7 fopen
Declaration:
FILE *fopen(const char *filename, const char *mode);

Opens the filename pointed to by filename. The mode argument may be one of the following constant strings:
r
read text mode
w
write text mode (truncates file to zero length or creates new file)
a
append text mode for writing (opens or creates file and sets file pointer to the end-of-file)
rb
read binary mode
wb
write binary mode (truncates file to zero length or creates new file)
ab
append binary mode for writing (opens or creates file and sets file pointer to the end-of-file)
r+
read and write text mode
w+
read and write text mode (truncates file to zero length or creates new file)
a+
read and write text mode (opens or creates file and sets file pointer to the end-of-file)
r+b or rb+ read and write binary mode
w+b or wb+ read and write binary mode (truncates file to zero length or creates new file)
a+b or ab+ read and write binary mode (opens or creates file and sets file pointer to the end-of-file)
If the file does not exist and it is opened with read mode (r), then the open fails.
If the file is opened with append mode (a), then all write operations occur at the end of the file regardless of the
current file position.
If the file is opened in the update mode (+), then output cannot be directly followed by input and input cannot be
directly followed by output without an intervening fseek, fsetpos, rewind, or fflush.
On success a pointer to the file stream is returned. On failure a null pointer is returned.

2.12.5.1 fgetc
Declaration:
int fgetc(FILE *stream);
Gets the next character (an unsigned char)

from the specified stream and advances the position indicator for

the stream.
On success the character is returned. If the end-of-file is encountered, then EOF is returned and the end-of-file
indicator is set. If an error occurs then the error indicator for the stream is set and EOF is returned.

2.12.5.2 fgets
Declaration:
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. The
newline character is copied to the string. A null character is appended to the end of the string.

On success a pointer to the string is returned. On error a null pointer is returned. If the end-of-file occurs before
any characters have been read, the string remains unchanged.

2.12.5.3 fputc
Declaration:
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.
On success the character is returned. If an error occurs, the error indicator for the stream is set and EOF is
returned.

2.12.5.4 fputs
Declaration:
int fputs(const char *str, FILE *stream);

Writes a string to the specified stream up to but not including the null character.
On success a nonnegative value is returned. On error EOF is returned.

2.12.5.5 getc
Declaration:
int getc(FILE *stream);
Gets the next character (an unsigned char)

from the specified stream and advances the position indicator for
the stream.
This may be a macro version of fgetc.
On success the character is returned. If the end-of-file is encountered, then EOF is returned and the end-of-file
indicator is set. If an error occurs then the error indicator for the stream is set and EOF is returned.

2.12.5.6 getchar
Declaration:
int getchar(void);
Gets a character (an unsigned char)

from stdin.
On success the character is returned. If the end-of-file is encountered, then EOF is returned and the end-of-file
indicator is set. If an error occurs then the error indicator for the stream is set and EOF is returned.

2.12.5.7 gets
Declaration:
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. The newline character is not copied to the
string. A null character is appended to the end of the string.
On success a pointer to the string is returned. On error a null pointer is returned. If the end-of-file occurs before
any characters have been read, the string remains unchanged.

2.12.5.8 putc
Declaration:
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.
This may be a macro version of fputc.
On success the character is returned. If an error occurs, the error indicator for the stream is set and EOF is
returned.

2.12.5.9 putchar
Declaration:
int putchar(int

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

On success the character is returned. If an error occurs, the error indicator for the stream is set and EOF is
returned.

2.12.5.10 puts
Declaration:
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.
On success a nonnegative value is returned. On error EOF is returned.

2.12.5.11 ungetc
Declaration:
int ungetc(int

char, FILE *stream);


Pushes the character char (an unsigned char) onto the specified stream so that the this is the next character
read. The functions fseek, fsetpos, and rewind discard any characters pushed onto the stream.
Multiple characters pushed onto the stream are read in a FIFO manner (first in, first out).
On success the character pushed is returned. On error EOF is returned.

<STDLIB>
2.13.3.1 calloc
Declaration:
void *calloc(size_t

nitems, size_t size);


Allocates the requested memory and returns a pointer to it. The requested size is nitems each size bytes long
(total memory requested is nitems*size). The space is initialized to all zero bits.
On success a pointer to the requested space is returned. On failure a null pointer is returned.

2.13.3.2 free
Declaration:
void free(void *ptr);

Deallocates the memory previously allocated by a call to calloc, malloc, or realloc. The argument ptr points
to the space that was previously allocated. If ptr points to a memory block that was not allocated with calloc,
malloc, or realloc, or is a space that has been deallocated, then the result is undefined.
No value is returned.

2.13.3.3 malloc
Declaration:
void *malloc(size_t

size);
Allocates the requested memory and returns a pointer to it. The requested size is size bytes. The value of the
space is indeterminate.
On success a pointer to the requested space is returned. On failure a null pointer is returned.

2.13.3.4 realloc
Declaration:
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. The contents pointed to by ptr are unchanged. If the value of size is greater than the previous size of the
block, then the additional bytes have an undeterminate value. If the value of size is less than the previous size of
the block, then the difference of bytes at the end of the block are freed. If ptr is null, then it behaves like malloc.
If ptr points to a memory block that was not allocated with calloc or malloc, or is a space that has been
deallocated, then the result is undefined. If the new space cannot be allocated, then the contents pointed to by
ptr are unchanged. If size is zero, then the memory block is completely freed.
On success a pointer to the memory block is returned (which may be in a different location as before). On
failure or if size is zero, a null pointer is returned.

<STRING>
2.14.7 strcat
Declaration:
char *strcat(char *str1, const char *str2);

Appends the string pointed to by str2 to the end of the string pointed to by str1. The terminating null
character of str1 is overwritten. Copying stops once the terminating null character of str2 is copied. If
overlapping occurs, the result is undefined.
The argument str1 is returned.

2.14.8 strncat
Declaration:
char *strncat(char *str1, const char *str2, size_t

n);
Appends the string pointed to by str2 to the end of the string pointed to by str1 up to n characters long. The
terminating null character of str1 is overwritten. Copying stops once n characters are copied or the
terminating null character of str2 is copied. A terminating null character is always appended to str1. If
overlapping occurs, the result is undefined.
The argument str1 is returned.

2.14.10 strcmp
Declaration:
int strcmp(const char *str1, const char *str2);

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


Returns zero if str1 and str2 are equal. Returns less than zero or greater than zero if str1 is less than or greater
than str2 respectively.

2.14.11 strncmp
Declaration:
int strncmp(const char *str1, const char *str2, size_t

n);
Compares at most the first n bytes of str1 and str2. Stops comparing after the null character.
Returns zero if the first n bytes (or null terminated length) of str1 and str2 are equal. Returns less than zero or
greater than zero if str1 is less than or greater than str2 respectively.

2.14.13 strcpy
Declaration:
char *strcpy(char *str1, const char *str2);

Copies the string pointed to by str2 to str1. Copies up to and including the null character of str2. If str1 and
str2 overlap the behavior is undefined.
Returns the argument str1.

2.14.14 strncpy
Declaration:
char *strncpy(char *str1, const char *str2, size_t

n);
Copies up to n characters from the string pointed to by str2 to str1. Copying stops when n characters are copied
or the terminating null character in str2 is reached. If the null character is reached, the null characters are
continually copied to str1 until n characters have been copied.
Returns the argument str1.

2.14.17 strlen
Declaration:
size_t strlen(const char *str);

Computes the length of the string str up to but not including the terminating null character.
Returns the number of characters in the string.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy