CS3251 Programming in C Department of IT
UNIT I BASICS OF C PROGRAMMING
Introduction to programming paradigms - Structure of C program - C programming: Data Types
– Storage classes - Constants – Enumeration Constants - Keywords – Operators: Precedence and
Associativity - Expressions - Input/Output statements, Assignment statements – Decision making
statements - Switch statement - Looping statements – Pre-processor directives - Compilation
process
1.1 Introduction to programming paradigms
The programming language „C‟ was developed in the early 1970s by Dennis Ritchie at Bell
Laboratories. Although C was initially developed for writing system software, today it has become
such a popular language that a variety of software programs are written using this language. The
greatest advantage of using C for programming is that it can be easily used on different types of
computers. Many other programming languages such as C++ and Java are also based on C which
means that you will be able to learn them easily in the future. Today, C is widely used with the
UNIX operating system.
Programming Languages
The purpose of using computers is to solve problems, which are difficult to solve manually. So the
computer user must concentrate on the development of good algorithms for solving problems.
After preparing the algorithms, the programmer or user has to concentrate the programming
language which is understandable by the computer.
Computer programming languages are developed with the primary objectives of facilitating a large
number of people to use computer without the need to know the details of internal structure of the
computer.
1.2 Structure of C program
A C program contains one or more functions, where a function is defined as a group of statements
that perform a well-defined task. Figure 1.1 shows the structure of a C program. The statements in
a function are written in a logical sequence to perform a specific task. The main() function is the
most important function and is a part of every C program. Rather, the execution of a C program
begins with this function.
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 1
,CS3251 Programming in C Department of IT
From the structure given in Fig. 1.1, we can conclude that a C program can have any number of
functions depending on the tasks that have to be performed, and each function can have any number
of statements arranged according to specific meaningful sequence. Note that programmers can
choose any name for functions. It is not mandatory to write Function1, Function2, etc., with an
exception that every program must contain one function that has its name as main().
Fig 1.1 Structure of C program
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 2
,CS3251 Programming in C Department of IT
1.3 C Programming : Data Types
Data type determines the set of values that a data item can take and the operations that can
beperformed on the item. C language provides four basic data types. Table 1.2 lists the data types,
their size, range, and usage for a C programmer.
Basic Data Types in C
Basic data types and their variants
1.4 Constants
A constant is an entity whose value can‟t be changed during the execution of a program.
Constants are classified as:
1. Literal constants
2. Qualified constants
3. Symbolic constants
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 3
, CS3251 Programming in C Department of IT
1.5.1 Literal constants
Literal constant or just literal denotes a fixed value, which may be an integer, floating point
number, character or a string.
Literal constants are of the following types.
1. Integer Literal constant
2. Floating point Literal constant
3. Character Literal constant
4. String Literal constant
Integer Literal constant
Integer Literal constants are integer values like -1, 2, 8 etc. The rules for writing integer literal
constant are:
• An Integer Literal constant must have at least one digit
• It should not have any decimal point It can be either positive or
negative.
• No special characters and blank spaces are allowed.
• A number without a sign is assumed as positive.
• Octal constants start with 0.
• Hexadecimal constant start with 0x or 0X
Floating point Literal constant
Floating point Literal constants are values like -23.1, 12.8, 4e8 etc.. It can be written in a fractional
form or in an exponential form.
The rules for writing Floating point Literal constants in a fractional form:
• A fractional floating point Literal constant must have at least one digit.
• It should have a decimal point.
• It can be either positive or negative. No special characters and blank spaces are
allowed.
The rules for writing Floating point Literal constants in an exponential form:
• A Floating point Literal constants in an exponential form has two parts: the mantissa part
and the exponent part. Both are separated by e or E.
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 4
UNIT I BASICS OF C PROGRAMMING
Introduction to programming paradigms - Structure of C program - C programming: Data Types
– Storage classes - Constants – Enumeration Constants - Keywords – Operators: Precedence and
Associativity - Expressions - Input/Output statements, Assignment statements – Decision making
statements - Switch statement - Looping statements – Pre-processor directives - Compilation
process
1.1 Introduction to programming paradigms
The programming language „C‟ was developed in the early 1970s by Dennis Ritchie at Bell
Laboratories. Although C was initially developed for writing system software, today it has become
such a popular language that a variety of software programs are written using this language. The
greatest advantage of using C for programming is that it can be easily used on different types of
computers. Many other programming languages such as C++ and Java are also based on C which
means that you will be able to learn them easily in the future. Today, C is widely used with the
UNIX operating system.
Programming Languages
The purpose of using computers is to solve problems, which are difficult to solve manually. So the
computer user must concentrate on the development of good algorithms for solving problems.
After preparing the algorithms, the programmer or user has to concentrate the programming
language which is understandable by the computer.
Computer programming languages are developed with the primary objectives of facilitating a large
number of people to use computer without the need to know the details of internal structure of the
computer.
1.2 Structure of C program
A C program contains one or more functions, where a function is defined as a group of statements
that perform a well-defined task. Figure 1.1 shows the structure of a C program. The statements in
a function are written in a logical sequence to perform a specific task. The main() function is the
most important function and is a part of every C program. Rather, the execution of a C program
begins with this function.
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 1
,CS3251 Programming in C Department of IT
From the structure given in Fig. 1.1, we can conclude that a C program can have any number of
functions depending on the tasks that have to be performed, and each function can have any number
of statements arranged according to specific meaningful sequence. Note that programmers can
choose any name for functions. It is not mandatory to write Function1, Function2, etc., with an
exception that every program must contain one function that has its name as main().
Fig 1.1 Structure of C program
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 2
,CS3251 Programming in C Department of IT
1.3 C Programming : Data Types
Data type determines the set of values that a data item can take and the operations that can
beperformed on the item. C language provides four basic data types. Table 1.2 lists the data types,
their size, range, and usage for a C programmer.
Basic Data Types in C
Basic data types and their variants
1.4 Constants
A constant is an entity whose value can‟t be changed during the execution of a program.
Constants are classified as:
1. Literal constants
2. Qualified constants
3. Symbolic constants
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 3
, CS3251 Programming in C Department of IT
1.5.1 Literal constants
Literal constant or just literal denotes a fixed value, which may be an integer, floating point
number, character or a string.
Literal constants are of the following types.
1. Integer Literal constant
2. Floating point Literal constant
3. Character Literal constant
4. String Literal constant
Integer Literal constant
Integer Literal constants are integer values like -1, 2, 8 etc. The rules for writing integer literal
constant are:
• An Integer Literal constant must have at least one digit
• It should not have any decimal point It can be either positive or
negative.
• No special characters and blank spaces are allowed.
• A number without a sign is assumed as positive.
• Octal constants start with 0.
• Hexadecimal constant start with 0x or 0X
Floating point Literal constant
Floating point Literal constants are values like -23.1, 12.8, 4e8 etc.. It can be written in a fractional
form or in an exponential form.
The rules for writing Floating point Literal constants in a fractional form:
• A fractional floating point Literal constant must have at least one digit.
• It should have a decimal point.
• It can be either positive or negative. No special characters and blank spaces are
allowed.
The rules for writing Floating point Literal constants in an exponential form:
• A Floating point Literal constants in an exponential form has two parts: the mantissa part
and the exponent part. Both are separated by e or E.
JEPPIAAR INSTITUTE OF TECHNOLOGY Page 4