Learning C as a Python Programmer (part 1)

Photo by Nubelson Fernandes on Unsplash

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.

  1. Any C program begins at the main() {} function.
  2. Any command has to end with a ; , so actually main() {};. However, after a }, one can omit the ;. Thus, in this case back to main() {} again.
  3. 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 prints Hello World! on the screen and has no return value. Thus: void main() {} .
  4. 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).
  5. The print() function of Python is in C printf() . It is more complicated than Python’s print(). 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;




Code tweaker + Human Geneticist (Ph.D.) | Join medium by: https://gwang-jin-kim.medium.com/membership

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How-to guide on deploying your nodejs to Fargate, AWS ECS/ECR via Terraform

Dropping Outliers

My experiences from creating a remote-site backup NAS For my dad (but mostly for me)

How to build a spinner (loader) component using Flowbite and Tailwind CSS

Learn Flutter Responsive UI by Building WhatsApp Clone

Brand-New Naughty Price Pool live at May 1st!

Ready, Set, GIT!

Training Custom Object Detector Step by Step

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Gwang Jin Kim

Gwang Jin Kim

Code tweaker + Human Geneticist (Ph.D.) | Join medium by: https://gwang-jin-kim.medium.com/membership

More from Medium

Using pointers in C

Memory ecosystem of python: Immutable and Mutabel Objects(a.k.a eveything is object)

My journey from the world of Java and Python to C++

Understanding Python for embedded systems developers