C Programming Basics Overview Basic Program Construction Output

C++ Programming Basics

Overview �Basic Program Construction �Output using cout �Directives �Comments �Integer Variables �Character Variables �Input using cin �Floating Point Types �Type bool �Endl and setw Manipulators �Type Conversion �Arithmatic Operators �Libaray Functions

Basic Program Construction #include <iostream> int main() { cout<<” Every age has a language of its ownn ”; return 0; } • • Fucntion Name Braces and Function Body Program state ments White spaces: ignored by compiler such as spaces, carriage returns, linefeeds, tabs

Always start with main() �Program entry point �Control always goes to main() before excuting other functions � main() function may also contain calls to other stand alone functions and member functions in various objects

Output using Cout<< �Cout is an object predefined in C++ corresposnds to the standard output stream �Standard output stream flows to the screen display �Cout<<”Every age has a language of its own n”; �<< Operator is called insertion or put to operator �String Constant is array of characters and retains its value throughout the program existance

Directives �Preprocessor Directives �It is instruction to the compiler �A part of compiler called preprocessor deals with directives before it begin real compilation process. �E. g. #include <iostream. h> �#include tells compiler to insert another file into source file �Same as pasting a block of text into a document with your word processor

Directives �Using Directive �C++ program is divided into namespaces �A namespace is part of the program in which certain names are recognized; outside namespace they are unknown �E. g. Using namespace std; � All program statements within std namespace �Used when working in multifiles environment �If not using directive then we need to add � Std: : cout<<”Hello World”;

Comments �Helps in understanding code: Optional �Syntax �// single line comment �/*this is multiline comment*/ �Can be inserted anywhere in program

Variables �Variable has symbolic name and can have varity of values �Variables are located in particular location in computer memory �Declaration introduce variable’s name and specify its type such as int � Int a; � Long num; �Definition introduce intialization of variable � Int a = 10; �Identifer vs keyword �Assignment statements � var 1 = 20; � var 2 = var 1 + 10;

Integer Variables �Integer variables represent integer numbers like 1, 30, 000 and - 26 �The amount of memory occupied by integers is system dependent � 32 bit system such as windows allows int to occupy 4 bytes � Ranges occupied by various types are listed in header file LIMITS

Character Variables �Type char stores integer having range from -128 to 127 �Occupy only 1 byte of memory �Commonly use to store ASCII characters such as ‘a’, ‘B’, ‘$’, ‘ 3’ etc �Character Constants vs String Constant �Character constant is translated into equivalent in ASCII code e. g. Constant ‘a’ will be translated into 97 �Escape Squences � causes an escape from normal way character are interpreted � t, n,

Input with cin>> � Keyword cin in an object defined in C++ to correspond to standard input stream. � >> is the extraction or get from operator #include<iostream> Using namespace std; Int main() { int ftemp; cout <<”Enter temperature in fahrenheit: ”; cin>>ftemp; int ctemp = (ftemp – 32) * 5/9; cout<<”Equivalent in Celsius is: ”; <<ctemp<<‘n’; return 0; }

Floating Point Types �Floating point variables represent numbers with decimal place e. g. 3. 1415927, 0. 0000635 and -10. 2 �Float �Stores number in range 3. 4 x 10 -38 to 3. 4 x 10 38 �Seven digits precision � Double � 8 bytes of storage and handles number in range from 1. 7 x 10 -308 to 1. 7 x 10 308 � 15 digits precision

Constants �Const Qualifier �const float PI = 3. 14159 F; // type const float � Value of variable will not change throughout the program � #define Directive �Can also be specified using #define directive �#define PI 3. 14159 �PI will be replaced by its value throughout the program

Variable Type Summary Name Description Size* Range* signed: -128 to 127 unsigned: 0 to 255 char Character or small integer. 1 byte short int (short) Short Integer. 2 bytes signed: -32768 to 32767 unsigned: 0 to 65535 int Integer. 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 long int (long) Long integer. 4 bytes signed: -2147483648 to 2147483647 unsigned: 0 to 4294967295 bool Boolean value. It can take one 1 byte of two values: true or false float Floating point number. 4 bytes +/- 3. 4 e +/- 38 (~7 digits) double Double precision floating point number. 8 bytes +/- 1. 7 e +/- 308 (~15 digits) long double Long double precision floating 8 bytes point number. +/- 1. 7 e +/- 308 (~15 digits) wchar_t Wide character. 1 wide character 2 or 4 bytes

Setw and endl Manipulator �Manipulators are used with insertion operator (<<) to modify or manipulate the way data is displayed �Endl manipulator for new line �setw() manipulator causes the number(string) that follows it in the stream to be printed within a field n characters wide �n is argument to setw(n) �Right justified �IOMANIP header file used for these manipulators

Type Conversion �Automatic Conversion �When two operands of different data types are encountered in the same expression , the lower type variable is converted to the type of higher type variable. �Casts ?

Arithmetic Operators + * / % addition subtraction multiplication division Modulo/Remainder

Arithmetic Assignment Operator �Combines an arithmetic operator and an assignment operator and eliminate repeated operand �total = total + items can be writen as total+=items � Increment Operator � Add 1 to the value of an existing variable � E. g. count = count +1 // adds 1 to count � � Count +=1; // adds 1 to count ++count; // adds 1 to count

Cont’d �Increment operator can be used in two ways �Prefix �Operator preceeds the variable �Total. Weight = avg. Weight * ++count; �Postfix �Operator follows variable �total. Weight = avg. Weight * count++; �Decrement Operator (--) �Same as increment but subtracts 1 from its operand �Can be used in both prefix and post fix forms

Library Functions �Many activities in C++ are carried out by library functions �Performs file access, mathematical computation and data conversions �E. g. Sqrt(number) is libaray function in cmath header file � Header File � File that contain declaration of any library functions used � E. g. #include <cmath> header file of SQRT() function

Library files vs Header files �Library files contain actual machine-executable code for functions �Have. lib file extension �Sqrt() function is part of library files �Automatically extracted from by the linker �Header files contain information for particular group of functions �Two Ways to use #include �#include ”myheader. h” // search in current directory �#include<cmath> �Search for particular header file in INCLUDE directory
- Slides: 22