7

I am beginning to work with micro controllers and programming them using C language.

All my programming experience is with Python language.

I know that if I want to test a script I have written in python, I can simply launch terminal and type in “python” with the path of the file I want to run.

I tried a web search, but most didn’t seem to understand what I was asking.

How do I run c from terminal?

Iam Pyre
  • 205
  • 1
  • 2
  • 5
  • 2
    Related: [Why can't I run this C program?](https://unix.stackexchange.com/questions/45340/why-cant-i-run-this-c-program) – steeldriver Apr 22 '18 at 23:04
  • 5
    compile it first. then run the resulting executable. – ivanivan Apr 22 '18 at 23:11
  • 2
    @ivanivan Can an IDE like Eclipse do all this for me? – Iam Pyre Apr 22 '18 at 23:13
  • 4
    Yes, but overkill for a single .c file. – ivanivan Apr 22 '18 at 23:26
  • 3
    @ivanivan: Even overkill for moderate-sized projects. Just learn how to write a simple Makefile. – jamesqf Apr 23 '18 at 04:42
  • 1
    Over on SO: [Is there an interpreter for C?](https://stackoverflow.com/q/584714/2072269) – muru Apr 23 '18 at 06:15
  • If this is a debian/ubuntu box, install the build-essentials package - it is there for a different purpose, but it will install a sane set of C compiler and C compiler accessories. – rackandboneman Apr 23 '18 at 11:43
  • 1
    I presume that the OP uses an IDE, like PyCharm for his Python development (if not, he should). If so, he will fell at home with Eclipse. it will take care of makefiles for him, but more importantly it gives him access to the debugger, where he can set breakpoints, run is program to that part, then examine the call stack & values of variables, etc. Developing any other way is masochism – Mawg says reinstate Monica Apr 23 '18 at 12:09
  • @Mawg I use ILDE actually, and I edit code with NotepadQ. Wasn't liking that setup though, so I was going to watch a few video's on Youtube to give me an overview of PyCharm, and decide if I wanted to download. – Iam Pyre Apr 23 '18 at 18:10
  • 1
    @Mawg I agree with your points. I"m just a hacker/hobbyist enthusiast, I know a little about a lot, expert in nothing, heck, even certain command-line methods are too advanced for me. I need something like eclipse to keep the "Training Wheels" on and automate processes for me in an intuitive enviorment. Python is my first language, and there is still a lot for me to learn with that. I am picking up C a bit at a time as I work with Micro-Controllers. Though I'm working on earning a BSCS degree, all this is stuff I learn through my hobby. – Iam Pyre Apr 23 '18 at 18:18
  • Go for it!! It's not so difficult :-) and very rewarding. For Python, you simply cannot beat PyCharm. Do you use it? Do you know how to use the debugger? For C, Eclipse CDT is great, although some chipsets, like Atmel, have their own (generally Eclipse based) IDE. Not that you longer need to use C for embedded projects (as I have for decades); for instance, the Raspberry Pi series (gotta love the Pi Zero W) has more books/tutorials about programming it in Python than in C. There's nothing wrong with C, but it will hold you back, when compared with Python. Good luck! – Mawg says reinstate Monica Apr 24 '18 at 05:55

4 Answers4

30

C is not an interpreted language like Python or Perl. You cannot simply type C code and then tell the shell to execute the file. You need to compile the C file with a C compiler like gcc then execute the binary file it outputs.

For example, running gcc file.c will output a binary file with the name a.out. You can then tell the shell to execute the binary file by specifying the files full path ./a.out.

Edit:
As some comments and other answers have stated, there are some C interpreters that exist. However, I would argue that C compilers are more popular.

Peschke
  • 4,028
  • 2
  • 16
  • 30
10

If you have a single foo.c, see if make can compile it for you:

make foo

No makefiles or anything needed.

mike3996
  • 1,539
  • 1
  • 13
  • 17
9

tcc -run file.c will sometimes run a .c file. If the .c file needs additional libraries, tcc -llib1 -llib2 ... -run file.c will succeed. Sometimes it just won't work. tcc is not gcc.

Incidentally, you can put #!/usr/bin/tcc -run on the top of a .c file, chmod +x the file, and run it like any other script. When the .c file works in tcc, this behaves itself just fine.

Simple .c files from someone new to C will almost always work.

Joshua
  • 1,733
  • 12
  • 19
3

I am beginning to work with micro controllers and programming them using C language.

In practice, that is an important consideration. I'm guessing that your microcontroller is something like an Arduino (or perhaps a Raspberry Pi).

In general, you need some cross-compiler. You'll then cross-compile, on your desktop system (e.g. some Linux, which is very developer friendly; but you can find cross-compilers hosted on Windows or MacOSX, for Arduinos), your source code into an executable targetted for your microcontroller and later transmit the binary executable to your microcontroller.

C is a difficult programming language.

In many cases, you might compile directly your code on your desktop (don't forget to enable all warnings and debug info, e.g. gcc -Wall -Wextra -g with GCC), test most of it on your desktop, and later adapt it and port it for your Arduino. Debugging on your laptop or desktop some code is a lot easier than debugging on your Arduino.

You'll later cross-compile the same source code for your microcontroller.

Basile Starynkevitch
  • 10,411
  • 1
  • 32
  • 52