You are not logged in.

#1 2010-05-11 16:45:09

geniuz
Member
Registered: 2010-04-10
Posts: 127

Project to learn C

Hey everyone,

While I'm still busy learning the last details of bash, I'm already thinking of learning a second UNIX related language after this. C was the first one I came up with. C, to me, seems very fast and not too difficult to learn. In the end I would love to master the language in order to do contribute in some way to open-source programs which appeal to me.I'm already quite familiar with Java, and from what I've read online the syntax is more or less similar.

However, I came up with this idea which could make the learning curve more fun and useful. Namely last semester, during a university project, we were given the task to write a Java program which could compute a receiver position from GPS data provided. The data provided was in the so-called RINEX format which is some standard format for GPS data. What this boils down to is basically two large files, one with data about the receiver (which satellites are seen at which time and the ranges) and one with satellite data (orbit parameters and signal corrections). These files obviously need to be read by the software and stored somewhere. In Java we used huge arraylists and objects to store and extract the data.

Then all kinds of math has to be applied: computing satellite orbits, linearization of non-linear systems of equations, iterations and matrix algebra are the main subjects here. In Java we wrote several methods and classes which could perform these tasks. After this, once we obtained the receiver position, it is written to a KML file which is basically an XML file containing the latitude, longitude, and height of the receiver above the Earth's surface. This file can be used with Google Earth to plot the position. Note that I talk about one position, but in reality the steps are performed for a huge number of data points. Namely every second in 4 hours of data.

I know that the Java program we came up with took about one second to compute one receiver position, which meant a program runtime of about 4 hours. Now I have to admit, the program was written in a group of 9 (in the end me and two other guys, since the rest didn't really know how to program). And it is a mess, because we really had to rush it. That's why I'm thinking of rewriting it in C, in the hope to bring the run-time down significantly.

My main goal is of course to learn the language, however before I start, I would like to ask whether this project might sound a bit to hard for a C beginner. I mean, I'm willing to spend time on this, but there is always the possibility that I might need too advanced topics in order to make it work.

As a start, I have found the book "The C Programming Language" by Brian Kernighan and Dennis Ritchie. I skimmed it and it seemes very nice. But I'm open for any further suggestions.

Thanks a lot in advance !

Offline

#2 2010-05-11 16:52:01

Nibble
Member
Registered: 2009-09-29
Posts: 34

Re: Project to learn C

Try to start simple and work your way up, just because one knows java there is a lot of difference in the simple things which you might miss if you rush ahead.

Then learn about things like buffer overflows, how to prevent them, memory errors etc.

Joining some open source project is a good idea, will learn you lots.

Just my few thoughts in this area. I am no expert myself but I try to learn more and more everyday.

Offline

#3 2010-05-11 18:24:17

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Project to learn C

The hardest part of projects like this one is usually not the implementation, but modeling the problem and designing the solution.  If you've already done that in Java, that will help in some places.  However, don't try to imitate your Java solution in C.  With the understanding of the problem you now have, you should be better able to devise a fast, modular, and stable solution than your first attempt, keeping the good parts and reworking the sticky places.

I say go for it, but if you're not familiar with C, be prepared to spend a lot of time working out details.  I think this is a great project -- in fact, it sounds like something I would like to pursue myself -- but design it only in small pieces and implement them as you learn how.  You'll also want to have room in your brain to peruse your copy of K&R2 for a few hours a week (do the exercises, they work wonders).

Offline

#4 2010-05-11 18:47:02

geniuz
Member
Registered: 2010-04-10
Posts: 127

Re: Project to learn C

Yes, the book is indeed very good. I agree with you that the solution design was indeed very difficult, but also very fun. In the end we really got a nice result showing the flight path of an aircraft a couple of years of ago. I also think I will first read the whole book and indeed do all the exercises to get a nice overview of what's possible with the language and after that I'll try to apply to the project. After all, that's how I learned Java.

Trent, if you're really interested, I could send you the project description and our report if you want to have a go at it yourself. You can also have the program, although really it's a mess for anybody but me to fully understand and I hate spending time to clean it up, since I'm not much of a Java fan. But nevertheless if its something you like...and have a strong mathematical background, I'll be happy to share it.

Thanks for your comments so far, it's really useful !

Offline

#5 2010-05-11 19:41:58

Cyrusm
Member
From: Bozeman, MT
Registered: 2007-11-15
Posts: 1,053

Re: Project to learn C

I have no doubt that implementing this program properly in C will decrease the runtime.  The trick is going to be properly implementing the program.
With C, you have the ability to manage your memory such that the program will run more efficiently than in Java.  however you may run into some pitfalls.  In java, all of the garbage collection and memory management is taken care of for you so that you can concentrate on designing the program with the sacrifice that you won't have as much control of the machine.  with C, you can control every bit and byte, but you don't have anyone or anything holding your hand. (for example, there's no "array out of bounds" error, C expects YOU to design your array correctly) Therefore you can implement a fast and efficient program with the sacrifice that you have to properly allocate and release memory, which can detract from the task at hand.   I really would suggest starting out with something smaller, at least until you can get the hang of alloc/malloc, working with strings, structs, unions, arrays etc.  once you get a good hang of these ideas ( and it shouldn't take too long really)  then I would move on to a larger project.  just my opinion, good luck to you though!


Hofstadter's Law:
           It always takes longer than you expect, even when you take into account Hofstadter's Law.

Offline

#6 2010-05-11 20:13:57

lagagnon
Member
From: an Island in the Pacific...
Registered: 2009-12-10
Posts: 1,087
Website

Re: Project to learn C

As someone who has attempted to learn both C, Tcl/Tk and recently Perl and Python a little word of advice. There is only really one way to learn and that is to code, code and code some more. If you start and then drop it you will loose it, just as surely as if you stop speaking a second (non-mother tongue) human language. But most of all enjoy.


Philosophy is looking for a black cat in a dark room. Metaphysics is looking for a black cat in a dark room that isn't there. Religion is looking for a black cat in a dark room that isn't there and shouting "I found it!". Science is looking for a black cat in a dark room with a flashlight.

Offline

#7 2010-05-11 20:33:33

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: Project to learn C

lagagnon wrote:

If you start and then drop it you will loose it, just as surely as if you stop speaking a second (non-mother tongue) human language.

You may be interested to learn that it is very possible to forget your native language. Happens all the time, actually. Mostly to speakers of minority languages when there is a strong incentive/enforcement to speak some other language instead.

Offline

#8 2010-05-12 04:22:15

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: Project to learn C

One interesting idea could be to write a program to automatically go through http://www.hackthissite.org/ and "hack it" using the methods a normal person would use. IOW, just automate breaking in smile You will learn a lot about networking if you never touched networking before. Also, getting a c program to do this would be a great experience in learning c.

Offline

#9 2010-05-12 09:45:46

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: Project to learn C

Network programming is in general very nice in C. One thing I had great fun with was making an IRC bot. smile

Offline

#10 2010-05-12 18:18:38

cschep
Member
Registered: 2006-12-02
Posts: 124
Website

Re: Project to learn C

Consider writing a small piece, the piece that does the longest/hardest calculations in C, then call that from the Java code. Even just as a system call (there is probably a way to call it from the code..no idea..) You'll get results fast (which really helps with staying motivated) and you'll learn a really valuable skill, mixing high and low level languages is super valuable because writing C to do higher level stuff is horribly inefficient.

Good luck!

Offline

#11 2010-05-12 23:05:08

xamaco
Member
From: Corsica, France
Registered: 2010-04-05
Posts: 87

Re: Project to learn C

I have found the book "The C Programming Language" by Brian Kernighan and Dennis Ritchie

You cannot find a better book... That's the book I read (in the 80s) to learn C. Examples and exercices in the book revolve around rewriting *nix utilities. Some of the exercises are really challenging and are probably a good start to build small projects. Again it's a really good book : Very small and elegant, as C, when properly written, can be. But C evolved since the writing of the book and some examples and exercises won't compile... For examples at the begining of the book, you will get errors about function redifinitions.
To overcome the problem, the simplest is to give to gcc the following option : -std=c89
Example : gcc -std=c89 getline.c

Basically you will have to learn rather quickly how to use gcc.

Offline

#12 2010-05-12 23:43:28

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Project to learn C

If I'm not mistaken, gcc accepts all strictly conforming ANSI C programs without any special flags.  If geniuz has the second edition of K&R, published 1988, then all the examples are ANSI C and will compile without a -std flag.  Examples in the first (1978) edition conform to no standard because there wasn't one at the time of writing, and -std=c89 may or may not help.

Nevertheless, it's best to be in the habit of specifying a standard.  gcc by default accepts GNU C, which is quite a different animal from ANSI or ISO C.  -std=c89 is not a bad idea for K&R2 (I use -ansi instead which does the exact same thing), but you will also probably want to use -pedantic (which enables standards-mandated diagnostic messages), -W (which enables some warnings), and -Wall (which enables "all" warnings -- note that this does not imply -W).

You can type this entire thing out every time you compile, or just stick 'alias cc=gcc -W -Wall -ansi -pedantic' in your .bashrc, as I did when I was learning from K&R2.

Offline

#13 2010-05-13 00:07:27

xamaco
Member
From: Corsica, France
Registered: 2010-04-05
Posts: 87

Re: Project to learn C

If you take the 1st example given in section 1.9, which I would call getline.c :

#include <stdio.h>
#define MAXLINE 1000    /* maximum input line length */
int getline(char line[], int maxline);
void copy(char to[], char from[]);
/* print the longest input  line */
main()
{
    int len;             /* current line length */
    int max;             /* maximum length seen so far */
    char line[MAXLINE];     /* current input line */
    char longest[MAXLINE];  /* longest line saved here */
    max = 0;
    while ((len = getline(line, MAXLINE)) > 0)
        if (len > max) {
             max = len;
            copy(longest, line);
        }
    if (max > 0) /* there was a line */
        printf("%s", longest);
    return 0;
}
/* getline: read a line into s, return length    */
int getline(char s[],int lim)
{
    int c, i;
    for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[])
{
    int i;
    i = 0;
    while ((to[i] = from[i]) != '\0')
        ++i;
}

it doesn't compile with my gcc (4.5.0). If I just do a : gcc getline.c, I get the following error :

getline.c:3:5: erreur: conflicting types for 'getline'
/usr/include/stdio.h:651:20: note: previous declaration of 'getline' was here
getline.c:23:5: erreur: conflicting types for 'getline'
/usr/include/stdio.h:651:20: note: previous declaration of 'getline' was her

I compiles fine if I type : gcc -std=c89 getline.c

Offline

#14 2010-05-13 09:31:07

geniuz
Member
Registered: 2010-04-10
Posts: 127

Re: Project to learn C

The edition of K&R I have is indeed the 2nd edition, reindexed that is, so pretty recent. I've done a large part of chapter 1 already and every program compiled fine. Thanks for the tips on gcc though, its a compiler with quite a few options so I'm glad you've mentioned the ones that I might have to be concerned with.

What I have noticed however, and I don't know whether this is due to the ANSI standard or something, but from other sources about C I find some write every program in the following manner:

#include ....

int main(void) {
       ...
       ...
       return 0;
}

While in K&R they do it like:

#include ...

main() {
      ...
      ...
}

From what I learned from bash sofar, I know the first way makes a lot more sense since the UNIX operating system expects an exit status (at least bash does), so the 0 means the program ran without errors and anything non-zero means failure somewhere along the way. Although the second is more appealing due to its simplicity. I'd like some clarification about wheter the return statement is actually recommended or not.

Offline

#15 2010-05-13 11:13:48

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Project to learn C

"int main()" is the proper way to declare main.

Offline

#16 2010-05-13 11:28:21

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: Project to learn C

main() {}

Is the old way. Without a return type, it would default to an "int" return type. Do _not_ do this in current code.

 int main(void)

or

 int main(int argc, char *argv[])

is the proper way of declaring main.

Offline

#17 2010-05-13 11:59:10

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Project to learn C

@xamaco: yup, you're right.  My bad.  This is not a syntax issue, though, but a name conflict: GNU libc includes GNU getline in <stdio.h>; ANSI C doesn't.  I should have remembered that the default has a polluted namespace.

@falconindy:  The problem with int main() is that it is not a function prototype, so while it is perfectly valid ANSI C, the compiler can't (read: probably won't) do any error checking if you later call main with the wrong arguments.  int main(void) is a prototype and ANSI-sanctioned to boot.  (The other acceptable prototype is as Themaister says.)

@geniuz:  Functions without a return type default to int -- the two declarations int main(void) and main(void) are identical, but the first is preferred.  Always return a success or error status from main, but prefer

#include <stdlib.h>

int main(void)
{
    ...
    if (...)
        return EXIT_FAILURE;
    return EXIT_SUCCESS;
}

Offline

#18 2010-05-14 17:12:02

geniuz
Member
Registered: 2010-04-10
Posts: 127

Re: Project to learn C

Ahh, that clarifies it, so I was right in the first place...I wonder why they do it without any return statements in K&R then? Or maybe they will introduce it later, I've only read chapter 1 so far...still finishing bash wink.

Last edited by geniuz (2010-05-14 17:12:30)

Offline

#19 2010-05-15 15:49:22

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: Project to learn C

Trent wrote:

@falconindy:  The problem with int main() is that it is not a function prototype, so while it is perfectly valid ANSI C, the compiler can't (read: probably won't) do any error checking if you later call main with the wrong arguments.  int main(void) is a prototype and ANSI-sanctioned to boot.  (The other acceptable prototype is as Themaister says.)

Thanks for pointing that out; it was news to me!  I had thought that was simply a matter of style, but I just tried it out and you are entirely correct:

[corey@sariss ~]$ cat wrong.c 
#include <stdio.h>

void doit()
{
        printf("doit\n");
}

int main(int argc, char **argv)
{
        doit(1, 2, 3);
        return 0;
}
[corey@sariss ~]$ cc wrong.c 
[corey@sariss ~]$ cat right.c 
#include <stdio.h>

void doit(void)
{
        printf("doit\n");
}

int main(int argc, char **argv)
{
        doit(1, 2, 3);
        return 0;
}
[corey@sariss ~]$ cc right.c 
right.c: In function 'main':
right.c:10:2: error: too many arguments to function 'doit'
right.c:3:6: note: declared here
[corey@sariss ~]$

Offline

#20 2010-05-21 10:57:57

benharminson
Member
Registered: 2010-05-20
Posts: 4

Re: Project to learn C

I had read this book when I was initially learning C. For beginners it's good but if you want to learn an expert level C then I think you have to switch to other C language books. And have to make many programs om your own.

Offline

#21 2010-05-21 14:08:15

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Project to learn C

benharminson wrote:

I had read this book when I was initially learning C. For beginners it's good but if you want to learn an expert level C then I think you have to switch to other C language books. And have to make many programs om your own.

I am interested to know what more advanced books you suggest, because I think K&R2 pretty much covers the entire language, even with some pretty advanced stuff in the later half of the book (implementing malloc(), for example).  The only thing it lacks is experience, which you can't learn from books.

Offline

#22 2016-05-29 02:55:43

jethronsun7
Member
Registered: 2016-04-29
Posts: 44
Website

Re: Project to learn C

I am a CS student and I have been programming for few years. One thing I believe is that C is very related to Assembly, i.e. you can't say you know C well without knowing Assembly.

In this case, one good project to start is definitely operating systems. I recommend projects on osdev.org. Really good stuff and you will learn a lot from them.

Offline

#23 2016-05-29 08:30:15

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,424
Website

Re: Project to learn C

jethrosun7: you have been repeatedly warned about necrobumping https://wiki.archlinux.org/index.php/Fo … bumping.22



Closing


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

Board footer

Powered by FluxBB