Unit - 01
Unit - 01
Unit - 01
INTRODUCTION OF C LANGUAGE
INTRODUCTION OF COMPUTER LANGUAGES
In all over the world, language is the source of communication among human beings.
In all over the world, we have different languages according to regions / countries.
To work with one another, we must need to know each other’s language then only we can
communicate properly.
Likewise, to work with the computer, user (a person who are using / operating the computer) has
to know the language understood by the computer.
For this purpose, different computer languages are developed to perform different types of work
on the computer.
According to their work, pros and cons, the computer languages are classified in the following three
levels:
1. Low level language
2. High level language
3. Middle level language
Page 1 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
Program / Software is required to help for growing business, academic or research work and other
factors.
With the help of program/software we can complete our task fast and with accuracy.
To solve any problem using program / software, we may require textual information – step by step
data (Known as ALGORITHM) as well as graphical view of information (Known as FLOWCHART).
The basic goal was to create a language for writing operating systems. C was a mechanism for
providing portable assembly language.
B was derived from BCPL, written in 1970 by Ken Thompson for the first UNIX system on the DEC
PDP-7.
C has its origin in the early 1970s in Basic Combined Programming Language (BCPL), a project
shared by AT&T Bell Labs, USA, and developed by Martin Richards.
Dennis Ritchie is credited with defining and creating C from the language B.
C is closely related to the UNIX operating system since it was developed to be used in it.
C language inherits the features of previous languages such as B and BCPL.
In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming
Language.
This book, known to C programmers as "K&R", served for many years as an informal specification of
the language.
The version of C that it describes is commonly referred to as K&R C.
C was finally standardized in 1989, and was named ANSI C.
Page 3 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
4 Features Very few features are exists. Very new features are added.
C PROGRAM STRUCTURE
C language program consists of following sections:
Documentation Section
Link Section
Definition Section
Global Declaration Section
main() function section
{
}
Subprogram Section (User Defined Function)
Function 1
Function 2
1. Document Section :
The documentation section consists of a set of comment lines giving program information.
2. Link Section :
The link section provides instruction to the compiler to link functions from the system library.
3. Definition Section :
The definition section defines all symbolic constants.
Page 4 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
5. Main() Section :
Every C program must have one main() function section.
6. Subprogram Section:
The subprogram section contains all the user defined functions that are called in the main function.
C CHARACTER SET
C language character set denotes any valid character in C programming language. This character can be
alphabet, digit, special symbols and white spaces. Following table shows the valid alphabets, numbers,
special symbols and white spaces allowed in C language.
Alphabet A, B, ……. , Z
a, b, …….., z
Digits 0, 1, 2, 3, ……, 9
Special Symbols , Comma
; Semi Colon
. dot (period)
/ slash
~ tilde
$ dollar sign
White spaces Blank Space
Horizontal Tab
Carriage Return
New Line
Form Feed
Page 5 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
Tokens
Tokens are Language Elements or Language Building Blocks.
Identifiers, operators, keywords, literals and special characters are example of tokens.
Tokens can be used to build high level constructs like expression, statements.
C Tokens
Identifiers and Variables
Keywords
Literals and Constants
Strings
Operators
Special Symbols
Keywords
Reserved identifiers used by the language.
Auto Break Case Char
Const continue Default Do
Double Else Enum extern
Float For Goto If
Int Long Register return
Short signed Sizeof static
Struct switch Typedef union
Unsigned Void Volatile while
Page 6 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
CONSTANTS
NUMERIC CHARACTER
CONSTANTS CONSTANTS
Integer Constants
An integer constant refers to a sequence of digits.
Decimal integers consist of a set of digits, 0 to 9, preceded by an optional – or + sign.
Valid examples of decimal integer constants are:
123, -321, 0, 456, +78.
Real Constants
Real Constants are numerical values with fractional part.
Valid examples of real constants are:
215.50, .98, .60, 456.89, +85.90, -0.80.
Single Character Constants
A single character constant contains a single character enclosed within a pair of single quote
marks.
Examples of character constants are:
‘5’, ‘A’, ‘X’, ‘x’.
String Constants
A String constant is a sequence of characters enclosed in double quotes.
The string may contain letters, numbers, special characters and blank space.
Examples are:
“Hello!”, “1987”, “Wel Come”, “?....!”, “ 5+3 “
String
As above specified, string is a sequence of characters enclosed in double quotes.
C language does not support String data type.
We can use String in C language with the help of an Array.
In double quotes we can have anything like alphanumeric, digits, symbols etc.
Page 7 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
Operators
Followings are different classifications for C language Operators:
Arithmetic Operators +, -, *, /. %
Relational Operators <, <=, >, >=, ==, !=
Logical Operators !, &&, ||
Ternary Operator (Conditional Operator) ?:
Bitwise Operators <<, >>
Assignment Operator =
Comparison Operator ==
Unary Operators +, -, ++, --
Arithmetic Operators
There are five arithmetic operators in C language.
Multiplication, Division and Modulus operator’s priority is higher than Addition and
Subtraction operator. So, if in any arithmetic statement these operators are available
then Multiplication, Division and Modulus Operation is performed first. After that
Addition and Subtraction is performed.
o Relational Operators
There are six relational operators in C language.
Operators Description Example Result
< Less Than n1 < n2 true if value of n1 is less than n2
true if value of n1 is less than or
<= Less Than equal to n1 <= n2
equal to n2
> Greater Than n1 > n2 true if value of n1 is greater than n2
true if value of n1 is greater than or
>= Greater Than equal to n1 >= n2
equal to n2
== Equality n1 == n2 true if n1 and n2 are equal
!= Inequality n1 != n2 true if n1 and n2 are not equal
Page 8 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
Relational operators are useful in relational conditions (i.e. decision making and looping
structures).
Relational operators are binary operators and require two operands.
Relational operator returns result of operation either true (1 or other than 0) or false.
o Logical Operators
There are three logical operators in C language.
Logical Operators are use to give multiple relations conditions.
if (x > y)
y = 3;
else
y =4;
Page 9 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
o Bitwise Operators
They work on bits and perform bit by bit operation.
When decrement operator works as a post decrement operator, the operand value is
decreased by one after any assignment.
Special Symbols
C language supports special symbols in the program. Some of them are as under:
# Before pre-processor directives
{ When program / block starts
} When program / block ends
; To terminate program statements
Hierarchy of Operators:
Operator Description Associativity Rank
() Function Call
L To R 1
[] Array Element reference
+ Unary Plus
- Unary Minus
++ Increment
-- Decrement
! Logical Negation
R To L 2
~ One’s Complement
* Pointer reference
Sizeof size of an object
& Address
(type) Type Conversion (case)
* Multiplication
/ Division L To R 3
% Modulus
+ Addition
L To R 4
- Substraction
<< Left Shift
L To R 5
>> Right Shift
< Less Than
<= Less Than Or Equal To
L To R 6
> Greater Than
>= Greater Than Or Equal To
== Equality
L To R 7
!= Inequality
& Bitwise AND L To R 8
^ Bitwise XOR L To R 9
| Bitwise OR L To R 10
&& Logical AND L To R 11
Page 11 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
|| Logical OR L To R 12
?: Conditional Expression R To L 13
= *= Assignment Operators
/= %=
+= -=
R To L 14
&= ^=
|= <<=
>>=
Signed Unsigned
TYPE CASTING
Any mathematical operation requires same data type of operands.
In mathematical expression, various data types of operands are used. In this situation expression
cannot be evaluated.
Type casting refers to changing a variable of one data type into another.
In C Language, there are two ways to change type of variable from one data type to another:
Automatic and Manual.
The Automatic process is known as Type Conversion which is done by compiler itself.
The Manual process is known as Type Casting which is done explicitly by the programmer.
If lower data type is converted to higher data type, this is known as type promotion.
If higher data type is converted to lower data type, this is known as type demotion.
int i, x;
float f;
double d;
long l;
Preprocessor Directives:
Preprocessor is one of the important features of C Language.
It makes program easy to read, easy to modify and more efficient.
As the name, it processes the source code before it passes through the compiler.
The preprocessor is the separate step in the compilation process.
The preprocessor directives operate under the control of preprocessor command line or
directives.
The preprocessor directives are placed before the main().
Every preprocessor begins with # and does not require semicolon at end.
Steps of execution...
Source Code => Preprocessor => Expanded Source Code =>
Compiler => Object Code => Linker => Executable Code
Page 13 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
The preprocessor converts the source code into the expanded source code, then it is
compiled by the compiler to convert it into the object code and the linker then converts this
into executable code.
The preprocessor is used with source code in 3 ways:
o File Inclusion:
Inserting the contents of another file into source file.
The directive #include "file name" is used to include any file in program.
The #include instructs to read entire file.
Generally used with header files.
Syntax:#include <filename> OR #include "filename"
The difference between the using of < > and " " is, If the file enclosed with <
>, then the search will be done in standard directories where the libraries are
stored. And in case of " ", search will be done in current directory or
directory containing the source file.
Example: #include<stdio.h> OR #include "stdio.h"
o Macro Substitution:
In macro substitution an identifier in a program is replaced with predefined
string.
Followings are different forms of macro substitution:
Simple Macro Substitution:
o In this #define is used.
o For Ex: #define pi 3.1415
Macros as Arguments:
o The preprocessors also have arguments as follow:
o Ex: #define area(r) (3.14 * r * r)
Nesting of Macros:
o We can also have one macro in the definition of another
macro. That means Nested Macro.
o Ex: #define SQUARE(x) (x*x) OR
#define CUBE(x) (SQUARE(x) * x)
Page 14 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
SYMBOLIC CONSTANT:
Symbolic constant is a name that substitutes for a sequence of characters or numeric constant, a
character constant or a string constant.
When a program is compiled each occurrence of a symbolic constant is replaced by its
corresponding character sequence.
Syntax:
#define symbolic_name value_of_constant
Where,
#define = Preprocessor Directive
symbolic_name = Name of Constant
value_of_constant = Value of Constant
Example: #define MAX 100
Here, the # character is used for preprocessor commands. A Preprocessor is a system program,
which comes into action prior to compiler, and it replaces the replacement text by the actual text.
Advantages:
o They can be used to assign names to values.
o Replacement of value has to be done at one place.
Rules:
o Symbolic names have same form as variable name.
o To distinguish them, we can use them in capital letters.
o # sign must be first character in statement
o No space is permitted between # and define
o It does not terminate with a semicolon sign.
o Symbolic names are not declared for data types.
o Once a value is assigned to symbolic constant, we cannot set new value for that variable.
o We can use #define statement anywhere in the program.
INTRODUCTION OF LOGIC:
Logic is a statements / instructions for solving any problem simply.
Any complex program / problem solution requires arithmetic – logical operations.
User can solve problem using simple / complex logic.
Page 15 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
Introduction of Algorithm
Algorithm can be defined as:
o It is a set of step-by-step instructions to execute the code.
o It is a part of computer program for solving a particular problem using a number of steps in
specific order.
o It is a step-by-step explanation of solution.
o It is a finite set of instructions to solve the problem.
To write an algorithm we must follow step-by-step method and for that different keywords are
used.
For Example: Begin / Start, End / Stop, Read / Input, Write / Output etc.
Features of Algorithm
o Specific Order
o Checking Conditions
o Repeat Statements
o Finite number of steps
Advantages of Algorithm
o A programmer can know identification of the processes, major decision statements and
required variables for solving the problem in the development time.
o It provides entire solution of problem in textual format.
o As step-by-step information is there in algorithm, then debugging is easy.
o Writing an algorithm is an easy as it is in regular English language like statements only.
Introduction of Flowchart
Flowchart can be defined as:
o It is a graphical or symbolic representation of a process of problem solution.
o Flowchart is pictorial representation of a problem with different shapes and in step by step
manner.
Symbols of Flowchart:
o Terminal Symbol: Used to represent start (begin) or stop (end) the process in flowchart.
o Parallelograms/ I/O Symbol: Used to get input or display output to the users. It is also
known as Data Shape.
Page 16 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
o Process: Used to show the process or arithmetic operation required for problem solution.
o Decision / Conditional / Diamond Shape: Used to show the process or arithmetic operation
required for problem solution.
o Connector: Used as a flow of process or jump from one point to another. Connectors are
labeled as A, B, C…. Mostly they are used to connect two or more pages.
o Arrow / Flow Line: Used to connect two symbols. It shows the direction / flow of process.
Advantages of Flowchart
o A graphical representation of any process is easy to understand than the textual
information. So, flowchart can be good interface for communication.
o The problem solution can be analyzed easily with the help of flowchart.
o As in this, our problem is represented with various symbols; it is required to debug the code
or to solve the error.
Disadvantages of Flowchart
o Drawing a flowchart becomes very difficult when we have complex logic (problem).
o The modification in flowchart is difficult.
o This is used only for those who know the meaning of flowchart symbols.
Comments
The C language supports two kinds of comments:
o Multi-line Comment : /* text */
o Single-line Comment : // text
The compiler ignores everything from // to the end of the line.
ASSIGNMENT – I
True / False:
1. Literals and constants are same.
2. Character constants can store one character.
3. String constants can store more than one character.
4. The main is a keyword in C language.
5. The keyword can be used as variable name.
6. Use of comments reduces the speed of execution of a program.
7. A printf statement can generate only one line of output.
8. Floating point constants, by default denote float data type.
9. The expression x >= y is same as x < y
10. The statement a = +10 is valid.
11. The expression a + 4 / 6 * 6 / 2 evaluates to 11.
State the output of following expressions. Assume the value a, b, and c is 10, 20 and 5 respectively.
Page 18 of 19
CS – 02 Problem Solving Methodologies And Programming In C Unit - 01
Page 19 of 19