Chapter 4
Chapter 4
Chapter 4
Chapter 4
C-Fundamentals
Introduction
C Language was originally developed at Bell Labs by Ken Thompson and Dennis Ritchie in the
mid of 1970’s. It is one of the most popular procedural, general purpose, and high-level
programming language used in all operating systems of today. Although C was designed for
writing system software, it is also widely used for developing application software. C is one of
the most popular programming languages of all time and there are very few computer
architectures for which a C compiler does not exist.
C has greatly influenced many other popular programming languages, most notably C++,
which began as an extension to C. It supports the programmer with a rich set of built-in functions
and operators. C supports different types of statements like sequential, selection, looping etc.
Procedural programming concept is well supported in C, this helps in dividing the programs into
function modules or code blocks.
Basic Structure
A C-program has the following basic structure:
documentation Section
link Section
definition Section
global declaration Section
main()
{
}
Sub-program Section
function1()
{
}
function2()
{
}
…….
C-Fundamentals
Chapter 4 Page 2 Nawaraj Paudel
functionn()
{
}
The documentation section consists of a set of comment lines giving the name of the program,
the author and other details such as a short description of the purpose of the program etc. The
link section provides instructions to the compiler to link functions from the system library. The
definition section defines all the symbolic constants.
The variables can be declared inside the main function or before the main function. Declaring
the variables before the main function makes the variables accessible to all the functions in a C
language program, such variables are called Global Variables. Declaring the variables within
main function or other functions makes the usage of the variables confined to the function only
and not accessible outside. These variables are called Local Variables.
Every C program must have one main function. It contains different executable statements
between the opening and closing braces. The sub-program section contains all the user-defined
functions that are called in other and main function. User-defined functions are generally placed
immediately after the main function although they may appear in any order.
Example:
/*
Author: Nawaraj Paudel
Date: 24 June, 2011
Puppose: Find area and circumference of a circle
*/
#include<stdio.h>
#include<conio.h>
#define PI 3.1415
float r,a,c;
float area();
float circumference();
void main()
{
clrscr();
C-Fundamentals
Chapter 4 Page 3 Nawaraj Paudel
printf("Radius=");
scanf("%f",&r);
a=area();
c=circumference();
printf("Area=%f",a);
printf("\nCircumference=%f",c);
getch();
}
float area()
{
return PI*r*r;
}
float circumference()
{
return 2*PI*r;
}
C-Fundamentals
Chapter 4 Page 4 Nawaraj Paudel
{
clrscr();
int a,b,c;
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
c=a+b;
printf("Sum=%d",c);
getch();
}
Identifiers
Identifiers are the names given to various program elements such as variables, functions, arrays,
and other user defined items. Identifiers can be a combination of letters, digits and underscore, in
any order, except that the first character must not be a digit.
In an identifier, upper- and lowercase are treated differently. For example, the identifier
count is not equivalent to Count or COUNT. There is no restriction on the length of an
identifier. However, only the first 31 characters are generally significant. For example, if your C
compiler recognizes only the first 3 characters, the identifiers pay and payment are same. Some
valid and invalid identifiers are given below.
C-Fundamentals
Chapter 4 Page 5 Nawaraj Paudel
Keywords
There are certain reserved words, called keywords that have standard, predefined meanings in C.
these keywords can be used only for their intended purpose; they cannot be used as programmer-
defined identifiers. The ANSI (American National Standards Institute) C defines the following
32 keywords.
Data Types
C supports several different types of data, each of which may be represented differently within
the computer’s memory. A data type defines the amount of memory that will be used during
program execution, valid range of values it can represent, and the operations that may be
performed on it. C is strongly typed language so every variable in C must be declared of specific
type explicitly. Data types can be either predefined or derived.
The C language supports four basic data types, each of which are represented differently
within the computer memory. The basic types for a particular compiler are listed below:
C-Fundamentals
Chapter 4 Page 6 Nawaraj Paudel
The basic types can be augmented by the use of data type qualifiers short, long, signed, and
unsigned. The int type can be qualified by signed, short, long and unsigned. The char type
can be modified by unsigned and signed. You can also apply long to double.
Note: Memory requirements for each data type may vary from one C compiler to another.
C-Fundamentals
Chapter 4 Page 7 Nawaraj Paudel
2. Type Casting
Type casting represents a request by the programmer to do an explicit type conversion that is,
converting form higher type to lower type (narrowing conversion). Casting tells the compiler to
convert the data to the specified type even though it might lose data. In C programming, casts are
done via the () operator, with the name of the type to cast to inside. For example,
double val1 = 456.9876;
float val2 = (float)val1;
Note: Casting must be carefully done since it may result in loss of data
Variables
A variable is an identifier that is used to represent some specified type of information in a
program. It is a named location in memory. It is used to hold a value that can be modified by a
program. It has a type associated with it. Specifying a variable requires two things:
C-Fundamentals
Chapter 4 Page 8 Nawaraj Paudel
Variable Initialization
When a variable is declared, its initial value is undefined. Before using a variable, you should
always initialize it to a known value. To initialize a variable, the declaration must consist of a
data type, followed by a variable name, and equal sign (=) and a literal constant of the
appropriate type. For example,
char ch = ‘a’;
int first = 0;
float balance = 123.23;
Constants
Like a variable, a constant is a data storage location used by your program. Unlike a variable,
the value stored in a constant can’t be changed during program execution. C has two types of
constants: literal constants (numeric, character, and string constants) and symbolic constants.
A literal constant is a value that is typed directly into the source code wherever it is needed.
Literal constants are also only referred as constants.
Literal Constants
1. Numeric Constants
Integer and floating point constants represent numbers. They are often referred to collectively as
numeric-type constants. Integer constants are written without decimal point and floating point
C-Fundamentals
Chapter 4 Page 9 Nawaraj Paudel
constants are written with a decimal point and/or exponent. The following rules apply to all
numeric-type constants:
• Commas and blank spaces cannot be included within the constant.
• The constant can be preceded by a minus (-) sign if desired. The minus sign is an operator
that changes the sign of a positive constant.
• The value of a constant cannot exceed specified minimum and maximum bounds. For each
type of constant, these bounds will vary from one C compiler to another.
2. Character Constants
Character constant is a single character, enclosed in apostrophes. Character constants have
integer values that are determined by the computer’s particular character set. Most computers
make use of the ASCII character set. In ASCII, each individual character is numerically encoded
with its own unique 7-bit combination. For example,
Constant Value
‘A’ 65
‘3’ 51
‘y’ 121
‘?’ 63
‘’ 32
C-Fundamentals
Chapter 4 Page 10 Nawaraj Paudel
Escape Sequences
Certain nonprinting characters like newline, as well as backslash (\), apostrophe ('), and certain
other characters can be expressed in terms of escape sequences. Escape sequences begin with a
backward slash and are followed by one or more special characters. An escape sequence is
regarded as a single character and is therefore valid as a character constant. It is also called
backslash character constant. The following are the commonly used escape sequences.
Character Escape Sequence ASCII Value
Bell (alert) \a 007
Backspace \b 008
Horizontal tab \t 009
Vertical tab \v 011
Newline (linefeed) \n 010
Form feed \f 012
Carriage return \r 013
Quotation mark (") \" 034
Apostrophe (') \' 039
Question mark (?) \? 063
Backslash (\) \\ 092
Null \0 000
3. String Constants
String constants consist of any number of consecutive characters (including none), enclosed in
(double) quotation marks. A character constant (e.g., 'A') and the corresponding single-character
string constant ("A") are not equivalent. For example,
“green” “Kathmandu, Nepal” “Nawaraj\tPaudel” “$19.95”
Symbolic Constants
Constant that is represented by a name (symbol) in your program is called symbolic constant.
The value represented by this constant cannot be changed during program execution. Whenever
you need the constant’s value in your program, you use its name as you would use a variable
name. Thus, a symbolic constant allows a name to appear in place of a numeric constant, a
character constant or a string constant. When a program is compiled, each occurrence of a
C-Fundamentals
Chapter 4 Page 11 Nawaraj Paudel
symbolic constant is replaced by its corresponding character sequence. C has two methods for
defining a symbolic constant: the #define directive and the const keyword. For example,
#define PI 3.14159
...
...
area = PI * (radius)*(radius);
OR
const double PI = 3.14159;
...
...
area = PI * (radius)*(radius);
Writing Comments
Comments are sentences that are not treated as a part of the program. These are helpful in
identifying the program’s principal features or in explaining the underlying logic of various
program features. In C there are two ways of putting comments on the program codes.
1. Single line comments
It begins with // and end at the end of the line. For example,
// This program adds two whole numbers
2. Multi line comments
It is enclosed in /*………..*/. For example,
/* This program adds
two whole numbers */
C-Fundamentals