0% found this document useful (0 votes)
375 views

PSPC Notes - Unit - 1

This document provides an overview of problem solving and programming using C. It discusses the basic components of a computer including input, output, memory, control unit, arithmetic logic unit. It also describes different types of computer languages like low-level languages (machine language, assembly language) and high-level languages (C, C++, Java, Python). Finally, it discusses compilers, interpreters and assemblers which are used to convert programs into machine executable code.

Uploaded by

Samba Siva
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)
375 views

PSPC Notes - Unit - 1

This document provides an overview of problem solving and programming using C. It discusses the basic components of a computer including input, output, memory, control unit, arithmetic logic unit. It also describes different types of computer languages like low-level languages (machine language, assembly language) and high-level languages (C, C++, Java, Python). Finally, it discusses compilers, interpreters and assemblers which are used to convert programs into machine executable code.

Uploaded by

Samba Siva
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/ 22

Problem Solving & Programming using C

VASIREDDY VENKATADRI INSTITUTE OF TECHNOLOGY


NBA, NAAC Accredited & Autonomous

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C

UNIT -1

Computer: A Computer is an electronic machine that can solve different problems,


process data, and store & retrieve data and perform calculations faster and efficiently
than humans. Or
Simply, a computer is a data processing unit.
A computer can i) accept data, ii) store data, iii) process data as desired, and iv)
retrieve the stored data as and when required and v) print the result in desired format.
The major characteristics of a computer are high speed, accuracy, diligence, versatility
and storage.

1.1 Computer Organization


The block diagram of computer is shown in Fig. 1.1.

Fig. 1.1 Block diagram of Computer Organization

The computer performs basically five major operations:

Input: this is the process of entering data and programs into the computer system.
Control Unit (CU): The process of input, output, processing and storage is performed
under the supervision of a unit called 'Control Unit'. It decides when to start receiving
data, when to stop it, where to store data, etc. It takes care of step -by-step
processing of all operations inside the computer.

Memory Unit: Computer is used to store data and instructions either temporarily or
permanently.
Arithmetic Logic Unit (ALU): The major operations performed by the ALU are
addition, subtraction, multiplication, division, logic and comparison.

Output: This is the process of producing results from the data for getting useful
information.
The ALU and the CU of a computer system are jointly known as the central
processing unit (CPU). You may call CPU as the brain of any computer system.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C

1.3 IO Devices

1.4 SOFTWARE:

As you are aware, computer cannot do anything on its own. It is the user who
instructs computer; what to do, how to do and when to do. In order to perform any
task, you have to give a set of instructions in a particular sequence to the computer.
These sets of instructions are called Programs. Software refers to a set of programs
that makes the hardware perform a particular set of tasks in particular order.
Software can be classified mainly into following categories and sub-categories are
shown in Figure:

System Software:

When you switch on the computer the programs stored in ROM are executed which
activates different units of your computer and makes it ready for you to work on it.
This set of programs can be called system software.
System software‟s are sets of programs, responsible for running the computer,
controlling various operations of computer systems and management of computer
resources. Operating System (OS) falls under this category.
An operating system is system software that provides an interface for a user to
communicate with the computer, manages hardware devices (disk drives, keyboard,
monitor, etc), manages and maintains disk file systems and supports application
programs. Some popular Operating systems are UNIX, Windows and Linux.
Although operating system provides all the features users need to use and
maintain their systems, inevitably, they still do not meet everyone‟s expectations.
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C
This has led to another type of system software called "Utilities". These are programs
that bridge the gap between the functionality of an OS and the needs of users. Utility
programs are a broad category of software such as compress (zip)/uncompress
(unzip) files software, antivirus software, split and join files software, etc.

Application Software

Application software is a set of programs, which are written to perform specific


tasks. It is also known as package or readymade program.
For example: An application package for managing library known as library
information system is used to manage information of library such as: keeping book
details, account holder details, book issue details, book return details etc. Application
software can be broadly classified into two types:

(a) Generalized packages


(b) Customized packages

1.5 COMPUTER LANGUAGES

Languages are a means of communication. Normally people interact with each other
through a language. On the same pattern, communication with computers is carried
out through a language. This language is understood both by user and the machine.
Just as every language like English, Hindi has its grammatical rules; every computer
language is bound by rules known as SYNTAX of that language. The user is bound by
that syntax while communicating with the computer system.

Computer languages are broadly classified as:

1. Low Level Language: The term low level means closeness to the way in which
machine understand. The low level languages are:

a. Machine Language: This is the language (in the form of 0‟s and 1‟s, called
binary numbers) understood directly by the computer. It is machine
dependent. It is difficult to learn and even more difficult to write programs.
b. Assembly Language: This is the language where the machine codes
comprising of 0‟s and 1‟s are substituted by symbolic codes (called mnemonics)
to improve their understanding. It is the first step to improve programming
structure.
Assembly language programming is simpler and less time consuming than
machine level programming, it is easier to locate and correct errors in assembly
language than in machine language programs. It is also machine dependent.
Programmers must have knowledge of the machine on which the program will
run.

2. High Level Language:


You know that low level language requires extensive knowledge of the hardware since
it is machine dependent. To overcome the limitation, high level language has been
evolved which uses normal English like, easy to understand statements to solve any

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
problem. Higher level languages are computer independent and programming
becomes quite easy and simple.

Various high level languages are given below:

BASIC (Beginners All Purpose Symbolic Instruction Code): It is widely used, easy to
learn general purpose language. BASIC is mainly used in microcomputers in earlier
days.

COBOL (Common Business Oriented language): A standardized language used for


commercial applications.

FORTRAN (Formula Translation): Developed for solving mathematical and scientific


problems. FORTRAN is the most popular languages among scientific community.

C: Structured Programming Language used for all purpose such as scientific


application, commercial application, developing games etc.

C++: Popular object oriented programming language, used for general purpose.

JAVA: It is a platform independent language. It is also object oriented programming


language. It is used for web based & enterprise level applications.

Python: It is an interpreted language mostly used for Data science, machine learning
applications.

1.6 COMPILER, INTERPRETER AND ASSEMBLER:

As you know that High Level language is machine independent and assembly
language though it is machine dependent yet mnemonics that are being used to
represent instructions are not directly understandable by machine. Hence to make
the machine understand the instructions provided by both the languages, Compiler
and Assembler are required to convert these instructions into machine language.
Definition of a Compiler: a translator (set of programs) that converts a program
written in high level language into an equivalent program in machine language is
called as Compiler. A compiler converts all instructions at a time.
An interpreter is also like a compiler. It also converts a high level language into
machine language. The difference is: a compiler converts all instructions at time
where as an interpreter performs line by line translation.
The program written by the programmer in high level language is called source
program and the program generated by the compiler after translation is called as
object program.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C

A translator (or software which is a set of programs) that reads a program written in
assembly language and translates it into an equivalent program in machine language
is called as Assembler.

Memory: Memory is a device where user data can be stored either temporarily or
permanently for further reference. It is a collection of bytes addressed in a sequence.
Data can be stored in form of bits (0/1). The word BIT is extracted from Binary Digit.

 a "bit", like an atom, the smallest unit of storage


 A bit stores just a 0 or 1
 "In the computer it's all 0's and 1's" ... bits
 In a chip: electric charge = 0/1
 In a hard drive: spots of North/South magnetism = 0/1

The memory can be measured in:

o 1 Bit = Binary Digit


o 8 Bits = 1 Byte
o 1024 Bytes = 1 KB (Kilo Byte)
o 1024 KB = 1 MB (Mega Byte)
o 1024 MB = 1 GB(Giga Byte)
o 1024 GB = 1 TB(Terra Byte)
o 1024 TB = 1 PB(Peta Byte)
o 1024 PB = 1 EB(Exa Byte)
o 1024 EB = 1 ZB(Zetta Byte)
o 1024 ZB = 1 YB (Yotta Byte)
o 1024 YB = 1 (Bronto Byte)
o 1024 Brontobyte = 1 (Geop Byte)

Types of memories: There are several types of memories in Computers.

1. Primary Memory
a. RAM (Random Access Memory)
1. Static RAM
2. Dynamic RAM
b. ROM(Read Only memory)
1. PROM (Programmable ROM)
2. EPROM(Erasable PROM)

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
3. EEPROM ( Electrically Erasable PROM)
2. Secondary Memory
1. Magnetic Tapes
2. Magnetic Disks
a. Floppy Disks
b. Hard disks (HDD)
3. Optical Disks
a. CD (Compact Disks)
b. VCD(Video CD)
c. DVD (Digital Versatile Disk)
4. Flash Memories
a. SD Cards
b. Pen Drives
3. Cache memory

Primary Memory (Main Memory)


Primary memory holds only those data and instructions on which
the computer is currently working. It has a limited capacity and
data is lost when power is switched off. It is generally made up of
semiconductor device. These memories are not as fast as registers.
The data and instruction required to be processed resides in the
main memory. It is divided into two subcategories RAM and ROM.

Characteristics of Main Memory

 These are semiconductor memories.


 It is known as the main memory.
 Usually volatile memory.
 Data is lost in case power is switched off.
 It is the working memory of the computer.
 Faster than secondary memories.
 A computer cannot run without the primary memory.

Secondary Memory: This type of memory is also known as external memory or non-volatile.
It is slower than the main memory. These are used for storing data/information permanently.
CPU directly does not access these memories; instead they are accessed via input-output
routines. The contents of secondary memories are first transferred to the main memory, and
then the CPU can access it. For example, disk, CD-ROM, DVD, etc.

Cache Memory: Cache memory is a very high speed semiconductor memory which
can speed up the CPU. It acts as a buffer between the CPU and the main memory. It
is used to hold those parts of data and program which are most frequently used by
the CPU. The parts of data and programs are transferred from the disk to cache
memory by the operating system, from where the CPU can access them.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
1.7 Software Development Process (Program development steps):

The software Development Lifecycle is a process of building good software and its
lifecycle stages provides Quality and correctness of good software. All the stages of
Lifecycle are important in itself. One wrong step in Lifecycle can create a big mistake
in the development of software. Being an owner or stakeholder of a software company
you must know the development lifecycle of the software.
There are different types of Software development Life Cycle models. They are
waterfall, Agile, Spiral etc. This entire SDLC model must follow these 6 steps for
developing errorless software. Software Development Life cycle has totaled 6 steps.

1. Requirement gathering and Analysis ( Identifying


& Understanding a problem)
2. Analysis
3. Design
4. Implement (or Coding)
5. Testing
6. Maintenance

Stage 1: Planning and Requirement Analysis

Requirement analysis is the most important and


fundamental stage in SDLC. It is performed by the
senior members of the team with inputs from the customer, the sales department,
market surveys and domain experts in the industry. This information is then used to
plan the basic project approach and to conduct product feasibility study in the
economical, operational and technical areas.
Planning for the quality assurance requirements and identification of the risks
associated with the project is also done in the planning stage. The outcome of the
technical feasibility study is to define the various technical approaches that can be
followed to implement the project successfully with minimum risks.
Stage 2: Defining Requirements
Once the requirement analysis is done the next step is to clearly define and
document the product requirements and get them approved from the customer or
the market analysts. This is done through an SRS (Software Requirement
Specification) document which consists of all the product requirements to be
designed and developed during the project life cycle.
Stage 3: Designing the Product Architecture
SRS is the reference for product architects to come out with the best architecture for
the product to be developed. Based on the requirements specified in SRS, usually
more than one design approach for the product architecture is proposed and
documented in a DDS - Design Document Specification.
This DDS is reviewed by all the important stakeholders and based on various
parameters as risk assessment, product robustness, design modularity, budget and
time constraints, the best design approach is selected for the product.
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C

A design approach clearly defines all the architectural modules of the product along
with its communication and data flow representation with the external and third
party modules (if any). The internal design of all the modules of the proposed
architecture should be clearly defined with the minutest of the details in DDS.
Stage 4: Building or Developing the Product
In this stage of SDLC the actual development starts and the product is built. The
programming code is generated as per DDS during this stage. If the design is
performed in a detailed and organized manner, code generation can be accomplished
without much hassle.
Developers must follow the coding guidelines defined by their organization and
programming tools like compilers, interpreters, debuggers, etc. are used to generate
the code. Different high level programming languages such as C, C++, Pascal, Java
and PHP are used for coding. The programming language is chosen with respect to
the type of software being developed.
Stage 5: Testing the Product
This stage is usually a subset of all the stages as in the modern SDLC models, the
testing activities are mostly involved in all the stages of SDLC. However, this stage
refers to the testing only stage of the product where product defects are reported,
tracked, fixed and retested, until the product reaches the quality standards defined
in the SRS.
Stage 6: Deployment in the Market and Maintenance
Once the product is tested and ready to be deployed it is released formally in the
appropriate market. Sometimes product deployment happens in stages as per the
business strategy of that organization. The product may first be released in a limited
segment and tested in the real business environment (UAT- User acceptance
testing). Then based on the feedback, the product may be released as it is or with
suggested enhancements in the targeting market segment. After the product is
released in the market, its maintenance is done for the existing customer base.

1.9 Algorithm

An Algorithm is a step by step procedure to solve a given problem. (or) An algorithm


consists of a set of explicit and unambiguous finite steps which, when carried out for
a given set of initial conditions, produce the corresponding output and terminate in a
fixed amount of time. By unambiguity it is meant that each step should be defined
precisely i.e., it should have only one meaning. This definition is further classified
with some more features.
According to D.E.Knuth, a pioneer in the computer science discipline, an
algorithm has five important features.

i) Finiteness: An algorithm terminates after a fixed number of steps.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C

ii) Definiteness: Each step of the algorithm is precisely defined, i.e., the actions to
be carried out should be specified unambiguously.
iii) Effectiveness: All the operations used in the algorithm are basic (division,
multiplication, comparison, etc.) and can be performed exactly in
a fixed duration of time.
iv) Input: An algorithm has certain precise inputs, i.e. quantities, which are
specified to it initially, before the execution of the algorithm begins.
v) Output: An algorithm has one or more outputs, that is, the results of
operations which have a specified relation to the inputs.

Example1: Suppose, we have to develop an algorithm to add two numbers

Step 1: Input Two numbers a, b


Step 2: sum = a + b
Step 3: Print sum
Step 4: Stop

The algorithm terminates after 4 steps. This explains the features of finiteness. Action
of each step is precisely defined. Input of our algorithm is two integers and output is
the „sum‟.

Example 2: Algorithm to find largest number from two numbers.

Step 1 : Read two numbers n1 and n2.


Step 2 : if n1 > n2 then
large = n1
else
large = n2
Step 3 : Print large
Step 4 : Stop

1.10 FLOWCHART

Flowcharts often facilitate communication between programmers and business


people. These flowcharts play a vital role in the programming of a problem and are
quite helpful in understanding the logic of complicated and lengthy problems. Once
the flowchart is drawn, it becomes easy to write the program in any high level
language.

The benefits of flowcharts are as follows:

 Communication: Flowcharts are better way of communicating the logic of a


system to all concerned.
 Effective analysis: With the help of flowchart, problem can be analyzed in more
effective way.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
 Proper documentation: Program flowcharts serve as a good program
documentation, which is needed for various purposes.
 Efficient Coding: The flowcharts act as a guide or blueprint during the systems
analysis and program development phase.
 Proper Debugging: The flowchart helps in debugging process.
 Efficient Program Maintenance: The maintenance of operating program
becomes easy with the help of flowchart. It helps the programmer to put efforts
more efficiently on that part.

Flowchart Symbols

Flowcharts are usually drawn using some standard symbols; however, some special
symbols can also be developed when required. Some standard symbols, which are
frequently required for flowcharting many computer programs are shown.

Terminator: An oval flow chart shape indicates the start or end of the process,
usually containing the word “Start” or “End”.

Process: A rectangular flow chart shape indicates a normal/generic process flow


step. For example, “M = M*F” or similar.

Decision: A diamond flow chart shape indicates a branch in the process flow. This
symbol is used when a decision needs to be made, commonly a Yes/No question or
True/False test.

Connector: A small, labeled, circular flow chart shape used to indicate a jump in the
process flow. Connectors are generally used in complex or multi-sheet diagrams.

Input/output: A parallelogram that indicates data input or output (I/O) for a


process. Examples: Read X from the user, Print X. or Display sum.

Direction: used to show the flow of control in a process. An arrow coming from one
symbol and ending at another symbol represents that control passes to the symbol
the arrow points to.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
These are the basic symbols used generally. Now, the basic guidelines for drawing a
flowchart with the above symbols are that: In drawing a proper flowchart, all
necessary requirements should be listed out in logical order.

 The flowchart should be neat, clear and easy to follow. There should not be any
room for ambiguity in understanding the flowchart.
 The flowchart is to be read left to right or top to bottom.
 A process symbol can have only one flow line coming out of it.

Ex: Flow Chart for Adding 3 numbers & Flow chart to find largest of two
numbers

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C

Introduction to C
C is the mid level language invented by Dennis Ritchie in early 1970s at AT&T Bell labs. Initially there was a
language called BCPL(Basic Combined Programming Language), from which C was extracted. Further
BCPL was renamed as lanauge B with updations. Further some more modifications have done the language
& a complete version of C was released in 1976.

C is a case sensitive language & every statement is to be written in lower case only. Moreover it is a
function dependent language. However, it is most flexible & portable language. It is also known to be a
structured programming language as it provides support for complex control structures & modular
programming concepts.

Features of C:

1. Simple to learn
2. Flexible
3. Machine language programs may be developed
4. Portable
5. Compact
6. User-friendliness
7. Case sensitive
8. Rich set of operators
9. Function dependent
10. Structure programming

Structure of a C program

Similar to many other languages, C language also possess certain structure. The following is the structure.

Documentation Section
Link Section
Function prototype declaration section
Global variable declaration section
void main()
{
Local variable declaration section;
------ // body of main()
------
other function calls
}

function definition-1.
function definition –2.
…………….
…………….
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C

Documentation section: It includes the comments. A comment is a statement, which is ignored by the
compiler at the time of compilation. Usually a statement can be made as a comment when it is enclosed in
between /* and */. These delimiters are used for single line commenting & multi line commenting.
However this section is not mandatory.

Link Section: Link section includes preprocessor directive statements like #include, # define, #if, #ifndef.
These statements are used to provide directions to the complier. The statements in the link section do not
get complied rather they intimate directly to the compiler that include the specified header files like stdio.h
& conio.h in the program before the actual processing gets started. This section is the most vital section in
every C program

Function Prototype declaration Section: This section also intimates the compiler that the user is going to
define the specified functions in the same program itself. The declaration includes return type, function
name & arguments type and order.

Void main(): It is the function that consists of the actual program body. It provides the necessary
code to solve a particular problem. It consists of variable declarations, input/output statements,
expressions and other function calls. The entire C program is based on main() itself. All the
statements of the program must be within { and }. This section is needed for every C program.

Local variable declaration section: This section is also vital for every C program. It allows the user to
declare & define certain data items required for the program.

Sample C program:

/* sample Hello program */


#include<stdio.h>
int main()
{
printf(“Hello !! This is Krishna!”);
return 0;
}

Procedure to Compile & Execute a C program:

For any C program, we require a translator called compiler. Especially for C, there are several C
compilers in which some of the compilers follow:
 Turbo C compiler
 ANSI C compiler
 Microsoft C compiler
 Borland C compiler
 Quick C compiler
 GCC Compiler

Among all the above compilers, ANSI & GCC C compiler is the user-friendliest. Hence many
programmers use GCC/ANSI C only.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
1. Get into Linux prompt. $
2. Open a text editor
3. Edit/Type C program
4. Save the program with .c extension.
5. Goto Terminal & type the following commands:
a. To compile - $ cc filename.c & press <Enter>
b. To Run/Execute - $ ./a.out

C character set:

 Alphabets: a to z and A to Z.
 Digits: 0 to 9
 Special symbols: #,+,-,*,/,&,%,!,?,:,; >,<,= etc.,

C Tokens

C provides a rich set of tokens and the list follows:

 Keywords
 Identifiers or Data names
 Literals
 Variables
 Constants
 Data types
 Operators
 Format specifiers
 Escape Sequences

Keywords: A keyword is a predefined or predetermined word defined by the compiler. C provides a rich
set of keywords. Totally 32 keywords are there in C. The following is the list of keywords:

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

Constants: A constant is an identifier whose value never changes during the program execution. In other
words, a constant is an identifier whose value remains unchanged during the program execution. In order to
define a constant, use „const‟ keyword.

Variable: A variable is an identifier whose value may or may not be changed during the program execution.

Data Types: Data type tells about the type of data being assigned to an identifier. C supports
several data types. The basic category of data types follows:
 Primary Data types
 Secondary Data types

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
The other category is:
 Primary Data Types
 Derived Data Types
 User Defined Data Types

Primary Data types: Primary data types are the basic data types that are regularly used in programs. The
following table illustrates the same.

Data Type No. of Bytes Range


char 1 -128 to 127
short int 1 -128 to 127
unsigned short int 1 0 to 255
int 2 -32,768 to 32,767
unsigned int 2 0 to 65535
long int 4 -2,147,483,647 to 2,147,483,647
float 4 -3.4 E38 to +3.4E38
double 8 -1.7E308 to +1.7E308
long double 10 -1.7E4932 to +1.7E4932

Note: signed & unsigned are known as sign qualifiers. Unsigned means „no sign‟ i.e., variable doesn‟t hold
negative values.

Secondary Data types: Secondary data types are used occasionally depends on the program type. The
following is the list of secondary data types:
o Arrays
o Functions
o Structures & unions
o Pointers
o enum
o typedef

Data Name or Identifier: A data name is nothing but a reference to a data item or an identifier is used
to identify a value. For example
int stno = 101;
char str[20] = “ Krishna”;

Here „stno‟ is an identifier that holds a value 101.

Operators: An operator is a symbol that performs an operation over one or more operands. C supports a
rich set of operators. The following is the category of operators:

1. Unary operator
2. Binary operator
3. Ternary operator

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C

Unary operators perform operation on single operand. Ex: ++, --, !, ~ (tilde) and unary -
Binary operators perform operation on two operands. Ex: +, -, *, /, %, <, >, ==, != etc.,
Ternary operator performs operation on three operands. Ex: ?:

The other category is:


 Arithmetic operators
 Assignment operators
 Conditional operators
 Increment/Decrement operators
 Relational operators
 Equality operators
 Logical operators
 Bit-wise operators
 Pointer operators
 Member selection
 Size of operator

Arithmetic operators: Arithmetic operators perform all arithmetic operations like addition, subtraction,
multiplication & division. The operators are +, -, *, / and %.

Assignment operators: Assignment operators are used in expressions. The operators are =, +=, -=, *=,
/= and %=.

Ex: A=5;

A += 4; // A = A+4
A -=3; // A = A-3
A *= 5; // A = A*5
A /= 2; // A = A/2
A %= 3; // A = A%3

Conditional operator: It is also known as ternary operator. It is used as an alternative to if..else control
structure/statement. The symbol for the operator is „ ?: ‟

Ex: c = (a>b)? a: b;

Here we verify biggest of a, b and place the result in c.

Increment/Decrement operators: A special set of operators exclusive to C is increment/decrement


operators. The operators are ++, --. They are unary operators.
Ex: int a = 5;
a++; // post- incrementation.
++a; // pre-incrementation.
a--; // post-decrementation.
--a; // pre-decrementation.

 In post incrementation, value is assigned first, then incremented.


 In pre incrementation, value is incremented first then assigned.
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C
 In post decrementation, value is assigned first, then decremented.
 In pre decrementation, value is decremented first then assigned.

Relational Operators: Relational operators are used to perform evaluations on expressions. Regularly used
relational operators are less than (<), greater than (>), less than or equal to (<=) and greater than or equal to
(>=).

Equality Operators: Equality operators are used to check whether two different entities are equal or not.
Usually numeric data & single character data may be evaluated using equality operators. The operators are
equal to (==) and not equal to (! =).

Logical Operators: Logical operators are to evaluate more than one condition or expression. Regularly
used logical operators are: and (&&), or (||), not (!).

Bit-wise operators: Bit-wise operators perform low-level operation on elementary data items. The
operators are: left shift (<<), right shift (>>), bit-wise &, bit-wise |, complement (~), ex-OR (^).

Pointer Operators: pointer operators are & (address of operator) and * (indirection operator). They are
used to handle memory addresses and the values at that addresses.

Sizeof operator: In order to obtain the no. of bytes occupied by an object (data item/ data type/ identifier)
in a program, use „sizeof‟ operator.
Ex: printf(“%d”, sizeof(int));
O/p: 2

Note: The result of relational, equality & logical operators will always be TRUE/FALSE i.e., 1/0.

Programming Style in C:

A programming style is a set of guidelines used to format programming instructions. It is useful to follow
a style as it makes it easier for programmers to understand the code, maintain it, and assists in reducing the
likelihood of introducing errors. C follows the given rules to maintain a better programming style:

1) All instructions will be in lower case only.


2) All instructions must be terminated by semi colon (;) except control statements
like if, else, switch, for and while.
3) Upper case can be used in output statements.
4) All C files must have .c extensions
5) There are two ways of comments in C. Single line comment (//) and multi-line
comments (/* ………………..*/).
6) Use proper indentation with the help of TABS.
7) Follow rules for identifiers ( or data names)

Rules for Identifiers:

1. Valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
2. The first letter of an identifier should be either a letter or an underscore.
3. You cannot use keywords as identifiers.
4. There is no rule on how long an identifier can be. However, you may run into problems in some
compilers if the identifier is longer than 31 characters.

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
Precedency of operators:

The rule of priority of operators is called operator precedence. The following table demonstrates the
operator priorities in C. it determines which operator is performed first in an expression with more than one
operators with different precedence. The operators not only have priority but also associativity too.

For example-
10 + 20 * 30 is calculated as 10 + (20 * 30) and not as (10 + 20) * 30

Operators Associativity is used when two operators of same precedence appear in an expression.
Associativity can be either Left to Right or Right to Left. The table is given below:

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C

Escape Sequences

In order to customize out put of a program C provides a special character set. Such special characters are
known to be escape sequences.

Escape Sequence Description


\n New line character
\t To provide tab space
\a To get Bell sound
\\ Single \
%% Single %
\b Backspace
\v Vertical tab

Format specifiers

A format specifier tells about the type of the data to be viewed. It is also called as format code or control
string. The following is the list of format specifiers in C.

Format code Data Type


%d or %I or %n For integers
%c For characters
%f For floating point values
%ld For long integer values
%lf For double type values
%s For char.arrays or strings
%x For hexa decimal values
%o For octal values
%u For unsigned integers
%e Exponential form
%g Similar to %e

Standard I/O functions

Usually the input is given & output is viewed on the screen with the help of input & output functions in C.
The standard or regularly used I/O functions are:
1. printf () – sends formatted output to stdout or console
2. scanf() – accepts formatted input from stdin or keyboard.

Ex:
printf(“Krishna”);
printf(“abcdefghijklmnopqrstuvwxyz”);

int a=34;
Dept of CSE/IT, VVIT Nambur
Problem Solving & Programming using C
float b = 11.23;
char c= „z‟;

printf(“%d%f%c”,a,b,c);

int a; float b; char c;


scanf(“%d%f%c”, &a, &b, &c);

The above two input & output functions are also known as formatted I/O functions.

Type casting:

It refers to changing a variable of one data type into another. The compiler will automatically change one
type of data into another if it makes sense. For instance, if you assign an integer value to a floating-point
variable, the compiler will convert the int to a float.

Cast operation is:


(type_name) expression;
Ex-1:

#include <stdio.h>
main() {

int sum = 17, count = 5;


double mean;

mean = (double) sum / count;


printf("Value of mean : %f\n", mean );
}

Ex-2:

#include <stdio.h>
main() {

int sum = 17, count = 5;


float mean;

mean = sum / (float) count;


printf("Value of mean : %f\n", mean );
}

Ex-3:
#include <stdio.h>

main() {

int i = 17;
char c = 'c'; /* ascii value is 99 */

Dept of CSE/IT, VVIT Nambur


Problem Solving & Programming using C
float sum;

sum = i + c;
printf("Value of sum : %f\n", sum );
}

Header Files: A header file is like a library with a predefined set of functions that help the programmer to
design codes quickly. The developer can use the library (header files) any number of times. There are more
than 20 header files in C library.

Ex: stdio.h, math.h, ctype.h, string.h, stdlib.h, dos.h, graphics.h, alloc.h etc.,

Math I/O: It includes all mathematical functions related to logarithms, trigonometric and statistical
functions in math.h. the list of functions is given below: Sqrt(), pow(), log10(), tan(), cos(), sin(), max(), sqr(),

Dept of CSE/IT, VVIT Nambur

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy