logo

 

     
 
Home
Site Map
Search
 
:: Bitwise Courses ::
 
Bitwise Dusty Archives
 
 
 

rss

 
 

ruby in steel

learn aikido in north devon

Learn Aikido in North Devon

 


Section :: C

- Format For Printing...

A Little Course in C. Part 1 – The Fundamentals

Getting started with C programming
Tuesday 12 November 2013.
 

C is one of the most important of all programming languages It was first developed by Dennis Ritchie in the late ‘60s and early ‘70s. The C language is widely used for all kinds of programming: everything from general-purpose applications, programming language tools and compilers – even operating systems and hardware devices. The language also forms the basis of both C++ and Objective-C.

The C language is fast and efficient – but it can be hard to learn. This course aims to simplify the learning process. It is based on my eBook, The Little Book Of C, which is provided with my multimedia online course, C Programming For Beginners. This ‘Little Course’ won’t be as in-depth as my eBook or my multimedia course but it will at least give you an understanding of the basics of C. Here I begin right at the very beginning with the classic ‘Hello world’ program.

In order to write C code you will need a programming editor or IDE (Integrated Development Environment) and a C compiler. For cross-platform development I generally use the CodeLite editor which is freely available for several operating systems: http://codelite.org/. Other good cross-platform IDEs include CodeBlocks (http://www.codeblocks.org/) and NetBeans (http://www.netbeans.org/).

A C program written and run using the CodeLite IDE

Hello World

This is the traditional “Hello World” program in C…

#include <stdio.h>

main() {
        printf("hello world\n");
}

This program includes code from the C-language ‘standard input/output library, stdio, using this statement:

#include <stdio.h>

The code that starts with the name main is the ‘main function’ – in other words, it is the first bit of code that runs when the program runs. The function name is followed by a pair of parentheses. The code to be run is enclosed between a pair of curly brackets:

main() {
       
}

In this case, the code calls the C printf function to print the string (the piece of text) between double-quotes. The “\n” at the end of the string causes a newline to be displayed:

printf("hello world\n");

Anatomy of a C Program

This shows the essential features of the simple ‘Hello world’ program…

puts and printf

There are several functions that can be used to display (print) information when your C programs run. Both printf and puts, can display a simple string.

printf("hello world\n");
puts("hello world again\n");

The printf function also allows you to embed ‘format specifiers’ into a string. A format specifier begins with a % and is followed by a letter: %s specifies a string. %d specifies a decimal or integer. When format specifiers occur in the string, the string must be followed a comma-delimited list of values. These values will replace the specifiers in the string. The programmer must take care that the values in the list exactly match the types and the number of the format specifiers in the string otherwise the program may crash. Here is an example:

printf("There are %d bottles standing on the %s.\n", 20, "wall\n" );

When run, the code produces the following output:

There are 20 bottles standing on the wall

Comments

It is a good idea to add comments to your programs to describe what each section is supposed to do. C lets you insert multi-line comments between pairs of /* and */ delimiters, like this:

/* This program displays any
* arguments that were passed to it */

In addition to these multi-line comments, modern C compilers also let you use ‘line comments’ that begin with two slash characters // and extend to the end of the current line. Line comments may either comment out an entire line or any part of a line which may include code before the // characters. These are examples of line comments:

// This is a full-line comment

for (i = 0; i < argc; i++)         // this comment follows some code
This course is based on my eBook, The Little Book Of C, which is provided with my multimedia online course, C Programming For Beginners.

Next lesson: Variables and types

AddThis Social Bookmark Button


Home