Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Started in C

Tags:

c

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.

like image 679
awesome_person Avatar asked Sep 13 '09 19:09

awesome_person


People also ask

Can I learn C as a beginner?

C is a powerful general-purpose programming language. It can be used to develop software like operating systems, databases, compilers, and so on. C programming is an excellent language to learn to program for beginners.

Is C language hard for beginners?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


3 Answers

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
like image 146
agsamek Avatar answered Sep 20 '22 20:09

agsamek


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.

like image 44
David Z Avatar answered Sep 22 '22 20:09

David Z


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

like image 31
Isak Savo Avatar answered Sep 22 '22 20:09

Isak Savo