0% found this document useful (0 votes)
7 views

c-functions-guide

The document is a comprehensive reference guide for C functions categorized into various sections including Input/Output, String, Math, Character, Memory, Conversion, File Operations, Time, Random Number, and System Functions. Each function is described with its return value, description, and an example of usage. It serves as a quick reference for developers to understand and utilize standard C library functions.

Uploaded by

Bibin K Bino
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 views

c-functions-guide

The document is a comprehensive reference guide for C functions categorized into various sections including Input/Output, String, Math, Character, Memory, Conversion, File Operations, Time, Random Number, and System Functions. Each function is described with its return value, description, and an example of usage. It serves as a quick reference for developers to understand and utilize standard C library functions.

Uploaded by

Bibin K Bino
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

Complete C Functions Reference Guide

Input/Output Functions (<stdio.h>)


Input Functions
int scanf(const char *format, ...)
• Return: Number of inputs successfully read or EOF on error
• Description: Reads formatted input from stdin
• Example: scanf("%d %s", &num, str);
int getchar(void)
• Return: Character read as int, or EOF on error
• Description: Reads single character from stdin
• Example: ch = getchar();
char *gets(char *str)
• Return: str on success, NULL on error
• Description: Reads line from stdin (deprecated, unsafe)
• Example: gets(str);
char *fgets(char *str, int n, FILE *stream)
• Return: str on success, NULL on error
• Description: Safely reads n-1 characters or until newline
• Example: fgets(str, 100, stdin);

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");

String Functions (<string.h>)


size_t strlen(const char *str)

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");

Math Functions (<math.h>)


double sqrt(double x)
• Return: Square root of x
• Description: Calculates square root
• Example: result = sqrt(16.0);
double pow(double x, double y)
• Return: x raised to power y
• Description: Calculates power
• Example: result = pow(2.0, 3.0);

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);

Character Functions (<ctype.h>)


int isalpha(int c)
• Return: Non-zero if c is alphabetic, 0 otherwise
• Description: Checks if character is alphabetic
• Example: if(isalpha('A'))
int isdigit(int c)
• Return: Non-zero if c is digit, 0 otherwise
• Description: Checks if character is digit
• Example: if(isdigit('5'))
int isalnum(int c)
• Return: Non-zero if c is alphanumeric, 0 otherwise
• Description: Checks if character is alphanumeric
• Example: if(isalnum('A'))
int tolower(int c)
• Return: Lowercase equivalent of c
• Description: Converts character to lowercase
• Example: ch = tolower('A');
int toupper(int c)
• Return: Uppercase equivalent of c
• Description: Converts character to uppercase
• Example: ch = toupper('a');

Memory Functions (<stdlib.h>)


void *malloc(size_t size)

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);

Conversion Functions (<stdlib.h>)


int atoi(const char *str)
• Return: Integer value of string
• Description: Converts string to integer
• Example: num = atoi("123");
long atol(const char *str)
• Return: Long value of string
• Description: Converts string to long
• Example: num = atol("123456");
double atof(const char *str)
• Return: Double value of string
• Description: Converts string to double
• Example: num = atof("123.456");

File Operations (<stdio.h>)


FILE *fopen(const char *filename, const char *mode)
• Return: File pointer on success, NULL on failure
• Description: Opens file in specified mode
• Example: fp = fopen("file.txt", "r");
int fclose(FILE *stream)
• Return: 0 on success, EOF on failure

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);

Time Functions (<time.h>)


time_t time(time_t *timer)
• Return: Current calendar time
• Description: Gets current time
• Example: current_time = time(NULL);
struct tm *localtime(const time_t *timer)
• Return: Pointer to tm structure
• Description: Converts time_t to local time
• Example: timeinfo = localtime(&current_time);

Random Number Functions (<stdlib.h>)


void srand(unsigned int seed)
• Return: void
• Description: Seeds random number generator
• Example: srand(time(NULL));
int rand(void)
• Return: Random integer between 0 and RAND_MAX
• Description: Generates random number
• Example: num = rand();

System Functions (<stdlib.h>)


void exit(int status)
• Return: Does not return
• Description: Terminates program
• Example: exit(0);
int system(const char *command)

5
• Return: Command’s exit status
• Description: Executes system command
• Example: system("ls");

You might also like