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

UNIT-1

unit1 ppsc

Uploaded by

mounica
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)
8 views

UNIT-1

unit1 ppsc

Uploaded by

mounica
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/ 23

Introduction to Computers:

Computer Systems:

A computer is a system made of 2 major components:


1.Computer Hardware:
• The computer hardware is the physical equipment.
• The hardware component of the computer system consists of five parts: input
devices, central processing unit (CPU),primary storage, output devices, and
auxiliary storage devices.

The input device is usually a keyboard where programs and data are entered into
the computers. Examples of other input devices include a mouse, a pen or stylus, a
touch screen, or an audio input unit.
The central processing unit (CPU) is responsible for executing instructions such as
arithmetic calculations, comparisons among data, and movement of data inside the
system.
The output device is usually a monitor or a printer to show output. If the output is
shown on the monitor, we say we have a soft copy. If it is printed on the printer, we
say we have a hard copy.
Auxiliary storage, also known as secondary storage, is used for both input and
output. It is the place where the programs and data are stored permanently. When
we turn off the computer, or programs and data remain in the secondary storage,
ready for the next time we need them.
2. Computer Software :
• The software is the collection of programs (instructions)
• Computer software is divided in to two broad categories: system software
and application software.
System software manages the computer resources .It provides the interface
between the hardware and the users.
Application software, on the other hand is directly responsible for helping users
solve their problems.
Computing Environments:
Computing Environment is a collection of computers / machines, software, and
networks that support the processing and exchange of electronic information meant
to support various types of computing solutions.
Types of Computing Environments:
1. Personal Computing Environment
2. Time sharing Environment
3. Client Server Computing Environment
4. Distributed Computing
Personal Computing Environment:

• A personal computer(PC) is a general purpose computer. The size and


capabilities of a personal computer makes it useful for end-users.
• In a personal computer, all the hardware components are tied together, so
that it can be used as per our need.

Time Sharing Environment:

• Employees in large companies often work in a time sharing environment. In


this environment, many users are connected to one or more computers.
• In time sharing environment, each computer must be operated by the central
computer.
• In computing, time-sharing is the sharing of a computing resource among
many users by means of multi programming and multi-tasking at the same
time. Users are connected to one or more computers.

Client Server Computing Environment:


• A client/server system is “a networked computing model that distributes
processes between clients and servers, which supply the requested service.”
• A client/server network connects many computers, called clients, to a main
computer, called a server. Whenever client requests for something, server
receives the request and process it.

Distributed Computing Environment:


• A distributed computing environment allows smooth integration of computing
functions between different internet server's clients and provides the
connection to different servers around the world.
• Distributed Computing is used by distributed systems to solve computational
problems
• In Distributed Computing, each problem is divided into several tasks, which
are solved by one or more computers.

Computer Languages:

These are the languages which are used to give instructions to computer to perform
some task.

1940’s Machine level Languages


1950’s Symbolic Languages
1960’s High-Level Languages

The computer languages are classified into

1.Machine Language

2.Assembly Language

3.High Level Language

1.Machine language:

It is the language which can be directly understandable by computer.

In this programs are written by using 0’s and 1’s.(binary code)

It is also called as binary language because it only support the binary value.

0 represent on and 1 represent off.

Advantages:

• Execution is faster because the programmer written the code using 0’s and
1’s can be directly understandable by computer.
• No translator is required.

Disadvantages:
• It is very difficult to read,understand,write program in machine language.
• It is very difficult to remember the machine language.
• It is machine dependant language

2.Assembly Language:

• It is a language which uses some words called mnemonics to write


instructions.
• Each mnemonic has some fixed meaning.
• This mnemonic are used to better understandable by users.

Advantages:

• It is easy to read,understand assembly program than machine language.


• Easy to remember mnemonics than 0’s and 1’s.
• Easy to correct errors.

Disadvantages:

• Requires translator to convert assembly level language to machine level


language.
• This is machine dependent language.
• It is difficult to remember mnemonics
• Execution is slower when compared to machine level language.

3.High Level Language:

• In this programs are written in English statements.


• The program contains letters,digits,symbols and and special characters.
• It is user friendly language.

Advantages:

• They are machine independent languages.


• User can easily understand the program when compared to machine and
assembly languages.
• Errors in the program can easily found.

Disadvantages:

• Translator is required to convert high level language into machine level


language.

Creating and running Programs:

Step 1: Creating a Source Code


• Source code is a file with C programming instructions in a high-level language.
• To create source code, we use any text editor to write the program
instructions.
• The instructions written in the source code must follow the C programming
language rules.
Step 2: Compile Source Code (Alt + F9)
• The compilation is the process of converting high-level language instructions
into low-level language instructions. We use the shortcut key Alt + F9 to
compile a C program in Turbo C.
• Whenever we press Alt + F9, the source file is going to be submitted to the
Compiler. On receiving a source file, the compiler first checks for the Errors.
If there are any Errors then compiler returns List of Errors, if there are no
errors then the source code is converted into object code and stores it as a
file with .obj extension. Then the object code is given to the Linker. The Linker
combines both the object code and specified header file code and generates
an Executable file with a .exe extension.

Step 3: Executing / Running Executable File (Ctrl + F9)


• After completing compilation successfully, an executable file is created with
a .exe extension. The processor can understand this .exe file content so that
it can perform the task specified in the source file.
• We use a shortcut key Ctrl + F9 to run a C program. Whenever we press Ctrl
+ F9, the .exe file is submitted to the CPU. On
receiving .exe file, CPU performs the task according to the instruction written
in the file. The result generated from the execution is placed in a window
called User Screen.

Step 4: Check Result (Alt + F5)


• After running the program, the result is placed into User Screen. Just we
need to open the User Screen to check the result of the program execution.
We use the shortcut key Alt + F5 to open the User Screen and check the
result.

Execution Process of a C Program


• When we execute a C program it undergoes with the following process…

Computer Numbering System:


The number systems give us different ways to represent numbers.
1.Binary number system:
• It supports only 2 digits 0’s and 1’s.
• The base of binary number system is 2.
• Binary number system is used by computer.
• Example
(1010)2, (1000)2
2.Decimal number system:
• Decimal number system supports 10 digits range from 0 to 9
• The base of decimal number system is 10.
• This is common number system used by users.
• Example
(10.25)10
3. Octal number system:
• Octal number system as base 8.
• This number system is used as a shorthand representation of long binary
numbers.
• Uses eight digits, 0,1,2,3,4,5,6,7
• Example
(12570)8, (26643)8
4.hexadecimal number:
• The hexadecimal number is derived from greek word hexadec meaning 16
• It uses 16 symbols 0,1,..9,A,B,C,D,E and F.
• The hexadecimal system uses the same first 10 symbols as the decimal
system,but instead of using 10,11,12,13,14,15 it uses A,B,C,D ,E and F.
• Example
• (3039)16
Binary to decimal conversion:
• Binary Number is : (00111001)2

=0+0+32+16+8+0+0+1
=57
Decimal to binary conversion:
• Decimal number is:(771)10
Decimal to hexadecimal number system:
Ex:1Decimal Number is : (12345)10

Hexadecimal Number :(3039)16


Ex:2 Decimal Number is : (725)10

Hexadecimal Number is (2D5)16


Binary to Hexadecimal Conversion:
• To convert a binary number into its hexadecimal equivalent ,divide the
binary number into groups of bits where each group consists of exactly 4
bits
• Convert each group into its equivalent hexadecimal digit.
Ex:Convert the binary number(1010101101001)2 to hexadecimal number
Step1:(1010101101001)2
Step2:(1 0101 0110 1001)2
step3:(0001 0101 0110 1001)2
Step4: (1 5 6 9)16
Step5:(1569)16
Ex: Convert Binary number(001100101.110111)2 to hexadecimal.
Step1:(001100101.110111)2
Step2: (0 0110 0101 . 1101 1100)2
Step3: 0110 0101 . 1101 1100
Step4: 6 5 . D C
Step6:(65.DC)16
Hexadecimal to Decimal Conversion:
Hexadecimal Number is : (2D5)16
=512+208+5
=725
Decimal Number is: (725)10
Introduction to the C Language:
Background:
• 1st widely used language was FORTRAN(Formula Translation) created
by John Backus and IBM team in 1957.
• FORTRAN was widely used in scientific and engineering applications.
• FORTRAN was followed by COBOL(common business oriented
language)used for developing business language-Admiral Hopper.

• ‘C’ like most modern languages is derived from ALGOL.


• ALGOL(Algortithmic language) is 1st language to use a block structure
developed in 1958.
• Several languages preceeded before ‘c’ language.
o Basic combined programming language(BCPL) Developed by martin
Richards in 1967.
o Ken Thompson followed in 1970 with similar language called ‘B’.
o ‘B’ was used to develop 1st version of unix, one of the popular network
operating system.
o ‘c’ language was developed by dennis Ritchie in the year 1972 at AT &T
bell laboratories.
‘C’ has taken many features from ALGOL,BCPL,B.
Features of c language:
• ‘C’ is a simple language.
• ‘C‘ is a high level language.it is sometimes called as middle level language
because it has features of low level as well as high level languages.
• ‘C’ is portable /platform independent language.
• ‘C’ is structured programming /procedural oriented language.
• ‘c’ is extensible language i.e., it allows users or developers to add new
concepts.
• Supports rich library-provides various in built functions.
Applications of c language:
• Used for developing operating system.
o 1st o.s(linux) was designed in ‘c’ language.
• Used to develop compilers.
• Computer professionals inorder to develop
o System software and
o Application software
• Used to develop GUI’s
• Used in developing embedded systems.
Structure of C program:
Any ‘C’ program consists of the following sections.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section
5. Main function section
6. Sub program or function section
1. Documentation section:
• Used for describing what a programmer does actually.
Comment lines:
//-for single line comments
/* and */ for multi line comments
2. Linking section :
• This section tells the compiler to link certain occurances or keywords or
functions in a program to the headerfiles,specified in this section.
Ex: #include<stdio.h>
3. Definition section :
• It is used to declare some constants and assign them with some values.
Ex: #define PI 3.14
4. Global declaration section :
• Here variables which are used throughout the program(including main and
other functions) are declared so that they can be accessed throughout the
program.
Ex: int I;
5. Main function section:
• It tells the compiler to start execution of the program.
6. Sub program or function section:
• This has all the sub programs or the functions which our program needs.
Sample program:
/*this is sample program */
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printf(“hello world”);
getch();
}
Output:
hello world
Identifiers:
• Names of the variables and other program elements such as functions,
array,etc,are known as identifiers.
Rules for identifiers:
1. Identifiers can be named from the combination of A-Z, a-z, 0-9, _(Underscore).
2. The first alphabet of the identifier should be either an alphabet or an underscore.
digit are not allowed.
3. It should not be a keyword.
Examples of valid identifiers:
Roll_number,marks,name,emp_id,HRA,Emp_no…roll_number
Examples of invalid identifiers:
23_student,%marks,@name,#emp_no…
KEYWORDS :
• There are certain words, called keywords (reserved words) that have a
predefined meaning in ‘C’ language.
• These keywords are only to be used for their intended purpose and not as
identifiers.

DATA TYPES :
• Data type represents the type of data we use in our program and how much
memory memory is used to store that data.
• Data types are classified as
o Primitive data types
o Derived data types and
o User defined data types
Primitive data types/basic data types:
• These data types are built-in or predefined data types and can be used
directly by the user to declare variables.
• C supports the following data types.

Character data type:


• Used to store character values
• Keyword used is char
• Format specifier is %c
Integer data type:
• Used to store integer or numeric values.
• Keyword used is int
• Format specifier is %d
Floating point data type:
• Used to store decimal values
• Keyword used is float
• Format specifier is %f
Double data type:
• Used to store big floating point values.
• Keyword used is double
• Format specifier is %lf
Void data type:
• Value less
• Keyword used is void.
Boolean data type:
• Can represent only 2 values true or false
• A non zero number is used to represent true and zero value is used to
represent false value.

Derived data types :


• Derived data types are used to store a set of data values.
Ex:Arrays, strings.
User defined data types:
• The data types that are defined by users are called user defined data types.
Ex: structure,union etc.
Example program for data types and variables:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10;
char b=’a’;
float c=10.203;
double d=1.34567812;
clrscr();
printf(“the value of a is %d\n”,a);
printf(“the value of b is %c\n”,b);
printf(“the value of c is %f\n”,c);
printf(“the value of d is %lf\n”,d);
getch();
}
Output:
the value of a is 10
the value of b is a
the value of c is 10.203
the value of d is 1.34567812
Variable:
• A Variable is used to store value.
• The quantity that changes throughout the program.
• Variable refers to the address of memory location where our data is to be
stored.
Syntax for declaring variables:
• A single variable can be declared as
datatype variable_name;
Ex: int i;
• Multiple variables can be declared as
datatype variable_name1,variable_name2…;
int a;
int b;
int c;
Ex: int a,b,c;

Syntax for initializing /assigning values to variable:


datatype variable_name=value;
Ex:int rollno=10;
char grade=’A’;//character values are specified within ‘ ‘
float f=10.23;
Constants:
• Constants are identifiers whose values do not change.
• values of constants can never be changed.
• Constants are used to define fixed values like pi or the charge on an
electron so that their value does not get changed in the program even by
mistake.
Declaring or defining a constant:
• There are 2 ways to define a constant in ‘c’ language.
o Using const keyword.
o Using #define preprocessor
Using const keyword:
• To declare a constant ,precede the variable declaration with ‘const’ keyword
and assign it a value.
Syntax:
const datatype variable_name=value;
Ex:
Const float pi=3.14;
Example program using const keyword:
#include<stdio.h>
#include<conio.h>
main()
{
const float PI=3.14;
clrscr();
printf(“the value of PI is %f\n”,PI);
getch();
}

Using #define preprocessor:


• The #define preprocessor is used to define a constant.
Syntax:
#define variablename constantvalue
Ex:
#define PI 3.14
Example program using #define preprocessor(Area and circumference of circle):
#include<stdio.h>
#include<conio.h>
#define PI 3.14
main()
{
float radius,area,circumference;
clrscr();
printf(“enter radius \n”);
scanf(“%f”,&radius);
area=PI*radius*radius;
printf(“the area of circle is %f\n”,area);
circumference=2*PI*radius;
printf(“circumference of circle is %f\n”,circumference);
getch();
}
Input/output:
‘c’ language supports 2 formatting functions
1.Printf(print formatting):
Printf function writes the output to the standard output stream stdout and produces
the output according to the format
The format can be %d-integer
%c-character
%f-floating point
%s-string…etc
Syntax for printing single value:
1. Syntax for printing integer values:
Printf(“%d”,variablename);
Ex: int a=10;
Printf(“%d”,a);
2. Syntax for printing float/real values:
Printf(“%f”,variablename);
Ex: float a=1.23;
Printf(“%f”,a);
3. Syntax for printing character values:
Printf(“%c”,variablename);
Ex: char a=’a’;
Printf(“%c”,a);
4. Syntax for printing string values:
Printf(“%s”,variablename);
Ex: char a[10]=’hello’;
Printf(“%s”,a);
Syntax for printing Multiple value:
Printf(“format specifiers”,variable1,variable2,…variablen);
Ex:
Int a=10;
Char c=’a’;
Printf(“%d%c”,a,c);//prints 10 a
Printf(“%c%d”,c,a);//prints a 10
Escape Sequence:
\a Audible signal
\b Backspace
\t Tab
\n New line
\v Vertical tab
\f New page\Clear screen
\r Carriage return
\?question mark
\\ backslash
\”double quote
\’single quote
Flags in printf():
– Leftjustify within the given field width
+ Displays the data with its numeric sign (either + or –)
# Used to provide additional specifiers like o, x, X, 0, 0x, or 0X for octal and
hexadecimal values respectively for values different than zero
0 The number is left–padded with zeroes (0) instead of spaces
Example:adding 2 numbers using printf:
#include<stdio.h>
#include<conio.h>
main()
{
int a=10,b=20,c;
clrscr();
c=a+b;
Printf(“the value of c is %d\n”,c);
getch();
}
2.scanf(scan formatting):
The scanf() method in c reads the value from the console as per type specified.
Syntax for reading single value:
Scanf(“format specifier”,&variablename);
1. Syntax for reading integer values:
Ex: int a;
scanf(“%d”,&a);
2. Syntax for reading float values:
Ex: float a;
scanf(“%f”,&a);
3. Syntax for reading character values:
Ex: char a;
scanf(“%c”,&a);

Syntax for reading multiple values:

Scanf(“format specifier1, format specifier1,…”,&variablename1, &variablename2…);


Ex:
Int a;
Char c;
Scanf(“%d%c”,&a,&c);
Example program to add to numbers using scanf:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b,c;
clrscr();
printf(“enter 2 numbers to add\n”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“the sum is %d”,c);
getch();
}

Scope, Storage Classes:


Scope:
• A scope is a region of the program, and the scope of variables refers to the
area of the program where the variables can be accessed after its declaration.
• Scope can be
1.local scope
2.global scope
Local variables:
• Variables that are declared within the function block and can be used only
within the function is called local variables.
1.local scope :
• A local scope or block is collective program statements put in and declared
within a function or block (a specific region enclosed with curly braces) and
variables lying inside such blocks are termed as local variables. All these
locally scoped statements are written and enclosed within left ({) and right
braces (}) curly braces.
• So it can be said that variable(s) that are declared within a block can be
accessed within that specific block and all other inner blocks of that block,
but those variables cannot be accessed outside the block.

#include <stdio.h>
int main ()
{
int x,y,z;
x = 20;
y = 30;
z = x + y;
printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
return 0;
}
Global variables:
• Variables that are declared outside of a function block and can be accessed
inside the function is called global variables.
2.Global scope
• Global variables are defined outside a function or any specific block, in most
of the case, on the top of the C program. These variables hold their values all
through the end of the program and are accessible within any of the functions
defined in your program.
• Any function can access variables defined within the global scope, i.e., its
availability stays for the entire program after being declared.
#include <stdio.h>
int z;
int main ()
{
int x,y;
x = 20;
y = 30;
z = x + y;
printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
return 0;
}
Storage classes:
• A variable mainly depends on
1.datatype
2.Storage class
• Datatype represents
1.what type of data has to be stored and
2.how much memory is allocated to store that data
• Storage class represents 4 things:
1.Storage Place
2.Default Value
3.Scope
4.Lifetime
Syntax:
Storageclass datatype variable name;
• There are four types of storage classes in C
1.Automatic
2.External
3.Static
4.Register

AUTOMATIC STATIC REGISTER EXTERNAL

1.Keyword used is 1.Keyword used is 1.Keyword used is 1.Keyword used is


auto static register extern

Syntax: Syntax: Syntax: Syntax:

auto datatype static datatype register datatype extern datatype


variablename; variablename; variablename; variablename;

2.Variable 2.Variable 2.Variable 2.Variable


declared declared static is declared register is declared extern is
automatic is stored in main stored in register stored in main
stored in main memory memory memory
memory

3.Garbage value zero Garbage value Zero

4.local local Local Global

5.Within the Within the Within the Throughout the


function\block function\block function\block program

1.Automatic:
• The keyword used for defining automatic variables is auto.
Synatx:
auto datatype variablename;
Example program:
#include <stdio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b,
and c.
return 0;
}
2.Static:
• The keyword used to define static variable is static.
Synatx:
static datatype variablename;
Example program:
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main ()
{
printf("%d %d %f %s",c,i,f); // the initial default value of c, i, and f will be printed.

}
3.Register:
The register keyword is used for the variable
Synatx:
register datatype variablename;
Example program:
#include <stdio.h>
int main()
{
register int a; // variable a is allocated memory in the CPU register. The initia
l default value of a is 0.
printf("%d",a);
}
4.External:
Keyword used is extern
Synatx:
extern datatype variablename;
Example program:
#include <stdio.h>
int a;
int main()
{
extern int a; // variable a is defined globally, the memory will not be allocated to a
printf("%d",a);
}

Type Qualifiers:
• In C programming language, type qualifiers are the keywords used to modify
the properties of variables. Using type qualifiers, we can change the properties
of variables. The c programming language provides two type qualifiers and
they are as follows...

• const
• volatile

const type qualifier in C:


• The const type qualifier is used to create constant variables. When a variable
is created with const keyword, the value of that variable can't be changed
once it is defined. That means once a value is assigned to a constant variable,
that value is fixed and cannot be changed throughout the program.
• The keyword const is used at the time of variable declaration. We use the
following syntax to create constant variable using const keyword.

const datatype variablename;


Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 9 ;
const int x = 10 ;
clrscr() ;
i = 15 ;
x = 100 ; // creates an error
printf("i = %d\nx = %d", i, x ) ;
}

volatile type qualifier in C:


• When a variable is defined as volatile, the program may not change the
value of the variable explicitly.
• But, these variable values might keep on changing without any explicit
assignment by the program. These types of qualifiers are called volatile.
Tips and Common Programming Errors:
•When you start writing your code in C, C++ or any other programming
language, your first objective might be to write a program that works.
• After you accomplished that, the following are few things you should consider
to enhance your program.
1. Security of the program
2. Memory consumption
3. Speed of the program (Performance Improvement)
Few general points to improve speed of program:
• You could optimize your code for performance using all possible techniques,
but this might generate a bigger file with bigger memory footprint.
• You might have two different optimization goals, that might sometimes conflict
with each other. For example, to optimize the code for performance might
conflict with optimize the code for less memory footprint and size. You might
have to find a balance.
• Performance optimization is a never-ending process. Your code might never
be fully optimized. There is always more room for improvement to make your
code run faster.
• Sometime we can use certain programming tricks to make a code run faster
at the expense of not following best practices such as coding standards, etc.
Try to avoid implementing cheap tricks to make your code run faster.
Structure of a C Program
Expressions Precedence and Associativity:
An expression is a sequence of operands and operators that reduces to single
value.
Expressions can be
• Simple-contains only one operator.
Ex: 2+5,-a
• Complex- contains more than one operator
Ex: 2*5+7

Precedence and Associativity:


C operator has 2 properties
1.precedence:
When an expression has more than one operator, precedence determines which
operator is executed first.
2. Associativity:
Associativity of operators determines the direction in which an expression is
evaluated.
• Left to right
• Right to left
Operator Associativity

() left–to–right

[]

—>
++(postfix) right–to–left

––(postfix)
++(prefix) right–to–left

––(prefix)

+(unary) – (unary)

!~

(type)

*(indirection)

&(address)
Sizeof

*/% left–to–right

+– left–to–right

<< >> left–to–right

< <= left–to–right

> >=
== != left–to–right

& left–to–right

^ left–to–right

| left–to–right

&& left–to–right

|| left–to–right

?: right–to–left

= right–to–left

+= –=

*= /=

%= &=

^= |=

<<= >>=

,(comma) left–to–right

Fig:operator precedence chart


Evaluating expressions:
I)expression without side effects:
Ex: Evaluate the following expression
a*4+b/2-c*b where a=3,b=4,c=5
To evaluate an expression without side effects,follow the simple rules:
Step 1:
• Replace the variables by their values.
3*4+4/2-5*4
Step 2:
• Evaluate the highest precedence operators and replace them with the
resulting value.
• In the above expression,the operators with the highest precedence are
the multiply and divide.we therefore evaluate them first from the left
and replace them with the resulting values.
(3*4)+(4/2)-(5*4)
12+2-20
Step 3:
• Repeat step2 until the result is a single value.
• In the above example,there is only one more precedence,binary addition and
subtraction.After they evaluated,the final result is -6.
II)expression with side effects:
Ex: Evaluate the following expression
--a*(3+b)/2-c++*b where a=3,b=4,c=5
Step1:
Calculate the value of the parenthesized expression (3+b)
--a*7/2-c++*b
Step2:
Evaluate the postfix expression (c++) next
--a*7/2-5*b
Step3:
Evaluate the prefix expression
2*7/2-5*b
Step4:
The multiply and division are now evaluated
14/2-5*b
7-5*4
7-20
Step5:
The last step is to evaluate the subtraction
7-20
-13
After the side effects the variables have
A=2 b=4 c=6
Example program that evaluates expressions:
#include<stdio.h>
#include<conio.h>
main()
{
int a=3,b=4,c=5,x,y;
clrscr();
printf(“initial values of the variables is a=%d\t b=%d\t c=%d\n”,a,b,c);
x=a*4+b/2-c*b;
printf(“value of a*4+b/2-c*b is %d\n”,x);
y=--a*(3+b)/2-c++*b;
printf(“value of –-a*(3+b)/2-c++*b is %d\n”,y);
printf(“values of variables now are a=%d\t b=%d\t c=%d\t”,a,b,c);
getch();
}
Output:
Intial values of the variables is a=3 b=4 c=5
Value of a*4+b/2-c*b is -6
Value of –a*(3+b)/2-c++*b is -13
Values of variables now are a=2 b=4 c=6
Type conversion and type casting:
Type conversion or type casting refers to changing a variable of one data type into
another.
Implicit type conversion:
• Type conversion is done implicitly by the compiler.It is also known as
automatic type conversion.
• Type conversion is done when the expression has variables of different data
types.
• The hierarchy of datatype are

Ex:
Float x;
Int y=3;
X=y;
Output:
X=3.0000000
This is also known as promotion(when lower level datatype is promoted to
higher level)
Example program for implicit type conversion:
#include<stdio.h>
#include<conio.h>
main()
{
float x=90.99;
char ch=’a’;
int i=95;
i=x;
printf(“I value is %d\n”,i);
x=i;
printf(“x value is %f\n”,x);
i=ch;
printf(“i value is %d\n”,i);
getch();
}
Disadvantages of implicit type conversion:
• In implicit type conversion,while converting the data is lost sometimes.
Ex:
• When a float value is converted to an integer value,the fractional part is
truncatenated.
• When a double value is converted to float value,rounding of digits is done.
• When a longint is converted into integer,the excess higher order bits are
removed.
All these changes may cause incorrect results.
Type casting/explicit type casting:
• Type casting is done explicitly by the programmer.
• Type casting is also known as forced conversion.
• It is done when the value of a higher datatype has to be converted into the
value of a lower data type.
Syntax:
(datatype)expression;
Ex; int a=70,b=20;
float res;
res=(float)a/b;
Example program for explicit type conversion:
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
float avg;
clrscr();
printf(“enter 2 integer values\n”);
scanf(“%d%d”,&a,&b);
avg=(a+b)/2;
printf(“the average before type casting is %f\n”,avg);
avg=(float)(a+b)/2;
printf(“the average after type casting is %f\n”,avg);
getch();
}
Command line arguments:
We are giving the arguments in the command line ie.,without using the input
function(i.e.,scanf)
All the arguments passed in the command line should be passed through main()
Syntax:
main(int argc,char *argv[])
here
argc-argument count which is always a integer value.
argv-argument value which is character data type.
Example program:
main(int argc,char *argv[])
{
printf(“%d”,argc);
printf(“%s”,argv[0]);
printf(“%s”,argv[1]);

You might also like