+ 1

Question on example of C Functions and Data Types

Four of the basic data types 'int', 'float', 'double' and 'char' were already presented in the Data Types lesson of the Basic Concepts section. Other variants of the 'int' data type are 'long int' and 'long long int', which apparently have vastly expanding numeric ranges than does 'int' itself. My question is on the unexpected results from adjusting the sample program in the Functions in C lesson to calculate cubes instead of squares, as follows: ------ code ------ #include <stdio.h> /* declaration */ long int cubed (int num); long int main() { int x; long int result; x = 1400; result = cubed(x); printf("%d to the third power is %d\n", x, result); return 0; } /* definition */ long int cubed (int num) { long int y; y = num * num * num; return(y); } ------------------ Why do the results of this code always give 1400 to the third power as -1550967296 regardless of whether 'long int', 'long long int', or basic 'int' are substituted into the corresponding declaration, main()-block, and definition sections just as above?? What further #include header, conversion keywords, and/or other additions should I use to get the expected results in the above cubed function and its main() call??

12th Jun 2019, 3:57 PM
itfactor36
5 Respuestas
+ 1
using "long" and "long long" are only supported in C99 standard, maybe check what compiler you're using? Edit: i figured it out! you need to include the "stdint.h" header file at the top of your code write "#include <stdint.h>" and it will work fine.
12th Jun 2019, 4:06 PM
haydenki
haydenki - avatar
+ 1
Robin wrote: "Did you remember to change the data type of the function 'cubed' as well?" As stated previously, "replaced *all* appearances of 'long int' and 'int' with 'long long' instead." Therefore, in addition to the two numbered main() codeblock variations listed previously.... /* declaration */ long long cubed (long long num); ... ... /* definition */ long long cubed (long long num) { long long y; y = num * num * num; return(y); } EDIT: Perhaps there is an additional '#include <>' header, C keyword, and/or data type required for obtaining the expected 32bit output for x >= 1291 ??
12th Jun 2019, 6:07 PM
itfactor36
0
pathetic_millenial wrote: "using "long" and "long long" are only supported in C99 standard, maybe check what compiler you're using? Edit: i figured it out! you need to include the "stdint.h" header file at the top of your code write "#include <stdint.h>" and it will work fine." TY for that :) The compiler is actually whatever C compiler SoloLearn has in its Code Playground. Added the '#include <stdint.h>' on top as suggested and then as an extra check replaced *all* appearances of 'long int' and 'int' with 'long long' instead. Still received negative-signed and unexpected output though. To demonstrate the unexpected output, we can contrast the output of the two below code snippet variations solely within the main() code-block: 1. /* Expected output of 2146689000 using x = 1290 */ long long main() { long long x; long long result; x = 1290; result = cubed(x); printf("%d to the third power is %d\n", x, result); return 0; } 2. /* Unexpected output of -2143282125 (not 2151685171) using x = 1291 */ long long main() { long long x; long long result; x = 1291; result = cubed(x); printf("%d to the third power is %d\n", x, result); return 0; }
12th Jun 2019, 5:30 PM
itfactor36
0
Robin wrote: " Change: printf("%I64d to the third power is %I64d\n", x, result); " You might want to re-test this printf statement replacement. My own output with the suggested long-long-64-decimal replacement of '%I64d' for '%d', and having *all* 'long long' data type specifiers in the above code, is "64d to the third power is 64d". Didn't run any of the suggested ln transformations though....
12th Jun 2019, 6:35 PM
itfactor36
0
> or maybe you forgot the "%I" ? Or misinterpreted the characters (mistaken captial i for lowercase L)? Ensuring that the characters are/were capital-i instead of lowercase-L in 'printf("%I64d to the third power is %I64d\n", x, result)' , the output was subsequently completely expected for x <= 2097151 in SoloLearn's Code Playground. So thanks :) In addition, by replacing 'long' for every occurrence of 'long long' in the complete code on top and using 'printf("%Id to the third power is %Id\n", x, result); /* capital-i and note omission of the "64" */' , the output was also accurate and expected for x <= 1625.
12th Jun 2019, 9:28 PM
itfactor36