I am learning C from this website. However, when I come up to this tutorial, I seem to be getting strange outcomes. The site recommended to try to make the code without looking at the example first, so I tried entering this code.
#include <stdio.h>
int main()
{
printf("The \"long\" keyword is pretty useful!\n");
int a=5;
long int la=5;
long long int lla=5;
double b=5;
long double lb=5;
printf("%d (as a normal int) is %d bytes big!\n", a, sizeof(a));
printf("%d (as a long int) is %d bytes big!\n",la, sizeof(la));
printf("%d (as a long long int) is %d bytes big!\n",lla, sizeof(lla));
printf("%lf (as a normal double) is %d bytes big!\n",b, sizeof(b));
printf("%lf (as a long double) is %d bytes big!\n"),lb, sizeof(lb);
}
and I get this response
The "long" keyword is pretty useful!
5 (as a normal int) is 4 bytes big!
5 (as a long int) is 4 bytes big!
5 (as a long long int) is 0 bytes big!
5.000000 (as a normal double) is 8 bytes big!
5.000000 (as a long double) is 8 bytes big!
It didn't make any sense, so eventually I looked at the example, and saw that it didn't declare any numbers, just the variables. Skipping a mistake I made regarding calling for a variable when there's no set number, I tried doing this instead:
int a;
long int la;
long long int lla;
double b;
long double lb;
printf("A normal int is %d bytes big!\n", sizeof(a));
printf("A long int is %d bytes big!\n", sizeof(la));
printf("A long long int is %d bytes big!\n", sizeof(lla));
printf("A normal double is %d bytes big!\n", sizeof(b));
printf("A long double is %d bytes big!\n"), sizeof(lb);
to which I get the outcome:
The "long" keyword is pretty useful!
A normal int is 4 bytes big!
A long int is 4 bytes big!
A long long int is 8 bytes big!
A normal double is 8 bytes big!
A long double is 8 bytes big!
Which is almost the intended outcome, except "long int" is 4 bytes big, the same size as a normally declared int, for some unknown reason. Same with "long double".
Eventually, I decided to just copy/paste the example the website gave, to make sure if I'm making a mistake or not, which is this:
#include <stdio.h>
int main() {
int a;
long b; // equivalent to long int b;
long long c; // equivalent to long long int c;
double e;
long double f;
printf("Size of int = %ld bytes \n", sizeof(a));
printf("Size of long int = %ld bytes\n", sizeof(b));
printf("Size of long long int = %ld bytes\n", sizeof(c));
printf("Size of double = %ld bytes\n", sizeof(e));
printf("Size of long double = %ld bytes\n", sizeof(f));
return 0;
}
And the site says the example should return
Size of int = 4 bytes
Size of long int = 8 bytes
Size of long long int = 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes
But for me, it returns:
Size of int = 4 bytes
Size of long int = 4 bytes
Size of long long int = 8 bytes
Size of double = 8 bytes
Size of long double = 12 bytes
This is the only part that I can't seem to find the answer as to why it's not returning the same as what the site says.
I have no clue as to why it's doing this, nor why my first attempt seemed to not return the "sizeof" parts correctly (especially the third line, that returns that a long long int is 0 bytes big.) Does anyone know?