Quick Lessons

Overview of Unix Commands for C Development

Terminal showing Unix commands for C development

TL;DR

A quick reference of Unix commands for managing C projects: sed to convert leading spaces to tabs, dos2unix to fix Windows line endings, style checkers to enforce coding standards, and gcc and gdb to compile with warnings and debug.

On this page

This document provides an explanation of various Unix commands used in the management and development of C software projects, highlighting their purposes and usage contexts.

File Formatting and Conversion

Convert spaces to tabs in C and header files:

sed -i 's/^[ ]\{4\}/\t/' *.c *.h
^[ \t]+

This command uses ‘sed’ to replace the first four spaces at the beginning of each line with a tab in all .c and .h files, modifying files in-place.

Convert DOS to Unix line endings in C and header files:

dos2unix *.c *.h

Applies the ‘dos2unix’ utility to convert line endings from CRLF (Carriage Return + Line Feed) to LF (Line Feed), which is standard in Unix/Linux environments.

Apply global DOS to Unix conversion and space-to-tab replacement:

find . -type f -exec dos2unix {} +
find . -type f -exec sed -i 's/^[ ]\{4\}/\t/' {} +

Runs ‘dos2unix’ and ‘sed’ on every file in the directory and subdirectories, converting line endings and replacing leading spaces with tabs, respectively.

Exclude .git directory during formatting:

find . -path './.git' -prune -o -type f -exec sed -i 's/^[ ]\{4\}/\t/' {} +
find . -path './.git' -prune -o -type f -exec dos2unix {} +

Specifically avoids modifying files within the ‘.git’ directory while performing the same operations as above.

Code Style Enforcement

Check coding style of all C and header files:

betty *.c *.h

Applies ‘Betty’ style checks to all .c and .h files in the directory, enforcing consistency and standards across the project.

Compilation and Debugging

Compile all C files into an executable:

gcc -Wall -Werror -Wextra -pedantic -std=gnu89 *.c -o shell

Compiles all .c files with strict compiler warnings and standards enforced, creating an executable named ‘shell’.

Memory leak check with Valgrind:

valgrind --leak-check=full ./shell

Runs the compiled ‘shell’ executable through ‘Valgrind’ with a full leak check enabled, identifying any memory management issues.

Key takeaways 5

  1. sed can convert leading spaces to tabs across .c and .h files.
  2. dos2unix converts CRLF line endings to Unix LF.
  3. Style checkers keep a C codebase consistent.
  4. Compile with gcc using strict warning flags.
  5. Use gdb and valgrind to debug and find memory errors.

Watch & learn

Basic UNIX CommandsGeorge Mason Bioinformatics · YouTube

Frequently asked questions

How do I convert Windows line endings to Unix?

Run dos2unix on the files, for example dos2unix *.c *.h, which converts CRLF (\r\n) endings to LF (\n).

What gcc flags should I use for C projects?

Common strict flags are -Wall -Werror -Wextra -pedantic, often with a standard such as -std=gnu89 or -std=c99, to catch mistakes early.

How do I debug a C program on Unix?

Compile with -g to include debug symbols, then run it in gdb to set breakpoints and inspect variables, and use valgrind to detect memory leaks.

Quick Lessons#c programming#unix#command line#gcc#sed#developer tools

Comments

No comments yet. Start the conversation.

Comments are reviewed before they appear. Be kind; one link max.

Learn it hands-on at AL Academy

Free practical programs in software, automation and IT, with labs and certificates.

Open AL Academy ↗
Keep reading

Related articles