C Glossary - d to f

default: This is optional part in switch statement. If none of the cases match the switch expression, the statements followed by default are executed. Note that default can be located anywhere in switch statement.

#include<stdio.h>
int main()
{
  int n1,n2;
  char op;
  double ans;
  scanf("%d",&n1,&n2);
  printf("Enter operator");
  scanf(" %c",&op);
  switch(op)
 {
       case '+': ans = n1+n2;
                    break;
       case '-': ans = n1-n2;
                    break;
      case '*': ans = n1*n2;
              break;
      case '+': ans = n1+n2;
              break;
       default: ans=-999;
 }
  if(ans!=-999)
  {
      printf("the answer is %f\n",ans);
  }
  else
  {
        printf("Invalid operator. Try again\n");
  }
  return 0;
}

Note that if user types any operator other than +-*/ , default statements are executed and he gets error message.

double: Double is a data type in C for non-integer numbers. Used to store double precision numbers with 15 digits accuracy using 8 bytes of memory. The range of numbers is +/- 1.7e +/- 308 .    
      ----
    double num1=1.2;
    double num2;

    When a non-integer literal is used in a program it is a double value not a float. Look at this program.

#include
int main()
{
   
   printf("size of 2 is %d\n",sizeof(2));//4. this is an int
   printf("size of 2.0 is %d\n",sizeof(2.0));//8 this is double
   printf("size of 2.0f is %d\n",sizeof(2.0f));//4 this is float
   printf("size of \'2\' is %d\n",sizeof('2'));//4 char literal
   // is integer
   return 0;
}


float: float is a non-integer data type in C which stores the numbers with 7 digits accuracy and is stored using 4 bytes. Range of numbers float can store is   3.4e - 38 to 3.4e + 38.
    e.g.
                         float num1 = 1.2f;
                float num2;

When in an expression if any one of the operands is non-integer, the entire expression is converted to double. Otherwise integer calculation takes place.

Round off error : double and float numbers in c programs cause round off error. If we compare these for equality, we get unexpected results. The reason is fractional numbers are stored in computer using mantissa and exponent. But this mantissa is stored using limited number of bits which causes 0.7 to be stored as 0.6999999 or as 0.7000000001. So we need to be careful when using these numbers in expressions.


for:  for is a repetition statement which lets us write loop in a concise manner.
The syntax is
 
  for(initialisation;comparison;modification)
      statement;
 
    The initialisation part initializes one/more variables. Then the comparison expression is evaluated. If it is true the for statement is executed. Then the modification statement is executed followed by comparison again. This way the loop of comparison - statement-modification repeats till comparison statement becomes false.
   for loop is ideal for where the loop has to be repeated definite number of times like array processing.
 
#include
int main()
{
      int a=10,b;
      //the for loop prints the squares of numbers from 10 to 1
      for(b=a;b>0;b--)
      {
         printf("%d\n",b*b);
      }
      return 0;
}

 To start with variable b is initialized to a i.e. 10. Then it is compared using b>0. Since the condition is true, body of the loop is executed which prints 10 squared. Then modification which is b-- is executed which maked b =9. Then again b is compared to 0. Since the condition is true, 9 squared is printed. In this way, the loop repeats till b becomes 0. Now the condition is false. Hence loop stops execution.

Note : You can find the ranges of floating point types for your compiler in the header file float.h. In this file, the macros FLT_MIN, FLT_MAX , DBL_MIN and DBL_MAX etc define the ranges.

Comments

Popular Posts