C Basics: Getting A Foot in the Door

c basics So you want to learn to program in C?  Whether you are new to coding or have some experience, learning C is a great idea.  It will show you the fundamentals of programming, and give you a sound foundation to continue learning.

It is also a powerful language that is widely used, much loved for its fast processing speed.  C is used for games, applications, operating systems, databases, and more.

Learn C “The Hard Way” with this 4-week crash course!

Now let’s get what we need to start programming with C.

Setting Up The C Environment

You will need two things – a text editor and a compiler, both free.  You can use practically anything as a text editor, such as Windows Notepad or Wordpad, or more complicated editors like EMACS.

The GNU C/C++ compiler is a good, free compiler that you can easily download and install.

Now you’re ready to open your text editor and start coding, so let’s look at the basic data types in C.

Understand the fundamentals of C programming with this online course.

Simple Data Types Used in C

Also known as primitives, these commands will be used quite often.  They are known as variables (like x in algebra) which will be able to take a value of their appropriate type.

  1. char – stands for a single character, but can also be a single-digit integer.
  2. int – short for integer, which is a whole number (not decimals or irrationals).
  3. bool – short for boolean, this holds either a true or false value.
  4. float – short for floating-point number. This is basically a decimal (2.3 or 4.0) or a fraction (2/3).
  5. double – this is much like float, but can hold twice as many values (don’t worry about this for now).

To use these commands, we will declare them in a program like so –

int i = 7;

This line of code will create an integer variable, named ‘i’, whose value will be 7.  Now, whenever the command ‘i’ is used, it will stand for 7.

Start learning C for FREE with this online course from Udemy!

And that’s it, except for a few variations like long and short int (for long and short integers) and long double.  Now let’s try writing the classic first program.

Your First Program in C – Hello, World!

The “Hello World” program is the typical first program that any beginner learns.  Open up your text editor, and type in the following code:

#include

void main()

{

printf(“Hello World, I can program in C!”);

}

What do these lines mean?  Ignore the brackets – { } – for a moment.

  1. The first line tells the computer to load and use a particular library of commands to use in this program.  In this case, we are loading , which is the standard input output header library.
  2. The second line is the main function, which marks the start of the main program to be executed.
  3. The third line of code uses the printf command, which tells the computer to print out whatever is inside the following parentheses.  In this case, the quotations marks (“ ”) indicate that a string of characters is to be printed.

The brackets are used to enclose lines of code that should be kept together.  For this simple program they only surround one line of code, but they will be used very often to create more complicated hierarchies of commands.

Now the coding is done, and we need to compile and execute it.

Learn C with an eye towards iPhone app development!

Save the text file as hello.c . Then, open up your command prompt (instructions here) and navigate to the directory where the file is saved.

Type gcc hello.c and press enter, which will create an executable file called a.out . To execute the file, simply type a.out and press enter.  Ta da!  You should see the results of your program printed out now.

And there you have it – your first program in C.  You can use the skills you’ve learned here to understand more complicated programs, and eventually create a program for any computer tasks you might need done.