Linux Lite Forums

Full Version: C Programming in Linux
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi i am trying to learn to program in c. and is using this guide. C Programming in Linux © David Haskins 2008
now at chapter2_1.c i got in to a problem the compelling will not run and I can not see what's wrong, is there som help to get?

Code:
#include <stdio.h> #DEFINE STRINGSIZE 256 int 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;

Code:
Programming_In_Linux$ gcc -o data1 chapter2_1.c -lc chapter2_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 in chapter2_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=]
Bumping this for the OP.

Anyone here code in C that can help?


,

I have next to no experience, but I think I see one error.  Correct this first, run program again and see if you still get other errors.

I could be wrong, but I believe that "DEFINE" should be "define" in this line:
Code:
#define STRINGSIZE 256


P.s.  Does the book you're working from have a free version on the Internet?  If yes, can you provide a link to the problem you're working on.
yes that made a difference, now it is compelling and the output is correct but there i still a problem.
Code:
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)));


and to your question,  i get my book from bookboom

Code:
http://bookboon.com/
I ran a test of your code in VirtualBox install of LL and I did not get the errors you are reporting.

I called the program "sample.c" and saved it to the Desktop.  Here is the exact code I used:

Code:
#include <stdio.h> #define STRINGSIZE 256 int 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; }


I compiled it with this code:

Code:
homey@homey-VirtualBox:~/Desktop$ gcc sample.c -o sample


Ran it with this command and got the following output:

Code:
homey@homey-VirtualBox:~/Desktop$ ./sample Town name:Guildford population:66773 County:Surrey Country:Great Britain Location latitude:51.238598 longitude: -0.566257 char=1 byte int=4 bytes float=4 bytes memory used:780 bytes


Compiled it again with the following command to match what you did, ran program again and got the same output as above:

Code:
homey@homey-VirtualBox:~/Desktop$ gcc -o sample sample.c -lc homey@homey-VirtualBox:~/Desktop$ ./sample Town name:Guildford population:66773 County:Surrey Country:Great Britain Location latitude:51.238598 longitude: -0.566257 char=1 byte int=4 bytes float=4 bytes memory used:780 bytes


P.s.  Is this line of output what you were expecting it to be?
Code:
char=1 byte int=4 bytes float=4 bytes
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
(04-19-2015, 12:05 PM)hhbuur link Wrote: [ -> ]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

I used 32-bit LL 2.2 and the gcc package is the same version# as yours.


I just downloaded the book so I could see exactly what the exercise was.  Aside from making the correction from "DEFINE" to "define", it all looks the same as what I input and the output from mine matches that of the book (with no error messages).  In all likelihood, if you're still getting error messages, you have a typo somewhere or a missed character.  Double-check what you have.

From what you've posted, the only other thing I noticed is that you had a space between "sizeof" and "(int)" on this line:
Code:
    printf("memory used:%d bytes\n",         ((STRINGSIZE*3) * sizeof(char)) + sizeof (int) + (2 * sizeof(float)));

However, I purposely ran the code with that space also and it did not give me an error -- so I doubt that is the source of your problem.

Only other thing that  I can think of is that you made the change from "DEFINE" to "define", saved the document, but did not re-compile the program with the new change.  You need to save the change in the text file, then re-compile it before running the program again.

Aside from that, I'm clueless as to why it's not working for you.
It appears that you missed the } on the very last line.
i haw the } on the last line. I will continue with exercises and see how it's going to proceed Wink