9

I know there are many tutorials out there for getting started in C. However Its hard for me to apply the knowledge. The way I've always started out in languages is by writing scripts. Of course C is not a scripting language.

My question isn't so much about learning C as much as it is about how to get started applying C. Great I can write a temperature converter or a text-based rpg. Maybe its because in python I just write up the code in somefile.py and chmod +x somefile.py && somefile.py .

I do not really have an equivalent process for C. Every time I read about C its a different compiling process with different flags. Can someone just give me some definite direction on best ways to apply C when you already work with higher-level dynamic scripting languages?

Btw. I'm asking about C and not C++.

I usually am on either OpenSuse 11 or Ubuntu 9.04 . "What compiler do i use" is part of the problem. In python there is no choice its just "python somefile.py" same with php or ruby. I didn't know there were choices.

5
  • 2
    If your coding in unix environments, to compile all you need is: gcc -o myprogram myprogram.c Commented Sep 13, 2009 at 19:34
  • What operating system and C compiler do you want to use? We have to know that in order to give you a Best Practice suggestion. Commented Sep 13, 2009 at 19:37
  • For large projects the flags are specified in a Makefile so you don't have to repeat a dozen steps each time you want to compile. You simply run `make' and that's it. When your programs will grow large enough that they can be split in libraries, I would suggest you to learn a bit about Makefile. Btw, another popular build tool is SCons. Commented Sep 13, 2009 at 19:54
  • Wikipedia has an example Makefile good for a simple program - en.wikipedia.org/wiki/Makefile#Example_makefile. Commented Sep 13, 2009 at 19:56
  • As you are working on Linux systems, you should have the basic GNU toolset (gcc, gdb, make, etc.) already available; you shouldn't need to install anything. gcc.gnu.org should have everything you need to get started building C programs. Note that gcc provides some extensions (inline assembly, nested functions) that aren't universally supported; read the gcc documentation carefully. Commented Sep 14, 2009 at 18:51

6 Answers 6

6

write w.c

#include <stdio.h>

int main(int argc, char *argv[]) {
    int i;
    for (i = 0; i < argc; ++i) {
        printf("Param %d is '%s'\n", i, argv[i]);
    }
    return 0;
}

and compile with

gcc -Wall -o w w.c

run

./w
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for -Wall. I would also add -Wextra and -std=c99 to the mix.
Its fun to name souce file w.c
5

As rogeriopvl wrote in a comment, the compilation process is really simple. Just write up the code in somefile.c and

gcc -o somefile somefile.c && ./somefile

(if you're using GCC, and if not, your compiler of choice can probably be invoked similarly) Unless/until you start getting into more complicated projects, it's barely any more complicated than a scripting language. (Well... okay, you may need to link some libraries, once you get beyond the basics. But still, not a huge deal.)

In fact, I did write myself a little shell script that allows me to use C as a scripting language. But the process for setting it up is a little more complicated than what you may want to get into at this stage - it's simpler to just run the compiler each time. Still, if you're interested, I can look up the directions (for Linux) and put them here.

1 Comment

Better yet, if your program doesn't require any external libraries or special flags (and if you're just starting out there's a good chance it won't) you can just write make somefile at the commandline and make's implicit rules will call cc -o somefile somefile.c for you even though you don't have a Makefile.
2

C code needs to be compiled before the program can be run. The exact process is different depending on which platform and compiler you are working on.

For the most part, using an IDE (such as Visual studio, Eclipse, MonoDevelop, and a bunch of others) will do the nasty work for you so that you just have to press a button or click an icon. Download one of these

Comments

2

I asked myself this question when I was learning C. The problem here, if I can say this is a problem, is that C can be used in a broad range of applications and in a broad range of environments, which one with its own IDEs or compilers and libraries.

Some examples where you can use C for real staff.

  • Embedded software. In this case you will probably use some lib.

  • Network programming (take a look at this book).

  • Device driver development.

  • Libraries (both for Linux/Windows and other OSs)

Well this list is endless.

I don't know if I help you with this question. If you give more details about what are you interested in, could be helpful

Good luck

Comments

0

The best advice I can give here is find a topic you're interested in, see if you can make a program to do what you want/assist in doing what you want/adding functionality to the interest of choice, and start coding.

This gives the bonus of doing something you're interested in, and at the same time making something that directly influences it. It should give the motivation to keep steaming onward with the learning process.

Comments

0

I'm working with C a lot at the moment with Linux Kernel modules and am relatively new to C. I've found this rewarding which I think is what's important for this sort of hobby 'temperature converter or a text-based rpg' type programming.

I also struggle finding an application of programming skills. Balance of challenge and reward is important I think.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.