Saturday 8 March 2014

Why modulus operator does not work with floating point values ?

We have seen that modulus operator (%) does not support floating point numbers.

(If you have any doubt regarding this visit http://implementc.blogspot.in/2013/12/trap-1-error-illegal-use-of-floating.html ).

If you want to know the reason behind this then continue reading :

Any arithmetic operation between :

1. int and int  = int
2. int and float = float
3. float and float = float

What a%b does is give the remainder of the two numbers. And remainder of any division will always be an integer value. Floating point remainder of any division has no meaning. Think about it.


So if you are using modulus operator with floating point numbers then according to Arithmetic Operation Table result will also have to be floating point number.

How can a remainder of division be a floating type of number?? 

So modulus of two floating point number has no meaning.

Enumeration data type : enum

With advancement in learning to program one encounters many new concepts, remember to all this new concepts were designed to solve complexity of programming. One such concept is of enumeration data type in C. Read further to know about it.


An enumeration is a list of constant integer values, as in

enum boolean { NO, YES };


The first name in an enum has value 0, the next 1, and so on, unless explicit values are specified. If not all values are specified, unspecified values continue the progression from the last specified value, as the second of these examples:

enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t',NEWLINE = '\n', VTAB = '\v', RETURN = '\r' };

enum months { JAN = 1, FEB, MAR, APR, MAY, JUN,JUL, AUG, SEP, OCT, NOV, DEC };
                                                                                                           /* FEB = 2, MAR = 3, etc. */

Names in different enumerations must be distinct. Values need not be distinct in the same enumeration. Enumerations provide a convenient way to associate constant values with names, an alternative to #define with the advantage that the values can be generated for you.

Although variables of enum types may be declared, compilers need not check that what you store in such a variable is a valid value for the enumeration. Nevertheless, enumeration variables offer the chance of checking and so are often better than #defines. In addition, a debugger may be able to print values of enumeration variables in their symbolic form.




Thursday 26 December 2013

Trap #1 :Error: Illegal Use Of Floating Point

There are  times when we write codes according to problem statement, Run it, and never be able to debug it.
Error: Illegal Use Of Floating Point is one such error. The problem is with the % operator you have used.

We all use '%' operator known as modulus operator in C. Lets focus on some facts about it :
  1. % operator gives Remainder of Division of the two numbers.
    Ex. 20 % 3 = 2
  2. On focusing a bit it will be clear that % operator gives its value in an int form.
  3. Hence its defined only for integer type variables. The above error occurs because you ave used % with a float type variable or constant.Ex #1:
    #include<stdio.h>
    main()
    {
    int a=20,b=3,c; //NO ERROR
    c=a%b;
    printf("%d",c);
    }
    ####No Error Output = 2

    Ex #2:
    #include<stdio.h>
    main()
    {
    float a=20,b=3,c;
    c=a%b;
    printf("%f",c);
    }
    ###Error illegal use of floating point
  4. If you want to use it with float values only then there is a solution for it too:
    use of fmod() Function defined under <math.h> library.

Wednesday 25 December 2013

Meaning, Introduction & the Target Of This Blog

Hello Friends

We are all well aware that there are many web material (may be written by experts) on C.
Here This Blog is a quite different as I am too a 'Beginner' and  a 'Learner' .

There is a lot of difference when you write programs while learning (Programming Exercise Of Books) and when you create a 'Real World Program' starting from scraps or a mere IDEA. Things in real world almost rarely work in beginners case.
Here we will be dealing with all those common Mistakes or Exceptions of C and also a discuss Tips on a effective programming.

And last and the least the most interesting part of the blog will be I will be teaching you to create many interesting projects which will include real life Implementation and also we will learn to write few VIRUSES.
Please feel free to ask anything.