C Beginner

Programming, for all ages and all languages.
AGI1122

C Beginner

Post by AGI1122 »

Well I decided to take a leap and learn C. I have already made a simple test program:

Code: Select all

#define TEST_VAR "test"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>

int initialize(void) {
   printf("\nThis %s was a success.\n",TEST_VAR);
   return 0;
}

int main(int argc, char ** argv)  {
   if (initialize()) {
      printf("\nmain: FATAL: initialization failed\n");
      return -1;
   }
   printf("\n");
   return 0;
}
Which compiles and works just fine under linux in the terminal.

Anyway, can anyone point me to some good tutorials, docs, and other such things that I can use to learn C? I mostly will be dealing with creating prorams for use in linux, although if you could link me to some docs that show how to make your programs cross platform that would be awsome, specifically dos, windows, and linux are my target platforms so libraries that work with all 3 would be usefull.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:C Beginner

Post by Solar »

Nag, nag, nag... ;)
  • <string.h>, <stdlib.h> and <time.h> are not needed.
  • <unistd.h> and <sys/stat.h> aren't needed either. And those are UNIX, not C. While you're learning, learn to tell standard and UNIX / POSIX apart; that's helpful later on.
  • initialize() cannot return anything other than 0, even if printf() fails, since you do not check the return code.
  • the "correct" values to use in return-from-main or exit() are EXIT_SUCCESS and EXIT_FAILURE, but next to no-one uses them. ;)
On first glance this tutorial seems to get "into business" right away.

HTH.
Every good solution is obvious once you've found it.
AGI1122

Re:C Beginner

Post by AGI1122 »

<string.h>, <stdlib.h> and <time.h> are not needed.
<unistd.h> and <sys/stat.h> aren't needed either. And those are UNIX, not C. While you're learning, learn to tell standard and UNIX / POSIX apart; that's helpful later on.
Those libraries are going to be needed for what I have plannd to do next though, which is the only reason they where in that code.
initialize() cannot return anything other than 0, even if printf() fails, since you do not check the return code.
I already know this, I wasn't just creating a "hello world" test, initilize is going to need to be tested for success or failure in the proram I am creating.
the "correct" values to use in return-from-main or exit() are EXIT_SUCCESS and EXIT_FAILURE, but next to no-one uses them.
I have no idea what you mean... I am not using exit(). :-\

And thanks for the tutorial link.
AR

Re:C Beginner

Post by AR »

When main() returns it falls on to hidden initalization code (you don't really need to know what it does) which calls exit with the return value from main so essentially when you "return 0;" it is equivalent to "exit(0);", Solar is stating that there are constants called EXIT_SUCCESS and EXIT_FAILURE that should be used in exit() or as main()'s return value.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:C Beginner

Post by distantvoices »

but chris, after main returns to the runtime stub, execution continues at the exit() which is called after main - and this exit gets the return codes you are returning in main. That's why the convention requires main to be of int main(int argc,char **argv); f. ex.

This might look like this:

say crt0.o:

Code: Select all

  //prepare args - put them on their apropriate places on stack ... snipped here
  //call main:
  call main // return code in eax.
  push eax
  call exit //exit calls the vectors stored in the atexit array and then calls system to finish off the process.
Stay safe :-)
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Kon-Tiki

Re:C Beginner

Post by Kon-Tiki »

Bruce Eckel's Thinking In C++ might be a good way to learn, too.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C Beginner

Post by Candy »

Kon-Tiki wrote: Bruce Eckel's Thinking In C++ might be a good way to learn, too.
Learning C with a C++ book is very hard at the very least. Only badly-written c++ books that actually teach you C are fit for that, and they were I believe not published since the early 90's.
ark

Re:C Beginner

Post by ark »

Have to agree there, except that many C++ books are still badly-written. The sad state of affairs of C++ books (although possibly improving) seems to be books that don't really teach you C++ (no STL, in particular) but that aren't really all that useful for learning C, either, due to trying to move you to OOP and some of the more subtle differences between the two (along with the many things C++ has that C doesn't and the fact that C99 has things that C++ doesn't).
ark

Re:C Beginner

Post by ark »

To the original question -- some of it depends on what you want to do. Are you writing a command-line program or a program with a GUI. There are cross-platform GUI libraries out there, although I haven't used any of them so I don't have a recommendation on the best one.

The biggest portability issues are recognizing platform-specific libraries and differing sizes of primitive types such as int. For the latter, using typedefs instead of raw data types can help. Beyond that, you're probably getting into the subtle stuff like the malloc issue noted above.
ark

Re:C Beginner

Post by ark »

Wait, the malloc thing was in a different thread...never mind.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C Beginner

Post by Candy »

Joel wrote:
Joel, you know about editing your messages, right?
User avatar
df
Member
Member
Posts: 1076
Joined: Fri Oct 22, 2004 11:00 pm
Contact:

Re:C Beginner

Post by df »

congrats on learning C, i learnt C on a DEC-VAX. mmm.. old-time C...

anyway, carefull with the pointers. always compile on linux and use valgrind.

assert macros are your friend, and if you want, a DBC pre-processor is pretty funky (I use DBC in ruby)...
-- Stu --
AGI1122

Re:C Beginner

Post by AGI1122 »

Well I mostly want to learn some general stuff that will be usefull in all applications both gui and command line.

But I also plan to work on some AGI related stuff, interpreting, and editing. I have written AGI tools in php with a web interface... but this will be something completey new to me, writing an actual program that works on the os level to do stuff with agi. So I need gui for the agi editors. And I beleive command line for a agi interpreter?

I also want to try something original. I have been working a fully xml output for my message board system. What I want to do is have a program access that xml output and parse it. Then have it all in a gui to do the features of the board in.

Of course I will probably make other random tools and scripts as well for the fun of it.
always compile on linux
Yeah compiling on linux is so much easier and better than it was on windows. It was such a nuisance to get anything working. Sarien was the only program that I could ever get to compile in windows. Well other than my own little test programs. So far I have compiled nagi, agil, sarien, wine and a few other various things in linux. I may be new to linux... but I am already feeling at home and love the power of it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:C Beginner

Post by Candy »

I also want to try something original. I have been working a fully xml output for my message board system. What I want to do is have a program access that xml output and parse it. Then have it all in a gui to do the features of the board in.
I think you could use my XML parser. I'll be releasing a working copy of it in public domain. It's mostly C++ oriented though, although I doubt you could find a decent XML parser for C. You can adjust it for C and C structs quite easily, I'd be OK with doing that for you (in a weeks time that is, busy right now).
AGI1122

Re:C Beginner

Post by AGI1122 »

Does it handle CDATA and such or will I have to work on stripping that stuff out of the struct to get to the real content? If it does then that would be perfect. It would save me from having to write a parser from scratch.
Post Reply