How to write C programs?
This hub is part of a series I'm writing about C programming in Linux, please check out the hub "C programming in Linux" to see the full coverage.
In this hub, I'd like to take you through a whirlwind journey of writing C programs.
Now, buckle up!
Writing C programs can be fun, exciting and rewarding, however for some, it can also be daunting. I will not pretend that the C language is for everyone. I do encourage you, however, to give it a try and recognize that sometimes it can be hard and can require a bit of persistence in order to carry on.
Programming is a craft, and like any other crafts, do require practice, patience and sometimes persistence.
Alright, I hope I haven't discouraged you in pursuing C programming. Take it from me, it can be really really rewarding. If you haven't already, don't forget to check out my hub about the features of the language; please click this link: Introduction to C programming.
I know you are as excited as I am to get right down to the nitty-gritty details and get you writing C programs immediately. However, in this hub, I'd like to quickly run you through a preview of writing C programs. I'm not delaying your progress in learning C but I believe it would be to your benefit if I lay down a good foundation of C on which you can build on to make your journey as smooth as possible.
Basic elements of a C program
A C program is really just a collection of the so-called "statements"; like "sentences" in spoken language like English. Just like the spoken language, C statements are also governed by some rules in order for them to be valid.
These rules are referred to as "syntax" and "semantics" in conjunction with "constructs". This is like "grammar" in conjunction with "lexicon" of a language.
A C statement is a collection of constructs that accomplishes something. It's like complete and valid sentences or phrases that mean something. For instance, this is a meaningful English phrase: "Print the word 'Hello' on the screen." If I were to compare this to an equivalent C statement, I would write it like this:
In the C statement above, I've written it in the manner that complies with the syntax and semantics of grouping some constructs in order to instruct the computer to print the word "Hello" on the screen.
These are the constructs that I've used:
- The function called "printf".
- A character array, also known as string, "Hello".
- The statement separator symbol which is the semi-colon character ";".
Please note that I'll use the word "symbol" and "character" interchangeably to mean any of the symbols you can type on your keyboard, like A, B, 1, 2, *, &, [, }, etc.
Notice that the word Hello is enclosed by double quotes which are further enclosed by parenthesis characters. This statement would not have been valid if I wrote it in the following three different ways:
So what are these constructs then? Please continue reading down below.
C language constructs
There are so-called "language words" in C that are defined by the C language and can only be used according to how they're defined. These words are also called "reserved words" or also "keywords".
Since you can define your own "words" (known as "identifiers") when writing your own programs, it's important to know what these C keywords are since you can't redefine them and make them your own. Luckily, there aren't that many reserved words, they're listed down below:
auto break case char const continue
default do double else enum extern
floatfor goto if int long register
return short signed sizeof static struct
switch typedef union unsigned void volatile
I'd like to describe each of the keyword above but it would be a bit too much for this hub. Don't worry, we'll get to discuss each one in depth when appropriate. If you really want to dive into them right now, please click this external link: http://gd.tuwien.ac.at/languages/c/cref-mleslie/SYNTAX/keywords.html.
Words that you create or make up when writing your C program are called identifiers. You use them to name "variables", "constants" and "functions".
As mentioned earlier, you can't redefine keywords, therefore you can't use keywords as identifiers.
To create an identifier, you simply use a letter or use words. Like the ones below:
x temp Height var total FinalGrade
You can also use letter-number combination, also known as alpha-numeric, like this:
x1 temp4 Height2 var6 total3 Final3Grade
Notice in the preceding examples that you can't use a number to start an identifier. Also, you can't use any other characters like $, *, % or # in your identifiers except for the underscore character which is valid even to start the name of an identifier, like this:
_x1 temp_4 _Height2_ _var_6 total_3 Final_3_Grade
Please note that identifiers are case-sensitive. In other words, the following identifiers are different:
Temp temp TemP TEMP TEmp
You need to remember that C language as a whole is case-sensitive. Thus, all the keywords above must also be typed as such. You can't use "WHILE" instead of "while".
As mentioned above, identifiers are used to name "variables", "constants" and "functions".
Variables are data placeholders or storage. A variable is an identifier that corresponds to a memory location in your computer where data can be stored or accessed. Remember that computers use a circuitry called "memory" in order to store and read data.
Constants are data placeholders or storage that can only hold one value that never changes. In other words, you can only assign it a value at the time you declare it. More on this later, for now, just think of this like PI or Euler's number or the golden ratio which are constants; values that don't change.
Functions are used to identify a block ("group") of C statements. Once you group C statements into a function, you can simply use the function name in order to execute the statements in that group.
C uses non-alphabet symbols in conjunction with identifiers in order to create expressions; like mathematical expressions for instance.
As an example, C uses the following symbols for arithmetic:
+ Addition- Subtraction* Multiplication/ Division
So using operators in conjunction with identifiers, you can write code like this:
x = y + 100;z = x * 2 - 4;a = b * b + c * c;
C is rich in operators like the ones above that you can use in your programs to enable you to create more complex expressions to help you achieve what you're trying to accomplish in your programs.
I'll finish this hub with "data types". You will almost always deal with different types of data in your programs. Data like numbers, names, measurements, labels, etc. And these data will most likely be stored in variables or constants or manipulated in functions.
When creating identifiers, a process known as "declaration" in C, you will need to specify the data type.
For instance, if you want a variable to hold a person's age, you will declare a variable whose type is an Integer, like this: (note the use of the keyword "int")
The declaration above creates a variable called "age" that can hold Integer values. In C, integers are whole numbers like 1, 5, 6, and 10000. It can also hold negative numbers and even the number zero.
If you want a variable to store a person's name, you'll need to declare a character array variable. A person's name is really just an array of letters, thus a character array in C. See below for an example:
The code above creates a variable called "name" and can hold up to 20 characters.
Here's the list of all data types used in C:
I hope this hub would have given you a glimpse on how to write C programs. Unfortunately I can't cover all the details of everything I've mentioned above in one hub. So please make sure you check out my other hubs.
What do you think of the style of the C language? Many would describe it as "spartan". What do you think?