0% found this document useful (0 votes)
7 views6 pages

2021w Man

The document provides an overview of several C standard library functions related to file and memory management, including functions for checking stream status (feof, ferror, clearerr, fileno), memory allocation (malloc, calloc, free, realloc), and formatted output (printf family). Each function is described with its purpose, syntax, return values, and potential errors. Additionally, it includes information on thread management functions like pthread_create and pthread_join, detailing their usage and behavior.

Uploaded by

Errorlevel 88
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)
7 views6 pages

2021w Man

The document provides an overview of several C standard library functions related to file and memory management, including functions for checking stream status (feof, ferror, clearerr, fileno), memory allocation (malloc, calloc, free, realloc), and formatted output (printf family). Each function is described with its purpose, syntax, return values, and potential errors. Additionally, it includes information on thread management functions like pthread_create and pthread_join, detailing their usage and behavior.

Uploaded by

Errorlevel 88
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/ 6

feof/ferror/fileno(3) feof/ferror/fileno(3) fflush(3) fflush(3)

NAME NAME
clearerr, feof, ferror, fileno − check and reset stream status fflush − flush a stream
SYNOPSIS SYNOPSIS
#include <stdio.h> #include <stdio.h>
int fflush(FILE *stream);
void clearerr(FILE *stream);
int feof(FILE *stream); DESCRIPTION
int ferror(FILE *stream); For output streams, fflush() forces a write of all user-space buffered data for the given output or update
int fileno(FILE *stream); stream via the stream’s underlying write function.
DESCRIPTION For input streams associated with seekable files (e.g., disk files, but not pipes or terminals), fflush() dis-
The function clearerr() clears the end-of-file and error indicators for the stream pointed to by stream. cards any buffered data that has been fetched from the underlying file, but has not been consumed by the
application.
The function feof() tests the end-of-file indicator for the stream pointed to by stream, returning non-zero if
it is set. The end-of-file indicator can only be cleared by the function clearerr(). The open status of the stream is unaffected.
The function ferror() tests the error indicator for the stream pointed to by stream, returning non-zero if it is If the stream argument is NULL, fflush() flushes all open output streams.
set. The error indicator can only be reset by the clearerr() function. For a nonlocking counterpart, see unlocked_stdio(3).
The function fileno() examines the argument stream and returns its integer descriptor. RETURN VALUE
For non-locking counterparts, see unlocked_stdio(3). Upon successful completion 0 is returned. Otherwise, EOF is returned and errno is set to indicate the
error.
ERRORS
These functions should not fail and do not set the external variable errno. (However, in case fileno() ERRORS
detects that its argument is not a valid stream, it must return −1 and set errno to EBADF.) EBADF
stream is not an open stream, or is not open for writing.
SEE ALSO
open(2), fdopen(3), stdio(3), unlocked_stdio(3) The function fflush() may also fail and set errno for any of the errors specified for write(2).
SEE ALSO
fsync(2), sync(2), write(2), fclose(3), fileno(3), fopen(3), setbuf(3), unlocked_stdio(3)

BS-Klausur Manual-Auszug 2022-03-02 1 BS-Klausur Manual-Auszug 2022-03-02 1


fopen/fdopen/fileno(3) fopen/fdopen/fileno(3) getc/fgets/putc/fputs(3) getc/fgets/putc/fputs(3)

NAME NAME
fopen, fdopen, fileno − stream open functions fgetc, fgets, getc, getchar, fputc, fputs, putc, putchar − input and output of characters and strings
SYNOPSIS SYNOPSIS
#include <stdio.h> #include <stdio.h>

FILE *fopen(const char * path, const char *mode); int fgetc(FILE *stream);
FILE *fdopen(int fildes, const char *mode); char *fgets(char *s, int size, FILE *stream);
int fileno(FILE *stream); int getc(FILE *stream);
int fclose(FILE *stream); int getchar(void);
DESCRIPTION
int fputc(int c, FILE *stream);
The fopen function opens the file whose name is the string pointed to by path and associates a stream with int fputs(const char *s, FILE *stream);
it. int putc(int c, FILE *stream);
int putchar(int c);
The argument mode points to a string beginning with one of the following sequences (Additional characters
DESCRIPTION
may follow these sequences.):
fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on
r Open text file for reading. The stream is positioned at the beginning of the file. end of file or error.
r+ Open for reading and writing. The stream is positioned at the beginning of the file. getc() is equivalent to fgetc() except that it may be implemented as a macro which evaluates stream more
w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning than once.
of the file. getchar() is equivalent to getc(stdin).
w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to
stream is positioned at the beginning of the file. by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A ’\0’ is
a Open for appending (writing at end of file). The file is created if it does not exist. The stream is stored after the last character in the buffer.
positioned at the end of the file. fputc() writes the character c, cast to an unsigned char, to stream.
a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. fputs() writes the string s to stream, without its terminating null byte ('\0').
The stream is positioned at the end of the file.
putc() is equivalent to fputc() except that it may be implemented as a macro which evaluates stream more
The fdopen function associates a stream with the existing file descriptor, fildes. The mode of the stream than once.
(one of the values "r", "r+", "w", "w+", "a", "a+") must be compatible with the mode of the file descriptor.
The file position indicator of the new stream is set to that belonging to fildes, and the error and end-of-file putchar(c); is equivalent to putc(c, stdout).
indicators are cleared. Modes "w" or "w+" do not cause truncation of the file. The file descriptor is not Calls to the functions described here can be mixed with each other and with calls to other output functions
dup’ed, and will be closed when the stream created by fdopen is closed. The result of applying fdopen to a from the stdio library for the same output stream.
shared memory object is undefined.
The function fileno() examines the argument stream and returns its integer descriptor. RETURN VALUE
fgetc(), getc() and getchar() return the character read as an unsigned char cast to an int or EOF on end of
The fclose() function flushes the stream pointed to by stream (writing any buffered output data using file or error.
fflush(3)) and closes the underlying file descriptor.
fgets() returns s on success, and NULL on error or when end of file occurs while no characters have been
RETURN VALUE
read. fputc(), putc() and putchar() return the character written as an unsigned char cast to an int or EOF
Upon successful completion fopen, fdopen and freopen return a FILE pointer. Otherwise, NULL is on error.
returned and the global variable errno is set to indicate the error. Upon successful completion of fclose, 0
is returned. Otherwise, EOF is returned and errno is set to indicate the error. fputs() returns a nonnegative number on success, or EOF on error.
ERRORS SEE ALSO
EINVAL read(2), write(2), ferror(3), fgetwc(3), fgetws(3), fopen(3), fread(3), fseek(3), getline(3), getwchar(3),
The mode provided to fopen, fdopen, or freopen was invalid. scanf(3), ungetwc(3), write(2), ferror(3), fopen(3), fputwc(3), fputws(3), fseek(3), fwrite(3), gets(3),
EBADF putwchar(3), scanf(3), unlocked_stdio(3)
The file descriptor underlying stream passed to fclose is not valid.
The fopen, fdopen and freopen functions may also fail and set errno for any of the errors specified for the
routine malloc(3).
The fopen function may also fail and set errno for any of the errors specified for the routine open(2).
The fdopen function may also fail and set errno for any of the errors specified for the routine fcntl(2).

BS-Klausur Manual-Auszug 2022-03-02 1 BS-Klausur Manual-Auszug 2022-03-02 1


malloc(3) malloc(3) printf/sprintf(3) printf/sprintf(3)

NAME NAME
calloc, malloc, free, realloc − Allocate and free dynamic memory printf, fprintf, sprintf, snprintf, vprintf, vfprintf, vsprintf, vsnprintf − formatted output conversion
SYNOPSIS SYNOPSIS
#include <stdlib.h> #include <stdio.h>

void *calloc(size_t nmemb, size_t size); int printf(const char * format, ...);
void *malloc(size_t size); int fprintf(FILE *stream, const char * format, ...);
void free(void *ptr); int sprintf(char *str, const char * format, ...);
void *realloc(void *ptr, size_t size); int snprintf(char *str, size_t size, const char * format, ...);
DESCRIPTION
...
calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the DESCRIPTION
allocated memory. The memory is set to zero. The functions in the printf() family produce output according to a format as described below. The function
malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared. printf() writes output to stdout, the standard output stream; fprintf() writes output to the given output
stream; sprintf() and snprintf(), write to the character string str.
free() frees the memory space pointed to by ptr, which must have been returned by a previous call to mal-
loc(), calloc() or realloc(). Otherwise, or if free( ptr) has already been called before, undefined behaviour The function snprintf() writes at most size bytes (including the trailing null byte ('\0')) to str.
occurs. If ptr is NULL, no operation is performed. These functions write the output under the control of a format string that specifies how subsequent argu-
realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be ments (or arguments accessed via the variable-length argument facilities of stdarg(3)) are converted for
unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr output.
is NULL, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free( ptr). Return value
Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). Upon successful return, these functions return the number of characters printed (not including the trailing
RETURN VALUE
'\0' used to end output to strings).
For calloc() and malloc(), the value returned is a pointer to the allocated memory, which is suitably aligned The functions snprintf() and vsnprintf() do not write more than size bytes (including the trailing '\0'). If
for any kind of variable, or NULL if the request fails. the output was truncated due to this limit then the return value is the number of characters (not including
free() returns no value. the trailing '\0') which would have been written to the final string if enough space had been available. Thus,
a return value of size or more means that the output was truncated.
realloc() returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable
and may be different from ptr, or NULL if the request fails. If size was equal to 0, either NULL or a If an output error is encountered, a negative value is returned.
pointer suitable to be passed to free() is returned. If realloc() fails the original block is left untouched - it is Format of the format string
not freed or moved. The format string is a character string, beginning and ending in its initial shift state, if any. The format
string is composed of zero or more directives: ordinary characters (not %), which are copied unchanged to
the output stream; and conversion specifications, each of which results in fetching zero or more subsequent
arguments. Each conversion specification is introduced by the character %, and ends with a conversion
specifier. In between there may be (in this order) zero or more flags, an optional minimum field width, an
optional precision and an optional length modifier.

The conversion specifier


A character that specifies the type of conversion to be applied. An example for a conversion specifier is:
o, u, x, X
The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned
hexadecimal (x and X) notation.

s The const char * argument is expected to be a pointer to an array of character type (pointer to a
string). Characters from the array are written up to (but not including) a terminating null byte
('\0'); if a precision is specified, no more than the number specified are written. If a precision is
given, no null byte need be present; if the precision is not specified, or is greater than the size of
the array, the array must contain a terminating null byte.

SEE ALSO
printf(1), asprintf(3), dprintf(3), scanf(3), setlocale(3), wcrtomb(3), wprintf(3), locale(5)

BS-Klausur Manual-Auszug 2022-03-02 1 BS-Klausur Manual-Auszug 2022-03-02 1


pthread_create(3) pthread_create(3) pthread_join(3) pthread_join(3)

NAME NAME
pthread_create − create a new thread / pthread_exit − terminate the calling thread pthread_join − join with a terminated thread
SYNOPSIS
SYNOPSIS #include <pthread.h>
#include <pthread.h> int pthread_join(pthread_t thread, void **retval);
int pthread_create(pthread_t * thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * Compile and link with −pthread.
arg); DESCRIPTION
The pthread_join() function waits for the thread specified by thread to terminate. If that thread has
void pthread_exit(void *retval); already terminated, then pthread_join() returns immediately. The thread specified by thread must be join-
able.
DESCRIPTION If retval is not NULL, then pthread_join() copies the exit status of the target thread (i.e., the value that the
pthread_create creates a new thread of control that executes concurrently with the calling thread. The new target thread supplied to pthread_exit(3)) into the location pointed to by retval. If the target thread was
thread applies the function start_routine passing it arg as first argument. The new thread terminates either canceled, then PTHREAD_CANCELED is placed in the location pointed to by retval.
explicitly, by calling pthread_exit(3), or implicitly, by returning from the start_routine function. The latter
case is equivalent to calling pthread_exit(3) with the result returned by start_routine as exit code. If multiple threads simultaneously try to join with the same thread, the results are undefined. If the thread
calling pthread_join() is canceled, then the target thread will remain joinable (i.e., it will not be detached).
The attr argument specifies thread attributes to be applied to the new thread. See pthread_attr_init(3) for a RETURN VALUE
complete list of thread attributes. The attr argument can also be NULL, in which case default attributes are On success, pthread_join() returns 0; on error, it returns an error number.
used: the created thread is joinable (not detached) and has default (non real-time) scheduling policy. ERRORS
EDEADLK
pthread_exit terminates the execution of the calling thread. All cleanup handlers that have been set for the A deadlock was detected (e.g., two threads tried to join with each other); or thread specifies the
calling thread with pthread_cleanup_push(3) are executed in reverse order (the most recently pushed han- calling thread.
dler is executed first). Finalization functions for thread-specific data are then called for all keys that have
non- NULL values associated with them in the calling thread (see pthread_key_create(3)). Finally, exe- EINVAL
cution of the calling thread is stopped. thread is not a joinable thread.
EINVAL
The retval argument is the return value of the thread. It can be consulted from another thread using Another thread is already waiting to join with this thread.
pthread_join(3).
ESRCH
No thread with the ID thread could be found.
RETURN VALUE
NOTES
On success, the identifier of the newly created thread is stored in the location pointed by the thread argu-
ment, and a 0 is returned. On error, a non-zero error code is returned. After a successful call to pthread_join(), the caller is guaranteed that the target thread has terminated. The
caller may then choose to do any clean-up that is required after termination of the thread (e.g., freeing
The pthread_exit function never returns. memory or other resources that were allocated to the target thread).
Joining with a thread that has previously been joined results in undefined behavior.
Failure to join with a thread that is joinable (i.e., one that is not detached), produces a "zombie thread".
ERRORS Avoid doing this, since each zombie thread consumes some system resources, and when enough zombie
EAGAIN threads have accumulated, it will no longer be possible to create new threads (or processes).
not enough system resources to create a process for the new thread. There is no pthreads analog of waitpid(-1, &status, 0), that is, "join with any terminated thread". If you
EAGAIN believe you need this functionality, you probably need to rethink your application design.
more than PTHREAD_THREADS_MAX threads are already active. All of the threads in a process are peers: any thread can join with any other thread in the process.
EXAMPLE
AUTHOR
See pthread_create(3).
Xavier Leroy <Xavier.Leroy@inria.fr>
SEE ALSO
pthread_cancel(3), pthread_create(3), pthread_detach(3), pthread_exit(3), pthreads(7)
SEE ALSO
pthread_join(3), pthread_detach(3), pthread_attr_init(3).

BS-Klausur Manual-Auszug 2022-03-02 1 BS-Klausur Manual-Auszug 2022-03-02 1


queue(3) queue(3) string(3) string(3)

NAME NAME
qCreate, qPut, qGet, qDestroy − A synchronized queue implementation strcat, strchr, strcmp, strcpy, strdup, strlen, strncat, strncmp, strncpy, strstr, strtok − string operations
SYNOPSIS SYNOPSIS
#include ’’queue.h’’ #include <string.h>
char *strcat(char *dest, const char *src);
QUEUE *qCreate(); Append the string src to the string dest, returning a pointer dest.
void qPut(QUEUE * q, void * value);
void* qGet(QUEUE * q); char *strchr(const char *s, int c);
void qDestroy(QUEUE * q); Return a pointer to the first occurrence of the character c in the string s.
DESCRIPTION int strcmp(const char *s1, const char *s2);
Bounded-buffer-based implementation of a FIFO queue with a capacity of 1024 entries. Manages void* Compare the strings s1 with s2. It returns an integer less than, equal to, or greater than zero if s1 is
and is internally synchronized to support multiple concurrent readers and writers. Provides the following found, respectively, to be less than, to match, or be greater than s2.
functions: char *strcpy(char *dest, const char *src);
qCreate() creates a new queue for up to 1024 elements. If an error occurs during the initialization, the Copy the string src to dest, returning a pointer to the start of dest.
implementation frees all resources already allocated by then and returns NULL. char *strdup(const char *s);
Return a duplicate of the string s in memory allocated using malloc(3).
qPut() stores the value in the queue. If the buffer is full (i.e., it currently contains 1024 elements), the call
to qPut() blocks until the value can be stored. size_t strlen(const char *s);
Return the length of the string s.
qGet() returns the next value from the queue. If the buffer is empty, the call blocks until a value is avail-
able. char *strncat(char *dest, const char *src, size_t n);
Append at most n characters from the string src to the string dest, returning a pointer to dest.
Both qPut() and qGet() are synchronized internally and thus can be called concurrently without the need
for further synchronization. int strncmp(const char *s1, const char *s2, size_t n);
Compare at most n bytes of the strings s1 and s2. It returns an integer less than, equal to, or
qDestroy() releases any resources related to the queue itself. It does not call free() on the elements stored greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
in the buffer.
char *strncpy(char *dest, const char *src, size_t n);
RETURN VALUE
Copy at most n bytes from string src to dest, returning a pointer to the start of dest.
qCreate() returns a pointer to the allocated queue. On error, NULL is returned and errno is set appropri-
ately. char *strstr(const char *haystack, const char *needle);
Find the first occurrence of the substring needle in the string haystack, returning a pointer to the
qPut() returns no value. found substring.
qGet() returns the next value stored in the queue. char *strtok(char *s, const char *delim);
qDestroy() returns no value. Extract tokens from the string s that are delimited by one of the bytes in delim.
DESCRIPTION
The string functions perform operations on null-terminated strings.

BS-Klausur Manual-Auszug 2022-03-02 1 BS-Klausur Manual-Auszug 2022-03-02 1


triangle(3) triangle(3)

NAME
countPoints, parseTriangle − count the number of integer coordinates on the boundary of and inside the tri-
angle
SYNOPSIS
#include ’’triangle.h’’
bool parseTriangle(char *line, struct triangle *tri)
void countPoints(const struct triangle *tri, int* boundary, int* interior);
DESCRIPTION
parseTriangle() parses the input line and stores the individual triangle coordinates into the memory pointed
to by tri. The input line is assumed be in the format (x1,y1),(x2,y2),(x3,y3). In case all six integral coordi-
nates are found in line, parseTriangle() returns true, in case of error (e.g., wrong line format, invalid num-
bers) false is returned.
Given a triangle tri with all corners on integer coordinates (see struct coordinate), countPoints() counts
the number of points (on integer coordinates) on the boundary of the triangle and the number of points
inside the triangle.
The parameters boundary and interior are output parameters that receive the number of points found on the
boundary and inside the triangle, respectively.
The struct coordinate represents a two-dimensional coordinate in the Cartesian coordinate system. The
struct triangle stores the three coordinates that make up a triangle.
struct coordinate {
int x;
int y;
};

struct triangle {
struct coordinate point[3];
};

RETURN VALUES
parseTriangle() returns true in case the line was successfully and completely parsed, false in case of error.
The countPoints() function returns no value.

BS-Klausur Manual-Auszug 2022-03-02 1

You might also like