Development > Coding

C Programming in Linux

(1/2) > >>

hhbuur:
hi i am trying to learn to program in c. and is using this guide. C Programming in Linux (c) 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;
--- End code ---


--- 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=]

--- End code ---

gold_finger:
Bumping this for the OP.

Anyone here code in C that can help?


@hhbuur,

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
--- End code ---


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.

hhbuur:
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)));

--- End code ---


and to your question,  i get my book from bookboom


--- Code: ---http://bookboon.com/
--- End code ---

gold_finger:
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;
}
--- End code ---


I compiled it with this code:


--- Code: [email protected]:~/Desktop$ gcc sample.c -o sample
--- End code ---


Ran it with this command and got the following output:


--- Code: [email protected]:~/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
--- End code ---


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


--- Code: [email protected]:~/Desktop$ gcc -o sample sample.c -lc
[email protected]:~/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
--- End code ---


P.s.  Is this line of output what you were expecting it to be?

--- Code: ---char=1 byte int=4 bytes float=4 bytes
--- End code ---

hhbuur:
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

Navigation

[0] Message Index

[#] Next page

Go to full version