Beruflich Dokumente
Kultur Dokumente
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly
available description of C, now known as the K&R standard.
The UNIX operating system, the C compiler, and essentially all UNIX
application programs have been written in C. C has now become a widely
used professional language for various reasons −
Easy to learn
Structured language
Facts about C
C was invented to write an operating system called UNIX.
The language was formalized in 1988 by the American National Standard Institute
(ANSI).
Today C is the most widely used and popular System Programming Language.
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.
Text Editor
This will be used to type your program. Examples of few a editors include
Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.
The name and version of text editors can vary on different operating systems.
For example, Notepad will be used on Windows, and vim or vi can be used
on windows as well as on Linux or UNIX.
The files you create with your editor are called the source files and they
contain the program source codes. The source files for C programs are
typically named with the extension ".c".
Before starting your programming, make sure you have one text editor in
place and you have enough experience to write a computer program, save it
in a file, compile it and finally execute it.
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. 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 −
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr .......
Thread model: posix
gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)
If GCC is not installed, then you will have to install it yourself using the
detailed instructions available at https://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++.
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 Min GW, at a minimum, you must install gcc-core, gcc-g++,
binutils, and the MinGW runtime, but you may wish to install more.
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.
Tokens in C
A C program consists of various tokens and a token is either a keyword, an
identifier, a constant, a string literal, or a symbol. For example, the following
C statement consists of five tokens −
printf
;
Semicolons
In a C program, the semicolon is a statement terminator. That is, each
individual statement must be ended with a semicolon. It indicates the end of
one logical entity.
return 0;
Comments
Comments are like helping text in your C program and they are ignored by
the compiler. They start with /* and terminate with the characters */ as
shown below −
/* my first program in C */
You cannot have comments within comments and they do not occur within a
string or character literals.
Identifiers
A C identifier is a name used to identify a variable, function, or any other
user-defined item. An identifier starts with a letter A to Z, a to z, or an
underscore '_' followed by zero or more letters, underscores, and digits (0 to
9).
Keywords
The following list shows the reserved words in C. These reserved words may
not be used as constants or variables or any other identifier names.
auto else long switch
double
Whitespace in C
A line containing only whitespace, possibly with a comment, is known as a
blank line, and a C compiler totally ignores it.
int age;
Data Types
Data types in c refer to an extensive system used for declaring variables or
functions of different types. The type of a variable determines how much
space it occupies in storage and how the bit pattern stored is interpreted.
1 Basic Types
They are arithmetic types and are further classified into: (a) integer types
and (b) floating-point types.
2 Enumerated types
They are again arithmetic types and they are used to define variables that
can only assign certain discrete integer values throughout the program.
4 Derived types
They include (a) Pointer types, (b) Array types, (c) Structure types, (d)
Union types and (e) Function types.
The array types and structure types are referred collectively as the aggregate
types. The type of a function specifies the type of the function's return value.
We will see the basic types in the following section, where as other types will
be covered in the upcoming chapters.
Integer Types
The following table provides the details of standard integer types with their
storage sizes and value ranges −
#include <stdio.h>
#include <limits.h>
int main() {
return 0;
When you compile and execute the above program, it produces the following
result on Linux −
Storage size for int : 4
Floating-Point Types
The following table provide the details of standard floating-point types with
storage sizes and value ranges and their precision −
#include <stdio.h>
#include <float.h>
int main() {
return 0;
When you compile and execute the above program, it produces the following
result on Linux −
Storage size for float : 4
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6
The void Type
The void type specifies that no value is available. It is used in three kinds of
situations −
There are various functions in C which do not return any value or you can
say they return void. A function with no return value has the return type
as void. For example, void exit (int status);
3 Pointers to void
A pointer of type void * represents the address of an object, but not its
type. For example, a memory allocation function void *malloc( size_t
size ); returns a pointer to void which can be casted to any data type.
Variable
A variable is nothing but a name given to a storage area that our programs
can manipulate. Each variable in C has a specific type, which determines the
size and layout of the variable's memory; the range of values that can be
stored within that memory; and the set of operations that can be applied to
the variable.
The name of a variable can be composed of letters, digits, and the underscore
character. It must begin with either a letter or an underscore. Upper and
lowercase letters are distinct because C is case-sensitive. Based on the basic
types explained in the previous chapter, there will be the following basic
variable types −
1 char
2 int
3 float
4 double
5 void
Variable Definition in C
A variable definition tells the compiler where and how much storage to create
for the variable. A variable definition specifies a data type and contains a list
of one or more variables of that type as follows −
type variable_list;
Here, type must be a valid C data type including char, w_char, int, float,
double, bool, or any user-defined object; and variable_list may consist of
one or more identifier names separated by commas. Some valid declarations
are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
The line int i, j, k; declares and defines the variables i, j, and k; which
instruct the compiler to create variables named i, j and k of type int.
For definition without an initializer: variables with static storage duration are
implicitly initialized with NULL (all bytes have the value 0); the initial value
of all other variables are undefined.
Variable Declaration in C
A variable declaration provides assurance to the compiler that there exists a
variable with the given type and name so that the compiler can proceed for
further compilation without requiring the complete detail about the variable.
A variable definition has its meaning at the time of compilation only, the
compiler needs actual variable definition at the time of linking the program.
A variable declaration is useful when you are using multiple files and you
define your variable in one of the files which will be available at the time of
linking of the program. You will use the keyword extern to declare a variable
at any place. Though you can declare a variable multiple times in your C
program, it can be defined only once in a file, a function, or a block of code.
Example
Try the following example, where variables have been declared at the top,
but they have been defined and initialized inside the main function −
#include <stdio.h>
// Variable declaration:
extern int a, b;
extern int c;
extern float f;
int main () {
/* variable definition: */
int a, b;
int c;
float f;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
f = 70.0/3.0;
return 0;
When the above code is compiled and executed, it produces the following
result −
value of c : 30
value of f : 23.333334
// function declaration
int func();
int main() {
// function call
int i = func();
}
// function definition
int func() {
return 0;
rvalue − The term rvalue refers to a data value that is stored at some address in
memory. An rvalue is an expression that cannot have a value assigned to it which
means an rvalue may appear on the right-hand side but not on the left-hand side
of an assignment.
Variables are lvalues and so they may appear on the left-hand side of an
assignment. Numeric literals are rvalues and so they may not be assigned
and cannot appear on the left-hand side. Take a look at the following valid
and invalid statements −
int g = 20; // valid statement
Constants
Constants refer to fixed values that the program may not alter during its
execution. These fixed values are also called literals.
Constants can be of any of the basic data types like an integer constant, a
floating constant, a character constant, or a string literal. There are
enumeration constants as well.
Constants are treated just like regular variables except that their values
cannot be modified after their definition.
Integer Literals
An integer literal can be a decimal, octal, or hexadecimal constant. A prefix
specifies the base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing
for decimal.
An integer literal can also have a suffix that is a combination of U and L, for
unsigned and long, respectively. The suffix can be uppercase or lowercase
and can be in any order.
Floating-point Literals
A floating-point literal has an integer part, a decimal point, a fractional part,
and an exponent part. You can represent floating point literals either in
decimal form or exponential form.
While representing decimal form, you must include the decimal point, the
exponent, or both; and while representing exponential form, you must
include the integer part, the fractional part, or both. The signed exponent is
introduced by e or E.
#include <stdio.h>
int main() {
printf("Hello\tWorld\n\n");
return 0;
When the above code is compiled and executed, it produces the following
result −
Hello World
String Literals
String literals or constants are enclosed in double quotes "". A string contains
characters that are similar to character literals: plain characters, escape
sequences, and universal characters.
You can break a long line into multiple lines using string literals and
separating them using white spaces.
Here are some examples of string literals. All the three forms are identical
strings.
"hello, dear"
"hello, \
dear"
Defining Constants
There are two simple ways in C to define constants −
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
int main() {
int area;
printf("%c", NEWLINE);
return 0;
When the above code is compiled and executed, it produces the following
result −
value of area : 50
#include <stdio.h>
int main() {
int area;
printf("%c", NEWLINE);
return 0;
}
When the above code is compiled and executed, it produces the following
result −
value of area : 50
A storage class defines the scope (visibility) and life-time of variables and/or
functions within a C Program. They precede the type that they modify. We
have four different storage classes in a C program −
auto
register
static
extern
int mount;
The example above defines two variables with in the same storage class.
'auto' can only be used within functions, i.e., local variables.
}
The register should only be used for variables that require quick access such
as counters. It should also be noted that defining 'register' does not mean
that the variable will be stored in a register. It means that it MIGHT be stored
in a register depending on hardware and implementation restrictions.
The static modifier may also be applied to global variables. When this is done,
it causes that variable's scope to be restricted to the file in which it is
declared.
#include <stdio.h>
/* function declaration */
void func(void);
main() {
while(count--) {
func();
}
return 0;
/* function definition */
i++;
When the above code is compiled and executed, it produces the following
result −
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
When you have multiple files and you define a global variable or function,
which will also be used in other files, then extern will be used in another file
to provide the reference of defined variable or function. Just for
understanding, extern is used to declare a global variable or function in
another file.
The extern modifier is most commonly used when there are two or more files
sharing the same global variables or functions as explained below.
First File: main.c
#include <stdio.h>
int count ;
main() {
count = 5;
write_extern();
#include <stdio.h>
void write_extern(void) {
Here, extern is being used to declare count in the second file, where as it has
its definition in the first file, main.c. Now, compile these two files as follows
−
$gcc main.c support.c
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
We will, in this chapter, look into the way each operator works.
Arithmetic Operators
The following table shows all the arithmetic operators supported by the C
language. Assume variable A holds 10 and variable B holds 20 then −
Show Examples
Relational Operators
The following table shows all the relational operators supported by C. Assume
variable A holds 10 and variable B holds 20 then −
Show Examples
> Checks if the value of left operand is greater than the (A > B)
value of right operand. If yes, then the condition becomes is not
true. true.
< Checks if the value of left operand is less than the value of (A < B)
right operand. If yes, then the condition becomes true. is true.
>= Checks if the value of left operand is greater than or equal (A >= B)
to the value of right operand. If yes, then the condition is not
becomes true. true.
<= Checks if the value of left operand is less than or equal to (A <= B)
the value of right operand. If yes, then the condition is true.
becomes true.
Logical Operators
Following table shows all the logical operators supported by C language.
Assume variable A holds 1 and variable B holds 0, then −
Show Examples
&& Called Logical AND operator. If both the operands are non- (A && B)
zero, then the condition becomes true. is false.
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth
tables for &, |, and ^ is as follows −
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
A = 0011 1100
B = 0000 1101
-----------------
~A = 1100 0011
Show Examples
& Binary AND Operator copies a bit to the result if it exists in (A & B)
both operands. = 12,
i.e.,
0000
1100
~ (~A ) =
Binary One's Complement Operator is unary and has the ~(60),
effect of 'flipping' bits. i.e,. -
0111101
<< Binary Left Shift Operator. The left operands value is A << 2
moved left by the number of bits specified by the right = 240
operand. i.e.,
1111
0000
>> Binary Right Shift Operator. The left operands value is A >> 2
moved right by the number of bits specified by the right = 15 i.e.,
operand. 0000
1111
Assignment Operators
The following table lists the assignment operators supported by the C
language −
Show Examples
Show Examples
Operators Precedence in C
Operator precedence determines the grouping of terms in an expression and
decides how an expression is evaluated. Certain operators have higher
precedence than others; for example, the multiplication operator has a higher
precedence than the addition operator.
Here, operators with the highest precedence appear at the top of the table,
those with the lowest appear at the bottom. Within an expression, higher
precedence operators will be evaluated first.
Show Examples
Decision making in C
Decision making structures require that the programmer specifies one or
more conditions to be evaluated or tested by the program, along with a
statement or statements to be executed if the condition is determined to be
true, and optionally, other statements to be executed if the condition is
determined to be false.
Show below is the general form of a typical decision making structure found
in most of the programming languages −
C programming language assumes any non-zero and non-null values
as true, and if it is either zero or null, then it is assumed as false value.
1 if statement
2 if...else statement
3 nested if statements
You can use one if or else if statement inside another if or else
ifstatement(s).
4 switch statement
The ? : Operator
We have covered conditional operator ? : in the previous chapter which
can be used to replace if...else statements. It has the following general form
−
Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement
of the colon.
If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the
expression.
######################################################
######################################################
1 while loop
2 for loop
3 do...while loop
It is more like a while statement, except that it tests the condition at the
end of the loop body.
4 nested loops
You can use one or more loops inside any other while, for, or do..while
loop.
1 break statement
2 continue statement
Causes the loop to skip the remainder of its body and immediately retest
its condition prior to reiterating.
3 goto statement
#include <stdio.h>
int main () {
for( ; ; ) {
}
return 0;
}