Cheat Sheet
Terminal/Cygwin Commands
Command Description
ls List the items in your current directory/folder
cd dir_name Change your directory to the specified dir_name
cd .. Change your directory to the parent directory
pwd Tells you the full path to your current directory
mkdir dir_name Create a new directory with the name dir_name
rmdir dir_name Delete the directory with the name dir_name
touch file_name Create an empty file with the name file_name
rm file_name Delete the file with the name file_name
cp file1 file2 Copy file1 to file2
mv file1 file2 Rename or move file1 to file2
cat file Display the contents of file
./program_name Run the specified program (.out for Mac/Linux .exe for Windows)
Control + C Stops a program that is currently running
Control + D End of file (EOF) character
Compiling in Terminal/Cygwin
Command Description
gcc code.c Compiles code.c and creates either a.out or a.exe. Run with ./
gcc code.c -o name.out Compiles code.c and creates name.out
gcc code.c -Wall Compiles code.c, displays code warnings, and creates a.out or a.exe
gcc code.c -g Compiles code.c and creates a debuggable a.out or a.exe
C Language Basics
The Hello World Program – Use this as a template for all programs.
#include <stdio.h>
int main() {
printf("Hello, world!\n");
return 0;
}
// This is a comment. It is ignored when the code is compiled.
#include <stdio.h> // This is why we can use printf() and scanf()
int main() { // The starting point of the program
printf("Hello, world!\n"); // Prints text to the command line. '\n' prints a newline
return 0; // Ends the program
}
Types
Type Description Example Usage Print/Scan %
int Holds integers numbers 0, 25, -10 int i; %d
float Holds real numbers (with decimals) 0.0, 13.37, -10.5 float f; %f
double Holds more precise real numbers 0.000000000005 double d; %lf
char Holds a single character 'A', 'z', '1', ' [' char c; %c
Integer vs Float Division
Example Result Reason
int n = 5 / 2; 2 Int division – 2 goes into 5 twice
int r = 5 % 2; 1 Modulo – 2 goes into 5 twice with 1 remainder
float a = 5 / 2; 2 Int division – even though it is stored in a float, at
least one operand must be a float
float b = 5.0 / 2; 2.5 Float division – at least one operand must be a float
float c = 5 / 2.0; 2.5 Float division – at least one operand must be a float
float d = 5.0 / 2.0; 2.5 Float division – at least one operand must be a float
Print vs Scan
printf() writes to the command line. It does not need the & on the variables it reads values from.
Examples:
Code Snippet Output
printf("Hello, world!"); Hello, world!
int i = 1; Value: 1
printf("Value: %d", i);
int num = 30; int is 30
char a = 'A'; char is A
float b = 1.5; float is 1.500000
printf("int is %d\nchar is %c\nfloat is %f\n", num, a, b); empty line
scanf() reads from the command line. It needs the & on the variables it stores values in.
Examples:
Code Snippet Input/Output
printf("Enter an integer:\n"); Enter an integer:
int i; 20 user input/scan
scanf("%d", &i); // with & You entered 20
printf("You entered %d"); // without &
printf("Enter four chars: "); Enter four chars: I J K L
int a, b, c, d; Reversed: L K J I
scanf("%c %c %c %c", &a, &b, &c, &d);
printf("Reversed: %c %c %c %c", d, c, b, a);
Enter two floats, separated by a comma:
printf("Enter two floats, separated by a comma:\n"); 1.5, 2.5
float a, b; You entered:
scanf("%f, %f", &a, &b); empty line
printf("You entered: \n\n%f and %f", a, b); 1.5 and 2.5