Cprogramming Tutorial
Cprogramming Tutorial
Audience................................................................................................................................................. i
Prerequisites........................................................................................................................................... i
Table of Contents................................................................................................................................... ii
1.OVERVIEW................................................................................................1
C Programs............................................................................................................................................. 2
2.ENVIORNMENT SETUP...............................................................................3
Installation on UNIX/Linux...................................................................................................................... 4
Installation on Windows......................................................................................................................... 5
3.PROGRAM STRUCTURE.............................................................................6
4.BASIC SYNTAX..........................................................................................8
Tokens in C............................................................................................................................................. 8
Semicolons............................................................................................................................................. 8
Comments.............................................................................................................................................. 8
Identifiers............................................................................................................................................... 9
Keywords............................................................................................................................................... 9
Whitespace in C.................................................................................................................................... 10
5.DATA TYPES............................................................................................11
Integer Types........................................................................................................................................ 11
Floating-Point Types............................................................................................................................. 13
6.VARIABLES..............................................................................................15
Integer Literals..................................................................................................................................... 19
Floating-point Literals........................................................................................................................... 20
Character Constants............................................................................................................................. 20
String Literals....................................................................................................................................... 21
Defining Constants............................................................................................................................... 22
The #define Preprocessor......................................................................................................................22
The const Keyword................................................................................................................................23
8.STORAGE CLASSES.................................................................................24
9.OPERATORS............................................................................................28
Arithmetic Operators............................................................................................................................ 28
Relational Operators............................................................................................................................ 30
Logical Operators.................................................................................................................................. 32
Bitwise Operators................................................................................................................................. 34
Assignment Operators.......................................................................................................................... 37
10.....................................................................................DECISION MAKING
..............................................................................................................45
if Statement......................................................................................................................................... 46
if…else Statement...........................................................................................................................................48
Nested if Statements............................................................................................................................ 51
switch Statement.................................................................................................................................. 53
The ? : Operator................................................................................................................................... 57
11........................................................................................................LOOPS
..............................................................................................................58
while Loop............................................................................................................................................ 59
for Loop................................................................................................................................................ 61
do…while Loop................................................................................................................................................63
Nested Loops........................................................................................................................................ 65
break Statement................................................................................................................................... 68
continue Statement.............................................................................................................................. 70
goto Statement.................................................................................................................................... 72
12................................................................................................FUNCTIONS
..............................................................................................................76
Defining a Function............................................................................................................................... 76
Function Declarations........................................................................................................................... 77
Calling a Function................................................................................................................................. 78
Function Arguments............................................................................................................................. 79
Call by Value......................................................................................................................................... 80
Call by Reference.................................................................................................................................. 81
13.............................................................................................SCOPE RULES
..............................................................................................................84
Local Variables..................................................................................................................................... 84
Global Variables................................................................................................................................... 85
Formal Parameters............................................................................................................................... 86
14......................................................................................................ARRAYS
..............................................................................................................89
Declaring Arrays................................................................................................................................... 89
Initializing Arrays.................................................................................................................................. 89
Arrays in Detail..................................................................................................................................... 91
Multidimensional Arrays........................................................................................................................92
Two-dimensional Arrays........................................................................................................................92
Initializing Two-Dimensional Arrays.......................................................................................................93
Accessing Two-Dimensional Array Elements.........................................................................................93
Passing Arrays to Functions...................................................................................................................94
Return Array from a Function................................................................................................................96
Pointer to an Array................................................................................................................................99
15...................................................................................................POINTERS
............................................................................................................101
Decrementing a Pointer.......................................................................................................................106
Pointer Comparisons............................................................................................................................107
Array of Pointers..................................................................................................................................108
Pointer to Pointer................................................................................................................................110
Passing Pointers to Functions..............................................................................................................112
Return Pointer from Functions............................................................................................................114
16.....................................................................................................STRINGS
............................................................................................................117
17.............................................................................................STRUCTURES
............................................................................................................120
18......................................................................................................UNIONS
............................................................................................................128
19.................................................................................................BIT FIELDS
............................................................................................................132
20....................................................................................................TYPEDEF
............................................................................................................136
22......................................................................................................FILE I/O
............................................................................................................143
Opening Files...................................................................................................................................... 143
23.......................................................................................PREPROCESSORS
............................................................................................................147
24............................................................................................HEADER FILES
............................................................................................................153
26......................................................................................ERROR HANDLING
............................................................................................................160
27................................................................................................RECURSION
............................................................................................................164
28..............................................................................VARIABLE ARGUMENTS
............................................................................................................167
29.............................................................................MEMORY MANAGEMENT
............................................................................................................170
Facts about C
C was invented to write an operating system called UNIX.
C is a successor of B language which was introduced around the early 1970s.
The language was formalized in 1988 by the American National Standard Institute
(ANSI).
The UNIX OS was totally written in C.
Today C is the most widely used and popular System Programming Language.
Most of the state-of-the-art software have been implemented using C.
Today's most popular Linux OS and RDBMS MySQL have been written in C.
Why Use C?
C was initially used for system development work, particularly the programs that make-up the
operating system. C was adopted as a system development language because it produces
code that runs nearly as fast as the code written in assembly language. Some examples of
the use of C might be:
Operating Systems
Language Compilers
Assemblers
Text Editors
Print Spoolers
Network Drivers
Modern Programs
Databases
Language Interpreters
Utilities
C Programs
A C program can vary from 3 lines to millions of lines and it should be written into one or
more text files with extension ".c"; for example, hello.c. You can use "vi", "vim" or any
other text editor to write your C program into a file.
This tutorial assumes that you know how to edit a text file and how to write source code
inside a program file.
2.ENVIORNMENT
SETUP
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
For most of the examples given in this tutorial, you will find the Try it option in our website
code sections at the top right corner that will take you to the online compiler. So just make
use of it and enjoy your learning.
The C Compiler
The source code written in source file is the human readable source for your program. It
needs to be "compiled" into machine language so that your CPU can actually execute the
program as per the instructions given.
The compiler compiles the source codes into final executable programs. The most frequently
used and free available compiler is the GNU C/C++ compiler, otherwise you can have
compilers either from HP or Solaris if you have the respective operating systems.
The following section explains how to install GNU C/C++ compiler on various OS. m We keep
mentioning C/C++ together because GNU gcc compiler works for both C and C++
programming languages.
Installation on UNIX/Linux
If you are using Linux or UNIX, then check whether GCC is installed on your system by
entering the following command from the command line:
$ gcc -v
If you have GNU compiler installed on your machine, then it should print a message as follows:
If GCC is not installed, then you will have to install it yourself using the detailed instructions
available at http://gcc.gnu.org/install/.
This tutorial has been written based on Linux and all the given examples have been compiled
on the Cent OS flavor of the Linux system.
Installation on Mac OS
If you use Mac OS X, the easiest way to obtain GCC is to download the Xcode development
environment from Apple's web site and follow the simple installation instructions. Once you
have Xcode setup, you will be able to use GNU compiler for C/C++.
Xcode is currently available at developer.apple.com/technologies/tools/.
Installation on Windows
To install GCC on Windows, you need to install MinGW. To install MinGW, go to the MinGW
homepage, www.mingw.org, and follow the link to the MinGW download page. Download the
latest version of the MinGW installation program, which should be named MinGW-
<version>.exe.
While installing MinGW, at a minimum, you must install gcc-core, gcc-g++, binutils, and the
MinGW runtime, but you may wish to install more.
Add the bin subdirectory of your MinGW installation to your PATH environment variable, so
that you can specify these tools on the command line by their simple names.
After the installation is complete, you will be able to run gcc, g++, ar, ranlib, dlltool, and
several other GNU tools from the Windows command line.
3. PROGRAM
STRUCTURE
Before we study the basic building blocks of the C programming language, let us look at a
bare minimum C program structure so that we can take it as a reference in the upcoming
chapters.
#include <stdio.h>
int main()
{
/* my first program in C */
printf("Hello, World! \n");
return 0;
}