[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
C-Programming-Tutorial

C Programming tutorial

-----------------------------------------------

This is a beginners tutorial on C programming covering all basic features that are necessary to write good C programs. This tutorial is more concise than the Beginners Guide to C Programming at the C/C++ main page.

A note on language version: this tutorial covers C as it is implemented in most popular compilers like MSVC, GNU C, etc. So it may differ from the original ANSI-C standard. I will highlight diffeences to ANSI-C occassionally .

I will add Notes throughout the text to summarize and highlight elementary facts or rules to emphasize their importance. These notes are formatted as code with red text.

 This is a note  


What is C?

C is a High-Level programming language that is very popular on Unix/Linux environments. C was founded by Brian Kernighan and Dennis Ritchie back in the early 70s.

First steps! Formatted printing

Unlike other programming tutorials I will skip data types in the first place and start with formatted printing functions of C. When Brian Kernighan and Dennis Ritchie published their famous C bible, "The C Programming language", they introduced it with a program called Hello World, and since then, virtually all writers and publishers stuck to this tradition and opened their hors d'oeuvres with the Hello World program. So does this tutorial.

/* The Hello World program */
  1. include <stdio.h>
int main(int argc, char** argv) { printf("Hello World\n"); return 0; }


If you look close at the sample program above, you should notice the following elements, which are core elements of any C program.

The #include statement. The #include statement is used to have access to the C library functions, like printf() . printf() is one of the most common used functions in C and one of the most featureful functions.

The main() function. The main() function is what is says: the main entry point of the program. Any code that needs to be execetuded has to be placed here.

The printf() function. The printf() function is used to display formatted data on screen. In this case, it prints the string "Hello World\n" on the screen.

The return 0; statement. This statement simply says, that our main function returns zero on success and -1 if not.

The sample program also shows 2 other particularites of the C language. The first obvious is, that all functions are followed by () round braces and the body of a function is embraced by {} curly braces .

The second, not so obvious, is, that all staments in C are marked with a ; semi-colon . This goes for all statements except conditionals and functions.

 Attention: if you want to test this program at home, 
note that C is a case-sensitive language, that is, it distinguishes 
between  main() ,  Main()  or  MAIN() . 
Only the first version is considered correct:  main()!!.


The same goes for all variable or function declarations. See the paragraph on variables for more info .

Several compilers nourdays offer the option to distinguish between ANSI-C comformity or not, or to distinguish between upper an lowercase characters, however, you should always try to be coherent in your style of coding.

Variables and data types

After this short introduction into formatted printing and the core elements of a C program, i will continue with variables and data types.

C is a so called type safe programming language, that is, any variable needs to be assigned a supported data type. C supports the following data types:


 /* elementary data types */
bool
char
short
int
double
float
long
signed char, int, short, 
unsigned char, int, short,
  /* customized or non-elementary data types */ 
struct
enum 



This list covers almost all data types supported by C. However, i have excluded one, because it belongs neither to elementary nor customized data types: pointers .

Explaining variables: . Variables are used to store data or information. This data then occupies a certain amount of memory. Consider the following example:

int i;  /* declaration of a variable of type int */ 
i = 5;   /* assigning i the value 5 */ 


In the example above, I declare a variable of type int and, in the second statement, I assign the value 5 to it. This variable will then be copied into memory and stays there until i manipulate it with another operation.

Note: All variables must be declared with a supported data type and can be assigned with a value.[/b]


long x;  /* declaration of x as long */ 
x = 0;   /* assigning 0 to x */ 
char c; / * declaration of c as char */
c = 'a';   /* assigning 'a' to c */ 
 
double k;  /* declaration of k as double */ 
k = 3.145;  /* assigning 3.143 to k */
unsigned short j;  /* declaration of j as unsigned short */
j = -1;            /* assigning -1 to j */ 


In the examples above, I have declared 4 variables of different data types: long, char, double and unsigned short . All hold different values.

Range of daty types:

All data types supported by C have a strictly defined range of value. That is, the amount of bytes a variable of a certain data type can occupy. The following table gives an overview of the data types and their range of value:

  /* elementary data types */
char            1 byte
int             2 bytes 
short           2 bytes
long            4 bytes
double          4 bytes
float           4 bytes
  /* non-elementary or customized data types */ 
struct          not defined
enum            2 bytes /* considered as int */


Note that the range of data type int depends greatly on the way it is implemented by the processor. That is, 32 bit processors deal int different than 64 bit processors.



last edited (March 7, 2005) by leeos, Number of views: 26700, Current Rev: 28 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.