x-pert
07-14-2007, 09:18 PM
Please go through
Lesson 01 (http://www.elakiri.com/forum/showthread.php?t=35788)
Lesson 02 (http://www.elakiri.com/forum/showthread.php?t=35876)
Lesson 03 (http://www.elakiri.com/forum/showthread.php?t=36705)
Lesson 04 (http://www.elakiri.com/forum/showthread.php?t=36710) and
Lesson 05 (http://www.elakiri.com/forum/showthread.php?t=37535) before reading this lesson. Actually this is a continuation of Lesson 05 (http://www.elakiri.com/forum/showthread.php?t=37535).
Floating point numbers
C language uses a Decimal point (Dashama thitha) to distinguish between integers and floating point numbers. Floating point numbers always contain a decimal point.
Numbers such as 5.0, 34.56, 23.5, 122.25 can be considered as floating point numbers.
KEEP IN MIND numbers such as 12, 3 and 666 are integers and 12.0, 3.0 and 666.0 are floating point numbers. (If you add a decimal point to the digit, it will become a float value.)
A floating point zero should be written as 0.0
And you can store exponential numbers in floating point type as well. You have to use e to denote an exponential number.
7.5e27 means 7.5 X 1027
The general form of defining a floating point variable is almost the same as defining an inger, but except int key word, you have to use float
float variable_name; /*comment*/
Like integers, floating point numbers also have limitations. We will discuss it in another lesson.
e.g. find the answer of the expression 1.0 / 3.0
Simply we can do it by
#include <stdio.h>
int main()
{
printf("The answer is %f\n", 1.0/3.0);
return (0);
}
DID YOU NOTICE WE HAVE USED %f as the place holder this time....
Earlier for Integers we used %d and for floating point numbers we have to use %f
Division operator is a special operator ( / )
Remember that there is a difference between integer division and float division.
In integer division, the fraction part will be discarded. So you won’t get the fraction part in the result.
17/10 in integer division will give you the result as 1
.7 will get discarded.
But on the other hand you will get the correct result as 1.7 if you divide float values 17.0 / 10.0
If either of the Denominator (haraya) or numerator (lawaya) is a float value, you will get the result as a float. So 17.0/10 , 17/10.0 or 17.0/10.0 will give you a correct floating point value. BUT NOT 17/10
e.g.
1 + 2 = 3 (Integer)
10/10 = 1 (Integer)
10.0/10 = 1.0 (float)
23/10 = 2 (Int) In here, since both the denominator and numerator are integers, the .3 will be discarded.
23.0/10.0 = 2.3 (float)
C language allows automatic conversion of integer to floating point numbers:
And it is possible to convert floating point numbers to integers. But the fraction part will be discarded. (Part after the dashama thitha will be discarded)
#include <stdio.h>
int any_integer; /*integer type variable named integer*/
float any_float; /*floating point type variable named float*/
int main
{
any_float = 2.0 / 4.0; /*this will assign 0.5 to any_float*/
integer = 2/5; /*this will assign 0 to integer variable*/
any_float = (2/5) + (2/5); /*this will assign 0 to any_float....... Why did this happen...?? the correct answer should be 0.8.... But why it is 0.....??? */
/* You have to figure out why this happens..... even though the any_float variable is a floating point number, we divide two integers (2/5) and not two floating point numbers like 2.0/5.0
So 2/5 is 0 when consider integer division and 0 + 0 is still 0.
That’s why you got the answer as 0 in the above example code line */
any_float = 6.0/4.0 /*assigns 1.5 to the any_float variable*/
integer = any_float
/* now we are asking the program to assign the value of any_float to integer variable. any_float has the value 1.5 coz of the line above... (6.0/4.0). But integer cannot contain a fraction part. So integer variable will now assigned 1.... just 1... And it has to discard the .5 */
return (0);
}
Just for an experiment.... If you like to print each and every result on the screen, just add a printf() line after each expression and see whether what I’m telling is true.. :) :) :)
If you followed the lesson correctly, this is not a big deal... But still if you need any help just let me know.... :) :)
Characters
Character type variables are denoted by char.
General form of defining a character type variable is
char variable_name /*comment*/
Characters are enclosed in single quotes like ‘A’. ‘A’ , ‘a’, ‘!’ are character constants.
Backslash character is called the escape character ( \ )
\b : Backspace (Moves the cursor to the left by one character)
\t : Tab (moves the cursor to the right by one tab stop and usually there are 8 tab spaces in a standard screen)
\n : new line (Moves the cursor to the beginning of next line)
\f : From feed (moves the cursor to the top of a new page)
\r : return (moves cursor to the beginning of the current line)
\’ : Apostrophe character ( ‘ )
\” : Double quote ( “ )
\\ : Backslash ( \ )
\nnn : character number in octal (ASCII)
Characters are enclosed in single quotes (‘a’) and strings are enclosed in double quotes (“string”)
When using the printf function to print a character on the string. You have to use the %c place holder.
Example (Taken from the source book)
#include <stdio.h>
char char1; /* first character */
char char2; /* second character */
char char3; /* third character */
int main()
{
char1 = 'A'; /* we are assigning A to variable char1*/
char2 = 'B'; /* assigns B to variable char2*/
char3 = 'C'; /*assignes C to variable char3*/
printf("%c%c%c reversed is %c%c%c\n",char1, char2, char3,char3, char2, char1);
return (0);
}
First three %c place holders will get A B and C and the second set of three %c place holders will get C B and A accordingly. Hope you can understand this coding. If you didn’t, please see the last lesson where I have used a diagram to show this.
To explain it further...........
http://img367.imageshack.us/img367/6560/pic3cqw0.jpg
Next lesson will be on arrays and capturing inputs from the key board.
Cheers...!!!
Lesson 01 (http://www.elakiri.com/forum/showthread.php?t=35788)
Lesson 02 (http://www.elakiri.com/forum/showthread.php?t=35876)
Lesson 03 (http://www.elakiri.com/forum/showthread.php?t=36705)
Lesson 04 (http://www.elakiri.com/forum/showthread.php?t=36710) and
Lesson 05 (http://www.elakiri.com/forum/showthread.php?t=37535) before reading this lesson. Actually this is a continuation of Lesson 05 (http://www.elakiri.com/forum/showthread.php?t=37535).
Floating point numbers
C language uses a Decimal point (Dashama thitha) to distinguish between integers and floating point numbers. Floating point numbers always contain a decimal point.
Numbers such as 5.0, 34.56, 23.5, 122.25 can be considered as floating point numbers.
KEEP IN MIND numbers such as 12, 3 and 666 are integers and 12.0, 3.0 and 666.0 are floating point numbers. (If you add a decimal point to the digit, it will become a float value.)
A floating point zero should be written as 0.0
And you can store exponential numbers in floating point type as well. You have to use e to denote an exponential number.
7.5e27 means 7.5 X 1027
The general form of defining a floating point variable is almost the same as defining an inger, but except int key word, you have to use float
float variable_name; /*comment*/
Like integers, floating point numbers also have limitations. We will discuss it in another lesson.
e.g. find the answer of the expression 1.0 / 3.0
Simply we can do it by
#include <stdio.h>
int main()
{
printf("The answer is %f\n", 1.0/3.0);
return (0);
}
DID YOU NOTICE WE HAVE USED %f as the place holder this time....
Earlier for Integers we used %d and for floating point numbers we have to use %f
Division operator is a special operator ( / )
Remember that there is a difference between integer division and float division.
In integer division, the fraction part will be discarded. So you won’t get the fraction part in the result.
17/10 in integer division will give you the result as 1
.7 will get discarded.
But on the other hand you will get the correct result as 1.7 if you divide float values 17.0 / 10.0
If either of the Denominator (haraya) or numerator (lawaya) is a float value, you will get the result as a float. So 17.0/10 , 17/10.0 or 17.0/10.0 will give you a correct floating point value. BUT NOT 17/10
e.g.
1 + 2 = 3 (Integer)
10/10 = 1 (Integer)
10.0/10 = 1.0 (float)
23/10 = 2 (Int) In here, since both the denominator and numerator are integers, the .3 will be discarded.
23.0/10.0 = 2.3 (float)
C language allows automatic conversion of integer to floating point numbers:
And it is possible to convert floating point numbers to integers. But the fraction part will be discarded. (Part after the dashama thitha will be discarded)
#include <stdio.h>
int any_integer; /*integer type variable named integer*/
float any_float; /*floating point type variable named float*/
int main
{
any_float = 2.0 / 4.0; /*this will assign 0.5 to any_float*/
integer = 2/5; /*this will assign 0 to integer variable*/
any_float = (2/5) + (2/5); /*this will assign 0 to any_float....... Why did this happen...?? the correct answer should be 0.8.... But why it is 0.....??? */
/* You have to figure out why this happens..... even though the any_float variable is a floating point number, we divide two integers (2/5) and not two floating point numbers like 2.0/5.0
So 2/5 is 0 when consider integer division and 0 + 0 is still 0.
That’s why you got the answer as 0 in the above example code line */
any_float = 6.0/4.0 /*assigns 1.5 to the any_float variable*/
integer = any_float
/* now we are asking the program to assign the value of any_float to integer variable. any_float has the value 1.5 coz of the line above... (6.0/4.0). But integer cannot contain a fraction part. So integer variable will now assigned 1.... just 1... And it has to discard the .5 */
return (0);
}
Just for an experiment.... If you like to print each and every result on the screen, just add a printf() line after each expression and see whether what I’m telling is true.. :) :) :)
If you followed the lesson correctly, this is not a big deal... But still if you need any help just let me know.... :) :)
Characters
Character type variables are denoted by char.
General form of defining a character type variable is
char variable_name /*comment*/
Characters are enclosed in single quotes like ‘A’. ‘A’ , ‘a’, ‘!’ are character constants.
Backslash character is called the escape character ( \ )
\b : Backspace (Moves the cursor to the left by one character)
\t : Tab (moves the cursor to the right by one tab stop and usually there are 8 tab spaces in a standard screen)
\n : new line (Moves the cursor to the beginning of next line)
\f : From feed (moves the cursor to the top of a new page)
\r : return (moves cursor to the beginning of the current line)
\’ : Apostrophe character ( ‘ )
\” : Double quote ( “ )
\\ : Backslash ( \ )
\nnn : character number in octal (ASCII)
Characters are enclosed in single quotes (‘a’) and strings are enclosed in double quotes (“string”)
When using the printf function to print a character on the string. You have to use the %c place holder.
Example (Taken from the source book)
#include <stdio.h>
char char1; /* first character */
char char2; /* second character */
char char3; /* third character */
int main()
{
char1 = 'A'; /* we are assigning A to variable char1*/
char2 = 'B'; /* assigns B to variable char2*/
char3 = 'C'; /*assignes C to variable char3*/
printf("%c%c%c reversed is %c%c%c\n",char1, char2, char3,char3, char2, char1);
return (0);
}
First three %c place holders will get A B and C and the second set of three %c place holders will get C B and A accordingly. Hope you can understand this coding. If you didn’t, please see the last lesson where I have used a diagram to show this.
To explain it further...........
http://img367.imageshack.us/img367/6560/pic3cqw0.jpg
Next lesson will be on arrays and capturing inputs from the key board.
Cheers...!!!