Monday, 21 March 2016

Keywords in C


A keyword is a reserved word. You cannot use it as a variable name, constant name etc. There are only 32 reserved words (keywords) in C language.
A list of 32 keywords in c language is given below:
autobreakcasecharconstcontinuedefaultdo
doubleelseenumexternfloatforgotoif
intlongregisterreturnshortsignedsizeofstatic
structswitchtypedefunionunsignedvoidvolatilewhile
We will learn about all the C language keywords later.

C Operators

An operator is simply a symbol that is used to perform operations. There can be many types of operations like arithmetic, logical, bitwise etc.
There are following types of operators to perform different types of operations in C language.
  • Arithmetic Operators
  • Relational Operators
  • Shift Operators
  • Logical Operators
  • Bitwise Operators
  • Ternary or Conditional Operators
  • Assignment Operator
  • Misc Operator

Precedence of Operators in C

The precedence of operator species that which operator will be evaluated first and next. The associativity specifies the operators direction to be evaluated, it may be left to right or right to left.
Let's understand the precedence by the example given below:
  1. int value=10+20*10;  
The value variable will contain 210 because * (multiplicative operator) is evaluated before + (additive operator).
The precedence and associativity of C operators is given below:
CategoryOperatorAssociativity
Postfix() [] -> . ++ - -Left to right
Unary+ - ! ~ ++ - - (type)* & sizeofRight to left
Multiplicative* / %Left to right
Additive+ -Left to right
Shift<< >>Left to right
Relational< <= > >=Left to right
Equality== !=Left to right
Bitwise AND&Left to right
Bitwise XOR^Left to right
Bitwise OR|Left to right
Logical AND&&Left to right
Logical OR||Left to right
Conditional?:Right to left
Assignment= += -= *= /= %=>>= <<= &= ^= |=Right to left
Comma,Left to right

Comments in C

Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types of comments in C language.
  1. Single Line Comments
  2. Multi Line Comments

Single Line Comments

Single line comments are represented by double slash \\. Let's see an example of single line comment in C.
  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. void main(){      
  4. clrscr();      
  5. //printing information  
  6. printf("Hello C");  
  7. getch();      
  8. }      
Output:
Hello C
Even you can place comment after statement. For example:
  1. printf("Hello C");//printing information  

Mult Line Comments

Multi line comments are represented by slash asterisk \* ... *\. It can occupy many lines of code but it can't be nested. Syntax:
  1. /*  
  2. code 
  3. to be commented 
  4. */  
Let's see an example of multi line comment in C.
  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. void main(){      
  4. clrscr();      
  5. /*printing 
  6. information*/  
  7. printf("Hello C");  
  8. getch();      
  9. }      
Output:
Hello C


Escape Sequence in C

An escape sequence in C language is a sequence of characters that doesn't represent itself when used inside string literal or character.
It is composed of two or more characters starting with backslash \. For example: \n represents new line.

List of Escape Sequences in C

Escape SequenceMeaning
\aAlarm or Beep
\bBackspace
\fForm Feed
\nNew Line
\rCarriage Return
\tTab (Horizontal)
\vVertical Tab
\\Backslash
\'Single Quote
\"Double Quote
\?Question Mark
\nnnoctal number
\xhhhexadecimal number
\0Null

Escape Sequence Example

  1. #include <stdio.h>      
  2. #include <conio.h>    
  3. void main(){      
  4. int number=50;    
  5. clrscr();      
  6. printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\"");  
  7. getch();      
  8. }      
Output:
You
are
learning
'c' language
"Do you know C language"

No comments:

Post a Comment