C Programming 09/10 Flashcards
What is realloc?
Allows you to change the size of a buffer which was previously malloced.
• void* realloc(void* ptr, int size);
What is memcpy?
Copies bytes from one memory area to another area.
• void *memcpy(void *restrict dst, const void *restrict src, size_t n);
What is alignment?
Many types have limitations on the memory address they can be placed at, it results in slower performance and wasted memory
What is getchar?
Function that gets a single character from stdin
• int getchar(void);
• Returns EOF or an unsigned char
What is putchar?
Function that puts a single character to stdout
• int putchar(int c);
• Should provide unsigned char, returns error value
What is fopen?
Opens a file and returns a file stream
• FILE *fopen(const char *fname, const char *mode);
• Returns a stream or NULL
What are the file modes?
- “r” = Open file for reading
- “w” = Open file for writing
- “a” = Open file for appending at end of file (retain existing data)
What is fclose?
Closes file associated with file stream
• int fclose(FILE *fp);
• Returns error value
What is fgetc?
Function that gets a single character from a file stream
• int fgetc(FILE *stream);
What is fputc?
Function that puts a single character to a file stream
• int fputc(int c, FILE *stream);
Why use fgetc/fputc over getchar/putchar?
- fgets/fputs take size parameter so safe
- fgets records \n
- fputs doesn’t put \n