0% found this document useful (0 votes)
10 views8 pages

3 C Fundamentals

The document discusses the basic elements of the C programming language including characters, constants, variables, keywords, expressions, and statements. It explains how these elements are used to construct simple C programs and provides examples of each concept.

Uploaded by

narasimhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

3 C Fundamentals

The document discusses the basic elements of the C programming language including characters, constants, variables, keywords, expressions, and statements. It explains how these elements are used to construct simple C programs and provides examples of each concept.

Uploaded by

narasimhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

EST102 : Programming in C

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)

Figure 3.1: Steps in learning English

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

Figure 3.2: Steps in learning C

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.2 Character set


The set of characters that are considered to be valid with respect to the syntax (rules) of
a programming language is called character set. A character denotes any alphabet, digit
or special symbol used to represent information. C supports the following characters:
• upper case alphabets (A–Z)
• lower case alphabets (a–z)
• digits (0–9)
• symbols like #,%, {, }, (, ), ? etc.

3.3 Constants, variables and keywords


The alphabets, numbers and special symbols when properly combined form constants, vari-
ables and keywords.

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

2. Floating-Point Constants – numbers with a decimal point. A floating-point num-


ber can be written using either ordinary decimal notation or scientific notation. Sci-
entific notation is often useful for representing very large or very small numbers. In
scientific notation, the letter E (or e) denotes the base 10. See the examples below:

Number Decimal notation Scientific notation


3.146 × 100 3.146 3.146e0
3.146 × 102 314.6 3.146E2
3.146 × 10−1 0.3146 3.146E-1
3.146 × 10−3 0.003146 3.146e-3

3. Character Constant – is a single character, enclosed in apostrophes (i.e., single


quotation marks).
Eg: 'a', 'G', ' '
Notice that the last constant above consists of a blank space, enclosed in apostrophes.
Character constants are internally represented as integer values that are determined
by the computer’s particular character set. Most computers make use of the ASCII
(American Standard Code for Information Interchange) character set with each char-
acter being assigned a value between 0 and 127. Some examples are given below:

Character constant ASCII value


'A' 65
'x' 120
'3' 51
'?' 63

Note that 3 is an integer constant, but '3' is a character constant.

4. String Constants – consists of any number (possibly zero) of consecutive characters,


enclosed in double quotation marks.
Eg: "program", "c", ""
The string "" is called a null or empty string.

3.3.2 Variables and keywords


The data on which programs operate are stored in various memory locations. To make the
retrieval and usage of the data values easy, these memory locations are given names. Since
the value stored in each location may change from time to time, the names given to these
locations are called ‘variable names’ or simply ‘variables’. Thus variable is a name that
refers to a value.
3.4. EXPRESSIONS AND STATEMENTS 35

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:

1. Must start with a letter or underscore (_).

2. Can be followed by any number of letters, digits, or underscores.

3. Both upper-case and lower-case letters are permitted, although lower case letters are
common.

4. Identifiers are case sensitive.

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.

auto double int struct


break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Figure 3.3: Keywords in C

3.4 Expressions and statements


An expression consists of a single entity such as a constant or a variable or a combination
of such entities, interconnected by one or more operators. Following are some examples:

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.

3.5 Data types and type qualifiers


The data stored in memory can be of different types. For example, your roll number is
stored as a number but your name as a string. Any variable has its associated data type.
The type of a variable determines the set of values it can have and what operations can be
performed on it. The basic data types in C and their memory requirements are listed in
Table 3.1. The memory requirements for a data type will determine the permissible range
of values for that data type. Note that the memory requirements for each data type is
dependent on the platform (hardware and software environments). The values shown in
Table 3.1 are for a typical Linux C compiler.
All variables must be declared before they can be used. A declaration consists of a data
type, followed by one or more variable names, ending with a semicolon. Following are some
examples.
int a,b;
float f;
char c;
3.5. DATA TYPES AND TYPE QUALIFIERS 37

Table 3.1: Data types in C and their memory requirements

Data type Description Memory requirements


char single character 1 byte
int an integer 4 bytes
floating-point number (i.e., a number
float containing a decimal point and/or an 4 bytes
exponent)
double-precision floating-point number
(i.e., more significant figures, and an
double 8 bytes
exponent which may be larger in mag-
nitude)

Table 3.2: Memory requirements for qualified data types

Qualified Data type Size


short int 2 bytes
long int 4 bytes
signed int 4 bytes
unsigned int 4 bytes
signed char 1 byte
unsigned char 1 byte
long double 12 bytes

Here a and b are variables of integer type, f is of floating-point type and finally c is a
character.

3.5.1 Data type qualifiers


Data type qualifiers are used to modify the memory requirements for the basic data types
as and when needed. The various qualifiers are:

• 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.

3.5.1.1 Applying qualifiers to int


• short int will require memory whose size is less than or equal to that of an ordinary
int.
38 CHAPTER 3. C FUNDAMENTALS

• 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.

3.5.1.2 Applying qualifiers to char


• signed and unsigned qualifiers can be applied.

• The range of values for signed char vary from -128 to +127.

• The range of values for unsigned char vary from 0 to 255.

3.5.1.3 Applying qualifiers to float and double


• no qualifier is applicable to float.

• only long qualifier is applicable to double.

3.6 Symbolic constants


Sometimes we want to have variables whose values should not be changed. In such cases, we
can define the values as symbolic constants instead of variables. A symbolic constant is a
name that substitutes for a constant. The constant may be a numeric constant, a character
constant or a string constant. Thus, a symbolic constant allows a name to appear in place
of a constant. When the program is compiled, each occurrence of a symbolic constant is
replaced by its corresponding value.
A symbolic constant is defined by writing

#define name constant-value

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.

An end-of-line comment might explain the purpose of a variable. Here is an example:


total = 5 + 7; // the variable total contains the sum of 5 and 7
You can also put comments on a separate line:
// The line below prints Hello
printf("Hello");
Here is an example of a multi line comment.
/* What you are reading now is a comment. It is included so that the
code is user friendly and is easy to understand. Happy programming!!
*/
printf("Now I understood the purpose of comments in a program.");

You might also like