Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 C Building Block Chapter 2

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

3 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

4 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

5 Data Types TypeSize in BytesData Storage Range int2-32768 to 32767 short int2-32768 to 32767 long int4-2147483648 to 21474836487 unsigned int20 to 65535 unsigned long int40 to 4294967295 float43.4 x 10 Ʌ - 38 to 3.4 x 10 Ʌ + 38 long float81.7x 10 Ʌ - 308 to 1.7 x 10 Ʌ + 308 double81.7x 10 Ʌ - 308 to 1.7 x 10 Ʌ + 308 long double103.4 x 10 Ʌ - 4932 to 3.4 x 10 Ʌ + 4932 char1For string, 1 bytes to 65535 bytes

6 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;

7 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 ); }

8 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.

9 Field Width Specifiers – printf(“Age is %3d”,40); 123 Ageis40

10 Escape Sequences Special non-printing characters to control printing on output device Combination of backslash ‘\’ and a code

11 “ 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); }

12 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) – -11 + 2 OperatorMeaning +Addition -Subtraction *Multiplication /Division %For remainder

13 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;

14 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

15 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

16 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 < 2 + 4 ); getch( ); } Output: Answer = 5 (Wrong) Answer = 1 ( Right )

17 Comments Helpful to understand the source code Invisible for compiler /* Initialize the variables*/ Nested comments /* Print/*Result*/Prompt*/ Multiple Line comments /* Print *Result *Prompt */

18 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


Download ppt "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."

Similar presentations


Ads by Google