Synapseindia Dot Net Development - Programming Overview
Synapseindia Dot Net Development - Programming Overview
What is to be taught?:
Why teach C?
Brackets
define code blocks
Library command
#
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
;
}
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
int start_time;
int no_students;
double course_mark;
/* This is a bit better */
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;
}
/* Prints 6 sets i to 7 */
What is a function?
An example function
#include <stdio.h>
int maximum (int, int);
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
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
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
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!!!