0%

Book Description

A comprehensive guide with curated recipes to help you gain a deeper understanding of modern C.

Key Features

  • Learn how to make your applications swift and robust by leveraging powerful features of C
  • Understand the workings of arrays, strings, functions, and more down to how they operate in memory
  • Master process synchronization during multi-tasking and server-client process communication

Book Description

C is a high-level language that's popular among developers. It enables you to write drivers for different devices, access machine-level hardware, apply dynamic memory allocation, and much more. With self-contained tutorials, known as recipes, this book will guide you in dealing with C and its idiosyncrasies and help you benefit from its latest features.

Beginning with common tasks, each recipe addresses a specific problem followed by explaining the solution to get you acquainted with what goes on under the hood. You will explore core concepts of the programming language, including how to work with strings, pointers, and single and multi-dimensional arrays. You will also learn how to break a large application into small modules by creating functions, handling files, and using a database. Finally, the book will take you through advanced concepts such as concurrency and interprocess communication.

By the end of this book, you'll have a clear understanding and deeper knowledge of C programming, which will help you become a better developer.

What you will learn

  • Manipulate single and multi-dimensional arrays
  • Perform complex operations on strings
  • Understand how to use pointers and memory optimally
  • Discover how to use arrays, functions, and strings to make large applications
  • Implement multitasking using threads and process synchronization
  • Establish communication between two or more processes using different techniques
  • Store simple text in files and store data in a database

Who this book is for

If you're a programmer with basic experience in C and want to leverage its features through modern programming practices, then this book is for you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. C Programming Cookbook
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  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. Section 1: Arrays, Strings, and Functions
  8. 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...
  9. 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...
  10. 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...
  11. Section 2: Pointers and Files
  12. 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...
  13. 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...
  14. Section 3: Concurrency, Networking, and Databases
  15. 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...
  16. Networking and Interprocess 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...
  17. 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...
  18. Appendix A
    1. Creating a sequential file and entering some data into it
      1. How to do it…
      2. How it works...
    2. Reading content from a sequential file and displaying it onscreen
      1. How to do it…
      2. How it works...
    3. Creating a random file and entering some data into it
      1. How to do it…
      2. How it works...
    4. Reading content from a random file and displaying it onscreen
      1. How to do it…
      2. How it works...
    5. Decrypting the contents of an encrypted file
      1. How to do it…
      2. How it works...
  19. Appendix B
    1. Installing Cygwin
  20. Appendix C
    1. Installing MySQL Server
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think