GCC Linux C compiler and C language hello world programs
The Linux gcc command is a C language complier that is right there to work with if one wants to get into C programing in a Linux system. I took a moment to start playing around with it, and just have some fun with a few quick, basic, C language hello world programs.
1 - Linux gcc and C Hello World
In this section I will be going over a very basic Hello world C language source code file called hello.c. This source code file is just a very simple typically hello world program that will just print Hello World to the standard output of the console. I then also worked out a simple bash script that will use gcc to build the hello.c file into a binary called just simply hello. I can then run the binary in the bash shell and get the expected result.
When it comes to learning any language one has to start somewhere, and this type of program is just that.
1.1 - The hello.c file
Here is the source code that I cam up with by just searching for a simple C language hello world program. It makes use of just one library called stdio.h which provides the printf function along with many other useful basic tools when it comes to working with standard output.
1.2 - The build.sh script
I then made a simple bash script that when called should build the hello.c file into a binary called just hello. In Linux systems binaries often seem to have no file name extension like that.
1.3 - Building my first C Programe with gcc
So when I run my build.sh script gcc compiles by hello.c into a hello binary. When I call my hello binary i end up getting the expected result in my console.
Well that was fun actually, it may just be a hello world program but it was fun to build a very simple yet functioning C binary. I should take a moment to look into making at least a few more C examples though, maybe sticking to what there is to work with in the stdio.h lib. Maybe even touch base on a few other features of gcc in the process, if I get to it.
2 - Printing an init value, and an improved build.sh
I should take at least one or two more steps beyond just a simple hello world example with this, just printing hello world to the console alone is not a good stopping point for me at least. I may or may not get into writing a collection of posts on C programing, but without even going that far there is the question of what the next step is from hello world. For me it seems like that next step is how to go about printing a number rather than a string to the console.
2.1 - The print_int.c file
With C I am dealing with string typing, and with that said I can not just pass a number for an argument where a string is expected. Still doing so is not at all that much harder, in fact I can use the same printf method to do so by passing the init as a second argument and then using a pattern in the string that I pass as the first.
2.2 - Improved build.sh
For this example I made an improved build.sh script that will work not just with this source file, but any file that I want to build with gcc. With this script I am just making it so I can give one argument to the script that is the name of the C source file that I want to build. I am then also using the Linux cut command to just cut the file extension off the end of the file name to produce a name for the output file to create with Linux gcc.
2.3 - Looking good
So this example seems to work as it should for me also. The source file builds without any problems with gcc, and when I run the resulting binary I get the number printed out to the standard output.
So now I have worked out how to just print a string to the console, and now a number. The process is not so hard of course I can just stick to using just this one simple library that can eb used to just print things out to the standard output. There are a few more basic things that come to mind though that I should get to with this post, so lets look at some of the other methods in this basic c library.
3 - Fopen example
In the stdio there are also some basic functions for opening, writing to, and closing files. There is the fopen function, followed by fputs, and fclose to work with. These methods can be used to create a very basic hello world example that creates a file rather than spiting something out to the command line.
Once again I just need to use gcc to create a little binary, and then it does just what I thought it would.
4 - Conclusion
So this was fun to take a moment to play around with gcc for a little while. I was wondering to myself how hard could it be to just cerate a few very basic hello world style examples with the C language and build them with gcc? As I expected it was not so hard to do that at least when it comes to just some very simple examples.
The next step from here is to maybe start a collection of a few posts on creating a few simple programs written in C and compiled with gcc. I might get around to doing that at some point, if so I am sure I will end up editing, and expanding this post as needed. So far I have wrote just one more c code example for another post on the linux test command. In that post I made a simple c program that will check a given folder for markdown files and if one or more files are in there the program will end with a status of zero, else it will end with a non zero exit code status.