C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Overview
  • 1. Random Number Generation
  • a. rand (Generate Random Number):
  • b. srand (Seed for Random Number Generator):
  • 2. Memory Allocation and Deallocation
  • a. malloc (Memory Allocation):
  • b. calloc (Contiguous Memory Allocation):
  • c. realloc (Reallocate Memory):
  • d. free (Free Allocated Memory):
  • 3. System Utilities
  • a. system (Execute System Command):
  • 4. Date and Time Functions
  • a. time (Get Current Time):
  • b. ctime (Convert Time to String):
  • 5. Conversion Functions
  • a. atoi (String to Integer):
  • b. atof (String to Double):
  • c. itoa (Integer to String):

Was this helpful?

  1. The Standard C Library

Utility Functions in C

Overview

Utility functions in C serve various purposes, providing functionalities beyond basic arithmetic and mathematical operations. These functions, often included in standard libraries, offer solutions for common programming tasks. Here's an overview of some commonly used utility functions in C:

1. Random Number Generation

a. rand (Generate Random Number):

  • Generates a pseudo-random integer.

    #include <stdlib.h>
    int rand(void);

b. srand (Seed for Random Number Generator):

  • Seeds the random number generator.

    #include <stdlib.h>
    void srand(unsigned int seed);

2. Memory Allocation and Deallocation

a. malloc (Memory Allocation):

  • Allocates a block of memory of specified size.

    #include <stdlib.h>
    void *malloc(size_t size);

b. calloc (Contiguous Memory Allocation):

  • Allocates a block of memory for an array.

    #include <stdlib.h>
    void *calloc(size_t num, size_t size);

c. realloc (Reallocate Memory):

  • Changes the size of a previously allocated block of memory.

    #include <stdlib.h>
    void *realloc(void *ptr, size_t size);

d. free (Free Allocated Memory):

  • Deallocates the memory block.

    #include <stdlib.h>
    void free(void *ptr);

3. System Utilities

a. system (Execute System Command):

  • Executes a shell command.

    #include <stdlib.h>
    int system(const char *command);

4. Date and Time Functions

a. time (Get Current Time):

  • Returns the current calendar time.

    #include <time.h>
    time_t time(time_t *timer);

b. ctime (Convert Time to String):

  • Converts the time to a string representation.

    #include <time.h>
    char *ctime(const time_t *timer);

5. Conversion Functions

a. atoi (String to Integer):

  • Converts a string to an integer.

    #include <stdlib.h>
    int atoi(const char *str);

b. atof (String to Double):

  • Converts a string to a double.

    #include <stdlib.h>
    double atof(const char *str);

c. itoa (Integer to String):

  • Converts an integer to a string.

    #include <stdlib.h>
    char *itoa(int value, char *str, int base);

These utility functions contribute to the versatility of C programming, allowing developers to address diverse requirements in their applications.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousMath Functions in C

Was this helpful?