C and C Report
C and C Report
Types Of Functions:
C functions can be classified into two categories:
1. Library functions and
2. User-defined functions.
.
Arrays
An Array is a collection of identical data objects which are stored in
consecutive memory locations under a common heading or variable
name. The objects are called elements of the array and are numbered
consecutively 0,1,2,3 and so on. These numbers are called Index Values
or Subscripts of the Array.
Types of Arrays:
One Dimensional Array
Multi Dimensional Array
Pointers
CS320 - C Programming 9
Pointers
• Declaring a pointer
int *iPtr;
float *fPtr;
– iptr is a pointer to an integer
– fptr is a pointer to a float
• initializing pointers:
int idx = 100;
int *ptr = &idx;
OR
ptr = &idx;
CS320 - C Programming 10
swap the value of two variables
Storage Classes
CS320 - C Programming 13
extern Storage Class
CS320 - C Programming 18
Global and Local Variables
• Local
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
circle
circle */*/
#include <stdio.h>
#include <stdio.h>
Variables float
float pi
pi == 3.14159;
3.14159; /* /* Global
Global */
*/
– These variables are main()
main() {{
declared inside some float
floatrad;
rad;/*
/* Local
Local */
*/
functions.
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
– Life time of a local );
scanf(“%f” , &rad);
scanf(“%f” , &rad);
variable is the entire
execution period of the if
if (( rad
rad >> 0.0
0.0 )) {{
function in which it is float
float area
area == pi
pi ** rad
rad ** rad;
rad;
defined. float peri = 2 * pi *
float peri = 2 * pi * rad; rad;
– Cannot be accessed by any
printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
other function. printf( “Peri = %f\n” , peri );
printf( “Peri = %f\n” , peri );
– In general variables }}
declared inside a block else
else
are accessible only in printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
that block.
printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
}}
Global and Local Variables
• Global
/*
/* Compute
Compute Area
Area and
and Perimeter
Perimeter of
of aa
circle
circle */*/
#include <stdio.h>
#include <stdio.h>
Variables float
float pi
pi == 3.14159;
3.14159; /* /* Global
Global */
*/
– These variables are main()
main() {{
declared outside all float
floatrad;
rad;/*
/* Local
Local */
*/
functions.
printf(
printf( “Enter
“Enter the
the radius
radius ““ );
– Life time of a global );
scanf(“%f” , &rad);
scanf(“%f” , &rad);
variable is the entire
execution period of the if
if (( rad
rad >> 0.0
0.0 )) {{
program. float
float area
area == pi
pi ** rad
rad ** rad;
rad;
– Can be accessed by any float peri = 2 * pi *
float peri = 2 * pi * rad; rad;
function defined below
printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
the declaration, in a printf( “Peri = %f\n” , peri );
file. printf( “Peri = %f\n” , peri );
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
}}
An Overview of C++
Introduction
• C++ is the C programmer’s answer to Object-
Oriented Programming (OOP).
• C++ is an enhanced version of the C
language.
• C++ adds support for OOP without
sacrificing any of C’s power, or flexibility.
• C++ was invented in 1979 by Bjarne
Stroustrup at Bell Laboratories in Murray
Hill, New Jersey, USA.
22
What is OOP?
• OOP is a powerful way to approach the task
of programming.
• OOP encourages developers to decompose a
problem into its constituent parts.
• Each component becomes a self-contained
object that contains its own instructions and
data that relate to that object.
• So, complexity is reduced and the
programmer can manage larger programs.
23
Sample of C++ program
- #include <iostream.h>
int main()
{
/* program code */
return 0;
}
24
Classes: A First Look
• General syntax -
class class-name
{
// private functions and variables
public:
// public functions and variables
}object-list (optional);
25
Constructors