Switch to full style
C++ code examples
Post a reply

Type Conversion

Thu Nov 13, 2008 2:36 pm

convert from type to type in C++
cpp code
/*
* Type conversion games.
*/
#include <stdio.h>

main()
{
/* Meaningless numbers. */
short s = -41;
int i = 9;
double d = 4.27;
double ld = 4983.22;
long l = 17;
long long ll = 170;
unsigned ui = 48;

/* When using printf, you must specify the size for items longer
than int or double. Many incorrect combinations work anyway,
but it's a good idea to get it right. */
printf("A: %d %ld %lld %f %lf\n", i, l, ll, d, ld);

/* Operators generally convert to the longer type, but you can
force other conversions with casts. */
printf("B: %f %d %d %f %f\n",
i * d,
i * (int)d,
i / 12,
i / 12.0,
(double)i / 12);

/* Conversion will convert to the left type. */
i = d;
d = s;
printf("C: %d %f\n", i, d);

/* Unsigned values can be printed with the u conversion. It generally
differs from %d only for large numbers. */
printf("D: %u\n", ui);

/* Constants can be appended with a letter to give them the
associated type. This generally matters only when the
constant you want won't fit into the unmodified length. */
l = 1598L;
ll = 9081092830129ll; /* Maybe LL would be better than ll. */
ld = 49.3981273394L;
printf("F: %ld %lld %.12lf\n", l, ll, ld);

/* Converting from a longer to a smaller size will also produce
junk if the value does not fit. */
i = 8000000;
s = i;
printf("G: %d == %d (sort of)\n", i, s);
}

convert from type to type in C++



Post a reply
  Related Posts  to : Type Conversion
 .dll conversion     -  
 Number Conversion     -  
 please get me the following Delphi to Java conversion     -  
 conversion from binary to decimal numbers as string     -  
 ramp Counter ANALOGUE TO DIGITAL CONVERSION (ADC)     -  
 SUCCESSIVE APPROXIMATION ANALOGUE TO DIGITAL CONVERSION     -  
 Type conversions     -  
 set MIME type in link     -  
 String type default value     -  
 Is String a data type     -  

Topic Tags

C++ Variables