Wiki/c.md

924 B

C Programming

  • Print string of arbitrary size ignoring null characters: printf("%.*s", stringLength, pointerToString);

  • Reuse socket:

int sockfd;
int option = 1;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option));

Formatting

clang-format

  • Format all files in-place: clang-format -i *.c *.h
  • Specify format:
    • Predefined style: -style=<STYLE>, e.g. -style=llvm
    • Custom file in current or parent directory: -style=file
  • Create style file based on default: clang-format -style=llvm -dump-config > .clang-format
  • If no -style is provided, -style=file is used. If there is no .clang-format, -style=LLVM is used
  • Use default style, but overwrite some key: --style={BasedOnStyle: <STYLE>, <KEY>: <VALUE>}

Example

  • clang-format --style="{BasedOnStyle: LLVM, IndentWidth: 4, ColumnLimit: 90}" -i *.c *.h