The C is so cool!
Need to build your own badass game? develop a browser? or create a brand new cryptocurrency? If so, then C’s here to help. C works at a much lower level than most other programming languages, so understanding C gives you a better idea of what’s going on. C can even help you better understand other languages as well. So dive in and grab your compile, and you’ll soon get started in no time.
C is a language for small, fast programs
The C language is designed to create small, fast programs. It’s lower-level than most other languages; that means it creates code that’s a lot closer to what machines really understand.
The way C works
Computers really only understand one language: machine code, a binary stream of 1s and 0s. You can convert your C code into machine code with the aid of a compiler.
Example 1
What do you think the above code does?
- create an integer variable called
age
and set its value to20
- create a conditional that checks if the value of the variable
age
is greater than18
- if line 2 is true, displays a text to the terminal or command prompt
Example 2
- create a variable called
score
and set its value to0
- create a while loop that checks that
score
is greater than1
3. displays a text on the terminal
4. set the value of score
to be score
minus 1
A while loop is used to repeat a series of actions/steps as long as a condition remains true. Every time the while loop goes through line 4, the value of
score
reduces by 1; the while loop finally stops whenscore
reduces to1
.
Example 3
- This is a comment, it’s meant for people reading the program
- Create an array of 11 character
- Displays a message including the text entered.
- Collect input from the user and store it in the array
- Display a message including the text you entered.
%s
is replaced withname
When creating an array of characters in C, you should leave additional space for one character just like we did on line 2. C uses this space internally.
But what does a complete C program look like?
Comments
The comment describes the purpose of the code in the file and might include some license or copyright information. A comment is also used to show what a line does or pass some information to other developers who are reading your program.
/*
* This is a comment
* It can span multiple
* lines
*/
The include section
C is a very, very small language and it can do almost nothing without the use of external libraries. You need to tell the compiler what external code to use by including header files for the relevant libraries.
/*
* The headers you'll see more than any other
* is the 'stdio.h'. It contains code that allows
* you to read and write data to and from the terminal
*/
#include <stdio.h>
#include <coollibrary.h>
The last thing you find in the source code are functions
All C code runs inside functions. The most important function you would find in any C program is called the main() function. The main() function is the starting point for all the code in your program.
int main()
{
/* write some code
* ...and some more
*
* return 0 to show that the program was successful
* return 1 to show that the program failed
*/
return (0)
}
Now we’ve looked at some basic concepts of C programs, we should write our first C program using all those concepts!
our first C program
But how do you run the program?
C is a compiled language, which means that the computer will not interpret the code directly. Instead, you will need to convert — or compile — the human-readable source code into machine-readable machine code.
To compile, you need a program called a compiler. One of the most popular C compilers is the GNU Compiler Collection or gcc for short. gcc is available on a lot of operating systems, and it can compile lots of languages other than C.
Here’s how you can compile and run the program above using gcc
- copy and save the program above to a file called
bigthings.c
- compile with
gcc bigthings.c -o bigthings
at a command line or terminal - run by typing
bigthings
on windows or./bigthings
on Mac and Linux machines.