Programming languages have many different ways of representing data. For example, two ways the C programming language can store a number are as an integer or as a double. An integer is a basic number that has no fractional component, like 1 and 2. A double is a floating point number, which can have a fractional component and an exponential component. This makes it suitable to store data like 2.718. Converting a double to an integer is guaranteed to lose a lot of precision, since all the decimal information is lost.
Things You'll Need
- C Compiler, such as GCC
- C Integrated Development Environment, such as Eclipse CDT
Load up your C Integrated Development Environment, by clicking on its icon, and start a new project. The workspace window will load up with a blank source code file.
Import these two libraries by writing the following lines at the top of the source code file:include <stdio.h>include <stdlib.h>
Create a main function by writing the following lines after the #include lines:
Declare an integer and double data type by writing the following two lines within the curly brackets of the main function:
int x = 0;
double e = 2.718;
Convert the double to an int by writing the next line of code:
x = (int) e;
Print out both the double and the int to see the results. You can use the function printf to accomplish this, like so:
printf("%d, %f\n", x, e);
Compile and run the program by pressing the "Run" button on the IDE. The program will compile and then execute. The output will be:
How to Convert String to Double Visual Basic
Conversion of variables to different types is an integral part of computer programming. The functions included in Visual Basic make this an...
Data Types for Turbo C
Turbo C is a compiler and development environment for the programming language C. It came out in 1987 and merged into Turbo...