0 Members and 1 Guest are viewing this topic.
i get the same outputs as before no mater witch of yours variations I try, can there may be a differences in the versions of gcc, makes the difference in the output, the version i haw install is 4:4.8.2-1ubuntu6 , and your ? I get the same result as in the book
printf("memory used:%d bytes\n", ((STRINGSIZE*3) * sizeof(char)) + sizeof (int) + (2 * sizeof(float)));
#include <stdio.h>#define STRINGSIZE 256int main(int argc, char*argv[]){ char town[STRINGSIZE]="Guildford"; char county[STRINGSIZE]="Surrey"; char country[STRINGSIZE]="Great Britain"; int population=66773; float latitude=51.238599; float longitude=-0.566257; printf("Town name:%s population:%d\n",town,population); printf("County:%s\n",county); printf("Country:%s\n",country); printf("Location latitude:%f longitude: % f\n",latitude,longitude); printf("char=%d byte int=%d bytes float=%d bytes\n", sizeof(char),sizeof(int),sizeof(float)); printf("memory used:%d bytes\n", ((STRINGSIZE*3) * sizeof(char)) + sizeof (int) + (2 * sizeof(float))); return 0;}
homey@homey-VirtualBox:~/Desktop$ gcc sample.c -o sample
homey@homey-VirtualBox:~/Desktop$ ./sample Town name:Guildford population:66773County:SurreyCountry:Great BritainLocation latitude:51.238598 longitude: -0.566257char=1 byte int=4 bytes float=4 bytesmemory used:780 bytes
homey@homey-VirtualBox:~/Desktop$ gcc -o sample sample.c -lchomey@homey-VirtualBox:~/Desktop$ ./sample Town name:Guildford population:66773County:SurreyCountry:Great BritainLocation latitude:51.238598 longitude: -0.566257char=1 byte int=4 bytes float=4 bytesmemory used:780 bytes
char=1 byte int=4 bytes float=4 bytes
chapter2_1.c: In function ‘main’:chapter2_1.c:23:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=] sizeof(char),sizeof(int),sizeof(float)); ^chapter2_1.c:23:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=]chapter2_1.c:23:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat=]chapter2_1.c:25:17: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=] ((STRINGSIZE*3) * sizeof(char)) + sizeof (int) + (2 * sizeof(float)));
http://bookboon.com/
#define STRINGSIZE 256
#include <stdio.h>#DEFINE STRINGSIZE 256int main(int argc, char*argv[]){ char town[STRINGSIZE]="Guildford"; char county[STRINGSIZE]="Surrey"; char country[STRINGSIZE]="Great Britain"; int population=66773; float latitude=51.238599; float longitude=-0.566257; printf("Town name:%s population:%d\n",town,population); printf("County:%s\n",county); printf("Country:%s\n",country); printf("Location latitude:%f longitude: % f\n",latitude,longitude); printf("char=%d byte int=%d bytes float=%d bytes\n", sizeof(char),sizeof(int),sizeof(float)); printf("memory used:%d bytes\n", ((STRINGSIZE*3) * sizeof(char)) + sizeof (int) + (2 * sizeof(float))); return 0;
Programming_In_Linux$ gcc -o data1 chapter2_1.c -lcchapter2_1.c:7:2: error: invalid preprocessing directive #DEFINE #DEFINE STRINGSIZE 256 ^chapter2_1.c: In function ‘main’:chapter2_1.c:11:12: error: ‘STRINGSIZE’ undeclared (first use in this function) char town[STRINGSIZE]="Guildford"; ^chapter2_1.c:11:12: note: each undeclared identifier is reported only once for each function it appears inchapter2_1.c:22:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=] sizeof(char),sizeof(int),sizeof(float)); ^chapter2_1.c:22:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘long unsigned int’ [-Wformat=]chapter2_1.c:22:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 4 has type ‘long unsigned int’ [-Wformat=]