How to use the command 'indent' (with examples)

How to use the command 'indent' (with examples)

  • Osx
  • December 25, 2023

The indent command is used to change the appearance of a C/C++ program by inserting or deleting whitespace. It allows you to format C/C++ source code according to different styles and configurations.

Use case 1: Format C/C++ source according to the Berkeley style

Code:

indent path/to/source_file.c path/to/indented_file.c -nbad -nbap -bc -br -c33 -cd33 -cdb -ce -ci4 -cli0 -di16 -fc1 -fcb -i4 -ip -l75 -lp -npcs -nprs -psl -sc -nsob -ts8

Motivation: The Berkeley style is a widely-used programming style that enforces consistent and readable code formatting. By formatting your C/C++ source code according to the Berkeley style, you can improve code readability and maintainability.

Explanation:

  • -nbad: No blank lines after definitions
  • -nbap: No blank lines after procedures
  • -bc: Comment delimiters on a separate line
  • -br: Braces on same line as control statement
  • -c33: Maximum line length set to 33
  • -cd33: Maximum comment line length set to 33
  • -cdb: Comment delimiter at the beginning of each comment line
  • -ce: Comment delimiters at the end of each comment line
  • -ci4: Indent with 4 spaces
  • -cli0: No maximum number of continuation line indentation levels
  • -di16: Indentation of preprocessor directives set to 16 spaces
  • -fc1: Force opening braces to be attached to the control statement
  • -fcb: Opening brace gets put on same line as closing brace
  • -i4: Indent with 4 spaces (if -ci is not specified)
  • -ip: Indent preprocessor directives just like any other line of code
  • -l75: Maximum line length set to 75
  • -lp: Long lines can be broken by indent
  • -npcs: No spaces inside parentheses and function arguments
  • -nprs: No space after cast parens or function calls
  • -psl: Start the arguments of a function or a struct on a new line
  • -sc: Comment indentation set to 0
  • -nsob: Maximum number of spaces before open brace set to 8
  • -ts8: Tabs are 8 characters wide

Example output:

#include <stdio.h>

int main() {
    int a = 5;
    int b = 10;
    int sum = a + b;

    printf("The sum of %d and %d is %d\n", a, b, sum);

    return 0;
}

Use case 2: Format C/C++ source according to the style of Kernighan & Ritchie (K&R)

Code:

indent path/to/source_file.c path/to/indented_file.c -nbad -bap -nbc -br -c33 -cd33 -ncdb -ce -ci4 -cli0 -cs -d0 -di1 -nfc1 -nfcb -i4 -nip -l75 -lp -npcs -nprs -npsl -nsc -nsob

Motivation: The Kernighan & Ritchie (K&R) style is a popular coding style for C, based on the book “The C Programming Language” by Brian Kernighan and Dennis Ritchie. It emphasizes simplicity and readability. By formatting your code according to the K&R style, you can make it easier for others to understand and maintain your code.

Explanation:

  • -nbad: No blank lines after definitions
  • -bap: Blank lines after procedures
  • -nbc: No brace braces on a separate line
  • -br: Braces on same line as control statement
  • -c33: Maximum line length set to 33
  • -cd33: Maximum comment line length set to 33
  • -ncdb: No comment delimiter at the beginning of each comment line
  • -ce: Comment delimiters at the end of each comment line
  • -ci4: Indent with 4 spaces
  • -cli0: No maximum number of continuation line indentation levels
  • -cs: Attach a star to other comments
  • -d0: Do not add or delete spaces around parens in declarations
  • -di1: Decrease indentation by 1 space for places like comments
  • -nfc1: Force opening braces to be attached to the control statement
  • -nfcb: Opening brace gets put on previous line
  • -i4: Indent with 4 spaces (if -ci is not specified)
  • -nip: Indent preprocessor directives just like any other line of code
  • -l75: Maximum line length set to 75
  • -lp: Long lines can be broken by indent
  • -npcs: No spaces inside parentheses and function arguments
  • -nprs: No space after cast parens or function calls
  • -npsl: Arguments of a function or a struct can be on the same line
  • -nsc: No comment indentation
  • -nsob: Maximum number of spaces before open brace set to 8

Example output:

#include <stdio.h>

int main() 
{
    int a = 5;
    int b = 10;
    int sum = a + b;

    printf("The sum of %d and %d is %d\n", a, b, sum);

    return 0;
}

Conclusion:

The indent command is a powerful tool for formatting C/C++ source code according to different styles and configurations. By using the various options and arguments provided by the command, you can easily change the appearance of your code to suit your preferences or adhere to specific coding standards. Properly formatted code not only improves readability but also makes it easier for others to understand and maintain the code.

Tags :

Related Posts

How to use the command diskpart (with examples)

How to use the command diskpart (with examples)

Diskpart is a command-line tool in Windows that allows users to manage disks, volumes, and partitions.

Read More
How to use the command 'lipo' (with examples)

How to use the command 'lipo' (with examples)

The lipo command is a tool for handling Mach-O Universal Binaries on macOS.

Read More
How to use the command chronyc (with examples)

How to use the command chronyc (with examples)

The chronyc command is used to query the Chrony NTP (Network Time Protocol) daemon.

Read More