Saturday, December 10, 2016

Over view of 'c' (1-1 DS)

'C' Portability: 
                Program written in one operating system is compiles and executed in other operating system.
‘C’ is middle-level language:
                ‘C’ uses both features of high-level language and law-level language.  So, it is called Middle-level language.
              User can use 'c' language to do System Programming (for writing operating system) as well as Application Programming (for generate menu driven customer billing system )
Structure oriented language: 
                Structure Oriented definite programming structure.

Eg:  Structure oriented language: ‘C’, C++, and java
       Non-structure oriented language: COBOL, PSCL, and BASIC
       Structure of 'c' program:
 comment section
 preprocessor directives
 globle variable declaration;
 function declaration;
 main()   function defination 
 {
  variable declaration;
  funtion declaration;
  executable statement;
 }
 sub function defination()
 {
  } 

Comment section:
    comment section is used to write author details, purpose of the program (or) title of the program and date of modification that text must be placed in between /*(text)*/ these symbols. It is a multiple line comment section.

Pre-processor directives:
    Every 'c' program must begin with per-processor directives (or) statements. These are so called because the statements processed before the function (or) program execution starts.

Eg: #include<stdio.h>, #include<conio.h>

Globle variable Declaration:
    The variables which are present immediate after of pre-processor statements or before the main function those are called globle variables.The life time of globle variables through entire program.

Function declaration:
   Function declaration may be present at immediate after of globle variable declaration or immediate after of local variable declaration.
Main function:
   Main function is a function where the program execution begins.
Variable declaration:
   Local variables must be present immediate after of main function open curly brase.
Executable statements:
The statements which are executed by ‘c’ compiler those are called executable statements.
'C' is compiler Oriented:          
    Entice ‘C’ program is converted into object program by compiler.
 Characterset of 'C':
     Every language will have its own set of characters using which the words in the language are written.
Alphabets: A-Z  , a-z
Digits: 0-9
Special symbols: +  -  _  *  %  /   \  :  ;  .  ,  “  ‘  ?  ¦  <  >  =  ( )  [ ]  { }  ~  !  @  #  ^  &
Key words:
                 Key words are predefined words also called as reserved words.  Used for predefined purpose or intended purpose.  ‘C’ has 32 key words.
auto
double
int
struct
break
else
Long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while
Variable:
                Variable are user defined words.  Variable is the name given to the memory location.
Rules of declaring variable names:
1.       Variable names are written by using alphabets, digits and underscore
2.       First letter of the variable name must be alphabet.
3.       No special symbols are allowed except underscore (‘_’).
4.       Variable name must not be keyword.
Eg: si_int
      Mark1
      M3
      Average
Constants:
            Whose value does not change is called Constants.
            Constants are divided in to two major categories.
1.Primary constants           2.Secondary constants
Primary constants are “Integer constants (Decimal, Octal and Hexadecimal), Real constants (Numeric constant value with precision 1. Decimal   2. Exponential), Character constants (Single character, String )”
   Decimal: inter value with base 10 consist digits from 0-9.
   Octal: base 8 consists digits from 0-7. If a contains two or more digits, the first digit must be 0.
   Hexadecimal: Base 16 consists digits from 0-7 and A-F. This constant must begin with either 0x or 0X
Secondary Constants are “Array, Pointer, Structure, Union, Enum etc.,”
Data types:
1. Data types in ‘C’ can be used to declare variables
2. Specifies the type of value the variables must contain
3. Specifies the size of the variable
Data Types are
1. Basic data types (int, float, char and double)
2. User defined data types (typedef, enum)
3. Derived data types (arrays, pointers, structure and union)
4. Empty data type (void)

Data type
Size (bytes)
Int or signed int or unsigned int
2
Char or signed char or unsigned char
1
Short int or signed short int or unsigned short int
2
Long int or signed long int or unsigned long int
4
Float
4
Double
8
Long double
10
scanf( ) :
   We can store the values in the computer memory by using standard input output device with the help of scanf().
Or
   scanf() is a function which is used to take input values at run time.
Syntax:  scanf(control string, arg1, arg2,-------);
Here arg1, arg2, ------- are the name of the variables and they are preceded by ‘&’ symbol.
Control string is a conversion character which converts the values into appropriate data types.
Type of data
ConversionString
Int
%d
Float
%f
Char
%c
String
%s
Double
%f
Unsigned
%u

Printf( ) :
                The data present in the computer memory is transformed by using printf( ).
Or
   Printf() is a method or function which is used to display string on the screen.
Syntax: printf(control string, arg1, arg2, arg3, --------);
Program to print a message:
/*program to print message*/
#include<stdio.h>
void main()
{
Printf(“welcome to c ”);
}
Output: welcome to c
Important shortcuts in ‘c’ software: 
F2-Save
Alt+F9-compiling
Ctrl+f9-compling, linking execution
Alt+F5-out put
Program to find average of 3 numbers:
/*average of 3 numbers*/
#include<stdio.h>
Void main()
{
 int a,b,c;
 float avg;
 printf(“Enter the values”);
 scanf(“%d%d%d”,&a,&b,&c);
 avg=(a+b+c)/3;
 printf(“Average is %f”, avg);
}

Operators:
    In ‘c’ language, operators are classified into eight types
1.       Arithmetic operators
2.       Relational operators
3.       Logical operators
4.       Assignment operators
5.       Increment and decrement operators
6.       Conditional operator
7.       Bitwise operator
8.       Special operators
1.       Arithmetic operators:
Ø  Used for arithmetic operations or calculations
Ø  ‘c’ provides all the arithmetic operators
Ø  These can operate on any built-in datatype allowed in ‘c’.

Operator
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Modulo Division
Integer Arithmetic:
                 When both the operands in a single arithmetic expression  are integers, then expression is called an Integer expression and operation is called Integer arithmetic .  Integer arithmetic yields an integer value.
If a=14 and b=4 then result is,
a-b =10
a+b =18
a*b =56
a/b =3
a%b =2 (remainder of division)
Real arithmetic:
                An arithmetic operation involving only real operands is called Real arithmetic.  A real operand may assume values either in decimal or exponential notation.
                If x, y and z are floats then we will have,
                                x= 6.0/7.0 = 0.857143
                                y= 1.0/3.0 = 0.33333
                                z= -2.0/3.0 = -0.666667
The operator % cannot be used with real operands.
Mixed-mode Arithmetic:
                When one of the operands is real and the other is integer, then expression is called a Mixed-mode arithmetic expression.
                                15/10.0 = 1.5
                                15/10 = 1
Program to perform arithmetic operations:
#include<stdio.h>
void main()
{
  int a,b,c; 
 printf(“Enter a,b,c values”);
 scanf(“%d%d”,&a,&b);
 c=a-b;
 printf(“Sum=%d”,c);
 c=a-b;
 printf(“subtraction=%d”,c);
 c=a*b;
 printf(“multiplication=%d”,c);
 c=a/b;
 printf(“division=%d”,c);
 c=a%b;
 printf(“Modulodivision=%d”,c)
}


2.  Relational operators:
Ø  Used to compare two values or expressions and depending upon their relation take certain decisions
Ø  Two real or integer variables, constants or expressions connected by a relational operator returns a value which can be either true or false i.e., either one or zero.
Operators
Meaning
< 
Is less than
<=
Is less than are equals to
> 
Is greater than
>=
Is greater than are equals to 
==
Is equals to
!=
Is not equal to

Program to find largest of three numbers:
#include<stdio.h>
void main()
{
 int a,b,c,big;
 printf(“enter a,b,c”);
 scanf(“%d%d%d”,&a,&b,&c);
 printf(“a=%d,b=%d,c=%d”,a,b,c);
  big=a;
 if(b>big)
 big=b;
 if(c>big)
 big=c;
 printf(“largest number is %d”,big);
}
3.  Logical Operators:
                Logical operators are used to combine two or more relational expressions.  The operators are as follows:
Operator
Meaning
&&
Logical AND
¦¦
Logical OR
!
LogicalNOT
The logical operators && and ¦¦ are used when we want to test more than one condition and make decisions.
Truth tables for AND, OR:
AND:
X
Y
X&&Y
0
0
1
1
0
1
0
1
0
0
0
1
Here 1(true) && 1(true)=1(true)
Remaining all 0(false)
Eg:
 #include<stdio.h>
main()
{
  int a;
  a=printf(“”)&&printf(“hai”);
  printf(“a=%d”,a);
}
OutPut: a=0
Here  in the first printf statement there is no any content so it is false(0)
But in second printf statement there is “hai” so it is true(1)
0&&1=0

            OR:


X
Y
X¦¦Y
0
0
1
1
0
1
0
1
0
1
1
1



Here if any one us 1(true) then true.
Eg:
#include<stdio.h>
main()
{
  int a;
  a=printf(“”)¦¦printf(“hai”);
  printf(“a=%d”,a);
}
Output: a=1

NOT:

NOT
0
1
1
0
Here it !(0)=1  and  !(1)=0
Eg:
#include<stdio.h>
main()
{
 Int a,b,c;
 a=5;
 b=6;
c=!(a<b);
 printf(“c=%d”,c);
}
Output: c=0
Here
 c=!(5<6)
c=!(true)
c=false
c=0


4.  Assignment Operators:
Ø  Assignment operators are used to assign the result of an expression to a variable.
Ø  We have seen the usual assignment operator,’=’ .
Ø  In addition, ‘c’ has set of shorthand assignment operators of from,
V op=exp
 Where,
 ‘V’ is variable
 ‘exp’ is an expression
 ‘op’ is a ‘c’ binary arithmetic operator
The operator ‘op=’ is known as shorthand assignment operator.
Statement with 
simple assignment operator
     statement with 
shorthand operator
a=a+1
a+=1
a=a-1
a-=1
a=a*(n+1)
a*=(n+1)
a=a/(n+1)
a/=(n+1)
a=a%b
a%=b
                   
                    
          
           
                 

Eg: If a=2 and b=3 then
                a+=1  =  3
                a-=1  =  2
                a*=b  =  6
                a/=b  =  2/3 = 0
                a%=b  =  2%3 = 2 is remainder
5.  Increment and Decrement operators:
 Increment operators:
1.Post-increment:   
syntax:  variable++; 
In this, first we perform operation and then increment it.
2. Pre-increment:    
syntax:  ++variable;
 In this, first we perform incrementation and then operation is done.
 Both are equivalent to,
    Variable = variable+1;
i.e., a=b++;  is equivalent to a=b;
                                                b=b+1;               
i.e., a=++b;  is equivalent to b=b+1;
                                                a=b;  
eg: b=5
i.e., a=b++; then a=5  b=6
i.e., a=++b; then b=6  a=6            
 Decrement operators:
  1.Post-decrement: 
syntax:   variable--;
 In this, first we perform operation and then decrement it.
2. Pre-decrement:    
syntax:   --variable;
 In this, first we decrement and then perform the operation.
i.e., a=b--;  is equivalent to a=b;
                                                b=b-1;               
i.e., a=--b;  is equivalent to b=b-1;
                                                a=b;  
eg: b=5
i.e., a=b--; then a=5  b=4
i.e., a=--b; then b=4  a=4            
                                              
6.  Conditional Operators (? :) :
Ø  Also called Ternary operators because it does three expressions.
Ø  The symbol “?:” is called ternary operator pair.
Ø  It is a short form of if-else statement.
Ø  The general form is,   exp1 ? exp2 : exp3 ;
     The operator “?:” works as follows,
                Exp1 is evaluated first. If it is true, then the ep2 is evaluated and it becomes value of expression. If exp1 is false, then ep3 is evaluated and it becomes the value of expression.
Eg:
Main()                                                                                                                                            
{
 int ,y;
 char z[20];
 printf(“enter  x,y”);
scanf(“%d%d”,&x,&y);
 z=(x>y)?”x is big”:”y is big”;
 printf(“%s”,z);
}
7. Bitwise operators:
                ‘c’ provides bitwise operators that act on the individual bits for manipulation of dta at bit level.  Bitwise operator’s may not be applied to float or double.  These operators work only on type operands.
Operator
Meaning
&
Bitwise AND
|
Bitwise OR
^
Bitwise XOR
<< 
Shift left
>> 
Shift right
~
One’s compliment
Bitwise AND:
  The truth table for Bitwise AND is as follows
A
B
A&B
1
1
1
1
0
0
0
1
0
0
0
0
 Eg:
a=7
b=12
a&b
(7)10  = 0000 0111(2)
(12)10 =0000 1100(2)
              0000 0100
Bitwise OR:
Truth table for bitwise OR
A
B
A¦¦B
1
1
1
0
1
1
1
0
1
0
0
0
Bitwise X-OR:
Truth table for bitwise X-OR
A
B
A^B
1
1
0
0
1
1
1
0
1
0
0
0

Bitwise Compliment:
Truth table for Bitwise Compliment
A
~A
1
0
0
1
1
0
0
1
Bitwise shift left:
The shift left operator add ‘R’ number of zero’s to the binary code of  ‘L’ value in right side.
Eg:
7<<2  (L<<R)
(7)10=00 00 01 11
7<<2=00 01 11 00
Bitwise shift Right:
The shift right operator added R number of zero’s to the binary code of L values in left side.
Eg:
7>>2   (L>>R)
(7)10=00 00 01 11
7>>2=00 00 00 01
Program to demonstrate Bitwise operator:
#include<stdio.h>
void main()
{
 int a,b,A,O,X,C,L,R;
 printf(“Enter two values=”);
scanf(“%d%d”,&a,&b);
A=a&b;
O=a¦b;
X=a^b;
C=~a;
L=a<<b;
R=a>>b;
printf(“AND=%d\n OR=%d\n –OR=%d\n Compliment=%d\n Left shift=%d\n Right shift=%d”,A,O,X,C,L,R); 
}
Output:
Enter two values=23 3
Add=3
OR=23
X-OR=20
Compliment=-24
Left shift=184
Right shift=2

Special Operators:
   These types of operators are used in ‘c’ language to perform some particular type of operations. ‘c’ supports some special operators such as Comma Operator, Size of operator, pointer operators(& and *).
Operator
Meaning
&
Address
*
Indirection
sizeof()
it returns length
,
comma
Eg:  
&a,&x[9]
*a

Int a;  à

No comments:

Post a Comment