File

advertisement
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 Type
Description
Syntax
Character data
char
No of total Bytes
occupied in
memory
1
2. Integer
Signed whole Integer
int
2
3. Float
Floating point number
float
4
Double Precision Floating
point number
Valueless
double
8
void
0
1. Character
4. Double
5. Void
Data Types
Type
Size in Bytes
Data Storage Range
int
2
-32768 to 32767
short int
2
-32768 to 32767
long int
4
-2147483648 to 21474836487
unsigned int
2
0 to 65535
unsigned long int
4
0 to 4294967295
float
4
3.4 x 10 Ʌ - 38 to 3.4 x 10 Ʌ + 38
long float
8
1.7x 10 Ʌ - 308 to 1.7 x 10 Ʌ + 308
double
8
1.7x 10 Ʌ - 308 to 1.7 x 10 Ʌ + 308
long double
10
3.4 x 10 Ʌ - 4932 to 3.4 x 10 Ʌ + 4932
char
1
For string, 1 bytes to 65535 bytes
Declaration/Initialization of Variables
• Declaration Sytax
– [Type]<space>[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<conio.h>
#include<stdio.h>
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.
%c
%s
%d
%f
%e
%u
%x
%o
l
Single character
String
Signed decimal integer
Floating point
Exponential notation
Unsigned Decimal integer
Unsigned Hexadecimal integer
Unsigned Octal integer
Prefi used with %d,%u,%x,%o (long) e.g %ld
Field Width Specifiers
– printf(“Age is %3d”,40);
1
A
g
e
i
s
2
3
4
0
Escape Sequences
• Special non-printing characters to control printing on
output device
• Combination of backslash ‘\’ and a code
\n
\t
\b
\r
\f
\’
\”
\\
\xdd
\ ddd
New line
Tab ( move 8 characters forward )
Backspace
Carriage Return
Form feed( move to top of next page on the printer )
Single Quote
Double Quote
Backslash
ASCII code in hexadecimal notation
ASCII code in Octal notation ( each d represents a
digit )
“getch/getche” Function
• get get from outside
• ch character
• e
echo ( write )
#include<stdio.h>
#include<conio.h>
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
Operator
Meaning
– (3*5)
+
Addition
– (4-15)
Subtraction
– -11 + 2
*
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
Operator
Meanings
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater then or equal to
==
Equal to
!=
Not equal to
Output:
Is age less than 20 ? 1
Is age less than 20 ? 0
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 ( );
}
Precedence between Operators
• Arithmetic Operators have higher precedence i.e. they are
evaluated before the Relational Operators.
#include<stdio.h>
#include<conio.h>
void main (void)
{
printf( “ Answer is %d “, 1 < 2 + 4 );
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
Download