LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER AND BRAINSTORMING QUESTION

LOGIC BOOSTER

  1. When an operation is to be performed on two values ( may be of different types ), the type of smaller value will automatically be converted into larger one and the result will be in large one's type.
  2. char or short values will get converted into integer ( int ) type if involved in calculation part.
  3. float value is converted into double value:
  • If either operand is double, the other is converted to double and the result is double.
  • Otherwise, if either operand is long, the other is converted to long, and the result is long.
  • Otherwise, if either operand is unsigned, the other is converted to unsigned, and the result is unsigned.
  • Otherwise, the operand must be int.
For example: Suppose
c: char
d: double
i: int
u: unsigned
s: short int
l: long
f: float

 Expressions   Final data type                                               Explanation
       i + d
         double
 One value is of type int and second is of type double, so smaller one  
 (int) will be converted into double first ( without loss of information ) 
 and then the result type of addition of two double is double.
   20 * s +3             int First short will be converted into int. All values are of type int, so
 result is of type int.
    12.0 + c         double First "c" is converted into int then to double. Result will be of
 type double.    
    3 * i / 4            int As 3 and "i" are of typ int, so result of 3*i is of type int, then int
 divided by int is again int.
    3 * i / 4.0         double The result of 3 * i is of type int, then int divided by double is
 double.


  • You must use "&" ( ampersand ) with scanf to enter integer or character value ( while using format specifier "%d" or "%c" or "%f" ). Omitting it will make the program not work correctly.
  • You must not use "&" with printf while printing the values of any variable.
  • If you use comma between format specifiers in scanf, user must have to provide comma between two values to differentiate them.
  • All statements must be terminated with semicolon.
  • C is case sensitive - all keywords and standard library functions are in lowercase.


BRAINSTORMING QUESTIONS

1. How can we display the float number in exponential notation.
  • We can use the format specifier %e to print float value in exponential notation.
2. Is int a; a declaration or definition.
  • The declaration of a variable means how it works but it does not allocate memory to an identifier. Since int a; statement introduces type of "a" and also allocates memory, it becomes definition of variable "a".
3. The following code finds the average of three numbers:

#include<stdio.h>
#include<conio.h> // Linux user - Remove this
int main()
{
int a=10, b=20, c=30, avg;
avg= a+b+c/3;
printf("Average is: %d", avg);
getch(); // Linux user - Remove this
return 0;
}
Does the user get the correct answer?

  • No, because of following reasons:
  • the division operator have higher precedence than the addition operator. Hence expression is evaluated as "avg = a+b+(c/3);"
  • The division operator operates on integer operand. So the result will be in integer. When "c" is divided by 3, only integer part is returned and fraction part is removed.
4. Every statement in C is terminated by a semicolon. Can we write the following codes which the first statement inside main is terminated by comma instead of a semicolon. Will it work?

#include<stdio.h>
#include<conio.h> // Linux user - Remove this
int main()
{
printf("This is C "),
printf("Programming."); 
getch(); // Linux user - Remove this
return 0;
}

  • Yes, the code will work. The code has only one statement containing two expression separated by comma, i.e printf ("This is C ") and printf ("Programming."); As the expression separated by comma are evaluated in left to right order, printf("This is C ") is evaluated followed by printf("Programming."). Therefore, the output is: This is C Programming.
5. Will the following code work. The two expressions are separated by two commas.

#include<stdio.h>
#include<conio.h> 
// Linux user - Remove this
int main()
{
int x=10, , y=32;
printf("%d %d"), x, y);
getch();// Linux user - Remove this
return 0;
}

  • No. The compiler gives compile time error. This is because two comma operators cannot appear consecutively.

Share this

Related Posts

Previous
Next Post »