Professional Documents
Culture Documents
C and C Report
C and C Report
of code, by breaking large file into smaller modules Comments in C provides easy
readability C is a powerful language
Structure Of C Program
Execution Of a C Program
Executing a C program involves following steps:
CS320 - C Programming
9
Pointers
• Declaring a pointer
int *iPtr; float *fPtr;
}
CS320 - C Programming 13
extern Storage Class
• Default storage class for variables defined outside a function body. • Memory
allocated for the life of process • Initialized to zero or initial value. • Visable
to functions that follow definition • Example:
int age; main() {...}
CS320 - C Programming 14
static Storage Class
• Memory allocated for the life of the process • Initialized to zero or initial
value. • Visable to containing block • Maintains value over invocations • Example:
myfunc() { static int age;
}
CS320 - C Programming 15
register Storage Class
• Compiler recommendation use CPU register • otherwise set to auto • can only be
part of control block
myfunc() { register int age; }
CS320 - C Programming 16
Classifications of Data Types
• Built-in data types
– Fundamental data types (int, char, double, float, void, pointer) – Derived data
types (array, string, structure)
18
Global and Local Variables
• Local Variables
– These variables are declared inside some functions. – Life time of a local
variable is the entire execution period of the function in which it is defined. –
Cannot be accessed by any other function. – In general variables declared inside a
block are accessible only in that block.
/* Compute Area and Perimeter of a circle */ #include <stdio.h> float pi = 3.14159;
/* Global */ main() { float rad;
/* Local */
printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { float area =
pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n” , area );
printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”); printf( “Area
= %f\n” , area ); }
Global and Local Variables
• Global Variables
– These variables are declared outside all functions. – Life time of a global
variable is the entire execution period of the program. – Can be accessed by any
function defined below the declaration, in a file.
/* Compute Area and Perimeter of a circle */ #include <stdio.h> float pi = 3.14159;
/* Global */ main() { float rad;
/* Local */
printf( “Enter the radius “ ); scanf(“%f” , &rad); if ( rad > 0.0 ) { float area =
pi * rad * rad; float peri = 2 * pi * rad; printf( “Area = %f\n” , area );
printf( “Peri = %f\n” , peri ); } else printf( “Negative radius\n”); printf( “Area
= %f\n” , area ); }
An Overview of C++
Introduction
• C++ is the C programmer’s answer to ObjectOriented 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
• Every object we create will require some sort of initialization. • A class’s
constructor is automatically called by the compiler each time an object of that
class is created. • A constructor function has the same name as the class and has
no return type. • There is no explicit way to call the constructor.
26
Destructors
• The complement of a constructor is the destructor. • This function is
automatically called by the compiler when an object is destroyed. • The name of a
destructor is the name of its class, preceded by a ~. • There is explicit way to
call the destructor but highly discouraged.
27
C++ Program
#include<iostream.h> #include<conio.h> class programming { private: int variable;
public: void input_value() { cout << "In function input_value, Enter an integer\n";
cin >> variable; } void output_value() { cout << "Variable entered is "; cout <<
variable << "\n"; } }; main() { programming object; object.input_value();
object.output_value(); //object.variable; Will produce an error because variable is
private return 0; }
Thank You