Practical C Programming

Book description

A comprehensive guide with practical instructions for learning data structures, low-level programming, high-performance computing, networking and IoT to help you understand the latest standards in C programming such as C11 and C18

Key Features

  • Tackle various challenges in C programming by making the most of its latest features
  • Understand the workings of arrays, strings, functions, pointers, advanced data structures, and algorithms
  • Become well-versed with process synchronization during multitasking and server-client process communication

Book Description

Used in everything from microcontrollers to operating systems, C is a popular programming language among developers because of its flexibility and versatility. This book helps you get hands-on with various tasks, covering the fundamental as well as complex C programming concepts that are essential for making real-life applications.

You'll start with recipes for arrays, strings, user-defined functions, and pre-processing directives. Once you're familiar with the basic features, you'll gradually move on to learning pointers, file handling, concurrency, networking, and inter-process communication (IPC). The book then illustrates how to carry out searching and arrange data using different sorting techniques, before demonstrating the implementation of data structures such as stacks and queues. Later, you'll learn interesting programming features such as using graphics for drawing and animation, and the application of general-purpose utilities. Finally, the book will take you through advanced concepts such as low-level programming, embedded software, IoT, and security in coding, as well as techniques for improving code performance.

By the end of this book, you'll have a clear understanding of C programming, and have the skills you need to develop robust apps.

What you will learn

  • Discover how to use arrays, functions, and strings to make large applications
  • Perform preprocessing and conditional compilation for efficient programming
  • Understand how to use pointers and memory optimally
  • Use general-purpose utilities and improve code performance
  • Implement multitasking using threads and process synchronization
  • Use low-level programming and the inline assembly language
  • Understand how to use graphics for animation
  • Get to grips with applying security while developing C programs

Who this book is for

This intermediate-level book is for developers who want to become better C programmers by learning its modern features and programming practices. Familiarity with C programming is assumed to get the most out of this book.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Practical C Programming
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewer
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Conventions used
    4. Sections
      1. How to do it…
      2. How it works…
      3. There's more…
      4. See also
    5. Get in touch
      1. Reviews
  7. Working with Arrays
    1. Inserting an element in an array
      1. How to do it…
      2. How it works...
      3. There's more...
    2. Multiplying two matrices
      1. How to do it…
      2. How it works...
      3. There’s more…
    3. Finding the common elements in two arrays
      1. How to do it…
      2. How it works...
    4. Finding the difference between two sets or arrays
      1. How to do it…
      2. How it works...
    5. Finding the unique elements in an array
      1. How to do it… 
      2. How it works...
    6. Finding whether a matrix is sparse
      1. How to do it…
      2. How it works...
      3. There's more...
    7. Merging two sorted arrays into a single array
      1. How to do it…
      2. How it works...
  8. Managing Strings
    1. Determining whether the string is a palindrome 
      1. How to do it…
      2. How it works...
    2. Finding the occurrence of the first repetitive character in a string
      1. How to do it…
      2. How it works...
    3. Displaying the count of each character in a string
      1. How to do it…
      2. How it works...
    4. Counting vowels and consonants in a sentence
      1. How to do it…
      2. How it works...
    5. Converting the vowels in a sentence to uppercase
      1. How to do it…
      2. How it works...
  9. Exploring Functions
    1. What is a stack?
    2. Finding whether a number is an Armstrong number
      1. How to do it…
      2. How it works...
    3. Returning maximum and minimum values in an array
      1. How to do it…
      2. How it works...
    4. Finding the greatest common divisor using recursion
      1. How to do it…
      2. How it works...
    5. Converting a binary number into a hexadecimal number
      1. How to do it…
      2. How it works...
    6. Finding whether a number is a palindrome 
      1. How to do it…
      2. How it works...
  10. Preprocessing and Compilation
    1. Performing conditional compilation with directives
      1. How to do it...
      2. How it works...
    2. Applying assertions for validation
      1. How to do it...
      2. How it works...
    3. Using assertions to ensure a pointer is not pointing to NULL
      1. How to do it…
      2. How it works...
    4. Catching errors early with compile-time assertions
      1. How to do it...
      2. How it works...
    5. Applying stringize and token-pasting operators
      1. How to do it...
      2. How it works...
  11. Deep Dive into Pointers
    1. What is a pointer?
    2. Reversing a string using pointers
      1. How to do it…
      2. How it works...
    3. Finding the largest value in an array using pointers
      1. How to do it…
      2. How it works...
    4. Sorting a singly linked list
      1. How to do it…
      2. How it works...
        1. Creating a singly linked list
        2. Sorting the singly linked list
          1. The first iteration
          2. The second iteration
          3. The third and fourth iterations
    5. Finding the transpose of a matrix using pointers
      1. How to do it…
      2. How it works...
    6. Accessing a structure using a pointer
      1. How to do it…
      2. How it works...
  12. File Handling
    1. Functions used in file handling
      1. Functions commonly used in sequential file handling
        1. fopen()
        2. fclose()
        3. fgets()
        4. fputs()
      2. Functions commonly used in random files
        1. fseek()
        2. ftell()
        3. rewind()
    2. Reading a text file and converting all characters after the period into uppercase
      1. How to do it…
      2. How it works...
    3. Displaying the contents of a random file in reverse order
      1. How to do it…
      2. How it works...
    4. Counting the number of vowels in a file
      1. How to do it…
      2. How it works...
    5. Replacing a word in a file with another word
      1. How to do it…
      2. How it works...
    6. Encrypting a file
      1. How to do it…
      2. How it works...
      3. See also
  13. Implementing Concurrency
    1. What are processes and threads?
      1. Mutual exclusion
    2. Performing a task with a single thread
      1. How to do it…
      2. How it works...
    3. Performing multiple tasks with multiple threads
      1. How to do it…
      2. How it works...
    4. Using mutex to share data between two threads
      1. How to do it…
      2. How it works...
    5. Understanding how a deadlock is created
      1. How to do it…
      2. How it works...
    6. Avoiding a deadlock
      1. How to do it…
      2. How it works...
  14. Networking and Inter-Process Communication
    1. Communicating between processes using pipes
      1. Creating and to connecting processes
        1. pipe()
        2. mkfifo()
        3. write()
        4. read()
        5. perror()
        6. fork()
      2. One process, both writing and reading from the pipe
        1. How to do it…
        2. How it works...
      3. One process writing into the pipe and another process reading from the pipe
        1. How to do it…
        2. How it works...
    2. Communicating between processes using FIFO
      1. Writing data into a FIFO
        1. How to do it…
        2. How it works...
      2. Reading data from a FIFO
        1. How to do it…
        2. How it works...
    3. Communicating between the client and server using socket programming
      1. Client-server model
        1. struct sockaddr_in structure
        2. socket()
        3. memset()
        4. htons()
        5. bind()
        6. listen()
        7. accept()
        8. send()
        9. connect()
        10. recv()
      2. Sending data to the client
        1. How to do it…
        2. How it works...
      3. Reading data that's been sent from the server
        1. How to do it…
        2. How it works...
    4. Communicating between processes using a UDP socket
      1. Using a UDP socket for server-client communication
        1. bzero()
        2. INADDR_ANY
        3. sendto()
        4. recvfrom()
      2. Await a message from the client and sending a reply using a UDP socket
        1. How to do it…
        2. How it works...
      3. Sending a message to the server and receiving the reply from the server using the UDP socket
        1. How to do it…
        2. How it works...
      4. Running Cygserver
    5. Passing a message from one process to another using the message queue
      1. Functions used in IPC using shared memory and message queues
        1. ftok()
        2. shmget()
        3. shmat()
        4. shmdt()
        5. shmctl()
        6. msgget()
        7. msgrcv()
        8. msgsnd()
      2. Writing a message into the message queue
        1. How to do it…
        2. How it works...
      3. Reading a message from the message queue
        1. How to do it…
        2. How it works...
    6. Communicating between processes using shared memory
      1. Writing a message into shared memory
        1. How to do it…
        2. How it works...
      2. Reading a message from shared memory
        1. How to do it…
        2. How it works...
  15. Sorting and Searching
    1. Searching for an item using binary search 
      1. How to do it...
      2. How it works...
    2. Arranging numbers in ascending order using bubble sort
      1. How to do it…
      2. How it works...
    3. Arranging numbers in ascending order using insertion sort
      1. How to do it...
      2. How it works...
    4. Arranging numbers in ascending order using quick sort
      1. How to do it...
        1. Quick sort
        2. FindingPivot
      2. How it works...
    5. Arranging numbers in descending order using heap sort
      1. How to do it...
        1. Creating a max-heap
        2. Deleting the max-heap
      2. How it works...
      3. See also
  16. Working with Graphs
    1. Types of graphs
      1. Directed graphs
      2. Undirected graphs
    2. Creating an adjacency matrix representation of a directed graph
      1. How to do it...
      2. How it works...
    3. Creating an adjacency matrix representation of an undirected graph
      1. How to do it...
      2. How it works...
    4. Creating an adjacency list representation of a directed graph
      1. How to do it...
      2. How it works...
    5. Carrying out the breadth-first traversal of a graph
      1. How to do it...
      2. How it works...
    6. Carrying out the depth-first traversal of a graph
      1. How to do it...
      2. How it works...
    7. Creating minimum spanning trees using Prim's algorithm
      1. How to do it...
      2. How it works...
        1. Displaying the adjacency linked list
        2. Creating the minimum spanning tree
    8. Creating minimum spanning trees using Kruskal's algorithm
      1. How to do it...
      2. How it works...
  17. Advanced Data Structures and Algorithms
    1. Stack
    2. Doubly linked lists (two-way linked lists)
    3. Circular linked lists
    4. Binary tree
      1. Binary search trees
      2. Traversing trees
    5. Implementing a stack using a singly linked list
      1. How to do it...
      2. How it works...
    6. Implementing a doubly or two-way linked list
      1. How to do it...
      2. How it works...
    7. Implementing a circular linked list
      1. How to do it...
      2. How it works...
    8. Creating a binary search tree and performing an inorder traversal on it recursively
      1. How to do it... – binary tree
      2. How to do it... – inorder traversal of the tree
      3. How it works... – binary tree
      4. How it works... – inorder traversal of the tree
    9. Performing postorder traversal of a binary tree non-recursively
      1. Getting started
      2. How to do it...
      3. How it works...
      4. See also
  18. Creativity with Graphics
    1. List of OpenGL functions
    2. Drawing four graphical shapes
      1. How to do it...
      2. How it works...
    3. Drawing a circle
      1. How to do it...
      2. How it works...
    4. Drawing a line between two mouse clicks
      1. How to do it...
      2. How it works...
    5. Making a bar graph of the supplied values
      1. How to do it...
      2. How it works...
    6. Making an animated bouncing ball
      1. How to do it...
      2. How it works...
  19. Using MySQL Database
    1. Functions in MySQL
      1. mysql_init()
      2. mysql_real_connect()
      3. mysql_query()
      4. mysql_use_result()
      5. mysql_fetch_row()
      6. mysql_num_fields()
      7. mysql_free_result()
      8. mysql_close()
    2. Creating a MySQL database and tables
      1. Create database
      2. Create table
    3. Displaying all the built-in tables in a default mysql database
      1. How to do it...
      2. How it works...
    4. Storing information in MySQL database
      1. How to do it…
      2. How it works...
    5. Searching for the desired information in the database
      1. How to do it…
      2. How it works...
    6. Updating information in the database
      1. How to do it…
      2. How it works...
    7. Deleting data from the database using C
      1. How to do it…
      2. How it works...
  20. General-Purpose Utilities
    1. Dynamic memory allocation
      1. malloc()
      2. calloc()
      3. realloc()
      4. free()
    2. Registering a function that is called when a program exits
      1. How to do it…
      2. How it works...
      3. There's more...
    3. Measuring the clock ticks and CPU seconds required in the execution of a function
      1. How to do it...
      2. How it works...
    4. Performing dynamic memory allocation
      1. How to do it...
      2. How it works...
    5. Handling signals
      1. Signals
        1. signal()
      2. How to do it...
      3. How it works...
  21. Improving the Performance of Your Code
    1. Using the register keyword in C code for better efficiency
      1. How to do it...
      2. How it works...
    2. Taking input faster in C
      1. How to do it…
      2. How it works...
    3. Applying loop unrolling for faster results
      1. How to do it…
      2. How it works...
  22. Low-Level Programming
    1. Introduction to bitwise operators
      1. & (binary AND)
      2. | (binary OR)
      3. ^ (binary XOR)
      4. ~ (binary complement)
      5. << (binary shift left)
      6. >> (binary shift right)
    2. Converting a binary number into a decimal using a bitwise operator
      1. How to do it…
      2. How it works...
    3. Converting a decimal into binary using a bitwise operator
      1. How to do it…
      2. How it works...
    4. Converting a decimal number into binary using bit masking 
      1. How to do it…
      2. How it works...
    5. Introduction to assembly coding
    6. Multiplying two numbers using the inline assembly language in C
      1. How to do it…
      2. How it works...
    7. Dividing two numbers using assembly code in C
      1. How to do it…
      2. How it works...
  23. Embedded Software and IoT
    1. Technical requirements
    2. Introduction to embedded systems
    3. Introduction to IoT
      1. Introduction to Arduino
    4. Toggling the port of a microcontroller in Embedded C (blinking LED)
      1. How to do it…
      2. How it works...
    5. Incrementing the value of a port in Embedded C
      1. How to do it…
      2. How it works...
    6. Toggle voltage in output pin using Arduino (blinking LED)
      1. How to do it…
      2. How it works...
    7. Taking input from the serial port using Arduino
      1. How to do it…
      2. How it works...
    8. Sensing the temperature using Arduino with the LM35 sensor
      1. Getting ready...
      2. How to do it...
      3. How it works...
  24. Applying Security in Coding
    1. Buffer overflow
      1. gets()
      2. fgets()
      3. fpurge(stdin)
      4. sprintf()
      5. snprintf()
      6. strcpy()
      7. strncpy( )
    2. Understanding how a buffer overflow occurs
      1. How to do it...
      2. How it works...
    3. Learning how to avoid a buffer overflow 
      1. How to do it...
      2. How it works...
    4. Understanding how a vulnerability occurs while copying strings
      1. How to do it...
      2. How it works...
    5. Learning how to write secure code while copying strings
      1. How to do it...
      2. How it works...
    6. Understanding the errors that occur while formatting strings
      1. How to do it...
      2. How it works...
    7. Learning how to avoid errors while formatting strings
      1. How to do it...
      2. How it works...
    8. Understanding how vulnerabilities occur while accessing files in C
      1. How to do it...
      2. How it works...
    9. Learning how to avoid vulnerabilities while writing a file in C
      1. How to do it...
      2. How it works...
  25. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Practical C Programming
  • Author(s): B. M. Harwani
  • Release date: February 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781838641108