Welcome to C
Typical C Workflow
- Edit files with emacs or vim.
- Compile using a GNU C compier (abbreviated as "gcc") to compile, link, and create executable.
- Run the executable file.
It's better to code incrementally as you will have a better time debugging.
Compiling
Step 1: Preprocessor
- Brings together all the code that belongs together
- The preprocessor quickly scans your code and processes any directives that start with
#, such as#includeor#define- Checks that the library exists.
Step 2: Compiler
- Turns human-readable source code into object code (executable code with the extension
.o)- Change the name of the output (executable) file from the default a.out using the
-ooutput flag.
- Change the name of the output (executable) file from the default a.out using the
- Might yield warnings and errors if your code has mistakes that are "visible" to the compiler.
Step 3: Linker
- Brings all relevant object code (the
.ofiles) into a single executable file (default name ofa.out). - Might yield warnings and errors if relevant code is missing, there's a naming conflict, etc.
- We need full implementation of the function.
Understanding Hello World program
// hello_world.c:
#include<stdio.h>
// Print "Hello, world!" followed by a newline and exit
int main(void) {
printf("Hello, world!\n");
return 0;
}
- The file is named
hello_world.c. C files have the.cextension. #includeis a preprocessor directive, similar to Javaimport. Omitting#include <stdio.h>results in an error.stdiois standard.io and.his an extension that is short for "header". Header functions are used to declare things.
mainis the function. Every program has exactly onemain. It is the entry point and where the execution starts.intis the function's return type.main(void)says thatmaintakes no parameters (inputs).printfprints a string to "standard out" (the terminal). This function is defined in the standard.io library (must include<stdio.h>).\nis a string that denotes the newline chracter.return 0means "program" completed with no errors.return 1(or any non-zero value) would indicate an error.// Print...Explanatory comments before functions are good practice. Ignored by the compiler
Compilation and Execution
To compile hello_world.c and link to give executable file:
gcc -std=c99 -Wall -Wextra -pedantic hello_world.c
- The flags and switches are the parts that start with a
-. Each switch has meaning.-std=c99sets the standards to c99 made in 1999 (a popular standard still used today).- The other three switches makes sure we get full verbose feedback from the compiler.
- If you get an error feedback, your code won't even compile. Warnings indicate potential problem but will continue to compile.
To run executable file named a.out:
./a.out
./refers to something inside the current directory.a.outexecutesa.outfrom the current directory.
Multiple Copies of Code
To "snapshot" code is making a copy. Frequent "snapshots" is bad because it's hard to remember the relationship of copies, wastes space, and is difficult for team members to track the latest version of each file.
Git uses a repository ("repo") to store all versions of all project files. A repo (master/origin) can be local (on your computer) or remote (e.g., on bitbucket.org or github.com).