C Cheat Sheet

ADVERTISEMENT

C Cheat Sheet
irt21@bath.ac.uk
Unix terminal commands
Prints present working directory (where you are).
pwd
Change Directory. Changes to a subdirectory.
cd subdirectory
Changes to home directory (H: drive) wherever you are.
cd
Moves up a level in the file tree.
cd ..
Compiles a C source file into an executable program.
cc code.c -o outputprogram
Runs an executable program located in current directory.
./programname
Prints contents of current directory (pwd).
ls
Runs gedit file editor and leaves terminal free.
gedit .&
Structure
All C programs have to have a main function. It is the function a C program executes from top
to bottom, all other functions used must be called from main.All functions when called run their
internal commands. printf is no different, just the internal commands are specified elsewhere
(in stdio.h). main is special as it is the only function that all C programs must have and it
doesn’t receive arguments, hence the empty (). It is wise to include #include<stdio.h> at the
very top of all your C files.
Types
Type
Declaration Definition
Placeholder
Integer
Positive or negative integer, no decimal point
int
%d or %i
Float
Positive or negative number with decimal point
float
%f
Character
A single character stored as ASCI code
char
%c
String
An array of char but can print as string
-
%s
Some more advanced types
Double
A larger floating point number
double
%f or %lf
Long
A larger integer
long
%ld
N.B. When declared a variable is not zero. It is some crazy number. The same is true if the
value is too large for the type or the wrong placeholder is used in printf.
Arrays
The number of elements in the array is defined at declaration as its length.
The length must be an integer value.
All elements must of the same type.
Declared as a normal variable followed by length as:
type arrayname[length]
Each element can be accessed as array[element ]
1

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2