Title: Further C Programming
1- Further C Programming
- Variable types
- Loops
- Conditional statements
- Local and Global variables
- Arrays
- Pointers
2Types
- Variable Types
- char character
- short short integer -32767 to 32767
- int integer -/2,147,483,647
- long int long integer -/
9E18 - float floating point 1.2E-38 to 3.4E38
- double double precision 2.2E-308 to 1.8E308
3for loop
- Can set up loops to repeat sections of code
- for (count 1 count lt3 count)
-
- printf("d \n", count)
- count is short hand for countcount1
4- While Loop
- Useful when we dont know how many times the loop
will repeat. - Eg
- while (agt0)
-
- fscanf(fin,"d d",a,b)
- c ab
- printf("d d d \n", a,b,c)
-
- Use to read a file to the end.
- while(!feof(fin))fscanf(fin,d d,a,b)
5- Conditional Statements
- Most common example is the ifelse construction
- Eg
- if(mark gt 40)
- printf(pass\n)
- else
- printf(fail\n)
- Boolian operators
-
! - Equals And Or
Not
6- Functions and Variables
- Common mathematical functions are defined in the
header file math.h - include ltmath.hgt
- Often useful to define your own functions.
- Functions must be prototyped before the main()
loop is reached.
7 / Program to calculate the product of two
numbers / / and the sine of the product/
include ltstdio.hgt include ltmath.hgt int
product(int, int) int main(void) int
a,b,c printf ("Enter a number between 1 and
100 ") scanf ("d", a) printf
("Enter a number between 1 and 100 ") scanf
("d", b) c product(a,b)
printf("d times d d \n", a, b, c)
printf("sin(d times d) f\n",a,b,sin(c))
return(0) int product(int x, int
y) int z zxy return(z)
8- Variables declared within a function are local to
that function. - i.e. their values are only know within the
function. -
- Changes made to the variables are not known
outside the function. - Variables declared above the main() loop are
global variables. - i.e. their values are known anywhere in
the program. - Changes can be made anywhere in the program.
9 / Program to calculate the product of two
numbers / / and the sine of the product/
include ltstdio.hgt include ltmath.hgt int
product(void) int a,b int main(void)
int c printf ("Enter a number between 1 and
100 ") scanf ("d", a) printf
("Enter a number between 1 and 100 ") scanf
("d", b) c product()
printf("d times d d \n", a, b, c)
printf("sin(d times d) f\n",a,b,sin(c))
return(0) int product(void) int
z zab return(z)
10- Arrays
- If you have lots of variables of the same type it
is inconvenient to have to define each separately - Eg. data1, data2, data3,
- Arrays are a way of storing a set of like
variables - Eg. double data100
- Values can be assigned directly
- Eg. double x423.4, 54.6, 66.1, 76.2
- or using loops
- Eg. for(i0 ilt4i)
- xisin(iM_PI/4)
11- For an array xN of size N, the elements are
labelled - x0, x1, x2, xN-2, xN-1
- Can be used just as other variables
- Eg.
- Sum20
- for(i0iltNi)
- sum2sum2xixi
- A common mistake is to access array elements that
dont exist. - Eg. xN
12Multidimensional Arrays
- int random_array101010
- for(a0alt10a)
-
- for(b0blt10b)
-
- for(c0clt10c)
-
- random_arrayabcrand()
-
-
-
-
13- Pointers
- When we define a variable x, the computer labels
a part of the memory x and stores the value of x
in that part of memory. - We can then use x as a variable in our
calculations. - Another way of accessing the variable x would be
to know where it is stored, i.e. its address. - We can define a pointer to x as a new type of
variable that stores the address of the variable
x.
14include ltstdio.hgt int main(void) double x
double p px x2.0
printf(xf\n, x) p3.0
printf(xf\n, x) return(0)
15- Pointers can be passed to functions to allow them
to change more than one variable. - Eg.
- include ltstdio.hgt
- void increase(int , int , int )
- int main(void)
-
- int x,y,z
-
- increase(x, y, z).
-
- void increase(int px, int py, int pz)
-
- pxpx1
- pypy1
16- Pointers and arrays are closely related.
- x0, x1, x2. are variables (eg.
doubles). - x0, x1, x2are their addresses.
- BUT
- x is a pointer to x0.
- Arrays elements can be accessed in two ways
- xi is the same as (xi)
17- Arrays can be passed between functions simply by
passing a pointer to the first array element. - Much quicker than copying one array to another.
- If you change elements of the array within the
function, these changes will be known everywhere
in the code.
18include ltstdio.hgt define N 5 void mean(double
) int main(void) int i double xN
for(i0iltNi) printf(input value d
,i) scanf(f,xi) mean(x)
return(0)
void mean(double x) int i double sum
sum0.0 for(i0iltNi) sumsumxi
printf(Mean f\n,sum/(N))