cd
cp
ls
mkdir
pwd
rm
cat
ci
co
echo
gcc
gdb
less
make
man
more
\n // new line
\r // return
\t // Horizontal Tab
\" // Double quote
\\ // backslash
\a // Alarm (the computers "bell)
// more http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/escape.html
char // Character, 1 byte
double // big decimal number
float // decimal number
int // Integer
long int
long long ?
long, short, signed, unsigned // types of int variables
// CS50 Variables
bool
string
%c // char
%d // int
%e // double in standard form
%E // double with exponent
%f // double in fixed point / Floating-point?
%.#f // limit output to # decimal places
%s // char * / string
%u // unsigned int
%x // unsigned int as a hexadecimal number
%% // literal percent sign
+ // add
- // subtract
* // multiply
/ // divide
% // modulo, remainder, 15 % 7 == 1
== // equal
< // less than
> // greater than
>= // greater than or equal to
<= // less than or equal to
!= // not equal to
&& // and
|| // or
! // not
?:
relation ? trueStatement : falseStatement;
++count/count++ // prefix/postfix increment
--count/count-- // prefix/postfix decrement
// prefix computes before anything else on the line.
// postfix computes after everything else on the line finishes
// works with: while, do-while, or for
continue // go back and start this loop agian
break // terminate the current loop
switch (choice)
{
case (1): statement
case (2): statement
case (3): statement
default: statement
}
char month[10]; /* Defines a character array */
char month[10] = "January"; /* Defines a character array */
char month[] = "January";
#include <string.h>
strcpy(month, "April"); /* Puts new string in month array */
#include <filename> // Use < and > when including compiler-supplied header files.
#include "filename" // Use " and " when including your own header files that you’ve stored in your source code’s directory.
#define CONSTANT constantDefinition
floor() // “push down” non-integers to their next lower integer values. returns a float.
ceil() // “push up” non-integers to their next higher integer value. returns a float.
fabs() // floating-point absolute value
pow() // raises a value to a power
sqrt() // returns the square root of a value
cos(x)
sin(x)
tan(x)
// If you want to supply an argument in degrees instead of in radians, you can convert from degrees to radians with this formula: radians = degrees * (3.14159 / 180.0);
exp(x)
log(x)
log10(x)
rand() // returns a random number from 0 to 32767
srand() // seed random number. i.e. srand(time(&t));
#include <time.h>
QuickSort, the Heapsort, and the Shell-Metzner sort
// CS50 Standard Input
char GetChar();
double GetDouble();
float GetFloat();
int GetInt();
long long GetLongLong();
string GetString();
// A basic "Hello World" C program
#include <studio.h>
int
main(int argc, char *argv[])
{
printf("Hello world!\n";
}
*= // Equal to X = X. i.e. X *= 2 is the same as X = X * 2
/=
%=
+=
-=
// low in order of operations. i.e. after addition and subtraction
(dataType)value i.e float(age) // changes the variable 'age' to a float
salaryBonus = salary * (float)age / 150.0; // has to be converted to another value to properly compute a result
value = (float)(number - 10 * yrsService);
CTYPE.H
isdigit()
isalpha()
isupper()
islower()
toupper()
tolower()
strcat(x,y)
puts()
gets()
((x < y) && (y < z)) // x is less than y and y is less than z
// if, else if, else
if (x < y)
{
printf("x is less than y\n");
}
else if (x > y)
{
printf("x is greater than y\n");
{
else
{
printf("x is equal to y\n");
}
while (1) //forever
{
printf("Hi");
}
for (int 1 = 0; i < 10; i++);
{
printf(hi\n");
}
int counter = 0;
{
printf("%d\n", counter);
counter++;
}
// switch
switch (expression
{
case i:
// do this
break;
case j:
// do this
break;
default:
// do this
}
for ( i = 0; condition, i++)
{
//do this again and again
}
// use if action needs to happen at least once, even if the contition is not met.
do
{
// do this again and agian
}
while (condition)
// Array
char *inventory[SIZE]; // does SIZE need to be a number?
inventory[i] = "Orange"; // add Orange to the inventory array. does i need to be a number?
int i[25]; /* Defines the array */
char name[6] = "Italy"; /* Leave room for the null! */
int vals[5] = {10, 40, 70, 90, 120}; // defines an integer array and initializes it with five values
char italCity[7] = "Verona"; /* Automatic null zero */
// how to write a program in C
Opt 1
nano hi.c // write
gcc -o hi hi.c -lcs50 // compile, name output file, and link cs50 library
hi // run. a.out is the default file name gcc creates if you don't use -o when you run gcc
Opt 2
nano hi.c
make hi
hi
// find the number of memory locations it takes to store values of any data type
// get the length of a string
ci <filename> // add file to version control. describe file the first time. log entry after that.
co –r1.1 <filename> // check out revition 1.1 of file. ci current file to keep a copy.
rlog <filename> // view log of changes
ci *.c *.h // check in all source files in a directory
rcsdiff <file> // compares the latest revision on the default branch to the working file
rcsdiff -r<rev1> -r<rev2> <file> // compares rev1 to rev2
*x // Go to the location in memory of x
&x // Give me the address in memory of x
// function prototype
void swap(int *, int *); // declare the swap function using memory addresses
int main(int argc, char *argv[])
{
int x = 1; int y = 2;
printf("x is %d\n", x);
printf("y is %d\n", y);
printf("Swapping...\n");
swap(&x, &y); // get the address in memory of x and y to pass to the swap function
printf("Swapped!\n");
printf("x is %d\n", x);
printf("y is %d\n", y);
}
/*
* void
* swap(int *a, int *b)
*
* Swap arguments’ values. Us * for a and b so the results of swap get sent back to x and y.
*/
void
swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}