You are on page 1of 17

LECTURE 3-1

C PROGRAMMING III
March 22, 2016
Outline
• Loop: for and while
• Pointer
• Passing by Values, Passing by References
• scanf() function to get User’s input
• Arrays
for-Loop
The following C-code prints from 1 thru 10
#include “stdio.h”
int main ()
{
int i;

for(i=1; i<=10; i++)


{ printf(”%d\n”, i ); }
}

Initial value of index variable i

Conditional: continue the loop if T, stop if not.

Increment or decrement of the index variable


while-Loop
The following C-code prints from 1 thru 10
#include “stdio.h”
int main ()
{
int i;

i=0;
while (i<10)
{ i++; printf(”%d\n”, i ); }
}

‘do-while’ is the same as ‘while’, but conditional check is done after the
loop body is executed.
do
{ i++; printf(”%d\n”, i ); } while (i<10);
Loop control
• Nested Loops can be used
for(…)
{
for(…)
{
while(…) {…}
}
}

• Break: escape the current loop.


• Continue: continue the loop from the next step.
Getting the Memory Location [Address] of
a Variable
Try to run the following C-code
#include “stdio.h”
int main ()
{
int var1;
char var2;

printf("Address of var1 variable: %x\n", &var1 );


printf("Address of var2 variable: %x\n", &var2 );

return 0;
}

The result looks


‘&’ means to get address of the
Address of var1 variable: bff5a400 variable
Address of var2 variable: bff5a3f6
Pointer
Pointer Variable

• A pointer is a variable whose value is the address of another variable.


• Like any variable or constant, you must declare a pointer before you use it to store
address of any variable.
• To declare a pointer variable, do it like ‘int *x;’ for integer variable’s pointer, and
‘float *y;’ for float-variable’s pointer, etc.
Pointer Example
Try to run the following code ip bffd8b3c

#include “stdio.h”
int main () 20
{
var
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */ *ip
ip = &var; /* store address of var in pointer variable*/

printf("Address of var variable: %x\n", &var );

/* address stored in pointer variable */


printf("Address stored in ip variable: %x\n", ip );

/* access the value using the pointer */


printf("Value of *ip variable: %d\n", *ip ); Result looks
Address of var variable: bffd8b3c
return 0; Address stored in ip variable: bffd8b3c
} Value of *ip variable: 20
Null Pointer

Initializing a Pointer by NULL

#include “stdio.h”
int main ()
{
int *ptr = NULL;

printf("The value of ptr is : %x\n", ptr );

return 0;
}
• It is a good habit to assign a NULL value to a pointer
Result variable in case you do not have exact address to be
The value of ptr is 0 assigned.
• This is done at the time of variable declaration.
• A pointer that is assigned NULL is called a null pointer.
Pointer Operations
Concept Description
C - Pointer arithmetic There are four arithmetic operators that can be
used on pointers: ++, --, +, -
C - Pointer to pointer C allows you to have pointer on a pointer and so on.
Passing pointers to functions Passing an argument by reference (i.e. by address)
in C enables the passed argument to be changed in the
called function.
Return pointer from functions C allows a function to return a pointer. Useful in
in C returning a dynamically allocated array.

aace714c bffd8b3c

aace714c bffd8b3c 20
ipp ip var
int **ipp int *ip int var
*ip, **ipp
Passing by Values and by References of
Function Arguments
Passing by Values Passing by References
#include “stdio.h” #include “stdio.h”

int main() int main()


{ {
float a=1,b=2,c; float a=1,b=2,c;
float f(float,float); float f(float *,float *);

c=f(a,b); c=f(&a,&b);
printf(“result=%f a=%f b=%f\n”,c, a,b); printf(“result=%f a=%f b=%f\n”,c, a,b);
} }

float f(float x1,float x2) float f(float *x1,float *x2)


{ {
x1++; x2++; (*x1)++; (*x2)++;
return x1+x2; return (*x1)+(*x2);
} }
Get User’s Input by scanf() function
The format of scanf() is very similar to printf(), but it uses ‘call-by-reference’.

Example.

#include “stdio.h”
#include “stdlib.h”

main()
{
float x,y;
int a;

scanf(“%g %g %d”,&x,&y,&a);

printf(“x=%g y=%g a=%d\n”,x,y,a);


}
One-dimensional Array
All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
Declaring Arrays Initializing Arrays
type arrayName [ arraySize ]; You can initialize array in C either one by one
The arraySize must be an integer constant or using a single statement as follows:
greater than zero and type can be any valid C double balance[5] = {1000.0, 2.0, 3.4, 17.0,
data type. 50.0};
For example, double balance[] = {1000.0, 2.0, 3.4, 17.0,
double balance[10]; 50.0};
Now balance is an array variable which is balance[0]=1000; balance[1]=2.0; …
sufficient to hold up to 10 double numbers.
Multi-dimensional Array
All arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
Declaring Arrays Initializing multi-dimensional Arrays
type arrayName [ arraySize1 ][arraySize2][…]; You can initialize array in C either one by one
For example, or using a single statement as follows:
double balance[10][20];
Now balance is an array variable which is double balance[2][3] = {{1.2, 2.0,-1.3},
sufficient to hold up to 10x20 double numbers. {0.1,02,0.3}};
Exercise
Exercise 1
Using a for-loop, make a program to calculate the sum from n1 through n2. Use
scanf() function to get integers n1 and n2.

Exercise 2
Type the programs on p. 11 of this note by yourself, compile them, and compare the
results.
Homework
Question 1
Make a C-program, which calculates N! and N!!. N can be input by scanf() function.

Question 2
Using 2D arrays, make any two 3x3 matrices A and B. Elements of the matrices can
be determined as you want either by ‘array initialization’ inside the main function,
or ‘scanf()’. Calculate the product of AB and BA. Using printf(), print the result on
the screen.

Due on midnight, March 29, 2015


END OF
LECTURE 3-1

You might also like