Hello World! (C)

Hello World! in C

With my Python skills coming along well (in my opinion) and with my continued interest in learning at least a little in other programming languages, I have decided to take a look at C using my Raspberry Pi.

C arrived on the scene in 1972 (at the time of writing that was 47 years ago!) so it is older than me and probably the oldest programming language I’ve attempted. C files are written in plain text and are saved with the file extension .c or .h

C has several keywords / reserved words that are used by C for predefined functions.

I have created a brief “Hello World!” demo in C:

c_geektechstuff_helloworld
Hello World! in C

Unlike Python, C does not use tabs / spaces / whitespace to structure code. However it is reliant on brackets, apostrophes, quotation marks and semi-colons.

Looking at the code:

#include <stdio.h>

This is a bit like the import line in Python in that it brings in the stdio.h code as if it was all typed into the top of the program. stdio stands for Standard Input Output and it is where the printf function is defined. Unlike Python # is not used to write comments.

void main (void)

This line is like the def function() line in a Python program and a main void is needed in C programs as it tells the C program what to do. The lines of instructions that the void carries out are placed between an opening and closing curly brace {}.

Comments in C are placed between an opening /* mark and a closing */ mark, apologies on the screen shot as I’ve not placed a comment but that is where it would go.

printf is like the print command in Python, the f stands for formatted. Just like Python the string needs putting in brackets with quotation marks (“string here”) and as in Python the \n (new line) character can be used to insert a new line. The line needs to end with a semi-colon (;).

With the code written and saved (in my case as “gts.c”) it needs compiling. On the Raspberry Pi this can be done via the terminal. With terminal open, navigate to the folder containing the C file and type:

gcc -o program_name c_file_name

For me this was:

gcc -o geek gts.c

As I was compiling a C file called gts.c and wanted to compile it into a program called geek. As long as the compiler completes without error, the program should appear in the same location as the C file.

gcc is the GNU Compiler Collection, the -o switch is used to define the output.

The program can be run on the Raspberry Pi by typing into the terminal:

./program_name

In my case this was:

./geek

Want to know more?

C information on Wikipedia.

GNU Compiler Collection information on Wikipedia.

 

 

 

One thought on “Hello World! (C)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.