UNIT-1: Computer Definition
UNIT-1: Computer Definition
1.1 INTRODUCTION
Computer Systems: Software and Hardware Concepts and introduction to C
Computer Definition:
A computer is a programmable machine. It allows the user to store all sorts of information and then ‘process’ that
information, or data, or carry out actions with the information, such as calculating numbers or organizing words.
Computer Systems:
A computer is a system made of two major components: Hardware and Software
Hardware:
The hardware are the parts of computer itself including the Central Processing Unit (CPU) and related microchips
and micro-circuitry, keyboards, monitors, case and drives (floppy, hard, CD, DVD, optical, tape, etc...).
Other extra parts called peripheral components or devices include mouse, printers, modems, scanners, digital
cameras and cards (sound, colour, video) etc... Together they are often referred to as a personal computers or PCs.
Central Processing Unit (CPU) - Though the term relates to a specific chip or the processor a CPU's performance is
determined by the rest of the computers circuitry and chips.
Keyboard - The keyboard is used to type information into the computer or input information.
Disk Drives - All disks need a drive to get information off - or read - and put information on the disk - or write. Each
drive is designed for a specific type of disk whether it is a CD, DVD, hard disk or floppy. Often the term 'disk' and
'drive' are used to describe the same thing but it helps to understand that the disk is the storage device which
contains computer files - or software - and the drive is the mechanism that runs the disk.
Mouse - Most modern computers today are run using a mouse controlled pointer. Generally if the mouse has two
buttons the left one is used to select objects and text and the right one is used to access menus. If the mouse has one
button (Mac for instance) it controls all the activity and a mouse with a third buttons can be used by specific
software programs. One type of mouse has a round ball under the bottom of the mouse that rolls and turns two
wheels which control the direction of the pointer on the screen. Another type of mouse uses an optical system to
track the movement of the mouse.
Monitors - This Visual Display Unit (VDU) shows information on the screen when you type. This is called
outputting information. When the computer needs more information it will display a message on the screen, usually
through a dialog box. Monitors come in many types and plus sizes from the simple monochrome (one colour) screen
to full color screens.
Most desktop computers use a monitor with a cathode tube and most notebooks use a liquid crystal display (LCD)
monitor. To get the full benefit of today's software with full colour graphics and animation, computers need a color
monitor with a display or graphics card.
Printers - The printer takes the information on your screen and transfers it to paper or a hard copy. There are many
different types of printers with various levels of quality. The three basic types of printer are; dot matrix, inkjet, and
laser.
Scanners- Scanners allow you to transfer pictures and photographs to your computer. A scanner 'scans' the image
from the top to the bottom, one line at a time and transfers it to the computer as a series of bits or a bitmap. You can
then take that image and use it in a paint program, send it out as a fax or print it.
Memory - Memory can be very confusing but is usually one of the easiest pieces of hardware to add to your
computer. It is common to confuse chip memory with disk storage. An example of the difference between memory
and storage would be the difference between a table where the actual work is done (memory) and and a filing
cabinet where the finished product is stored (disk). To add a bit more confusion, the computer's hard disk can be
used as temporary memory when the program needs more than the chips can provide.
Random Access Memory or RAM is the memory that the computer uses to temporarily store the information as it is
being processed. The more information being processed the more RAM the computer needs.
Types of software:
The relationship between system and application software is shown in fig. In this figure, each circle represents an
interface point .The inner core is hard ware. The user is represented by the out layer. To work with the system, the
typical user uses some form of application software. The application software in turn interacts with the operating
system, which is a part of the system software layer. The system software provides the direct interaction with the
hard ware. The opening at the bottom of the figure is the path followed by the user who interacts directly with the
operating system when necessary.
System Software: It helps in running the computer hardware and the computer system. System software is a
collection of operating systems; devise drivers, servers, windowing systems and utilities. System software helps an
application programmer in abstracting away from hardware, memory and other internal complexities of a computer.
System software consists of programs that manage the hardware resources of a computer and perform required
information processing tasks. These programs are divided into three classes:
operating system, system support, and system development.
The operating system provides services such as a user interface, file and database access, and interfaces to
communication systems such as Internet protocols. The primary purpose of this software is to keep the system
operating in an efficient manner while allowing the users access to the system.
System support software provides system utilities and other operating services. Examples of system utilities are
sort programs and disk format programs. Operating services consists of programs that provide performance statistics
for the operational staff and security monitors to protect the system and data.
The last system software category, system development software, includes the language translators that convert
programs into machine language for execution ,debugging tools to ensure that the programs are error free and
computer –assisted software engineering(CASE) systems.
Application Software: It enables the end users to accomplish certain specific tasks. Business software, databases and
educational software are some forms of application software. Different word processors, which are dedicated for
specialized tasks to be performed by the user, are other examples of application software.
Application software is broken in to two classes: general-purpose software and application – specific software.
General purpose software is purchased from a software developer and can be used for more than one application.
Examples of general purpose software include word processors, database management systems ,and computer aided
design systems. They are labeled general purpose because they can solve a variety of user computing problems.
Application–specific software can be used only for its intended purpose. A general ledger system used by
accountants and a material requirements planning system used by a manufacturing organization are examples of
application-specific software. They can be used only for the task for which they were designed they cannot be used
for other generalized tasks.
1.1 PROBLEM SOLVING
Algorithm/Pseudo code
An algorithm is a finite set of instructions to accomplish a particular task in a given problem. In other words, it is a
step by step procedure to solve a problem. There are time and space complexity associated with each algorithm.
Time complexity specifies the amount of time required by an algorithm to perform a desired task.
The space complexity specifies the amount of memory space required by the algorithm to perform the desired task.
Important criteria for algorithms are as follows: (Characteristics of Algorithms)
1. Input
2. Output
3. Finiteness
4. Definiteness
5. Solvable
1. Input:
For every program input is essential. Sometimes it is not needed. Input means the data supplied to the program.
Hence we have to supply zero or more data items externally to the algorithms.
2. Output:
The aim of the program is to get an output. Output is nothing but processed information. At least one output data
item should be produced by the given algorithm.
3. Finiteness:
A problem should be solved with in some number of steps. In all the cases the algorithm must reaches to the ending
statement in finite number of steps. This is called finiteness.
4. Definiteness:
Every instruction should be clear and understandable form. There should be no confusion in the algorithm.
5. Solvable:
A Step specified in an algorithm must be solvable and it it can be done by person with the help of a pencil and paper.
There should be a feasible solution.
In summary the criteria for algorithms can be represented as follows:
An algorithm can be written in English like sentences or in any standard representation. Sometimes algorithm
written in English like language is called pseudo code.
The above algorithm terminates after some time, which explains the property of finiteness. Action of each step is
precisely defined. In this example each step requires simple operations such as reading, assigning comparing,
printing and so on. This explains the feature of definiteness and effectiveness. The total number of entries and their
values will be input for the algorithm and the average of the entered values will be the output. The above algorithm
is producing the correct result hence, it is solvable.
Characteristics of Algorithms:
Advantages of Algorithms:
i. It provides a solution to a given problem. This solution can be easily implemented through a computer language.
ii. It is easy to identify the removal of logical errors in aprogram
iii. It is easy to find out the most efficient solution for given problem.
iv. It facilitates the program development by acting as a design document for the given solution.
Disadvantages of Algorithms:
i. In case of large algorithms the flow of the program control becomes defiifcult to track
ii. Algorithms lack visual representation of program constructs like flow charts.
Flow chart
Flowchart is a graphical representation of an algorithm.
Flowchart is the diagrammatic representation of an algorithm. It is constructed with different types of symbols and
boxes. The operation to be performed is written inside the box. All symbols are connected by arrows among
themselves to indicate the flow of information and processing.
The symbols used in flowcharts have been standardized by the American National Standards Institute. Arrows are
used on the connecting flow lines if the direction of flow is not clear. A flowchart should have one start and one or
more stop points and should be arranged so that the direction of processing is from top to bottom and from left to
right. Although flowlines can cross, the crossing flowlines are independent of each other. Whenever possible,
crossing of flowlines should be avoided, since it makes the flowchart difficult to read.
The Symbols used in Flow charts are:
Terminal symbol: The terminal symbol, which is oval shaped, represents start and end points of an algorithm. The
words START and STOP or END are usually placed as narratives within the start and end symbols, respectively.
Typically, there is only one starting point, but there may be more than one end points, one to indicate the end of
normal processing and others to indicate an exit resulting from one or more unrecoverable error conditions.
Input/output symbol: Both input and output operations are represented by a parallelogram tilted to the right. The
nature of the operation to be performed is indicated by including the terms READ and INPUT to denote input
operations and the terms PRINT and WRITE to denote output operations.
Processing symbol: The rectangular processing symbol is used to indicate operations involving arithmetic and data
manipulation. This symbol is also used to represent a collection of statements that perform computations.
Decision symbol: The diamond-shaped decision symbol is used to indicate a point in the algorithm at which a
branch to one or more alternative paths is possible. The condition upon which each of the exit paths will be executed
is identified within the diamond shaped symbol. Also, flow lines leaving the corners of the diamond are labeled with
the decision results associated with each path.
On-page connector: A small circle is used to indicate a connection between two points on the same page in a
flowchart. For complex programs containing numerous branches and loops, the number and direction of flow lines
can result in confusion. In such situations connectors are used to replace flow lines. Whenever possible, connectors
should be used instead of flow lines. Connectors are used in pairs, one indicating an exit from and the other an entry
to a point in a flowchart. The connector pairs are identically labeled.
Off-page connector: Off-page connectors are used to connect flowcharts that are too large to fit on one page. They
represent an entry or an exit from, a point in a flowchart on a different page. As with on-page connectors, they
appear in identically labeled pairs.
Example:
Flow chart to evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)
6. Program documentation: Program documentation is an important step in program development, but one that is
neglected much too often. Documentation should be included so that the intended users can understand the program
and use it effectively.
Internal documentation of computer code is needed to facilitate understanding of the solution's logic. External
documentation consists of a user's manual and a technical reference manual. Some programmers prefer to write a
large part of the documentation before the program is actually coded. This can be helpful since it forces them to
decide on the details of the program before the program is actually written.
7. Maintenance and modification: The last step is maintenance and modification. Programs for real-world
applications will likely be used for a number of years. As a program is used, its limitations become apparent.
Typically, more errors are also discovered. A good program usually undergoes a number of revisions. Maintenance
involves correcting errors as they are discovered as well as making modifications to the program to incorporate
additional features. Software maintenance is a major component of the life cycle of a program and may account for
as much as 80 percent of the total cost of the program.
The above steps are summarized in figure bellow:
Creating phase:
i. We can create a C program in any text editor or any any integrated environment. i.e., the code must be entered into
a file.
ii. The created program is called as source code.
iii. Once the editing or creating is over, we have to save the file. For Ex: “filename.c”
iv. The File must have an extentionof “.c” for every c program file.
Compiling phase:
iv. If there are no errors, the compiler generates the object code. Ex: “filename.obj”
Linking phase:
Execution phase:
i. In this phase the executable object code is loaded into the memory and the program execution begins.
ii. At the time of execution, we may get some logical errors or run time errors.
iii. If any logical errors are found, we have to re edit the program and proceed to all pahses again.
iv. If we gave any wrong data as the input, we have to re run (execute) the exe file to give the coorect data as input to
get the correct output.
Computer Languages:
The operations of a computer are controlled by a set of instructions, which is called a program. In order to write a
program, you must use a computer language. They will be written to tell the computer to perform:
i) What operation to perform
ii) Where to locate data
iii) How to present results
iv) When to make certain decisions
Types of computer Languages:
Computer
Languages
Procedure
Problem Oriented
Oriented Natural languages
Languages
languages V Generation
IV Generation
III Generation
Machine Language:
At the lowest level computer understands only 0 and 1. Programs expressed in terms of binary language are called
machine language. A computer’s programming language consists of strings of binary numbers (0’s and 1’s) and is
the only one language computer can understand. This language is the lowest level of computer language recognized
and used by the CPU. An instruction prepared in any machine language consists of 2 parts. The first part is the
command or opcode or operation code. The second part of the instruction is the operand/s or data and it tells the
processor where to find or store the data or other instructions that are manipulated. A short sample of machine
language to perform addition in the storage location 0166 will look like this
00010000 00000001 01100010
A machine language programmer has to know the binary code for each operation to be carried out. Machine
language programmers must also be familiar with the internal organization of the computer. A machine language
programmer must also keep track of all the addresser of main memory locations that are referred to in the program.
The machine language format is slow and tedious. We the human beings work on natural language and not on binary
language. Hence writing machine language program is difficult for the humans.
The off state can be represented by 0 and the on state can be represented by 1. Each 0 or 1 digit is referred to as a
bit.
Advantages:
Disadvantages:
An assembler is a special program used to translate symbolic code into machine code. Symbolic languages also
became known as assembly languages.
passes. Normally, most of the compilers use two passes to convert source program into the machine language
program.
Translators used:
Compiler & Interpreter
Compiler:
Compilers convert the program instructions from human understandable form to the machine understandable form
and the translated program instruction is called object code. Compiler is nothing but a language translator used to
translate the entire program of the high level language into machine language. Every programming language
requires its own compiler to translate the program. For example, the programming language PASCAL requires
PASCAL compiler and C uses C compiler.
Interpreter:
Interpreters also convert the source program to machine language instruction but execute each line as it is entered.
The translation of the source program takes place for every run and is slower than the compiled code. An interpreter
must accompany the object code to run a program. Programming languages BASIC and LISP use interpreters.
High level languages designed to solve general purpose problems and they are called procedural languages. They are
designed to express logic and procedure of a problem.
They are used to solve specific problems and they are known as fourth generation languages. Generally these
languages uses WYSIWYG( What You See Is What You Get) environment
For ex: Querry languages like SQL, Visual C++, Visual BASIC etc.,
Natural Languages:
Natural Languages behave like an expert to solve problems and tend to use artificial intelligence. These languages
are called Fifth Generation Languages and still they are in developmental stage.
Ex: LISP and Prolog are having the characteristics and features of such languages.
Why C is Popular?
C language became popular because of the following reasons:
1. C is a robust language, which consists of number of built-in functions and operators can be used to write any
complex program
2. C compiler combines the capabilities of assembler and
3. Programs written in c are executed fast compared to other languages.
4. C language is highly portable
5. C language is well suited for structured programming.
6. C is a simple language and easy to learn.
ADVANTAGES/FEATURES OF C:
C language has become the language of choice of two decades among system programmers and application
programmers. Reason for its popularity can be summarized as follows:
1. Powerful and flexibility:
The power and popular UNIX as was written in C. The complier and interpreter for FORTAN, PASCAL, LISP, and
BASIC are written in C.
2. Portability:
C program written in one system can be run on other system with little modifications.
3. Efficiency:
The program written in C language is highly efficient like assembly language in speed and memory management.
4. Programmer oriented:
It has the flexible control structure and gives access to hardware and enables to manipulate individual bits of
memory.
5. Modularity:
C program can be modularizing for step wise refinement. The complex program can be modularize into simple
programs.
C program structure:
C is a structured programing language. A C program consists of one or more functions. Each function is a collection
of statements, which perform a specific task in the program.
A C program in general, consists of various sections which can be elaborated as follows.
….
….
…..
Definition Section # define pi 3.1415 /* Here we can define some symbolic constants
Global variable Declaration int var1, var2 ….. var n;
Section
main Function Section main()
{ {
Declaration section int x=10;
float a = 5.25;
Executable statements printf(“The value of x is =%d, the value of a= %f”,x,a);
}
}
Sub program Section add (int x, int y)
Or {
User Defined Function Section --------
---------
}
Documentation Section:
The Documentation Section consists of a set of comment lines giving the name of the program, the author and other
details, which the programmer would like to use later.
To enhance the readability of the program, programmers can provide comments about the program in this section.
They will not be executed and ignored by the compiler at compilation time. The comments are two types.
They are
1. Single line comments
The text should be started with //
Ex:
// This program caluculates the area of a triangle
2. Multi line comments
The text should be included in /* and */
For ex:
/* This program caluculates the area of a traingle */
Header files Section:
i. A C program depends on the header files to a great extent.
ii. A header file contains the information required by the compiler when call to the library functions are required.
iii. Every header file should have an extention “.h”.
iv. These must be included with the pre-procerssor directive # and a keyword include.
Syntax: #include<header file>
#include<stdio.h>
Definition section:
It is used to assign symbolic names to constants and define macros. The preprocessor directive #define is used for
this purpose.
Ex: # define pi 3.1415
In the above example pi is the symbolic constant and its value is 3.1415 and it is fixed throuh ou the execution of the
program.
Note:
i) We should not use equal operator to assign a value to the symbolic constant.
ii) We should not use statement terminator (;) at the end of declaration.
Global Variable Declaration section:
i. In this section, we can declare the varibles and they are availble in all the user defined functions if any, along with
the main() function.
ii. These variables are declared before main() function only.
main() Program Section:
i. The main() functionis the starting point of execution of C program.
ii. In every program we must use the main() function.
iii. The main function may or may not have arguments or parameters associated with it.
iv. The program execution starts with open brace ({) and terminate with end brace (}).
v. The main() function consists of declarations and executable statements.
vi. The statements are executs sequentially.
vii. Every statement must be terminated with statement terminator semicolumn (;).
User defined function section:
i. C language provides the facility for the user to define their own functions.
ii. These user defined functions are defined after the main() function.
iii. This sectionis optional in C programs.
Ex:
/* To find the area of and circumference of a circle */
#include<stdio.h>
#define PI 3.1415 /* defining the symbolic constant */
main()
{
int r;
float a, c;
clrscr(); /* Clears the screen */
printf(“\nEnter radius :\n”);
scanf(“%d”,&r);
a = 2 * PI * r;
printf(“Area = %f \n”,a);
printf(“Circuference = %f \n”);
getch(); /* waits for the user to enter any character from keyboard */
}
C Character Set:
The characters that can be used to form words, numbers and expressions are called as the C character set which are
grouped into the following categories.
1. Letters.
2. Digits.
3. Special characters.
4. White spaces.
The compiler ignores the white spaces unless they are a part of string constant.
C TOKENS:
In the passage of a text individual words and punctuation marks are called tokens. In C, the smallest individual units
are called as c Tokens. C has six types of tokens that are used to write the c programs.
Keywords: Keywords are preserved words that have special meaning in C language. The meaning has already been
described. These meaning cannot be changed. There are total 32 keywords in C language.
No special characters, such as semicolon, period, whitespaces, slash or comma are permitted to be used in or as
Identifier.
Examples of legal and illegal identifiers follow, first some legal identifiers:
float _number;
float a;
int this_is_a_very_detailed_name_for_an_identifier;
The following are illegal (it's your job to recognize why):
float :e;
float for;
float 9PI;
float .3.14;
float 7g;
C – Data types:
There are four data types in C language. They are,
S.no Types Data Types
1 Basic data types int, char, float, double
2 Enumeration data type enum
3 Derived data type pointer, array, structure, union
4 Void data type void
Note:
We can’t store decimal values using int data type.
If we use int data type to store decimal values, decimal values will be truncated and we will get only whole number.
In this case, float data type can be used to store decimal values in a variable.
1.2. Character data type:
Character data type allows a variable to store only one character.
Storage size of character data type is 1. We can store only one character using character data type.
“char” keyword is used to refer character data type.
For example, ‘A’ can be stored using char datatype. You can’t store more than one character using char data type.
Please refer C – Strings topic to know how to store more than one characters in a variable.
Modifiers in C:
The amount of memory space to be allocated for a variable is derived by modifiers.
Modifiers are prefixed with basic data types to modify (either increase or decrease) the amount of storage space
allocated to a variable.
For example, storage space for int data type is 4 byte for 32 bit processor. We can increase the range by using long
int which is 8 byte. We can decrease the range by using short int which is 2 byte.
There are 5 modifiers available in C language. They are,
a. short
b. long
c. signed
d. unsigned
e. long long
Below table gives the detail about the storage size of each C basic data type in 16 bit processor.
Please keep in mind that storage size and range for int and float datatype will vary depend on the CPU processor
(8,16, 32 and 64 bit)
Integer Constants
An integer constant refers to a sequence of digits, There are three types integers, namely, decimal, octal, and hexa
decimal.
Decimal Constant
Eg: 123,-321 etc.,
Note: Embedded spaces, commas and non-digit characters are not permitted between digits.
Eg: 1) 15 750 2)$1000
Octal Constant
An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading 0.
Eg: 1) 037 2) 0435
Hexadecimal Constant
A sequence of digits preceded by 0x or 0X is considered as hexadecimal integer. They may also include alphabets A
through F or a through f.
Eg: 1) 0X2 2) 0x9F 3) 0Xbcd
Program for representation of integer constants on a 16-bit computer.
/*Integer numbers on a 16-bit machine*/
main()
{
printf(“Integer values\n\n”);
printf(“%d%d%d\n”,32767,32767+1,32767+10);
printf(“\n”);
printf(“Long integer values\n\n”);
printf(“%ld%ld%ld\n”,32767L,32767L+1L,32767L+10L);
}
OUTPUT
Integer values
32767 -32768 -32759
Long integer values
32767 32768 32777
Real Constants
Certain quantities that vary continuously, such as distances, heights etc., are represented by numbers containing
functional parts like 17.548.Such numbers are called real (or floating point)constants.
Eg:0.0083,-0.75 etc.,
A real number may also be expressed in exponential or scientific notation.
Eg:215.65 may be written as 2.1565e2
Single Character Constants
A single character constants contains a single character enclosed within a pair of single quote marks.
Eg: ’5’
‘X’
‘;’
String Constants
A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers,
special characters and blank space.
Eg:”Hello!”
“1987”
“?….!”
Variables:
Definition:
A variable is a data name that may be used to store a data value. A variable may take different values at different
times of execution and may be chosen by the programmer in a meaningful way. It may consist of letters, digits and
underscore character.
Eg: 1) Average
2) Height
Rules for defining variables
They must begin with a letter. Some systems permit underscore as the first character.
ANSI standard recognizes a length of 31 characters. However, the length should not
be normally more than eight characters.
Uppercase and lowercase are significant.
The variable name should not
Expressions and Operators
Like in any other programming language, in C, there are a number of arithmetic relational and logical operators we
can use to write expressions that are made up of simpler basic types.
Operators are the symbol which operates on value or a variable. For example: + is a operator to perform addition. C
programming language has wide range of operators to perform various operations. For better understanding of
operators, these operators can be classified as:
1. Arithmetic Operators
c=a%b;
printf("Remainder when a divided by b=%d\n",c);
return 0;
}
a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b=1
Explanation:
Here, the operators +, - and * performed normally as you expected. In normal calculation, 9/4 equals to 2.25. But,
the output is 2 in this program. It is because, a and b are both integers. So, the output is also integer and the compiler
neglects the term after decimal point and shows answer 2 instead of 2.25. And, finally a%b is 1,i.e. ,when a=9 is
divided by b=4, remainder is 1.
Suppose a=5.0, b=2.0, c=5 and d=2
In C programming,
a/b=2.5
a/d=2.5
c/b=2.5
c/d=2
Note: % operator can only be used with integers.
2. Increment and Decrement Operators
In C, ++ and -- are called increment and decrement operators respectively. Both of these operators are unary
operators, i.e, used on single operand. ++ adds 1 to operand and -- subtracts 1 to operand respectively. For example:
Let a=5 and b=10
a++; //a becomes 6
a--; //a becomes 5
++a; //a becomes 6
--a; //a becomes 5
Difference between ++ and -- operator as postfix and prefix
When i++ is used as prefix(like: ++var), ++var will increment the value of var and then return it but, if ++ is used as
postfix(like: var++), operator will return the value of operand first and then only increment it. This can be
demonstrated by an example:
#include <stdio.h>
int main(){
int c=2,d=2;
printf("%d\n",c++); //this statement displays 2 then, only c incremented by 1 to 3.
= a=b a=b
+= a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b
The most common assignment operator is =. This operator assigns the value in right side to the left side. For
example:
var=5 //5 is assigned to var
a=c; //value of c is assigned to a
5=c; // Error! 5 is a constant.
4. Relational Operators
Relational operators checks relationship between two operands. If the relation is true, it returns value 1 and if the
relation is false, it returns value 0. For example:
a>b
Here, > is a relational operator. If a is greater than b, a>b returns 1 if not then, it returns 0.
Logical operators are used to combine expressions containing relation operators. In C, there are 3 logical operators:
Explanation
For expression, ((c==5) && (d>5)) to be true, both c==5 and d>5 should be true but, (d>5) is false in the given
example. So, the expression is false. For expression ((c==5) || (d>5)) to be true, either the expression should be true.
Since, (c==5) is true. So, the expression is true. Since, expression (c==5) is true, !(c==5) is false.
6. Conditional Operators
Conditional operator takes three operands and consists of two symbols ? and : . Conditional operators are used for
decision making in C. For example:
c=(c>0)?10:-10;
If c is greater than 0, value of c will be 10 but, if c is less than 0, value of c will be -10.
7. Bitwise Operators
A bitwise operator works on each bit of data. Bitwise operators are used in bit level programming.
READING A CHARACTER:
getchar( ): Reading a single character can be done by using the function getchar( );
varname = getchar( );
Ex: Where varname is a valid C identifier. When this statement is encountered, the computer waits until a key is
pressed and then assigns this character as a value to getchar function. Since getchar() is used on the right hand side
of an assignment statement, the character is assign to the variable.
putchar (var-name)
char ch; ch=getchar( ); putchar(ch); putchar(‘\n’);
is used to write character on the terminal.Where var-name is a character var or char constant. putchar (‘\n’); would
cause the cursor on the screen to move to the beginning of the next line. char c= ‘a’;
putchar(c);
scanf( ) Function :
Input data can be entered into the computer from a standard input device by means of the C library function
scanf().This function can be used to enter any combination of numerical values, single characters and strings.
scanf (control_string, &arg1, &arg2,……&argn);
Where control string refers to a string containing certain required formatting information and arg1, arg2,…. argn are
arguments that represent the individual input data items.
%c single character
%d decimal integer
%e floating point value
%f floating point value
%s string
%x Hexa decimal
%o Octal integer
%u unsigned decimal integer
Each variable name must be preceded by an ampersand (&). The arguments are actually pointers, which indicate
where the data items are stored in the computer memory.
int a;
float b;
char c;
scanf(“%d”, &a); scanf(“%f”,&b); scanf(“%c”,&c);
scanf(“%d%f%c”,&a,&b,&c);
char name[20]
scanf(“%s”, name);
printf( ) Function:
Output data can be written from the computer on to a standard output device using the library printf().This function
can be used to output any combination of numerical values, single characters and strings.
printf (control-string, arg1, arg2,……..);
Where control string refers to a string that contains formatting information, and arg1, arg2 are arguments that
represent individual output data items.
%c single character
%d decimal integer
%e floating point value
%f floating point value
%s string
%x Hexa decimal
%o Octal
int x;
x=20; output printf (“%d”,x); 20
printf (“x=%d”,x); x=20
printf(“value of x=%d”,x) value of x=20
int a,b; a=10; b=20;
printf(“%d %d”,a,b); 10 20
printf(“a=%d b=%d”,a,b); a=10 b=20
Assume i=integer; d=double; c=character pow() is a library function for exponential
abs(i) returns positive value of the given integer
ceil(d) returns the nearest integer greater than the number
cos(d) returns cosine of the given value
exp(d) returns e to the power d
fabs(d) returns positive value of the given integer
floor(d) returns the nearest integer less than the number
log(d) returns the logarithm of the given value
tolower(c) returns the lowercase character of the given character
toupper(c) returns the upper case character of the given character to
ascii(c) returns the ASCII value of the given character
sqrt(d) returns the square root of the given number