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

Synapseindia Dot Net Development - Programming Overview

Synapseindia Dot Net Development -Programming Overview Synapseindia PHP Development.Synapseindia Magento Development, Synapseindia Reviews, Synapseindia Complaints, Synapseindia Sharepoint Development
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Synapseindia Dot Net Development - Programming Overview

Synapseindia Dot Net Development -Programming Overview Synapseindia PHP Development.Synapseindia Magento Development, Synapseindia Reviews, Synapseindia Complaints, Synapseindia Sharepoint Development
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Programming Overview

What is to be taught?:

How to program C stylishly and elegantly.


Small and fast mathematical programs.
Documenting code.
Writing good algorithms.
NO fancy graphics that go out of date quickly.

Why teach programming?:

Some maths relies on computers (4 colour theorem).


Simulation lets us apply maths to the real world.
It might get you a job after you finish your maths course.
It can be fun to do.

Why teach C?

C is small (only 32 keywords).


C is common (lots of C code about).
C is stable (the language doesnt change much).
C is quick running.
C is the basis for many other languages (Java, C++,
awk, Perl).
It may not feel like it but C is one of the easiest
languages to learn.
NOTE: Obviously programmers will find this course
easier than everyone else. BUT, this course is for nonprogrammers. If you cannot understand what I say,
please please ask me either in the lecture or afterwards.

Some programmer jargon


Some words that will be used a lot:
Source code: The stuff you type into the computer. The
program you are writing.
Compile (build): Taking source code and making a program
that the computer can understand.
Executable: The compiled program that the computer can run.
Language: (Special sense) The core part of C central to
writing C code.
Library: Added functions for C programming which are
bolted on to do certain tasks.
Header file: Files ending in .h which are included at the start
of source code.

More about Hello World


Preprocessor
#include <stdio.h>

Comments are good

/* My first C program which prints Hello World */

main() means start here

int main (int argc, char *argv[])


{
printf ("Hello World!\n");
return 0;
}

Brackets
define code blocks

Library command

Return 0 from main means our program


finished without errors

C doesnt care much about spaces

#
i
n
c
l
u
d
e
<
s
t
d
i
o
.
h
>
/
*
M
y
f
i
r
s
t
C
p
r
o
g
r
a
m
w
h
i
c
h
p
r
i
n
t
s
H
e
l
l
o
W
o
r
l
d
*
/
i
n
t
m
a
i
n
(
)
{
p
r
i
n
t
f
(
"
H
e
l
l
o
W
o
r
l
d
!
\
n
"
)
;
r
e
t
u
r
n
0
;
}
#
i
n
c
l
u
d
e<
s
t
d
i
o
.
h
>
/
*M
yf
i
r
s
t
Cp
r
o
g
r
a
m
w
h
i
c
hp
r
i
n
t
s
H
e
l
l
oW
o
r
l
d*
/
i
n
t
m
a
i
n
(
)
{
p
r
i
n
t
f
(
"
H
e
l
l
oW
o
r
l
d
!
\
n
"
)
;
r
e
t
u
r
n
0
;
}

Both of these programs are exactly


the same as the original as far as
your compiler is concerned.
Note that words have to be kept together
and so do things in quotes.
In the next lecture we'll learn how we
SHOULD lay out our C program to
make them look nice

Keywords of C
Flow control (6) if, else, return, switch,
case, default
Loops (5) for, do, while, break, continue
Common types (5) int, float, double, char,
void
structures (3) struct, typedef, union
Counting and sizing things (2) enum, sizeof
Rare but still useful types (7) extern, signed,
unsigned, long, short, static, const
Evil keywords which we avoid (1) goto
Wierdies (3) auto, register, volatile

Types of variable
We must declare the type of every variable we
use in C.
Every variable has a type (e.g. int) and a name.
We already saw int, double and float.
This prevents some bugs caused by spelling
errors (misspelling variable names).
Declarations of types should always be together
at the top of main or a function (see later).
Other types are char, signed, unsigned,
long, short and const.

Naming variables

Variables in C can be given any name made from


numbers, letters and underlines which is not a
keyword and does not begin with a number.
A good name for your variables is important
int a,b;
double d;
/* This is
a bit cryptic */

int start_time;
int no_students;
double course_mark;
/* This is a bit better */

Ideally, a comment with each variable name helps


people know what they do.
In coursework I like to see well chosen variable
names and comments on variables (I dont always
do this in notes because there is little space).

The char type

char stores a character variable


We can print char with %c
A char has a single quote not a double quote.
We can use it like so:

int main()
{
char a, b;
a= 'x'; /* Set a to the character x */
printf ("a is %c\n",a);
b= '\n'; /* This really is one character*/
printf ("b is %c\n",b);
return 0;
}

More types: Signed/unsigned,


long, short, const

unsigned means that an int or char value can


only be positive. signed means that it can be
positive or negative.
long means that int, float or double have
more precision (and are larger) short means they
have less
const means a variable which doesn't vary
useful for physical constants or things like pi or e
short int small_no;
unsigned char uchar;
long double precise_number;
short float not_so_precise;
const short float pi= 3.14;
const long double e= 2.718281828;

A short note about ++


++i means increment i then use it
i++ means use i then increment it
int i= 6;
printf ("%d\n",i++);

/* Prints 6 sets i to 7 */

Note this important difference


int i= 6;
printf ("%d\n",++i);

/* prints 7 and sets i to 7 */

It is easy to confuse yourself and others with the difference


between ++i and i++ - it is best to use them only in simple ways.
All of the above also applies to --.

Some simple operations for variables


In addition to +, -, * and / we can also use +=, -=, *=,
/=, -- and % (modulo)
-- (subtract one) e.g. countdown--;
+= (add to a variable) e.g. a+= 5;
-= (subtract from variable) e.g. num_living-=
num_dead;
*= (multiply a variable) e.g. no_bunnies*=2;
/= (divide a variable) e.g. fraction/= divisor;
(x % y) gives the remainder when x is divided by
y
remainder= x%y; (ints only)

Casting between variables


Recall the trouble we had dividing ints
A cast is a way of telling one variable type
to temporarily look like another.
int a= 3;
int b= 4;
Cast ints a and b to be doubles
double c;
c= (double)a/(double)b;

By using (type) in front of a variable we tell the variable to


act like another type of variable. We can cast between any
type. Usually, however, the only reason to cast is to stop
ints being rounded by division.

What is a function?

The function is one of the most basic things to


understand in C programming.
A function is a sub-unit of a program which
performs a specific task.
We have already (without knowing it) seen one
function from the C library printf.
We need to learn to write our own functions.
Functions take arguments (variables) and may
return an argument.
Think of a function as extending the C language to
a new task.
Or perhaps variables are NOUNS functions are
VERBS.

An example function
#include <stdio.h>
int maximum (int, int);

/* Prototype see later in lecture */

int main(int argc, char*argv[])


Prototype the function
{
int i= 4;
int j= 5;
Call the function
int k;
k= maximum (i,j); /* Call maximum function */
printf ("%d is the largest from %d and %d\n",k,i,j);
printf ("%d is the largest from %d and %d\n",maximum(3,5), 3, 5);
return 0;
}
function header
int maximum (int a, int b)
/* Return the largest integer */
The function itself
{
if (a > b)
return a; /* Return means "I am the result of the function"*/
return b;
/* exit the function with this result */
}

Functions can access other functions


Once you have written a function, it can be
accessed from other functions. We can therefore
build more complex functions from simpler
functions
i
n
t
m
a
x
_
o
f
_
t
h
r
e
e
(
i
n
t
,
i
n
t
,
i
n
t
)
;
/
*
P
r
o
t
o
t
y
p
e
*
/
.
.
/
*
M
a
i
n
a
n
d
r
e
s
t
o
f
c
o
d
e
i
s
i
n
h
e
r
e
*
/
.
i
n
t
m
a
x
_
o
f
_
t
h
r
e
e
(
i
n
t
i
1
,
i
n
t
i
2
,
i
n
t
i
3
)
/
*
r
e
t
u
r
n
s
t
h
e
m
a
x
i
m
u
m
o
f
t
h
r
e
e
i
n
t
e
g
e
r
s
*
/
{
r
e
t
u
r
n
(
m
a
x
i
m
u
m
(
m
a
x
i
m
u
m
(
i
1
,
i
2
)
,
i
3
)
)
;
}

void functions
A function doesn't have to take or return
arguments. We prototype such a function
using void.
Prototype (at top of file remember)
v
o
i
d
p
r
i
n
t
_
h
e
l
l
o
(
v
o
i
d
)
;
v
o
i
d
p
r
i
n
t
_
h
e
l
l
o
(
v
o
i
d
)
Function takes and returns
/
*
t
h
i
s
f
u
n
c
t
i
o
n
p
r
i
n
t
s
h
e
l
l
o
*
/
{
void (no arguments)
p
r
i
n
t
f
(
"
H
e
l
l
o
\
n
"
)
;
}
void odd_or_even (int);
v
o
i
d
o
d
d
_
o
r
_
e
v
e
n
(
i
n
t
n
u
m
)
/
*
t
h
i
s
f
u
n
c
t
i
o
n
p
r
i
n
t
s
o
d
d
o
r
e
v
e
n
a
p
p
r
o
p
r
i
a
t
e
l
y
*
/
{
i
f
(
(
n
u
m
%
2
)
=
=
0
)
{
p
r
i
n
t
f
(
"
E
v
e
n
\
n
"
)
;
r
e
t
u
r
n
;
}
p
r
i
n
t
f
(
"
O
d
d
\
n
"
)
;
}

Another prototype

Function which takes one


int arguments and returns none

Notes about functions


A function can take any number of arguments
mixed in any way.
A function can return at most one argument.
When we return from a function, the values of the
argument HAVE NOT CHANGED.
We can declare variables within a function just
like we can within main() - these variables will
be deleted when we return from the function

Where do functions go in the


program

Generally speaking it doesn't matter too much.


main() is a function just like any other (you could
even call it from other functions if you wanted.
It is common to make main() the first function in
your code.
Functions must be entirely separate from each other.
Prototypes must come before functions are used.
A usual order is: Prototypes THEN main THEN
other functions.

What are these prototype things?


A prototype tells your C program what to expect
from a function - what arguments it takes (if
any) and what it returns (if any)
Prototypes should go before main()
#include finds the prototypes for library
functions (e.g. printf)
A function MUST return the variable type we
say that it does in the prototype.

What is scope?
The scope of a variable is where it can be used in a
program
Normally variables are local in scope - this means
they can only be used in the function where they
are declared (main is a function)
We can also declare global variables.
If we declare a variable outside a function it can
be used in any function beneath where it is
declared
Global variables are A BAD THING

The print stars example


#
i
n
c
l
u
d
e<
s
t
d
i
o
.
h
>
v
o
i
dp
r
i
n
t
_
s
t
a
r
s
(
i
n
t
)
;

This program prints five rows of


*****
five stars
*****
*****
*****
*****

i
n
tm
a
i
n
(
)
{
i
n
ti
;
f
o
r(
i
=0
;i<5
;i
+
+
) Loop around 5 times
p
r
i
n
t
_
s
t
a
r
s
(
5
)
;
print the stars
r
e
t
u
r
n0
;
Variables here are LOCAL variables
}
v
o
i
dp
r
i
n
t
_
s
t
a
r
s(
i
n
tn
) This prints 'n' stars and
{
a new line character
i
n
ti
;
f
o
r(
i
=0
;i<n
;i
+
+
)
p
r
i
n
t
f(
"
*
"
)
;
p
r
i
n
t
f(
"
\
n
"
)
;
}

to

then

Why global is bad


Variable here is global variable
#
i
n
c
l
u
d
e<
s
t
d
i
o
.
h
>
v
o
i
dp
r
i
n
t
_
s
t
a
r
s
(
i
n
t
)
;
i
n
ti
; /
*D
e
c
l
a
r
eg
l
o
b
a
li*
/
i
n
tm
a
i
n
(
)
{
f
o
r(
i
=0
;i<5
;i
+
+
)
p
r
i
n
t
_
s
t
a
r
s
(
5
)
;
r
e
t
u
r
n0
;
}
v
o
i
dp
r
i
n
t
_
s
t
a
r
s(
i
n
tn
)
{
f
o
r(
i
=0
;i<n
;i
+
+
)
p
r
i
n
t
f(
"
*
"
)
;
p
r
i
n
t
f(
"
\
n
"
)
;
}

This program only


prints ONE row
of five stars

Thinking like the computer for


debugging

A good technique for "debugging" code is to


think of yourself in place of the computer.
Go through all the loops in the program and ask
"what is in each variable?"
Each time you go through a loop ask "is the
condition met" and "should I continue"
The factorial program shows how to do this.

Factorial program (and thoughts)


int main()
{
int number= 4;
int answer;
int count;
answer= 1;
count= number;
while (count >= 0) {
answer= answer* count;
count--;
}
printf ("%d! = %d\n",
number,answer);
return 0;
}

number= 4
answer= 1
count= 4
enter while loop
answer= 1*4=4
count=3
enter while loop
answer=4*3= 12
count=2
enter while loop
answer=12*2= 24
count= 1
enter while loop
answer= 24*1= 24
count= 0
enter while loop
answer= 24*0= 0
AHA I see!!!

Other techniques for debugging


Check missing brackets and commas.
Check that you have a semicolon at the end of
every line which needs one.
Put in some printfs if you know what your
program is DOING you will know what it is
DOING WRONG.
Try to explain to someone else what the program
is meant to do.
Take a break, get a cup of coffee and come back
to it fresh. (Debugging is FRUSTRATING).

You might also like