0% found this document useful (0 votes)
792 views

CS50 Cheat Sheet - Notes

The document is a cheat sheet for C programming that contains summaries of key concepts like the command line, variables, operators, functions, loops, and more. It provides code examples and brief explanations for many common tasks in C like defining variables, using conditionals, writing functions, and compiling/running programs. The cheat sheet acts as a quick reference guide to the essential elements of C syntax, standard library functions, and programming techniques.

Uploaded by

snadmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
792 views

CS50 Cheat Sheet - Notes

The document is a cheat sheet for C programming that contains summaries of key concepts like the command line, variables, operators, functions, loops, and more. It provides code examples and brief explanations for many common tasks in C like defining variables, using conditionals, writing functions, and compiling/running programs. The cheat sheet acts as a quick reference guide to the essential elements of C syntax, standard library functions, and programming techniques.

Uploaded by

snadmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

10/04/2015

CS50CheatSheetnotes

CS50CheatSheet
Contents
1commandline
2EscapeSequences
3Variables
4FormatStrings
5Operators
5.1Arithmetic/operators
5.2Conditions/RelationalOperators
5.3LogicalOperators
5.4ConditionalOperator
5.5Incrementanddecrementoperators
6BreakandContinue
7Switch
8String
9PreprocessorDirectives
10MathFunctions
11sort
11.1BubbleSort
12CompoundAssignment
13TypeCasting
14CsBuiltInCharacterandStringFunctions
14.1CharacterTestingFunctions
14.2CaseChangingFunctions
14.3StringFunctions
15whileandforloops
16sizeof()
17strlen()
18Sourcecontrol(RCS)
19PassingbyPointer(Swap)

commandline
cd
cp
ls
mkdir
pwd
rm
cat
ci
co
echo
gcc
gdb
less
make
man
more
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

1/9

10/04/2015

CS50CheatSheetnotes

EscapeSequences
\n//newline
\r//return
\t//HorizontalTab
\"//Doublequote
\\//backslash
\a//Alarm(thecomputers"bell)
//
morehttp://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/escape.html

Variables
char//Character,1byte
double//bigdecimalnumber
float//decimalnumber
int//Integer
longint
longlong?
long,short,signed,unsigned//typesofint
variables
//CS50Variables
bool
string

FormatStrings
%c//char
%d//int
%e//doubleinstandardform
%E//doublewithexponent
%f//doubleinfixedpoint/Floatingpoint?
%.#f//limitoutputto#decimalplaces
%s//char*/string
%u//unsignedint
%x//unsignedintasahexadecimalnumber
%%//literalpercentsign

Operators
Arithmetic/operators
+//add
//subtract
*//multiply
///divide
%//modulo,remainder,15%7==1
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

2/9

10/04/2015

CS50CheatSheetnotes

Conditions/Relational
Operators
==//equal
<//lessthan
>//greaterthan
>=//greaterthanorequalto
<=//lessthanorequalto
!=//notequalto

LogicalOperators
&&//and
||//or
!//not

ConditionalOperator
?:
relation?trueStatement:falseStatement

Incrementanddecrement
operators
++count/count++//prefix/postfixincrement
count/count//prefix/postfixdecrement
//prefixcomputesbeforeanythingelseon
theline.
//postfixcomputesaftereverythingelseon
thelinefinishes

BreakandContinue
//workswith:while,dowhile,orfor
continue//gobackandstartthisloopagian
break//terminatethecurrentloop

Switch
switch(choice)
{
case(1):statement
case(2):statement
case(3):statement
default:statement
}
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

3/9

10/04/2015

CS50CheatSheetnotes

String
charmonth[10]/*Definesacharacter
array*/
charmonth[10]="January"/*Definesa
characterarray*/
charmonth[]="January"
#include<string.h>
strcpy(month,"April")/*Putsnewstring
inmontharray*/

PreprocessorDirectives
#include<filename>//Use<and>when
includingcompilersuppliedheaderfiles.
#include"filename"//Use"and"when
includingyourownheaderfilesthatyouve
storedinyoursourcecodesdirectory.
#defineCONSTANTconstantDefinition

MathFunctions
floor()//pushdownnonintegerstotheir
nextlowerintegervalues.returnsafloat.
ceil()//pushupnonintegerstotheirnext
higherintegervalue.returnsafloat.
fabs()//floatingpointabsolutevalue
pow()//raisesavaluetoapower
sqrt()//returnsthesquarerootofavalue
cos(x)
sin(x)
tan(x)
//Ifyouwanttosupplyanargumentin
degreesinsteadofinradians,youcan
convertfromdegreestoradianswiththis
formula:radians=degrees*(3.14159/
180.0)
exp(x)
log(x)
log10(x)
rand()//returnsarandomnumberfrom0to
32767
srand()//seedrandomnumber.i.e.
srand(time(&t))
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

4/9

10/04/2015

CS50CheatSheetnotes

#include<time.h>

sort
QuickSort,theHeapsort,andtheShell
Metznersort

BubbleSort
//CS50StandardInput
charGetChar()
doubleGetDouble()
floatGetFloat()
intGetInt()
longlongGetLongLong()
stringGetString()
//Abasic"HelloWorld"Cprogram
#include<studio.h>
int
main(intargc,char*argv[])
{
printf("Helloworld!\n"
}

CompoundAssignment
*=//EqualtoX=X.i.e.X*=2isthesame
asX=X*2
/=
%=
+=
=
//lowinorderofoperations.i.e.after
additionandsubtraction

TypeCasting
(dataType)valuei.efloat(age)//changesthe
variable'age'toafloat
salaryBonus=salary*(float)age/150.0//
hastobeconvertedtoanothervalueto
properlycomputearesult
value=(float)(number10*yrsService)

CsBuiltInCharacterand
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

5/9

10/04/2015

CS50CheatSheetnotes

StringFunctions
CharacterTestingFunctions
CTYPE.H
isdigit()
isalpha()
isupper()
islower()

CaseChangingFunctions
toupper()
tolower()

StringFunctions
strcat(x,y)
puts()
gets()
((x<y)&&(y<z))//xislessthanyandy
islessthanz
//if,elseif,else
if(x<y)
{
printf("xislessthany\n")
}
elseif(x>y)
{
printf("xisgreaterthany\n")
{
else
{
printf("xisequaltoy\n")
}

whileandforloops
while(1)//forever
{
printf("Hi")
}
for(int1=0i<10i++)
{
printf(hi\n")
}
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

6/9

10/04/2015

CS50CheatSheetnotes

intcounter=0
{
printf("%d\n",counter)
counter++
}
//switch
switch(expression
{
casei:
//dothis
break
casej:
//dothis
break
default:
//dothis
}
for(i=0condition,i++)
{
//dothisagainandagain
}
//useifactionneedstohappenatleast
once,evenifthecontitionisnotmet.
do
{
//dothisagainandagian
}
while(condition)
//Array
char*inventory[SIZE]//doesSIZEneed
tobeanumber?
inventory[i]="Orange"//addOrangeto
theinventoryarray.doesineedtobea
number?
inti[25]/*Definesthearray*/
charname[6]="Italy"/*Leaveroomfor
thenull!*/
intvals[5]={10,40,70,90,120}//
definesanintegerarrayandinitializesit
withfivevalues
charitalCity[7]="Verona"/*Automatic
nullzero*/
//howtowriteaprograminC
Opt1
nanohi.c//write
gccohihi.clcs50//compile,name
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

7/9

10/04/2015

CS50CheatSheetnotes

outputfile,andlinkcs50library
hi//run.a.outisthedefaultfilename
gcccreatesifyoudon'tuseowhen
yourungcc
Opt2
nanohi.c
makehi
hi

sizeof()
//findthenumberofmemorylocationsit
takestostorevaluesofanydatatype

strlen()
//getthelengthofastring

Sourcecontrol(RCS)
ci<filename>//addfiletoversioncontrol.

describefilethefirsttime.logentryafter
that.
cor1.1<filename>//checkoutrevition
1.1offile.cicurrentfiletokeepacopy.
rlog<filename>//viewlogofchanges
ci*.c*.h//checkinallsourcefilesina
directory
rcsdiff<file>//comparesthelatest

revisiononthedefaultbranchtothe
workingfile
rcsdiffr<rev1>r<rev2><file>//
comparesrev1torev2

PassingbyPointer(Swap)
*x//Gotothelocationinmemoryofx
&x//Givemetheaddressinmemoryofx
//functionprototype
voidswap(int*,int*);//declarethe

swapfunctionusingmemoryaddresses
intmain(intargc,char*argv[])
{
intx=1;inty=2;
printf("xis%d\n",x);
printf("yis%d\n",y);
data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

8/9

10/04/2015

CS50CheatSheetnotes

printf("Swapping...\n");
swap(&x,&y);//gettheaddressinmemory

ofxandytopasstotheswapfunction
printf("Swapped!\n");
printf("xis%d\n",x);
printf("yis%d\n",y);
}

/*
*void
*swap(int*a,int*b)
*
*Swapargumentsvalues.Us*foraandb
sotheresultsofswapgetsentbacktoxand
y.
*/
void
swap(int*a,int*b)
{
inttmp;
tmp=*a;
*a=*b;
*b=tmp;
}

data:text/htmlcharset=utf8,%3Ch3%20xmlns%3D%22https%3A%2F%2Ffanyv88.com%3A443%2Fhttp%2Fwww.w3.org%2F1999%2Fxhtml%22%20id%3D%22sitespagetitleheader%22

9/9

You might also like