Sunteți pe pagina 1din 24

What is a Computer?

Computer is a machine meant for computing, it is made up of many electronic and


electrical components.

How the Computer Process?

The computer needs processing instruction to be given to it for performing the


computations.

? Command: The given instruction


? Program: Set of instructions
? Group of many related programs is a software

Relation between Command, Program, Software

Software

Program 1 Program 2

Commands Commands Commands Commands Commands

Computer language: it includes wide variety of languages used to communicate with the
computers. Computer languages can be divided into two types

Language

Low Level High Level


Language Language

-1-
What is a Programming Language?
• It is an artificial language mainly used to control the behavior of a machine,
particularly a computer
• Programming languages are sub set of computer languages

Low level languages are further divided into Machine language and Assembly language

Low Level
Language

Machine Assembly
Language Language

Machine Language:
• This language is directly understood by the computer
• It doesn’t needs any translator
• Programs developed using M.L consists only 0’s and 1’s
• It is efficient for the computer and inefficient for the programmer
• It is difficult to debug programs written in this language
• The programmer has to know the details about hardware to write a program using
this language
Assembly Language:
• It is considered to be second generation language
• Unlike machine level language here we use come codes known as nmemonic
codes to develop a program
• But a translator program is required to translate assembly language to machine
language. This translator program is called “assembler”
Disadvantages:
• One of the major disadvantages is that assembly language is machine dependent.
A program written for one computer might not run in other computers with
different hardware configuration.
HIGH LEVEL LANGUAGES:
• Higher level languages are simple languages that use English and mathematical
symbols like +, -, %, / etc. for its program construction.
• Higher level languages are problem-oriented languages because the instructions
are suitable for solving a particular problem
• For EX: COBOL is suitable for business applications, FORTRAN is for scientific
purpose
• These are very easy to learn and use

-2-
Compiler:
? It is a computer program that translates one text written in computer
language to another computer language
? Original code is known as source code
? Output or target code is known as object code

Interpreter:
• It is a type of program translator used for translating higher level language into
machine language. It takes one statement of higher level languages, translate it
into machine language and immediately execute it.
• Translation and execution are carried out for each statement. It differs from
compiler, which translate the entire source program into machine code and does
involve in its execution.
• The advantage of interpreter compared to compiler is its fast response to changes
in source program.
• It eliminates the need for a separate compilation after changes to each program.
• Interpreters are easy to write and do not require large memory in computer.
• The disadvantage of interpreter is that it is time consuming method because each
time a statement in a program is executed then it is first translated.
• Thus compiled machine language program runs much faster than an interpreted
program.

Software: It is a collection of computer programs

System software: It is a computer software which manages and controls the hardware
Ex: Operating system

Application software: It is a computer software designed to perform a specific action


directly for the user.

Ex: word processor, paint etc.

Remember it is not possible to run application software without


system software . In general system software’s are developed using low
level language whereas application software’s are developed using
high level languages

Problem Solving Approach

• A step-by-step approach is used to solve any problem


• The step-by-step approach that is applied to solve a problem may vary according
to the situation
• The commonly used approaches are
– Programs
– Flow Charts
– Algorithms

-3-
Programs

• Instructions grouped together in a sequential manner is called a program.


• Program helps perform a task or solve a problem.
• A program ensures that the task takes place without any obstructions.
• When the set of instructions are known, the execution of the program takes place
smoothly.

Flow Charts

• Pictorial representation of the approach to solving a problem, with use of standard


set of symbols, is a Flow Chart.
• As a picture speaks better than words, drawing a diagram will help the user
understand the solution easily.
• The Flow chart can also help the user to understand the correct flow of actions
and steps to follow.
• Flow charts use a standard set of symbols.
• Rectangles for processing
• Diamond shapes for decision making
• The symbols are accepted and understood uniformly by all the programmers in
the world.
• Connectors are used to link flow charts that are in multiple pages.
• Any flow chart always has a START and STOP.

START Terminator Symbol: Used here


to begin the Flow chart

Accept num1 Input / Output Symbol: Used here


and num 2 to accept numbers

Process Symbol: Used here to


Result = multiply two numbers and obtain
num1 * num2 the result

Display Input / Output Symbol: Used here


Result to display the result

Terminator Symbol: Used here


STOP to end the Flow chart

-4-
Algorithms :

• Formulation of an approach to solve a problem in simple and understandable


language is called an algorithm.
• Statements in algorithms clearly explain the action that is to be performed by that
step.
• The steps have to be short and precise.
• Collection of these steps, helps solve the problem.
• Two different individuals can write two different algorithms to achieve the same
result.
Eg:
Algorithm to calculate the area of a rectangle

1. Accept two values from the user as the length and breadth of the rectangle.
2. Multiply the length and breadth to obtain the area.
3. Display the area.

HISTORY OF ‘C’ LANGUAGE

By 1960 number of computer languages into existing almost each for a specific purpose.

Eg: COBOL : Used for commercial applications


FORTRAN: for engineering applications

At this stage people state to think why should be learn so many languages for each for
different purpose. Why not use only one language which can be programmed all possible
applications. An international committee were setup and they come with following.

Year Language Developed by Remarks


1960 ALGOL I.C. Too General, Too Abstract
1963 CPL Cambridge Hard to learn, difficult to
University implement
1967 BCPL Martin Richard Could deals with specific
problems
1970 B Ken Thompson Could deals with specific
problems
1972 C Dennis Ritche Lost generally of BCPL, B

Advantages:

1. The increasing popularity of c due to its many desired qualities. C language is


robust, its rich set of built in functions and operators can be used for writing
complex programs.

-5-
2. c complier combines the capabilities of assemb ly language with the features of
high- level language, it is well suited for writing system software and commercial
applications.
3. Due to its variety of data types and powerful operators, programs written in c are
fast and efficient. C languages strength lies in built- in functions.
4. C programs written for one computer can be run in another with little or no
modification.
5. Major advantage of c language is its ability to support structured programming so
complex programs can be easily split into modules. This facilitates easier
debugging ,testing and maintenance.
6. Most important feature of c language is its ability to extend it self. Though c
program is a collection of functions supported by c library ,new functions can be
added to the library with ease.
7. ‘c’ having 32 key words

Basic Structure of a Program

Documentation section
Link section
Definition section
Global declaration section

main()
{
Declaration Part
Executable Part
}

Subprogram section
FUNCTION 1
FUNCTION 2

Documentation Section:

Comments are used to enhance the programs readability. Comments may appear any
where in the program. They are placed within the delimiters /* and*/.while execution the
c compiler skips the lines that are marked as comment.

Link section:

The link section provides instructions to the compiler to link functions from the system
library.

-6-
Definition section:

C language allows you to assign names to constants. The constants with symbolic
constants.

Symbolic constants are used in the program for two purposes: To facilitate easier
modification, for better understanding.

General format of symbolic constant is

#define strength 100

#define pass_mark 60

#define pi 3.14159

The rules that are to be adhered to while using #define directive to define a symbolic
constant are as follows:

1. Symbolic names have the same from as variable names

2. No blank space between ‘#’ and the word define is permitted

3. # must be the first character in the line

4. A blank space is required between the #define and the symbolic name and also
between symbolic name and constant

5. #define statement should not end with semicolon

6. Symbolic name should not assigned another value within the program after
definition

7. Symbolic names are not declared for data types

8. #define statements can appear any where in the program but it should appear
before it is referenced in the program

Global declaration section:

There are some variables that are used in more than one function. Such variables are
called global variables and are declared in the global declaration section that is outside of
all the functions.

-7-
main() function section:

Every c program must have one main() function. This section contains two parts,
declaration part and executable part. The declaration part declares all the variables used
in the executable part. There is at least one statement in the executable part. These two
parts must appear between the opening and the closing braces. The program execution
begins at the opening brace and ends at the closing brace. The closing brace of the main
function section is the logical end of the program. All statements in the declaration and
executable parts end with a semicolon.

Subprogram section:

The subprograms section contains all the user-defined functions that are called in the
main function. User-defined functions are generally placed immediately after the main
function.

Executing a c program

Steps for executing a C program:

1. Program creation
Program creation is done with the help of a text editor.
2. program compilation
During the program compilation process, the source code is translated into
a form is more suitable for execution. The translated code is known as
object code.
3. linking the program with c library functions
Linking the program is the process of putting together other program files
and functions that are required by the program for its execution. The
compiled and linked program is called the executable object code.
4. program execution
During the program execution, the executable object code is loaded into
the computer memory and the instructions are executed. During program
execution, data may be requested for input, which can be entered either
through keyboard or through a file

-8-
System Start

Program Code Enter Program

Edit Source Program

C compiler Compile Source Program

Syntax Yes
Errors
No Object Code

System Library Link With System Library


Executable Object Code
Input Data Execute Object Code

Data Errors Logic & data Logic Errors


errors

Correct Output

Stop

C
CHARACTERS

Letters Digits Special White


Characters Spaces

-9-
Complete character set

Letters
Digits
Uppercase A……..Z All decimal digits 0…..9
Lowercase a……..z
, comma & ampersand ! Exclamation ( left parenthesis
. Period ^ caret | vertical bar ) right
; semicolon * Asterisk / slash [ left bracket
: colon - Minus sign \ back slash ] right bracket
? Question mark + plus sign ~ tile symbol { left brace
‘ apostrophe < open angle _ under score } right brace
bracket
“ quotation mark > Close angle $ dollar sign
bracket
# number sign

White Spaces
Blank Spaces Carriage return Form Feed
Horizontal tab New Line

C –TOKENS
Smallest individual units of program are known as c tokens. C programs are written using
these tokens. C language has six types of tokens.

OPERATO
RS

SPECIAL KEYWOR
SYMBOLS DS

C
TOKENS

STRINGS CONSTAN
TS

IDENTIFIE
RS

- 10 -
Identifiers :

1. Names that are given to various elements of a program are called identifiers
2. Identifiers Consists of letters and digits, in any order
3. First character of an identifier must be a letter. Both lower and upper case letters
are permitted.
4. The underscore character, which is considered to be a letter, can be included
5. Some of the valid examples of an identifier are listed in the figure…

Eg:
y
calc-2
temp
volume
income_tax

Key words :

1. Key words are reserved words that have predefined meanings in c.


2. The key words can be used only for their intended purpose.
3. They can not be used as programmer defined identifiers.
4. Since key words are all lower case, it is possible to utilize an upper case key word
as an identifier.
5. c-language having 32 keywords.

auto extern sizeof


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

- 11 -
Constants
Fixed value in the program. C has 2 types of constants.

CONSTANTS

NUMERICAL CHARACTER

INTEGER REAL SINGLE STRING

Integer: An arithmetic number without decimal point

Rules for Constructing Integer Constant

1) It must have at least one digit


2) It must not contain any decimal part
3) No special symbols are allowed with in the integer constant
4) It may be either +ve or – ve
5) Range : -32768 to +32767

Valid Invalid
78 10.2, $455, 412589
1256

Real: An arithmetic number with decimal point

Rules for Constructing Real Constant


1) It must have at least one digit
2) It must contain any decimal part
3) No special symbols are allowed with in the integer constant
4) It may be either +ve or – ve
5) It can’t contain more than one decimal point

- 12 -
6) Range : 3.4 e -38 to 3.4 e +38

Valid : 3.25,555.1023
Invalid : 9.2.05

Single Character Constant

1. In C single character constant must be represented with in the single quote


2. It may be an alphabet, digit ,white space or special cha racter
3. We can’t write more than one character in single quotes
4. Ex: ‘a’ ‘A’ ‘ ‘ ‘9’ ‘^’
5. Character constants represent integer values known as ASCII values. Hence it is
also possible to perform arithmetic operations on character constants.
(American Standard Code for Information Interchange)

Ex: ‘a’ – 97 ‘A’ – 65 ‘0’ – 48 etc.

There are 256 ASCII values Range: 0 - 255

String Constant: It is a collection of characters represented with in the double quotes

String may contain alphabets, digits, & specia l characters. String is also known as
alphanumeric

Ex: “rajesh” “134” “a*&5}”

ASCII values not defined for strings whereas each character within the string has ASCII
value

Backslash character constants

These are special characters are used in output statements. each of them represents one
character, although they consists of two characters. These characters are known as escape
sequences.

Constant Meaning
\a Audio alert (bell)
\b Back space
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab

- 13 -
\v Vertical tab
\’ Single quote
\” Double quote
\? Question mark
\\ Backslash
\0 Null

VARIABLE

1. A quantity that may vary during programs execution is called a variable.


2. Variable names are names assigned to the locations where the data is stored in the
memory.
3. These locations can contain data of either predefined or user-defined type.
4. It can hold a maximum one value at a time
5. When ever a new value is given to a variable ,its previous value is lost
6. Unlike constant it can change its value
7. C compiler can distinguish between the variable names, by making it compulsory
to declare the type of the variable names S_hra and s_hra are thread differently.

VALID INVALID
Sum 2add
Product abc$
Digit break
A1b2c3 class strength
Reverse
Class_strength

Rules for constructing variable names

1. Variable name is any combination of alphabets, digits or underscores. Some


compilers allow variable names whose length can be up to 40 characters, but it
would be safer to stick with eight characters.
2. The first character must be an alphabet
3. No commas or blanks are allowed with a variable name.
4. No special symbol other than an underscore can be used in a variable name.
5. It can’t be a reserved or keyword

Variable declaration having 2 parts

Data type
Declaration
Variable Name

- 14 -
DATA TYPES

Data type specifies the properties of a variable like type of value that it can hold, memory
occupied by a variable

C supports the following Data Types

1. Primary data types,


2. User-defined types,
3. Derived data types and
4. Empty data set.

Primary Data Types

C compilers support four fundamental data types, namely Integer

Integer data type:

1. An arithmetic no without decimal point


2. It takes 2 byes of memory space.
3. Range : -32768 to 32767
4. Keyword: int

Float data type:


1. An arithmetic no with decimal point
2. It takes 4 byes of memory space.
3. 1st 26 bits are for integer part, next 6 bits are for fractional part
4. Range : 3.4 e -38 to 3.4 e+ 38
5. Keyword: float

Double data type:


1. It is an extension to float
2. It is also known as long float
3. It takes 8 bytes of memory
4. 1st 52 bits are for integer part. Next 12 bits are for fractional part
5. Range : 1.7 e -308 to 1.7 e + 308
6. Keyword: double

Character data type:


1. It takes 1 byte of memory
2. Range : -128 to +127
3. Keyword : char

- 15 -
Qualifiers: using which we can modify the behavior of the data type

1) Sign ( to modify sign behavior)


2) Size ( to modify size behavior)
3) Constant
4) Volatile
Data Types and Their Keywords

Data type Keyword equivalent


Character char
Unsigned character unsigned char
Signed character signed char
Signed short integer signed short int or short int or short
Signed long integer signed long int or long int or long
Unsigned integer unsigned int or unsigned
Unsigned short integer unsigned short int or unsigned short
Unsigned long integer unsigned long int or unsigned long
Floating point float
Double precision floating point double
Extended double-precision floating point long double

Data Types and Their Memory Sizes

Type Size (bits) Range


char or signed char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to 32767
unsigned int 16 0 to 65535
short int or signed short int 8 -128 to 127
unsigned short int 8 0 to 255
long int or signed int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
float 32 3.4E-38 to 3.4E+38
double 64 1.7E-308 to 3.4E+308

- 16 -
long double 80 3.4E-4932 to 3.4E+4932

INPUT/OUTPUT STATEMENTS

1. C language helps create interactive programs for the user.


2. Displaying appropriate captions and correct messages can help enhance the
usability of the programs.
3. To perform these tasks of accepting and displaying values and messages, C
provides standard library functions.
4. These functions are also referred to as Input – Output functions.

printf()

1. It is an output function
2. It is used to display message & values of variables
3. It is predefined in “stdio.h” ( header) file
4. It is call by value function
5. It deals with monitor

Syntax:
printf(“format specifier”, list of variables);

format specifier conatains Conversion characters like %d, %c, %f, %s and escape
characters like \n, \t, \b, \f

Variables from which values are to be displayed

scanf()
1. It is an input function
2. It is used read some value into memory variables
3. It is predefined in “stdio.h” ( header) file
4. It is call by address function
5. It deals with keyboard

Syntax:
scanf(“format specifier”, list of addresses of variables);

Conversion characters like %d, %c, %f, %s and escape characters are ignored

Address of the variable is specified using the & (ampersand) symbol with name of the
variable

- 17 -
Data type Conversion
character
int %d or %i
unsigned long int %u
hexa decimal integer %x
octal integer %o
short int %h
long int %ld
long double %Ld
float %f
exponential %e
double precision %lf
char %c
string %s

C-IDE Environment

IDE stands for integrated development environment. It is one of the package was
designed by various companies. But now days in our locality all can use turbo company
C-IDE. Simply called as TC.

Various commands and their short cut in TC- IDE

• Alt + F9 ------ for compiling the program


• F9 ----- for Linking
• Ctrl + F9 ---- for Executing the program
• Alt + F5 ---- to see the output

OPERATORS

An operator is a symbol that tells the computer to perform certain mathematical or logical
manipulations

Operand: Operand is the data upon which operation is carried out

C supports the following operators

1) Arithmetic operators
2) Relational operators
3) Logical operators
4) Assignment operators
5) Bitwise operators
6) Conditional operators
7) increment and decrement operators
8) Special operators

- 18 -
Arithmetic Operators:

Ex: + - * / %

1. All the above operators are binary operators. These can operate on any built –
in data type
2. Integer division truncates any fractional part. The modulo division produes the
remainder of an integer division
3. The modulo division operator % cannot be used on floating point data

Integer Arithmetic:

1. It is an arithmetic expression in which both operands are integers


2. Integer arithmetic always yields integer value
3. Ex: 3+5*6

Real Arithmetic:

1. An arithmetic operation involving only real operands is called real arithmetic


2. Real arithmetic always yields real value
3. Ex: 5.0/2.5

Mixed Mode Arithmetic:

1. When one of the operands is real and other is integer the expression is called
mixed mode arithmetic expression.
2. Always the result of mixed mode arithmetic is of higher type
3. Ex: 9+3.6/2-1.5

Relational Operators

1. These operators can be used to compare give two quantities


2. Relational expression returns Boolean value (i.e. either true or fals e)
3. Relational Operators are used for decision making in C.
4. They test the relation between two operands or two expressions.
5. They are also used in iterations.

> operand1 > operand2

< operand1 < operand2

>= operand1 >= operand2

- 19 -
<= operand1 <= operand2

== operand1 == operand2

!= operand1 != operand2

Logical Operators

1. Logical operators are used to join one or more conditions.


2. Types of Logical Operators are :

&& means logical AND


|| means logical OR
! Means logical NOT

AND OR NOT

Action is Action is Action is


performed only performed only OPPOSITE
if BOTH the if ANY ONE of to the
conditions are the condition is condition
TRUE TRUE
AND

Expression 1 Expression 2 Outcome


0 0 0
0 1 0
1 0 0
1 1 1

OR

Expression 1 Expression 2 Outcome


0 0 0

- 20 -
0 1 1
1 0 1
1 1 1

NOT

Expression Outcome
1 0
0 1

Assignment operator:

Assignment operators are used to assign the result of an expression to a variable. We


have seen the usual assignment operator =. In addition, c has a set of shorthand
assignment operator of the form

V op= exp
Statement with simple Statement with
assignment operator shorthand operator
a=a+1 a+=1
a=a-1 a-=1
a=a*(n+1) a*=(n+1)
a=a/(n+1) a/=(n+1)
a=a%b a%=b

Conditional operator:

• “ ?: “ is ternary operator
• Conditional expression takes the following form
• Exp1 ? Exp2 : Exp3
• Exp1 is evaluated first. If it is true then the Exp2 is evaluated and its value
becomes the value of the exp ression.
• Note that only one of the expressions is e

Increment and decrement operators

• ++ is the symbol for increment operator


• -- is the symbol for decrement operator
• Both are unary operators
• ++ adds 1 to the operand whereas – subtracts 1
• Post increment - m++ pre increment - ++m

- 21 -
• Post decrement – m– pre decrement - --m
• m++ and ++m mean the same thing when they form statements independently,
they behave differently when they are used in expressions on the right hand side
of an assignment statement.
• Expans ion for m++ or ++m is m = m+1
• Expansion for m-- or --m is m = m-1

Bitwise Operators:

• These operators are used for manipulation of data at bit level


• Bitwise operators may not be applied to float or double
• Except bitwise complement operator all other operators are binary operators
• Bitwise complement operator is a unary operator

Operator Meaning

& Bitwise AND

| Bitwise OR

^ Bitwise exclusive OR

<< Bitwise leftshift

>> Bitwise rightshift

~ Bitwise one’s complement

Special operators

Special operators Meaning


Comma(,) Used for linking related expression together
Sizeof It returns the number bytes the operand occupies
Address operators(&) Used for pointing the address of a variable
Pointer operator(*) Points the value stored in the address specified

Operator precedence and associativity

? Each operator in c has a precedence associated with it . So in an expression


involving more than one operator, this precedence is used for evaluating the
expression. These are various distinct levels of precedence available and the

- 22 -
operators will belong to any one of these levels. The operators that are in the
higher levels of precedence are evaluated first.

? If the operators belonging to the same precedence level are present in an


expression, they are evaluated either from left to right or from right to left
depending upon their level. This method of evaluating the operators belonging
to the same precedence level is called assocativity.

Hierarchy of operators and associativity

Description Operator Associativity


Unary Minus - Right to Left
Increament/Decrement ++ -- Right to Left
One’s compliment ~ Right to Left
Negation ! Right to Left
Address of & Right to Left
Value at address * Right to Left
Type Cast (type) Right to Left
Size in Bytes Sizeof Right to Left
Multiplication * Left to Right
Division / Left to Right
Modulus % Left to Right
Addition + Left to Right
Subtraction - Left to Right

Left Shift << Left to Right


Right Shift >> Left to Right
Less than < Left to right
Less than or equal to <= Left to right
Greater than > Left to right
Greater than or equal >= Left to right
to
Equal to == Left to right
Not equal to != Left to right
Bitwise AND & Left to right
Bitwise exclusive OR ^ Left to right
Bitwise exclusive OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to Left
Assignment =,+=,- Right to Left

- 23 -
=,*=,/=,%=
Comma , Right to Left

Type Casting:

• It is a process of conversion of one data type to another data type


• These are of 2 types 1) implicit type casting 2) explicit type casting

Implicit type casting : Here type casting is done by system automatically

• Ex: int a = 9.5; here system truncates the decimal part of 9.5 and assigns only
integral part to integer variable ( TRUNCATION)
• float x=10; here system promotes value 10 to 10.0 (PROMOTION)
• char ch=65; here integer value 65 is converted into ‘A’

Explicit type casting : Here type casting is to be done by the programmer or user

• The general form of explicit type cast is (type – name) expression


• type – name is one of the standard C data types whereas expression may be a
constant, variable or an expression
• Ex: (int) 9.6%(int) 3.2
• Now modulo division is performed in integer mode

- 24 -

S-ar putea să vă placă și