Beginner’s ‘C’
Nice word play, huh?😏
Hello everyone!
It’s been a while. Apparently, my resolution to consistently write about/document my journey into tech has not been very successful. I’m an avid reader of Medium articles, and I feel bad that I’m not contributing to the community. So here we are.
I’m writing again though (obviously). So I’ll take this as a good sign (I hope). In the spirit of documentation, here is a mini-life update:
I’ve pivoted into the world of Software Engineering…and I’m loving it!
After the first 3 modules of the Google design course, I realized that I wasn’t as excited to learn any more. I think it’s because I haven’t always had an eye for design or colours. Not to say that its absolutely necessary to be a UX/UI designer, but for me, it just wasn’t jelling.
On the other hand, I signed up for a coding bootcamp, and it has me enthralled. I’ve always like maths and logic, so it was a no-brainer. Again, not to say that it’s necessary for coding, but you catch my drift.
I don’t intend to abandon design forever, however. I like a good challenge. So it’s just bye for now.
My end of this year (2022) goal can be summarised into 2 statements:
- Pop bottles (or 1 bottle lmao) at the club to celebrate financial blessings at the end of the year — This may or may not happen. I’m probably too financially sensible to follow through, but it’s good motivation.
- Break into a serious role in Software Development by the end of the year (this has been reframed to be end of next year due to my issues with consistency…but we’ll see 🤷🏾♀️).
Now, we can delve into the reason for this write-up; a beginner’s dive into the C programming language.
Back to C.
‘C’ programming language is one of the earliest languages and still holds relevance to this day. It is the foundation that C# and C++ were built upon. I have heard people say that the C# language is what many teams at Microsoft use, so you can imagine its relevance.
Many of my coding tutors also say that the C language is a very good fundamental language to know and build on for other programming languages. It also helps that it is a pretty straightforward language. I can’t help but notice it’s similarities to the ‘middle C’, which is the scale most music learners are first taught, when learning to play a musical instrument.
I’ll be sharing tools that I’m currently using or that have helped me at the end of the article, however, you’ll see the most important one before you get to the end of this article. I say it’s the most important one because the only way to understand is to practice, practice and practice! For this article, I’m only focusing on the fundamentals of writing C code. We will not touch compilation or how it works.
To write aprogram in C, you need a gcc compiler. A very simple online compiler can be used for this purpose. I’m currently using this Online Compiler for C by Programmiz. I find it super helpful to practice what I’m learning. I included a screenshot from the compiler in this article.
In this crash course, we will learn about the components of the C programming language, using a simple ‘Hello Word’ program in C.
Components of the Basic C program
Most programmers learn about a new language using a simple program that prints ‘Hello World’.
Here’s the code for an ‘Hello World’ program in C:
Now, let’s break down the code one by one.
The main.c part of the display is where you write your code, Output is where the result of your compiled code displays and the RUN button is what you click to ask the compiler to compile your code.
‘// Online C compiler…’ represents a comment. Because of the double slash at the beginning of the comment, the C compiler ignores the rest of the text on that line, and does not interpret it as code. Comments are important to give more information about a code and aid its readability.
‘/*……*/’ is another way of writing comments. It’s typically used for ‘multi-line’ comments or inputing comments on more than one line.
‘#include <stdio.h>’ is a command for the C compiler to use references from a standard C library called <stdio.h>[I say standard-I-O-(dot)-h]. It is called a preprocessor directive. stdio.h is an example of a header file. Header files have the ‘.h’ extension at the end of their names and contain definition of some C functions pre-saved in your compiler and can be used in your program. You use the ‘#include’ directive to access them. Other common ones are ‘string.h’ and ‘math.h’.
‘int main()’ tells the C compiler that the next part is where the main code is contained. ‘main()’ is a function that essentially signals to the C program that the code is coming next. Functions are basically ‘commands’ that do ‘something’. ‘int’ indicates that the function returns an ‘integer’.
‘{…..}’ usually contain a block of code. They are especially important in segmenting different blocks of code or functions. ‘{‘ indicates the start of the code for the main() function.
‘// Write C code here’ is another comment. The C code usually goes in that area. In this case, no extra code is needed.
‘printf()’ is a function. The ‘printf()’ is defined in the <stdio.h> library and asks the C compiler to display whatever is defined in the brackets as an output.
‘;’ is super important in C. It basically signals the end of a [code] statement in C. I think of it in my head as a full-stop in English language writing.
‘return 0’ terminates the function main (), and since it’s defined as 0, it returns 0-in this case is success.
‘}’ closes off the main() function.
And there you have it! Our first C program that prints ‘Hello World’ as an output.
/* If you have any questions, contributions or corrections, please add them as a comment. It’ll make me happy to read through.*/
More resources to learn C, that helped me are:
- Cprogramming.com
- tutorialspoint
- GeeksforGeeks
- Programmiz and their youtube page
- freeCodeCamp and their youtube page
Thank you and see you in the next one ❤️