Tuesday, September 10, 2013

Programming Problems(Find the output??)


What is the output of following programe?
Ques1.
#include <stdio.h>
main( )
{
i n t a, b y c;
scanf ( "%3d %3d %3d" , &a, &b, &c) ;
. . . . 

}
Input : 
1234 5678 9

Ans. Assignment will be like this:

a=123 b=4 c=567

Ques2.

Wednesday, September 4, 2013

Some interesting programs

#1 Printing the string containing Blank spaces while using Scanf

Problem: While using scanf for getting a string we have the limitation of string upto blank space.As soon as blank spaces occurs in a string scanf terminates to take that input.
 i.e .
char line[80];
scanf("%s",line);

for above program if we type
 India is my country. 
then line[ ] be assigned only "India"

Solution:
To solve this problem we could use a conversion process. 
Actually the "%s " in scanf converts our input to some string terminated with \0. As soon as the \0 occurs it does not take input. So following the rule of type conversion we can instruct our compiler to take input according to our own way.

An alternative method to print a string containing white space chararcter and uppercase character is that , write all uppercase letters including white sapce should be added in a square bracket in place of s, So that our compiler could interpret them and read them.

scanf (" %[  ABCDEFGHIJKLMNOPQRSTUWXYZ]", line ) ;

Now if our input is 
INDIA IS MY COUNTRY.

then line will be assigned the whole string "INDIA IS MY COUNTRY"
but now since we have not added the lowercase character hence as soon as it recognizes the lowercase letter it will terminate the scanning process.
Obviously , we could add lowercase character as well, but it will become cumbersome for us to write all those we want.


Now seeing the above solution, it comes to our mind that the above solution could be better if we could make compiler interpret the above things in opposite manner.i.e. what I mean to say that there should be some instruction , applying which we could help to ignore those characters which we are writing in code.
Because the ignored characters will be lesser than the used ones.

So ans is yes , we could use a circumflex (^)before our letters to be interpreted in opposite manner.

thus for ignoroing \n
the code will be simply like this :

scanf("%[^\n]",line);

Some C Facts

Scanf : In scanf all the operators are preceeded with the % sign because in scanf the argument presented by it are actually address. But the same does not apply to an array or string,why?
Because an array or string name is already an address of its first location , so no need to use % sign.

main( )
{char item[20];
int partno;
float cost;
scanf(" %s %d %f', item, &partno, &cost);
}

Sunday, September 1, 2013

Operator Precedence Groups

Operator category                                            Operators                                                  Associativity
unary operators                                           - ++ ! sizeof (type)                                           Right to Left
arithmetic  multiply, divide and remainder               * I %                                                      Left to Right
arithmetic add and subtract                                  + -                                                           Left to Right
relational operators                                           < <= > >=                                                  Left to Right
equality operators                                           !=                                                                 Left to Right
logical and                                                       &&                                                             Left to Right
logical or                                                            ||                                                               Left to Right
conditional operator                                      ? :                                                                   Right to Left
assignment operators                                 = += -= *= /= %=                                             Right to Left

Modulus Operator (%)

How to find remainders sign when one of the operand is negative??

When performing division it is very easy to find sign of quotient on the basis of elementary mathematics that if the two operands are of same sign then quotientwill be positive but negative in other case.

Now it feels cumbersome to us when we have to find the sign of remainder.It seems to us that the 'C' language is violating the rule of mathematics, But it is not actually true.
The mathematics is correct behind this , while our concepts have  proven to be weak.

In all opeartions of modiulus following ciondition should be satisfied:

a = ( ( a / b) * b) + (a % b)
i.e.
Dividend = Quotient * Divisor+ Remainder

From above equation

  Remainder = Dividend - Quotient * Divisor

 following the above equation we can calculate the remainder for following cases :
Finding remainder with negative opearnds 

Dividend||Divisor||Quoteint||Remainder

 10              3           3            1     // Remainder = 10- 3*3

-10             3          -3           -1     //Remainder = 10- {(-3)*3} 


 10            -3          -3            1     //Remainder = 10- {(-3)*(-3)} 

-10            -3           3           -1     //Remainder = -10- {3*(-3)} 


Division operator in c (Carefully divide)

Division Operator(/)

Integer division:
If two operands performing division operation are integer then the result will be an integer iwith truncated decimal values.

e.g.

a=10; b=3;
then  a/b = 3 ;  (rather than 3.333 in normal algebra.)


Ques. What will be the output of the following programme?

#include<stdio.h>
int main()
{
   float c;
 
    printf("%d\n", 10/3);
    printf("%f\n", 10/3);
    c=10/3;
    printf("%f\n",c);


return 0;

    }

output: 



 

3
0.000000 
3.000000

Notice that  all three expressions has both integers but all three expressions are having different outputs. all three expressions are performing integer division hence output first will be integer.

That is 10/3 =3 in short.


If one of the operands in division operation are float then result will be float.

#include<stdio.h>
int main()
{
   float c;    printf("%d\n", 10.0/3);
    printf("%f\n", 10.0/3);
    c=10.0/3;
    printf("%f\n",c);
  
    printf("%d\n", 10/3.0);
    printf("%f\n", 10/3.0);
    c=10/3.0;
    printf("%f\n",c);
  
    printf("%d\n", 10.0/3.0);
    printf("%f\n", 10.0/3.0);
    c=10.0/3.0;
    printf("%f\n",c);
  

            return 0;
   }


 Ans.


-1431655765
3.333333
3.333333

-1431655765
3.333333
3.333333

-1431655765
3.333333
3.333333

C-Programming questions part-1(basic)

1.How many characters are in the following string constant?

" \ t To continue, press the \"RETURN\" key\n"

Ans: total 38
27 normal characters
five blank spaces,
one special character(,)
four escape sequences (horizontal tab, two quotation marks and newline)
and the null character (\ 0) at the end of the string.


2.Which of the following are not valid identifires?
4th , _radius, value, the_tax_return,"My_name", area of circle

Ans.   4th,"My_name",area of circle

3.What is the differences between string constant "A"and character constant 'A'.

Ans. a character constant has an equivalent integer value, whereas a single-character string constant does not have an equivalent integer value and, in fact, consists of two characters -the specified character followed by the null character ( \ 0).

4.What will be the output of following programme?
 char array[11] = "Hello World";

    printf("%s\n",array);

Ans. Error

5. The unary operator always preceeds their operands (Trur/False)

Ans. Flase

i++,i-- are examples where unary operators comes after the operands

6. Write the unary operators.


Ans. ++,--,(type),sizeof,-,!

7. While considering the operators precedence and associativity , what is the difference between precedence and associativity?
Ans. Precedence tells the priority order of the operators in any expression.but ambuiguity arises while having same prirority  opereators occur in same expression.In that case an order from either right to left or left to right is followed to resolve this problem.

For example :
in     a=3+4-5
 + and - are of same priority.
then according to their associativity(Left to right) their operation will be performed in that order. Hence first addition is performed then subtraction.

NOTE: In all the operators only unary operators ,conditional operators and assignment operators are having associativity from right to left.

8.What is the value of following expression?
 i = 2 * 5 / 2
Ans. Since * and / are having same priority then according to their associativity left to right * operation is performed first.

hence i=2*5/2=10/2=5

if expression would be like this i = 2 * (5/2) then answer will be i=2*2=4 

9. In what general category do the #define and #include statements fall?

Ans. Preprocessor Statement