C Programming Part 1: Terminal Mastery & Hello World


Objective

Establish a strict C89/C90 development environment using only the command line, construct a file, edit it, and compile the executable.

Key Concepts

  • The Shell: File creation via cat, echo, and Heredoc.
  • Stream Editing: Mutating source code safely via sed.
  • GCC Compiler: Translating C text into a machine binary.
  • Strict Compliance: Ensuring portability with ISO C flags.

The Terminal Workspace


Navigating the File System

Before writing code, we must create an isolated environment.

  • mkdir memory_project creates a new directory.
  • cd memory_project changes your current shell location to that new directory.

Every subsequent command will now execute within this project folder.

Writing Code via Shell Streams


File Creation Methods

Instead of an interactive editor, use standard input redirection.

  • Interactive cat: cat > main.c followed by raw typing.
  • Heredoc Block: cat << 'EOF' > main.c to ingest entire logic blocks at once.
  • Line-by-line Echo: echo 'text' >> main.c to programmatically append individual lines.

Making Edits with sed


The Stream Editor

Modify source code directly from the command line without opening the file.

Find and Replace

sed -i 's/World/Terminal/' main.c
  • -i: Edit the file "in-place".
  • s: The substitution command.

Inserting New Lines

sed -i '/return 0;/i \    printf("Edit successful!\\n");' main.c

Compilation and Execution


Building the Binary

gcc -ansi -pedantic main.c -o memory_app
  • -ansi -pedantic strictly enforces classic C rules.
  • -o directs the binary to a specific file name.

Running the Application

./memory_app

The ./ instructs the terminal to execute the binary located in the current working directory.