Escape Sequences in C

Escape Sequences in C
Escape sequence are useful for formatting the Input and Output. All Escape sequences are start from Back slash " \" followed by a character.  The " \n " new line is most commonly used in printing statement.

Escape Sequence with ASCII Value


Character   Escape Sequences     ASCII Value  
Null \0 0
Bell (alert) \a 7
Backspase \b 8
Horizontal tab \t 9
New line \n 10
Vertical tab \v 11
Formfeed \f 12
  Carriage return   \r 13
Double quote \" 34
Single quote \' 39
Question mark \? 63
Backslash \\ 92
Octal number \ooo any
Hexadecimal
number
\xhh any

Octal Number:
Example: \7, \007, \786
Hexadecimal Number:
Example: \x5, \x57, \x7f

Program to display ASCII value
#include <stdio.h>
int main(){
printf("Null=%d\n",'\0');
printf("Bell (alert)=%d\n",'\a');
printf("Backspase=%d\n",'\b');
printf("Horizontal tab=%d\n",'\t');
printf("New line=%d\n",'\n');
printf("Vertical tab=%d\n",'\v');
printf("Formfeed=%d\n",'\f');
printf("Carriage return=%d\n",'\r');
printf("Double quote=%d\n",'\"');
printf("Single quote=%d\n",'\'');
printf("Question mark=%d\n",'\?');
printf("Backslash=%d\n",'\\');
return 0;
}

Output

Null=0
Bell (alert)=7
Backspase=8
Horizontal tab=9
New line=10
Vertical tab=11
Formfeed=12
Carriage return=13
Double quote=34
Single quote=39
Question mark=63
Backslash=92

Example program to print Escape sequences and percentage symbol.
#include <stdio.h>
int main(){
printf("\aC Program\n");
printf("New line =\\n\n");
printf("Percentage symbol=%%");
return 0;
}


Operator Precedence in c

Operator Precedence in c
When two or more operators share an operand the operator with the higher precedence goes first. For an example, 1 + 2 * 3 is treated as 1 + (2 * 3), because multiplication has a higher precedence than addition.

Operator Precedence and Associativity


Priority Category Operator What is it (or does) Associativity
1 Highest ( )
[ ]
->
.
Function call
Array subscript
Indirect component selector
Direct component selector
Left to Right
2 Unary !
~
+
-
++
--
&
*
sizeof
(type)
Logical negation (NOT)
Bitwise (1's) complement
Unary plus
Unary minus
Preincrement or postincrement
Predecrement or postdecrement
Address
Indirection
Size of operand, in bytes
TypeCast
Right to Left
3 Multiplicative *
/
%
Multiply
Divide
Remainder (modulus)
Left to Right
4 Aditive +
-
Binary plus
Binary minus
Left to Right
5 Shift <<
>>
Shift left
Shift right
Left to Right
6 Relational <
<=
>
>=
Less than
Less than or equal to
Greater than
Greater than or equal to
Left to Right
7 Equality ==
!=
Equal to
Not equal to
Left to Right
8 Bitwise AND & Bitwise AND Left to Right
9 Bitwise XOR ^ Bitwise XOR Left to Right
10 Bitwise OR | Bitwise OR Left to Right
11 Logical AND && Logical AND Left to Right
12 Logical OR || Logical OR Left to Right
13  Conditional ?: "a ? x : y" means "if a then x, else y" Right to Left
14 Assignment =
*=
/=
%=
+=
-=
&=
^=
|=
<<=
>>=
Simple assignment
Assign product
Assign quotient
Assign remainder (modulus)
Assign sum
Assign difference
Assign bitwise AND
Assign bitwise XOR
Assign bitwise OR
Assign left shift
Assign right shift
Right to Left
15 Comma , Evaluate Left to Right


Example for Operatro precedence
#include <stdio.h>
int main(){
float a=1,b=2,c=3,d;
d=a+b*c;
printf("a+b*c=%f\n",d);

d=(a+b)*c;
printf("(a+b)*c=%f",a);
return 0;
}

Output

a+b*c=7.000000
(a+b)*c=1.000000

Punctuator in C

Punctuator in C
A punctuator is also a token and it also defined in compiler like keywords. A punctuator can also be a token that is used in the syntax of the preprocessor. some Operator or punctuator alternative representations for some operators and punctuators.

Punctuator

Operator Meanning
[ ] Array subscript Operator
( ) Parentheses Operator
{ } Braces
= Equal sign
, Comma Operator
(or)
Punctuator
: Colon
; Semicolon
* Astrisk
" " Quotes
' ' Ellipsis


C and C++ ( C99 ) provide the following alternative representations for some operators and punctuators.
Operator
or
punctuator
Alternative
Representation
# %:
## %:%:
{ <%
} %>
[ <:
] :>

Example for using Alternative representation.
// Alternative Representation Example
%:include<stdio.h>
int main()
<%  //  Open Brace {.
char a<:5:>="MDSN";  // Array subscript operator Ex: a[5]="MDSN"
printf("%s",a);  // Output MDSN
return 0;
%>  // Close Brace {.

Some more alternative representation defined as macros in the header file " iso646.h".
Operator
or
punctuator
Alternative
Representation
& bitand
&& and
&= and_eq
| bitor
|| or
|= or_eq
^ xor
^= xor_eq
! not
!= not_eq
~ compl

Example:
  %:include<stdio.h> //  # preprocessor 
  %:include<iso646.h>
  int main()
  <%  //  Open Brace {.
  int a=5,b=5,c=2;
  if((a==5)and(a==b)) //if((a==5)&&(a==b))
  {printf("a=5 and a=b\n");}
  
  if(a not_eq c) //if(a != c)
  {printf("a is not equal to c\n");}
  return 0;
  %>  // Close Brace {.

Unary Operators in C

Unary Operators in C
Unary Operators are written before their single operand some Unary Operators written after their operands. Most commonly unary minus is used in the program.

Unary Operator

  Operator   Meaning
& Referencing Operator
* Dereferencing Operator
+ Unary PlusOperator
- Unary Minus Operator
++ Increment Operator
-- Decrement Operator
! Logical Not Operator
~   Bitwise Complement Operator  
sizeof Size of operand in bytes
(type) Type Cast

Referencing operator ('&')

Referencing operator ( & ) produces the address of a variable, Ex: &a is a pointer to a. the
parameters are declared as pointers, and the operands are accessed indirectly through them.

Dereferencing Operator ('*')

The unary operator "*" is Dereferencing Operator and it applied to a pointer, it
accesses the object the pointer variable. Ex: *a.

Unary ( +, - ) Operator

The operands of the unary "+" and "-"operator must have arithmetic type, and the result is the value of the operand. The type of the result is the type of the promoted operand.

Increment Operator (++) & Decrement Operator (--)

The "++" adds one to the variables and "--" subtracts one from the variable. Because it acts upon only one variable.
  Operator   Meaning
++a Pre increment
a++   Post increment  
--a Pre decrement
a-- Post decrement
  Consider the statement
a=5, b=5
  Expression     Value  
++a 6
a++ 6
--b 4
b-- 4
Coding for the above Example:
#include<stdio.h>
  int main()
  {
  int a=5,b=5;
  printf("Value of a=%d\n",a);
  printf("Pre increment %d\n",++a);
  printf("After pre increment the value of a=%d\n",a);
  printf("Post increment %d\n",a++);
  printf("After post increment the value of a=%d\n\n",a);
printf("Value of b=%d\n",b);
  printf("Pre decrement %d\n",--b);
  printf("After pre decrement the value of a=%d\n",b);
  printf("Post decrement %d\n",b--);
  printf("After post decrement the value of a=%d\n",b--);
return 0;
  }

Output

Value of a=5
Pre increment 6
After pre increment the value of a=6
Post increment 6
After post increment the value of a=7

Value of b=5
Pre decrement 4
After pre decrement the value of a=4
Post decrement 4
After post decrement the value of a=3

Logical Not Operator

The variable(operand) must be of scalar type (i.e. not an array, a structure or an union).
Consider the statement
a=5,b=7,c=5
  Expression     Interpretation     Value  
!(a==c) False 0
!(a==b) True 1

Bitwise Complement Operator

operand must be of integral type. Each 0 bit in the operand is set to 1, and each 1 bit in the operand is set to 0.
Consider the statement
X=19;
Z=~x;
X=0001 00112
Z=1110 11002

Sizeof Operator

The "sizeof" Operator returns the size of the variable or type. It returns the size in bytes.
#include<stdio.h>
int main()
{
int m;
printf("%d",sizeof(int));
printf("\n%d",sizeof(m));
return 0;
}

Type Cast

Type casting is a way of converting a variable form one data to another data type. For an example if you want to store a integer value into a float then you can type cast int to float.
#include<stdio.h>
int main()
{
int a=5,b=3;
float c;
c=(float) a/b;
printf("a/b=%f",c);
return 0;
}

Logical Operators in C

Logical Operators in C
Logical Operators are useful in combining one or more conditions. C allows usage of three logical operators namely " &&, ||, ! ".

Logical Operators

  Operator   Meaning
&&   Logical AND  
|| Logical OR
! Logical NOT

Truth Table of Logical Operator

In the following table
if True(T) means value is one.
If False(F) means valuse is Zero.
  X     Y     !X     !Y     X && Y     X || Y  
F F T T F F
F T T F F T
T F F T F T
T T F F T T
Consider the statement
a=5,b=7;c=5;
  Expression     Interpretation     Value  
(a>=5)&&(a<b) True 1
!(a==c) False 0
(a!=5)||(b>c) True 1
!(a==b) True 1

Codings for the above Example:
#include<stdio.h>
  int main()
  {
  int a=5,b=7,c=5;
  
  if((a>=5)&&(a<b)){printf("\"(a>=5)&&(a<b)\" = True\n");}
  else{printf("\"(a>=5)&&(a<b)\" = False\n");}
  
  if(!(a==c)){printf("\"!(a==b) or a is not equal to c\" = True\n");}
  else{printf("\"!(a==b) or a is not equal to c\" = False\n");}
  
  if((a!=5)||(b>c)){printf("\"(a!=5)||(b>c)\" = True\n");}
  else{printf("\"(a!=5)||(b>c)\" = False\n");}
  
  if(!(a==b)){printf("\"!(a==b) or a is not equal to b\" = True\n");}
  else{printf("\"!(a==b) or a is not equal to b\" = False\n");}
  
  return 0;
  }

Relational operator in C

Relational operator in C
The relational operators allows to compare two or more values to see whether they are equal or unequal.

Relational operator

  Operator   Meanning
< Is less than
<= Is less then or equal to
> Is greater then
>=   Is greater then or equal to  
!= Is not equal to
== Is equal to

Consider the statement
a=1,b=2,c=3;
  Expression     Interpretation     Value  
a<b True 1
b>c False 0
a<=b True 1
c>=b True 1
c!=3 False 0
a==1 True 1

Codings for the above Example:
#include<stdio.h>
  int main()
  {
  int a=1,b=2,c=3;
if(a<b)
  {printf("a is less then b  ,value= 1 \n");}
  else{printf("a is not less then b  ,value=0 \n");}
if(b>c)
  {printf("b is greater then c  ,value=1 \n");}
  else{printf("b is not greater then c  ,value=0 \n");}
if(a<=b)
  {printf("a is less then or equal to b  ,value=1 \n");}
  else{printf("a is not less then or equal to b  ,value=0 \n");}
if(c>b)
  {printf("c is greater then or equal to b  ,value=1 \n");}
  else{printf("c is not greater then or equal to b  ,value=0 \n");}
if(c!=3)
  {printf("c is not equal to 3  ,value=1 \n");}
  else{printf("c is equal to 3  ,value=0 \n");}
if(a==1)
  {printf("a is equal to 1  ,value=1 \n");}
  else{printf("a is not equal to 1  ,value=0 \n");}
  return 0;
  }

Output

a is less then b ,value= 1
b is not greater then c ,value=0
a is less then or equal to b ,value=1
c is greater then or equal to b ,value=1
c is equal to 3 ,value=0
a is equal to 1 ,value=1

Conditional Operator in C

Conditional Operator in C
Conditional Operator is also known as Turnary Operator. It is an alternate method to using a simple if-else. It checks the condition and executes the statement depending on the condition.

Syntex

condition?expression1:expression2;

if condition is True or value 1 means expression1 will execute.
if condition is False or value 0 means expression2 will execute.
Example:
Consider the statement
a=7,b=5
c=(a>b) ? a:b;
after execution the value of c=7
Example 1:
#include<stdio.h>
int main()
{
int a=7,b=5,c;
c=(0) ? a:b;   // c=5
printf("c=%d\n",c);
c=(1) ? a:b;   // c=7
printf("c=%d\n",c);
return 0;
}

Example 2:
#include<stdio.h>
int main()
{
int a=7,b=5,c;
c=(a>b) ? a:b;
printf("c=%d",c);
return 0;
}

Example :3
// Conditional operator using multiple function
  #include<stdio.h>
  int comp();
  int prin1();
  int prin2();
  int main()
  {
  comp()?prin1():prin2();//multiple function using Turnary Operator
return 0;
  }
 /* Comparing the values in comp()[function]
  int comp()
  {
  int a=7,b=5;
  if(a<b)
  {return 0;}
  if(a>b)
  {return 1;}
  }
 
int prin1() // printing function 1
  {
  printf("A is bigger\n");
  return 0;
  }
  
int prin2() // printing function 2
  {
  printf("B is bigger\n");
  return 0;
  }

Bitwise operator in C

Bitwise operator in C
Bitwise operator permit the programmer to access and manipulate individual bits within a piece of data. These operators can operate upon ints and chars but not on floats and doubles.

Bitwise operators

  Operator   Meaning
& Bitwise AND
| Bitwise OR
^
Bitwise XOR
<< Shift Left
>> Shift Right
~   Bitwise Complement 
(or)
One's Complement

Bitwise Operatot Truth Table

Bitwise Operator performs bit by bit operation.
  X     Y     X & Y     X | Y     X ^ Y  
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Left Shift Operator

Consider the statement
X=19;
Z=X<<2;
The value in the integer X is shifted left by two bit positions. The result is assigned to Z.The value of X is 0001 00112 the value of Z after the execution the value of Z is 0100 11002.

  0001 00112   Left Shift(2) 
0100 11002  <-- Insert 2 Zero's (0) 

Right Shift Operator

Consider the statement
X=19;
Z=X<<2;
The value in the integer X is shifted Right by two bit positions. The result is assigned to Z.The value of X is 0001 00112 the value of Z after the execution the value of Z is 0000 01002.

 Right Shift(2)   0001 00112 
  Insert 2 Zero's (0) -->  0100 11002

Bitwise Complement Operator

operand must be of integral type. Each 0 bit in the operand is set to 1, and each 1 bit in the operand is set to 0.
Consider the statement
X=19;
Z=~x;
X=0001 00112
Z=1110 11002

Bitwise operator Example:
#include<stdio.h>
int main()
{
int X=19,Y=16,Z;
Z=X&Y;
printf("X&Y=%d\n",Z);
Z=X|Y;
printf("X|Y=%d\n",Z);
Z=X^Y;
printf("X^Y=%d\n",Z);
Z=X<<2;
printf("X<<2=%d\n",Z);
Z=X>>2;
printf("X>>2=%d\n",Z);
Z=~X;
printf("~X=%d\n",Z);
return 0;
}

Output

X&Y=16
X|Y=19
X^Y=3
X<<2=76
X>>2=4
~X=-20

Assume that X=19, Y=16, the binary format will be as follows.
X = 0001 00112 - 1910
Y = 0001 00002 - 1610
Binary  Decimal 
 X & Y   0001 0000  1610
X | Y 0001 00112 1910
X ^ Y 0000 00112 310
X<<2 0100 11002 7610
X>>2 0000 01002 410
~X 1110 11002 -2010

Assignment Operators in C

Assignment Operators in C
Assignment Operators are used to assign a value of an expression or a value of a variable to another variable. The most commonly used assignment operator is "=". There are two different Assignment Operators are available in C 1.Compound Assignment, 2.Multiple Assignment.

Syntex

variable = expression (or) value;
The '=' operator is the simple Assignment operator. Example:
#include<stdio.h>
int main()
{
int x; // Declaration of a variable
x=5;   // Assignment Operator
printf("x=%d",x);
return 0;
}

Compound Assignment

Compound Assignment Operator is to assign a value to a variable in order to assign a new value to a variable arter performing a specifed operation.
  Operator     Example     Meaning  
+ = X + = Y X = X + Y
- = X - = Y X = X - Y
* = X * = Y X = X * Y
/ = X / = Y X = X / Y
% = X % = Y X = X % Y
#include<stdio.h>
int main()
{
int x=5,y=5; // Declaration of a variable
x+=y;   // Compound Assignment X = X + Y
printf("x=%d",x);
return 0;
}

Multiple Assignment

Using Multiple Assignment we can assign a single value or an expression to multiple variables. Example:
#include<stdio.h>
int main()
{
int x,y,z=5; // Declaration of a variable
x=y=z;   // Multiple Assignment X=Y=Z(5)
printf("x=%d,y=%d,z=%d",x,y,z);
return 0;
}