Digital Image Processing C Basics
Digital Image Processing C Basics
Digital Image Processing C Basics
USING C
CHAPTER -1: BASICS OF C-LANGUAGE
CONTENTS
C-coding in MS Visual
Studio 2010
Calculate the area of
triangle
Solve the quadratic
equation
Calculate factorial of a
number
Accept characters and
terminate program
Output arrays and pointer
values
Input values from external
text file
Dynamic memory
allocation for calculations
INTRODUCTION
If you are already an expert programmer of C and you are merely
looking for some ideas or codes on how to process images then this is
the right tutorial for you. If you are a beginner in C programming then
you are recommended to more detailed tutorials or books. Nevertheless,
we begin with some of the basics of C in this chapter.
In the entire tutorial including all chapters we will be using Microsoft
Visual Studio 2010. Interestingly, C coding in MS Visual Studio is not so
straight forward as the way it used to be in MS Visual Studio 2006.
Therefore our first task would be to familiarize oneself with Microsoft
Visual Studio 2010 and to learn the way to code, debug, compile and
execute C programs.
JPS Gill
jvpsgill@gmail.com
5
3
7
6
12
8
12
13
Figure 1: Steps showing on how to use Visual Studio 2010 for C programming.
JPS Gill
jvpsgill@gmail.com
Now that we have learnt how to use Visual Studio 2010 for C programming, lets practice few examples to familiarize
ourselves with the environment and gain some insight into the C coding.
Program 1:
#include<stdio.h>
#include<math.h>
main()
{
int i=0;
double base,height,area;
for (i=0;i<=5;i++)
{
printf("enter value of base and
height\n");
scanf("%lf%lf",&base,&height);
area=0.5*base*height;
printf("area of triangle
=%lf\n\n",area);
}
}
#include<stdio.h>
#include<math.h>
When you build an application, C compiler consists of three individual modules or programs. The first is a
preprocessor, the second is a compiler, and the last is a linker or assembler. Any symbol with a # is a preprocessor
command. The preprocessor will go through your source code and execute any of its commands. #include is one of
these commands that essentially links previously written code into your program. printf is a function. When you use
it, it must be defined somewhere. stdio.h is a header file, where this and other similar functions are defined. stdio.h
stands for standard input output header. <> symbols mean that the header is located in the standard search path. The
<> tell the compiler to search for stdio.h in the path specified by the include environment variable first. This allows
newer libraries to supercede older ones without having to make any changes manually.
main ()
All C language programs must have a main() function. It's the core of every program. It's required. The main() function
doesn't really have to do anything other than be present inside your C source code. Eventually, it contains instructions
that tell the computer to carry out whatever task your program is designed to do. When the operating system runs a
program in C, it passes control of the computer over to that program. This is like the captain of a huge ocean liner
handing you the wheel. Aside from any fears that may induce, the key point is that the operating system needs to know
where inside your program the control needs to be passed. In the case of a C language program, it's the main() function
that the operating system is looking for (adopted from http://www.dummies.com).
{}
Inside the parenthesis {} the code that is used to perform some action resides. It also marks the beginning and end of
a code snippet. In case of program 1, we are calculating the area of triangle. We use for loop to run the program for 5
JPS Gill
jvpsgill@gmail.com
times. The code lines within the {} after the for loop will be
executed 5 times. printf is a function that is used to print or show
messages on the screen. scanf is a function that is used to accept the
values from the user and initialize the variables base and height
that have been declared outside the loop. Once the values are
accepted from the user, the output is shown on the screen which is
the area of triangle.
Program 2:
Program 2 has been broken down into two functions i.e. the main
function and a triangle function. The main function is used to
initialize and accept values from the user for base and height
variables, which are then passed to the triangle function by calling
it using the following code line.
area = triangle(base, height);
The triangle function accepts the two values received from the
main function and calculates the area of triangle. Finally it passes
the computed area to the main function. Main function then prints
the accepted area of the triangle.
Program 3a:
#include<stdio.h>
#include"Program 3c.h"
main()
{
int i=0,x=0;
double base,height,area;
for (i=0;i<=2;i++)
{
x=i+1;
printf("\nentry no.= %d",x);
printf("\ni am in function main\n");
printf("enter value of base and
height\n");
scanf("%lf%lf",&base,&height);
area=triangle(base,height);
printf("i am back in function
main\n");
printf("area of triangle
=%lf\n\n\n\n",area);
}
printf("\nThe Program Ends
Here\n");
}
JPS Gill
jvpsgill@gmail.com
Program 2:
#include<stdio.h>
#include<math.h>
main()
{
int i=0;
double base,height,area;
for (i=0;i<=5;i++)
{
printf("\n i am in function main\n");
printf("enter value of base and
height\n");
scanf("%lf%lf",&base,&height);
area=triangle(base,height);
printf("i am back in function
main\n");
printf("area of triangle
=%lf\n\n\n\n",area);
}
}
double triangle(double base,double
height)
{
printf("i am in function area\n");
printf("calculating area\n\n");
return 0.5*base*height;
}
Program 3:
Similar to program 2 where the source code was broken down into two
functions to perform the task, in program 3 the same code is written in
two separate programs (Program 3a & Program 3b). An additional
third program (Program 3c) is used to link the two programs.
Interestingly you will notice that the header of Program 3a and
Program 3b now have the name of the Program 3c as follows:
#includeProgram 3c.h
In order to calculate the area of triangle, since the main() function lies
in Program 3a, it must be executed first. Within the program 3a, a
function named triangle is called and passed on the values of base
and height. Since the function triangle is not a part of the program
3a, it will not execute without the header #includeProgram 3c.h.
The use of header will allow it to look for the function triangle in the
program 3c. Program 3c will transfer the command to the program 3b
and finally the program will run successfully.
Program 3b:
#include<stdio.h>
#include"Program 3c.h"
double triangle(double base,double
height)
{
printf("i am in function area\n");
printf("calculating area\n\n");
return 0.5*base*height;
}
Program 3c:
double triangle(double base,double
height);
EXAMPLE PROGRAMS
Following are some of the examples of C programs for practice. Only the source code is provided for each of the
problems. For detailed discussion or analysis contact jvpsgill@gmail.com.
Program to calculate factorial
#include<stdio.h>
int factor(int n);
main()
{
while(1)
{
int n,factorial;
printf("please enter the value for calculating factorial\n");
scanf("%d",&n);
factorial=factor(n);
printf("factorial of %d = %d\n",n,factorial);
}
}
int factor(int n)
{
int i,factor=1;
for(i=1;i<=n;i++)
{
factor*=i;
}
return factor;
}
JPS Gill
jvpsgill@gmail.com
Program to accept character values and print exit if condition not met
#include<stdio.h>
main()
{
char x;
do
{
printf("Enter The Character\n");
scanf("%c",&x);
}
while (x!='z');
}
JPS Gill
jvpsgill@gmail.com
printf("%d\t%d\t%d\t%d\t%d\t%d\n",arr[i],p1[i],*(p1+i),*p2,p2);
p2++;
Program to accept data from text file and to allocate and use dynamic memory
#include<stdio.h>
#include<math.h>
#include<malloc.h>
double input(double *data, int n);
double std_dev(double *data, int n);
main()
{
int n;
double stand_dev;
double *prt;
printf("enter no of elements\n");
scanf("%d",&n);
prt=malloc(sizeof(double)*n);
if(prt==NULL)
printf("not enough memory\n");
input(prt,n);
stand_dev=std_dev(prt,n);
printf("standard deviation=%lf\n\n",stand_dev);
free(prt);
JPS Gill
jvpsgill@gmail.com