Linux Lite Forums

Development => Coding => Topic started by: hhbuur on April 14, 2015, 07:16:47 AM

Title: C Programming in Linux
Post by: hhbuur on April 14, 2015, 07:16:47 AM
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: [Select]
#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: [Select]
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=]
Title: Re: C Programming in Linux
Post by: gold_finger on April 17, 2015, 10:46:41 PM
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: [Select]
#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.
Title: Re: C Programming in Linux
Post by: hhbuur on April 18, 2015, 06:29:02 AM
yes that made a difference, now it is compelling and the output is correct but there i still a problem.
Code: [Select]
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: [Select]
http://bookboon.com/
Title: Re: C Programming in Linux
Post by: gold_finger on April 18, 2015, 09:55:14 AM
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: [Select]
#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: [Select]
[email protected]:~/Desktop$ gcc sample.c -o sample

Ran it with this command and got the following output:

Code: [Select]
[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


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

Code: [Select]
[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


P.s.  Is this line of output what you were expecting it to be?
Code: [Select]
char=1 byte int=4 bytes float=4 bytes
Title: Re: C Programming in Linux
Post by: hhbuur on April 19, 2015, 08:05:24 AM
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
Title: Re: C Programming in Linux
Post by: gold_finger on April 19, 2015, 10:24:49 AM
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: [Select]
    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.
Title: Re: C Programming in Linux
Post by: avj on April 19, 2015, 12:54:42 PM
It appears that you missed the } on the very last line.
Title: Re: C Programming in Linux
Post by: hhbuur on April 21, 2015, 05:27:37 AM
i haw the } on the last line. I will continue with exercises and see how it's going to proceed ;)