PPS ch2
PPS ch2
Extensibility
Elegant syntax
FEATURES Case sensitive
Data types in C
a b a&&b a||b
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 1
ASSIGNMENT OPERATORS
Assignment operators (=) is used to assign the result of an expression to a variable.
Assignment operator stores a value in memory.
C also supports shorthand assignment operators which simplify operation with assignment.
Operator Meaning
= Assigns value of right side to left side
+= a += 1 is same as a = a + 1
-= a -= 1 is same as a = a - 1
*= a *= 1 is same as a = a * 1
/= a /= 1 is same as a = a / 1
%= a %= 1 is same as a = a % 1
INCREMENT AND DECREMENT OPERATORS
Increment (++) operator used to increase the value of the variable by one.
Decrement (--) operator used to decrease the value of the variable by one.
Example Explanation
x=100; After the execution the
x++; value of x will be 101.
Example Explanation
x=100; After the execution the
x--; value of x will be 99.
INCREMENT AND DECREMENT OPERATORS (CONT…)
Operator Description
Pre increment operator (++x) value of x is incremented before assigning it to the variable on the
left
Operator Description
Post increment operator (x++) value of x is incremented after assigning it to the variable on the
left
Example Example
m=2, n=3; m=2, n=3;
r=(m>n) ? m : n; r=(m<n) ? m : n;
Explanation Explanation
Value of r will be 3 Value of r will be 2
BITWISE OPERATORS
Bitwise operators are used to perform operation bit by bit.
Bitwise operators may not be applied to float or double.
Operator Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left (shift left means multiply by 2)
>> shift right (shift right means divide by 2)
BITWISE OPERATORS
Example: Bitwise << (Shift Left) Example: Bitwise >> (Shift Right)
int a=8, b; int a=8, b;
b = a << 1; b = a >> 1;
printf("Output = %d", b); printf("Output = %d", b);
Output Output
16 (multiplying a by a power of two) 4 (dividing a by a power of two)
SPECIAL OPERATORS
Operator Meaning
& Address operator, it is used to determine address of the variable.
* Pointer operator, it is used to declare pointer variable and to get value from it.
, Comma operator. It is used to link the related expressions together.
sizeof It returns the number of bytes the operand occupies.
. member selection operator, used in structure.
-> member selection operator, used in pointer to structure.
EXPRESSIONS
An expression is a combination of operators, constants and variables.
An expression may consist of one or more operands, and zero or more operators to produce
a value.
answer = a + b * c;
Output
Enter a character: a
Entered character is: a
GETS AND PUTS
gets function reads a line from stdin into the buffer pointed to by s until either a terminating
newline or EOF (End of File) occurs.
puts function writes the string 's' and 'a' trailing newline to stdout.
Program
1 #include <stdio.h>
2 void main( )
3 {
4 /*Character array of length 100*/
5 char str[100];
6 printf("Enter a string: ");
7 /* Take a string as input */
8 gets( str );
9 /* Display the string */
10 printf("Entered string is: "); Output
11 puts( str ); Enter a string: india
12 } Entered string is: india
PREPROCESSOR
Preprocessors are programs that process our source code before compilation.
There are a number of steps involved between writing a program and executing a program in
C.
Let us have a look at these steps before we actually start learning about Preprocessors.
C Program
Object Executable
Are there No Code Code
preprocessor Compiler Linker
directive
Yes
TYPES OF directives:
Macros
PREPROCESS File inclusion
OR Conditional compilation
Other directives
A macro is a fragment of code which has been given a name.
Whenever the name is used in program, it is replaced by the
contents of the macro.
Macro definitions are not variables and cannot be changed by
MACRO
your program code like variables.
The ‘#define’ directive is used to define a macro.
Do not put a semicolon ( ; ) at the end of #define statements.
There are two types of macros:
Object-like Macros
Function-like Macros
MACRO
Description Object-like Macros Function-like Macros
Definition The object-like macro is an identifier that is The function-like macro looks like function
replaced by value. call.
Use It is used to represent numeric constants. It is used to represent function.
Syntax #define CNAME value #define CNAME (expression)
Example #define PI 3.14 #define MIN(a,b) ((a)<(b)?(a):(b))
Program 1 #include <stdio.h> 1 #include <stdio.h>
2 #define PI 3.14 2 #define MIN(a,b) ((a)<(b)?(a):(b))
3 void main() 3 void main()
4 { int r=2; 4 {
5 float a; 5 printf("%d", MIN(2, 5));
6 a=PI*r*r; 6 }
7 printf("%f", a);
8 }