c-functions-guide
c-functions-guide
Output Functions
int printf(const char *format, ...)
• Return: Number of characters printed or negative value on error
• Description: Prints formatted output to stdout
• Example: printf("Value: %d", num);
int putchar(int char)
• Return: Character written as int, or EOF on error
• Description: Writes single character to stdout
• Example: putchar('A');
int puts(const char *str)
• Return: Non-negative on success, EOF on error
• Description: Writes string and newline to stdout
• Example: puts("Hello");
1
• Return: Length of string
• Description: Calculates string length excluding null terminator
• Example: len = strlen(str);
char *strcpy(char *dest, const char *src)
• Return: Pointer to dest string
• Description: Copies src string to dest
• Example: strcpy(dest, src);
char *strncpy(char *dest, const char *src, size_t n)
• Return: Pointer to dest string
• Description: Copies up to n characters from src to dest
• Example: strncpy(dest, src, 5);
char *strcat(char *dest, const char *src)
• Return: Pointer to dest string
• Description: Concatenates src to end of dest
• Example: strcat(dest, src);
int strcmp(const char *str1, const char *str2)
• Return: 0 if equal, <0 if str1<str2, >0 if str1>str2
• Description: Compares two strings
• Example: if(strcmp(str1, str2) == 0)
char *strchr(const char *str, int c)
• Return: Pointer to first occurrence of c, NULL if not found
• Description: Finds first occurrence of character in string
• Example: ptr = strchr(str, 'a');
char *strstr(const char *haystack, const char *needle)
• Return: Pointer to first occurrence of needle, NULL if not found
• Description: Finds first occurrence of substring
• Example: ptr = strstr(str, "hello");
2
double ceil(double x)
• Return: Smallest integer value greater than or equal to x
• Description: Rounds up to nearest integer
• Example: result = ceil(3.7);
double floor(double x)
• Return: Largest integer value less than or equal to x
• Description: Rounds down to nearest integer
• Example: result = floor(3.7);
double fabs(double x)
• Return: Absolute value of x
• Description: Calculates absolute value
• Example: result = fabs(-5.5);
3
• Return: Pointer to allocated memory, NULL on failure
• Description: Allocates specified bytes of memory
• Example: ptr = malloc(sizeof(int) * n);
void *calloc(size_t num, size_t size)
• Return: Pointer to allocated memory, NULL on failure
• Description: Allocates and zeroes specified memory
• Example: ptr = calloc(n, sizeof(int));
void *realloc(void *ptr, size_t size)
• Return: Pointer to reallocated memory, NULL on failure
• Description: Reallocates memory block
• Example: ptr = realloc(ptr, new_size);
void free(void *ptr)
• Return: void
• Description: Frees allocated memory
• Example: free(ptr);
4
• Description: Closes file stream
• Example: fclose(fp);
size_t fread(void *ptr, size_t size, size_t count, FILE *stream)
• Return: Number of elements successfully read
• Description: Reads data from file
• Example: fread(buffer, sizeof(char), 100, fp);
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
• Return: Number of elements successfully written
• Description: Writes data to file
• Example: fwrite(buffer, sizeof(char), 100, fp);
5
• Return: Command’s exit status
• Description: Executes system command
• Example: system("ls");