You are Here:
Linux Lite 4.2 Beta has been released. See the Release Announcements Section.




C Programming in Linux

Author (Read 4414 times)

0 Members and 1 Guest are viewing this topic.

C Programming in Linux
« on: April 14, 2015, 07:16:47 AM »
 

hhbuur

  • New to Forums
  • *
  • 10
    Posts
  • Reputation: 3
  • Linux Lite Member
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=]
 


Re: C Programming in Linux
« Reply #1 on: April 17, 2015, 10:46:41 PM »
 

gold_finger

  • Documentation Writer
  • Platinum Level Poster
  • *****
  • 1095
    Posts
  • Country: us
  • Reputation: 321
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel Core2 Duo E7500 3.0GHz

  • MEMORY: 4Gb

  • VIDEO CARD: Intel 4 Series Integrated Graphics
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.
Try Linux Beginner Search Engine for answers to Linux questions.
 

Re: C Programming in Linux
« Reply #2 on: April 18, 2015, 06:29:02 AM »
 

hhbuur

  • New to Forums
  • *
  • 10
    Posts
  • Reputation: 3
  • Linux Lite Member
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/
 

Re: C Programming in Linux
« Reply #3 on: April 18, 2015, 09:55:14 AM »
 

gold_finger

  • Documentation Writer
  • Platinum Level Poster
  • *****
  • 1095
    Posts
  • Country: us
  • Reputation: 321
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel Core2 Duo E7500 3.0GHz

  • MEMORY: 4Gb

  • VIDEO CARD: Intel 4 Series Integrated Graphics
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]
homey@homey-VirtualBox:~/Desktop$ gcc sample.c -o sample

Ran it with this command and got the following output:

Code: [Select]
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: [Select]
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: [Select]
char=1 byte int=4 bytes float=4 bytes
Last Edit: April 18, 2015, 09:58:55 AM by gold_finger
Try Linux Beginner Search Engine for answers to Linux questions.
 

Re: C Programming in Linux
« Reply #4 on: April 19, 2015, 08:05:24 AM »
 

hhbuur

  • New to Forums
  • *
  • 10
    Posts
  • Reputation: 3
  • Linux Lite Member
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
 

Re: C Programming in Linux
« Reply #5 on: April 19, 2015, 10:24:49 AM »
 

gold_finger

  • Documentation Writer
  • Platinum Level Poster
  • *****
  • 1095
    Posts
  • Country: us
  • Reputation: 321
  • Linux Lite Member

  • Linux Lite: 3.2 64bit

  • CPU: Intel Core2 Duo E7500 3.0GHz

  • MEMORY: 4Gb

  • VIDEO CARD: Intel 4 Series Integrated Graphics
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.
Try Linux Beginner Search Engine for answers to Linux questions.
 

Re: C Programming in Linux
« Reply #6 on: April 19, 2015, 12:54:42 PM »
 

avj

  • Gold Level Poster
  • *******
  • 530
    Posts
  • Country: us
  • Reputation: 110
  • Linux Lite Member

  • Linux Lite: 2.8 64bit

  • CPU: Dual core Intel Pentium D 2.80GHz

  • MEMORY: 2Gb

  • VIDEO CARD: AMD/ATI RC410 Radeon Xpress 200/1100
It appears that you missed the } on the very last line.
“I have not failed. I’ve just found 10,000 ways that won’t work.” - Thomas Edison
 

Re: C Programming in Linux
« Reply #7 on: April 21, 2015, 05:27:37 AM »
 

hhbuur

  • New to Forums
  • *
  • 10
    Posts
  • Reputation: 3
  • Linux Lite Member
i haw the } on the last line. I will continue with exercises and see how it's going to proceed ;)
 


Tags:
 


Linux Lite 4.2 Beta has been released. See the Release Announcements Section.