C Building Block Chapter 2
Variables A variable is a space in the computer’s memory set aside for a certain kind of data and given a name for easy reference. A quantity whose value may change during execution of the program Variable represent the memory location in the computer memory Data store into memory location Name of variable remain fixed Data may change time to time Also known as object in C++ Consists of alphabets and digits
Rules for Variable Names First Character must be alphabetic character Underscore can be used as First Character Blank Space are not allowed Special Characters i.e. # ! are not allowed Reserved words are not allowed Variable name length depend upon compiler of C++ A variable named declared with one data type cannot be used to declare another data type. Case sensitive
Data Types in C++ Five Basic data types Data TypeDescriptionSyntax No of total Bytes occupied in memory 1. CharacterCharacter datachar1 2. IntegerSigned whole Integerint2 3. FloatFloating point numberfloat4 4. Double Double Precision Floating point number double8 5. VoidValuelessvoid0
Data Types TypeSize in BytesData Storage Range int to short int to long int to unsigned int20 to unsigned long int40 to float43.4 x 10 Ʌ - 38 to 3.4 x 10 Ʌ + 38 long float81.7x 10 Ʌ to 1.7 x 10 Ʌ double81.7x 10 Ʌ to 1.7 x 10 Ʌ long double103.4 x 10 Ʌ to 3.4 x 10 Ʌ char1For string, 1 bytes to bytes
Declaration/Initialization of Variables Declaration Sytax – [Type] [list of variables]; – int a, b, c; Initialization - Assign value at the time of declaration – int a= 110, b= 90, c;
Input/Outputs “printf ”– Output Stream – printf(“Output String”,var1[,var2…]); – printf(“Output String”); “scanf ”– Input Stream – scanf(&var1[,&var2…]); #include void main (void) { float years, days; printf ( “ Please type your age in years. “ ); scanf ( “ %f “, &years ); days = years * 365; printf ( “ You are %.1f days old.”, days ); }
Format Specifiers The format specifier in printf() determines the interpretation of a variable’s type, the width of the field, the number of decimal places printed and the justification.
Field Width Specifiers – printf(“Age is %3d”,40); 123 Ageis40
Escape Sequences Special non-printing characters to control printing on output device Combination of backslash ‘\’ and a code
“ getch/getche ” Function get get from outside ch character e echo ( write ) #include void main() { char ch; printf(“Type any character: ”); ch = getche(); printf(“\nCharacter is :%c ”,ch); }
Operators Address Operator (&) Provide the memory address of variable – int i = 2; printf(“value = %d, address = %d”, i,&i); – value = 2, address= 3536 Arithmetic Operators Order of Precedence – Order in which arithmetic expression evaluate e.g. (4-(3*5))+2 – (3*5) – (4-15) – OperatorMeaning +Addition -Subtraction *Multiplication /Division %For remainder
Compound Assignment / Arithmetic Assignment Add, subtract, multiply or divide a value to or from a variable with out writing the variable on either side of assignment operator (=) – xy += 10 ; equal to xy = xy +10;
Increment & Decrement Operators Increment Operator represent by double plus(++) – x++ ; equal to x = x +1; Decrement Operator represent by double minus(--) – x-- ; equal to x = x -1; Prefix / Postfix – Calculation change – x--, --x, x++,++x sum = a+b+ c-- will not be equal to a+b+ --c
Rational Operators OperatorMeanings <Less than >Greater than <=Less than or equal to >=Greater then or equal to ==Equal to !=Not equal to void main (void) { int age ; age = 15; printf ( “ Is age less than 20 ? %d \n “, age < 20 ); age = 30; printf ( “ Is age less than 20 ? %d \n “, age < 20 ); getch ( ); } Output: Is age less than 20 ? 1 Is age less than 20 ? 0
Precedence between Operators Arithmetic Operators have higher precedence i.e. they are evaluated before the Relational Operators. #include void main (void) { printf( “ Answer is %d “, 1 < ); getch( ); } Output: Answer = 5 (Wrong) Answer = 1 ( Right )
Comments Helpful to understand the source code Invisible for compiler /* Initialize the variables*/ Nested comments /* Print/*Result*/Prompt*/ Multiple Line comments /* Print *Result *Prompt */
Lab Work Write Program that print age in minutes. Hint: [1 year = 365 days 1 days = 24 hours 1 hour = 60 min] Write a program to calculate radius of circle. radius = 2πr π = 3.14