230 likes | 405 Views
Programming Fundamentals. Lecture #03. #include < iostream.h > main ( ) { cout << “ Welcome to Virtual University “; }. # include:This is a pre-processor directive. It is not part of our program; it is an
E N D
Programming Fundamentals Lecture #03
#include <iostream.h> main ( ) { cout << “ Welcome to Virtual University “; }
#include:This is a pre-processor directive. It is not part of our program; it is an instruction to the compiler.Ittells the C compiler to include the contents of a file Your program will almost certainly want to send stuff to the screen and read things from the keyboard. iostream.h is the name of the file in which has code to do that work for you A C program is made up of a large number of functions. Each of these is given a name by the programmer and they refer to each other as the program runs C regards the name "main" as a special case and will run this function first.
If you forget to have a main function, or mistype the name, the compiler will give you an error. parentheses (“( )”, normal brackets) with main. Here the parentheses contain nothing. There may be something written inside the parentheses. There is a curly bracket also called braces("{ }"). For every open brace there must be a matching close. Braces allows to group together pieces of a program. The body of main is enclosed in braces.
Coutknown as out put stream in C and C++ Cout takes data from computer and sends it to the output. The thing between the double quotes (“ ”) is known as character string. there is a numerical address for each location of memory (block).It is difficult for us to handle these numerical addresses in our programs. So we give a name to these locations. These names are variables.
Variable • X
Variable Variable starts with • Character • Underscore _ (Not Recommended)
Variable Variable is the name of a location in the memory e.g. x= 2;
Variable In a program a variable has: • Name • Type • Size • Value
= x = 2 X 2
Assignment Operator L.H.S = R.H.S. X+ 3 = y + 4 Wrong Z = x +4 x +4 = Z Wrong
10 X = 10 ; X = 30 ; X 30 X
X = X + 1; • X = 10 + 1 11 X 11
Data type • inti ; -> Declaration line
#include <iostream.h> main ( ) { int x ; int y ; int z ; x = 10 ; y = 20 ; z = x + y ; cout << " x = " ; cout << x ; cout << " y = " ; cout << y ; cout << " z =x + y = " ; cout << z ; }
int x, y, z ; int x; int y; int z ;
Data Types • Int • short • long • Float(real numbers or floating point) • Double(Larg real num…twice of Float) • Char(x=‘a’)
Arithmetic operators Plus + Minus - Multiply * Divide / Modulus %
Arithmetic operators i + j x * y a / b a % b
% = Remainder 5 % 2 = 1 2 % 2 = 0
4 / 2 = 2 5 / 2 = ?
Precedence • Highest: ( ) • Next: * , / , % • Lowest: + , -