Learning C as a Python Programmer (part 1)
Learning C as someone coming from an interpreted (dynamical) language — first steps
Minimal C Program (Hello world!)
Other than Python, C programs have minimal requirements which have to be met to be eligible by the compiler to compile and run at all.
- Any C program begins at the
main() {}
function. - Any command has to end with a
;
, so actuallymain() {};
. However, after a}
, one can omit the;
. Thus, in this case back tomain() {}
again. - Any function has to indicate the returned value’s type of the function upfront. (If no return value, then
void
is the return type.) A ‘Hello World!’ program just printsHello World!
on the screen and has no return value. Thus:void main() {}
. - Printing on the screen is not something inbuilt to the language in C, but needs to import the package (or "header file”)
stdio.h
. Files ending with.h
are library files with C code in them.stdio.h
belongs to the standard libraries. So in contrast to Python, one has to know which standard libraries to include to simple do stuff which are in Python mostly already part of the language. To import the standard input output library, you have to#include
it. Since you include a library file, you put<>
around the filename:#include <stdio.h>
. — the compiler then searches for the header file in a predetermined directory path. If you do#include “stdio.h"
, it would first search in the current folder for the header file. Such commands beginning with#
are compiler directives and they are executed during compilation procedure and thus are not part of the run time code. Python, as an interpreted language doesn’t distinguish between compilation time and run time, since everything is run time in Python (interpreted). - The
print()
function of Python is in Cprintf()
. It is more complicated than Python’sprint()
. And it doesn’t add newline automatically. Therefore, you have to write\n
at of the string explicitly whenever you want it. Strings are declared like in Python. However,""
and''
do have different meanings in C. For escape characters like\n
being evaluated/executed, one has to use""
as string quotations. Strings declared with''
are taken literally (so any\n
will appear on the screen or in a string literally as\n).
Thus, we have:
#include <stdio.h>
void main() { printf("Hello World!\n"); }
We saved it into a file named minimal.c
indicating, that it is a C program by the ending .c
.
Then, to run it, we have to compile it — in Ubuntu Linux, this is:
$ gcc -o minimal minimal.c
Where we tell by -o minimal
to gcc
, the GNU compiler of C (already shipped in ubuntu right at the beginning), how the output file should be named — and that it should compile minimal.c
. Alternatively, you can give the full path to minimal.c
, especially if the terminal was opened in a different folder than where your minimal.c
file is. In that case -o minimal
should be modified to display the absolute path.
When successfully compiled, to run the program, you type into the shell either the absolute path to minimal
or the relative path, however beginning with ./
, thus in our case: $ ./minimal
.
To be able to call it directly $ minimal
, we have to add the path of this file or the path to the folder containing minimal
to the $PATH
variable, e.g. $ export PATH=$PATH:~/myc
. Now, we can fire $ minimal
, and it will print Hello World!
.
Congratulations, this was the first C program you compiled and run as a Python programmer!
The program however is not very C-ic (or how is the equivalent to ‘pythonic’ in C?).
C programs should return 0 when successful. So we should do:
#include <stdio.h>
int main() { printf("Hello World!\n"); return 0; }
Or arrange it like this:
#include <stdio.h>int main()
{
printf("Hello World!\n");
return 0;
}