Data Types and their format specifiers
1. Integer - 4 bytes
Format specifier- %d
2. Float - 4 bytes
Format specifier- %f
3. Double- 8 bytes
Format specifier- %lf
4. Long double- 16 bytes
Format specifier- %Lf
5. Character- 1 byte
Format specifier- %c
Tokens-
Smallest individual element that can be easily identified by the compiler.
1. Keywords
Unique words or Reserved words
Example:
auto
switch
enum
break
continue
if
else
case
for
do
while
extern
2. Identifiers:
A valid identifier can have letters (both uppercase and lowercase letters), digits
and underscores.
The first letter of an identifier should be either a letter or an underscore.
You cannot use keywords like int, while ,etc. as identifiers.
Must not contain a white space.
3. Operators:
a. Unary Operators:
Those operators that require only a single operand to act upon are known
as unary operators. For Example increment and decrement operators.
Increment operators:
Pre (before) and post(after):
1. Pre increment and decrement:
a=5;
b=++a; (or) b= - -a;
printf(“%d %d”,a,b);
` 2. Post increment and decrement:
a=5;
b=a++; (or) b=a- -;
printf(“%d %d”,a,b);
b. Binary Operators:
*Acts on two operands*
Arithmetic- +, -, /, *
Relational- >, <, ==, >=, <=
Logical- Logical OR(||) and Logical AND (&&)
Assignment- ‘=’
Bitwise-
Bitwise And- &
Bitwise OR- |
BItwise X-OR- ^
Shift left- <<
Shift right- >>
Complement - ~
Ex:
1&1=1
1||1=1
9<<2
9- 00001001
Ans:
00100100
9>>2
Ans:
00000010
c. Constants: fixed values
2 ways to declare:
1. #define pi 3.14
2. const int x=3.14;
d. Characters: ASCII values
A-Z = 65-90
a-z = 97-122
e. Special Characters
ex: \n=new line
\t= tab(4 spaces)
Scope of variables: (where can it be accessed? Initial value? Lifetime?)
1. Local variables:
Accessibility: Within the block it is declared in
Default Value: Junk or garbage value
Lifetime: Til the block of statements terminate
Ex:
int x;
auto int x;
2. Static variables:
Accessibility: Can only get declared once, retains value, within block
Default value: 0
Lifetime: Till program ends
Ex:
void fn()
{
static int x;
x=x+1;
printf(“%d”,x);
}
int main()
{
fn();
fn();
fn();
}
3. Global variables
Accessibility: Throughout the program
Default value: 0
Lifetime: Till program ends
Ex:
int x;
void fn()
{
x=x+1;
printf(“%d”,x);
}
int main()
{
fn();
printf(“%d”,x);
}
4. External variables (scope is same as global, must use ‘extern’ keyword)
void fn()
{
extern int x; // no memory space allocated
x=x+1;
printf(“%d”,x);
}
int x; // memory space is allocated
int main()
{
fn();
printf(“%d”,x);
}
Write a menu driven program for a simple calculator using a user defined function
for addition, subtraction, multiplication and division.
Menu driven program- Provides a list of choices or options for the user, so
that they can choose any one of the options.
1. Tell the user what is available(+, -, *, /)
2. Define a function for each operation
3. Use the function to get the desired output
Output:
Enter any two integers: 2 3
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice: 1
You have picked addition: 5