Friday 30 August 2013

Special Operators | C Programming Language Tutorial pdf



special operators : 

these are two other operators in c.

sizeof operator : is used to find the on. of bytes occupied by a variable / data type in computer memory.
eg : sizeof (float) returns 4
int m, x [ 50 ]
sizeof (m) returns 2
sizeof ( x ) returns 100 ( 50 x 2 )

i) comma operator : can be used to link the related expressions together. A comma- linked: list of expressions are evaluated left to right and the value of
right-most exp is the value of combined expression.
Eg : value = ( x = 10, y = 5, x = y)

First 10 is assigned to x
then 5 is assigned to y
finally x + y i .e. which 15 is assigned to value .
since comma has the lowest precedence of all operator, the parantheses are necessary . Operator - precedence & Associativity precedence is nothing but priority that indicates which operator has to be evaluated first when there are more than one operator.

Associativity : when there are more than one operator with same precedence [ priority ] then we consider associativity , which indicated the order in? which the expression has to be evaluated. It may be either from Left to Right or Right to Left.
Note : Unary, Conditional & Assignment operators are evaluated from Right to Left, remaining operators are from Left to Right

Type Casting: Normally before an operation takes pace both the operands must have the same type. C converts One or both the operands to the appropriate date types by “Type conversion”. This can be achieved in 3 ways.

Implicit Type conversion : In this the data type /Variable of lower type (which holds lower range of values or has lower precision ) is converted to a higher type (which holds higher range of values or has high precision). This type of conversion is also called “promotion”.

If a 'char' is converted into „int? it is called as Internal promotion
Eg: int I;
char C;
C = 'A';
I = C;

Now the int Variable I holds the ASCII code of the char 'A'
a) An arithmetic operation between an integer and integer yields an integer result.
b) Operation b/w a real yields a real
c) Operation b/w a real & an integer always yields a real result

Eg: 5/2 = 2        2/5 = 0
    5.0/2 = 2.5    2.0/50. = 0.4
    5/2.0 = 2.5    2/5.0 = 0.4
    5.0/2.0 = 2.5  2.0/5.0 = 0.4

Assignment Type Conversion: If the two Operands in an Assignment operation are of different data types the right side Operand is automatically converted to the data type of the left side.
Eg: Let 'k' is an int var & 'a' is a float var
   int k;        yes       float a;     yes
   k= 5/2        2        k=2/5        0        a = 5/2      2.0
   k=5.0/2      2        k=2.0/5      0        a = 5.0/2   2.5
   k=55.0/2    2        k=2/5.0      0        a = 5/2.0   2.5
   k=5.0/2.0   2        k=2.0/5.0    0       a = 2/5      0.0
                                              a = 2.0/5   0.4
                                              a = 2.0/0.5 0.4
Program: WAP to read & display characters & strings
Using unformatted I/O functions
main ( )
{ char ch,s[20] ;
printf(“press a special char”);
printf(“ press any char”);
ch = getch ( );
printf(“you pressed”);
putchar ( ch );
printf(“press any number”)
ch = getch ( );
printf(“ Enter your name”);
gets (s);
printf(“ Your name is “);
puts (s);
}

Output:
press Special c  * <- ch = *
Special char is  *
Press any char        ch = z
You pressed      z
Press any number 1    ch = 1
You pressed      1
Enter your name  C Language
Your name is C language

Explicit Type Conversion: When we want to convent a type forcibly in a way that is different from automatic type conversion, we need to go for explicit type conversion.
                    (type name) expression;
Type name is one of the standard data type. Expression may be a constant variable Or an expression this process of conversion is called as casting a value.
Eg: 
x = (int) 7.5
A = (int) 21.3/(int) 4.5
Y =( int) (a + b)
P = (double)sum/n

Basic Input output : C has many input output functions in order to read data from input devices and display the results on the screen.
Formatted Functions: These functions read and write all types of data values. They
require a conversion symbol to indents the data type using these functions the O/P can be presented in an aligned manner.
=> Data Types with conversion symbol (format string):
                  Data Type                   Conversion symbol
Integer          Short integer                   %d or % i
                    Short unsigned                    % u
                    long signed                          % ld
                    Long unsigned                     % lu
                   Unsigned hexadecimal            % x
                   Unsigned octal                       % 0

real             float                                 % f or % g
                 double                                    % lf
                 signed character                      % c
                 unsigned char                          % c
                 string                                      % s

   Escape Sequences with their ASC|| values:
Escape Sequence       Use          ASC|| Value
    \n                     New line              10
    \b                     Backspace            8
    \f                     Form feed            12
    \'                     Single Quote         39
    \\                     Back slash            92
    \o                    Null                      0
    \t                    Horizontal tab        9
    \r                    Carriage return      13
    \a                   Alert                      7
    |?                  Question marks       63
    \“                  Double Quote         34
    \v                 Vertical tab              11

scanf ( ): function is used to read values using key board. It is used for runtime assignment of variables.
The general form of scanf( ) is
scanf(“format String “ , list_of_addresses_of_Variables );
The format string contains
- Conversion specifications that begin with % sign
Eg: Scan f(“ %d %f %c”, &a &b, &c)
'&' is called the “address” operator. In scanf( ) the '&' operator indicates the memory location of the variable. So that the Value read would be placed at that location.

printf( ): function is used to Print / display values of variables using monitor:
The general form of printf( ) is
printf(“control String “ , list_of_ Variables );
- Characters that are simply printed as they are
- Conversion specifications that begin with a % sign
- Escape sequences that begin with a '\' sign.

Eg: Program
main ( )
{
int avg = 346;
float per = 69.2;
printf(“ Average = %d \n percentage = %f”, avg, per);
}

O/P:
Average = 346
Percentage = 69.200000

printf( ): examines the format string from left to right and prints all the characters until it encounter a '%' or '\' on the screen. When it finds % (Conversion Specifier) it picks up the first value. when it finds '\' (escape sequence) it takes appropriate action (\n-new line). This process continues till the end of format string is reached.

Eg: Program ( )
main ( )
{
float per;
printf(“Enter values for avg & per”);
scanf(“ %d %f”, & avg, & per);
printf( “ Average = %d \n Percentage = %f”, avg. per);
}

O/P: Enter values for avg & per 346 69.2
Average = 346
Percentage = 69.200000

=> Unformatted functions character I/O functions:
getchar ( ): function is used to read one character at a time from the key board
Syntax ch = getchar ( ); where ch is a char Var.
main ( )
{
char ch;
printf(“Enter a char”);
ch = getchar ( );
printf(“ch =%c”, ch);
}

O/P:
Enter a char M
M
ch = M
When this function is executed, the computer will wait for a key to be pressed and assigns the value to the variable when the “enter” key pressed.

putchar ( ): function is used to display one character at a time on the monitor.
Syntax: putchar (ch);
Ex char ch = 'M'
putchar (ch);
The Computer display the value char of variable 'ch' i.e M on the Screen.

getch ( ): function is used to read a char from a key board and does not expect the “enter” key press.
Syntax: ch = getch ( );
When this function is executed ,computer waits for a key to be pressed from the dey board. As soon as a key is pressed, the control is transferred to the nextline of the program and the value is assigned to the char variable. It is noted that the char pressed will not be display on the screen.

getche ( ): function is used to read a char from the key board without expecting the enter key to be pressed. The char read will be displayed on the monitor.
Syntax: ch = getche ( );

Note that getche ( ) is similar to getch ( ) except that getche ( ) displays the key pressed from the dey board on the monitor. In getch ( ) 'e' stands for echo.

=> Strng I/O functions:
gets ( ): function is used to read a string of characters including white spaces. Note that wite spaces in a strng cannot be read using scanf( ) with %s format specifier.
Syntax: gets (S); where „S? is a char string variable
Ex: char S[ 20 ];
gets (S);
When this function is executed the computer waits for the string to be entered.

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved