Data types & Operators
Description of data types and operators
Last updated
sum of 1..5 = 15? {
float var1 = 11.5;
float var2 = 0.5;
float var3 = .5;
float var4 = 11.;
float negative1 = -11.5;
float negative2 = -.5;
printf("floating point: %f %f %f %f\n", var1, var2, var3, var4);
printf("negative floating point: %f %f\n", negative1, negative2);
}floating point: 11.500000 0.500000 0.500000 11.000000
negative floating point: -11.500000 -0.500000? {
float floatAdd = 11.5 + 0.5;
float floatSub = 11.5 - 0.5;
float floatMul = 1.5 * 2.0;
float floatDiv = 7.5 / 2.5;
printf("add=%f sub=%f mul=%f div=%f\n", floatAdd, floatSub, floatMul, floatDiv);
}add=12.000000 sub=11.000000 mul=3.000000 div=3.000000? {
double var5 = 0.789;
double negative3 = -0.789;
double negativeZero = -0.0;
double positive = +.5;
printf("double: %f\n", var5);
printf("negative double: %f %f\n", negative3, negativeZero);
printf("positive: %f\n", positive);
printf("runtime negative: %f\n", -var5);
}double: 0.789000
negative double: -0.789000 -0.000000
positive: 0.500000
runtime negative: -0.789000? {
double doubleAdd = 0.75 + 0.25;
double doubleSub = 5.5 - 2.0;
double doubleMul = 1.25 * 4.0;
double doubleDiv = 9.0 / 4.0;
printf("add=%f sub=%f mul=%f div=%f\n", doubleAdd, doubleSub, doubleMul, doubleDiv);
}add=1.000000 sub=3.500000 mul=5.000000 div=2.250000? {
float mixedSingle = 1.5;
double mixedResult = mixedSingle + 0.25;
double precedenceResult = 1.0 + 2.0 * 3.0;
double negativeResult = -2.0 * 3.0;
printf("mixed=%f precedence=%f negative=%f\n", mixedResult, precedenceResult, negativeResult);
}mixed=1.750000 precedence=7.000000 negative=-6.000000? {
float floatAdd = 11.5 + 0.5;
float floatSub = 11.5 - 0.5;
double negativeZero = -0.0;
if (floatAdd > floatSub && floatSub >= 11.0 &&
floatSub < floatAdd && floatSub <= 11.0 &&
floatAdd != floatSub && negativeZero == 0.0)
{
printf("floating arithmetic was successful\n");
}
else
{
printf("floating arithmetic was failed\n");
}
}floating arithmetic was successfulsizeof(type_or_variable)? {
struct VariableTypePair {
int left;
unsigned short right;
};
printf("char=%lld\n", sizeof(char));
printf("short=%lld\n", sizeof(short));
printf("int=%lld\n", sizeof(int));
printf("long=%lld\n", sizeof(long));
printf("long long=%lld\n", sizeof(long long));
printf("struct=%lld\n", sizeof(struct VariableTypePair));
}char=1
short=2
int=4
long=8
long long=8
struct=8? {
implicit_variable = 0xffffffffffffffff; // the default variable type is unsigned long long
printf("value=%llu\n", implicit_variable);
printf("size=%lld\n", sizeof(implicit_variable));
if (implicit_variable > 0) {
printf("implicit variable is unsigned\n");
}
}value=18446744073709551615
size=8
implicit variable is unsigned