3 C Fundamentals
3 C Fundamentals
Chapter 3
C Fundamentals
Narasimhan T.
3.1 Introduction
Communicating with a computer needs the knowledge of binary language that the computer
understands. Since we are not comfortable with binary language, we use the language
(high level languages like C, C++) we are comfortable with, and tools like compilers and
interpreters convert them into binary language.
Let us now get started with learning C. There is a close analogy between learning En-
glish language and learning C (or any other programming language). The classical method
of learning English is to first learn the alphabets used in the language, then learn to com-
bine these alphabets to form words, which in turn are combined to form sentences and
later, sentences are combined to form paragraphs. Finally, an article consists of multiple
paragraphs. This is shown in Figure 3.1.
Article
Alphabets Words Sentences Paragraph
(Essay)
Learning C is similar and easier as shown in Figure 3.2. We first learn about alphabets,
numbers and special symbols that are used in C, then learn how these are used to construct
constants, variables and keywords. These are then combined to form an expression or an
instruction. A group of instructions would be combined later on to form a function. A
program consists of multiple functions.
A computer program is nothing but an ordered collection of instructions or statements.
These instructions perform some useful “operations” on data which are represented as con-
stants or variables. This chapter is concerned with the basic elements used to construct
32
3.2. CHARACTER SET 33
Alphabets
Constants
Digits Expressions
Variables Functions Program
Special Instructions
Keywords
symbols
simple C statements. These elements include the C character set, constants, variables,
keywords, expressions, statements etc. We will see later how these basic elements can be
combined to form more comprehensive program components.
3.3.1 Constants
A ‘constant’ is an entity whose value doesn’t change. 3, 98.4 etc. are all constants. There
are four basic types of constants in C. They are:
1. Integer Constants – numbers without decimal point. Integer constants can be
written in three different number systems:
(a) decimal integer constant : can consist of any combination of digits from 0 through
9.
Eg: 7, 25, 0
(b) octal integer constant : can consist of any combination of digits from 0 through
7. However the first digit must be 0, in order to identify the constant as an octal
number.
Eg: 01, 076
34 CHAPTER 3. C FUNDAMENTALS
(c) hexadecimal integer constant : must begin with either 0x or 0X. It can then be
followed by any combination of digits from 0 through 9 and A through F (or a to
f). The letters A through F represent the numbers 10 through 15, respectively.
Eg: 0x27, 0XA
Programmers generally choose meaningful names for the various program elements.
Identifiers are names given to different entities such as constants, variables, structures,
functions, etc. They can contain both letters and numbers. You need to be careful of a few
rules when forming identifiers:
3. Both upper-case and lower-case letters are permitted, although lower case letters are
common.
5. Cannot be a keyword.
There are certain reserved words, called keywords, that have standard, predefined mean-
ings in C. These keywords are listed in Figure 3.3 and can be used only for their intended
purpose; they cannot be used as identifiers.
a + b
x = y
d
c = a + 7
78
Thus expressions combine variables and constants and provide an easy way to specify
the various operations on data values to produce new values.
36 CHAPTER 3. C FUNDAMENTALS
A statement causes the computer to carry out some action. There are three different
classes of statements in C. They are expression statements, compound statements and
control statements.
An expression statement consists of an expression followed by a semicolon. The execution
of an expression statement causes the expression to be evaluated. Following are some
examples:
a = 3;
c = a + b;
;
The first statement assigns the value 3 to the variable a. The second evaluates a+b and
assigns the result to c. The last expression statement does nothing, since it consists of
only a semicolon. It is simply a mechanism for providing an empty expression statement in
places where this type of statement is required. Consequently, it is called a null statement.
A compound statement consists of several individual statements enclosed within a pair
of braces { }. Unlike an expression statement, a compound statement does not end with
a semicolon. However each individual statement within the compound statement must end
with a semicolon. Following is an example for a compound statement.
{
a=2;
b=5;
c=a+b;
}
Control statements control the sequence of execution of various statements in a program. A
control statement is used to represent advanced features of a language like decision making
and looping.
Here a and b are variables of integer type, f is of floating-point type and finally c is a
character.
• short
• long
• signed
• unsigned
The memory requirements for the qualified data types are also platform dependent. For a
Linux C compiler, the size of various qualified data types is shown in Table 3.2.
• the memory requirements of a long int will be greater or equal to that of an ordinary
int.
• if short int and int both have the same memory requirements, then long int will
generally have double the requirements.
• if int and long int both have the same memory requirements, then short int will
generally have half the memory requirements.
• unsigned int and signed int have the same memory requirements as an ordinary
int.
• with int (ordinary, short or long), the leftmost bit is reserved for the sign. The sign
bit is 1 for negative numbers and 0 for positive numbers.
• With an unsigned int, all of the bits are used to represent the numerical value.
• The range of values for signed char vary from -128 to +127.
where name represents a symbolic constant and constant-value is the value associated with
the symbolic constant. Symbolic constants are usually written in upper-case, to distinguish
them from ordinary C variables. See the examples below:
3.7. COMMENTS 39
#define PI 3.141
#define E 2.718
#define FRIEND "xyz"
Here PI is a symbolic constant with the constant value 3.141 and thus all occurrences of
PI in the program will be replaced by the value 3.141.
Symbolic constants are usually defined at the beginning of the program. Note that the
definition does not end with a semicolon. If you unknowingly put a semicolon, it will be
taken as part of the constant value that is substituted for the symbolic constant. Symbolic
constants contribute to the development of readable programs. When a constant is used
at many places in a program, due to some reason if the value of that constant needs to be
changed, then the change is to be made at every statement where that constant occurs in
the program. If it were a symbolic constant, you just need to change at one place – the
place where it is defined.
3.7 Comments
As programs get bigger and more complicated, it becomes difficult to read and understand
them. For this reason, it is a good idea to add notes to your programs to explain (in
English) what the program is doing. These notes are called comments. A comment is
a piece of program text that the computer ignores but provides useful documentation to
programmers.
Comments can be single lined or can also span multiple lines. Single line comments are
included in the program by starting the comment text with // symbol. Multi-line com-
ments start with /* and end with */. Anything written after // symbol or between /* and
*/ symbols are ignored by compiler – it has no effect on the program.