programming in c,Overview of C,History, Importance ,Sample Programs ,Basic Structure, Programming Style , Executing ,Unix System , MS-DOS System ,Constants, Variables, and Data Types: Character Set , C Token ,Keyword and Identifiers ,Constants ,Variables ,Data Types ,Declaration of Storage Class ,Assigning Values to Variables ,Defining Symbolic Constants ,Declaration, Overflow and Underflow of Data ,Operators and Expressions: Arithmetic, Relational, Logical, Assignment, Increment and Decrement, Conditional, Bitwise, Special Operators ,Arithmetic Expressions, Evaluation of Expressions ,Precedence of Arithmetic Operators ,Some Computational Problems ,Type Conversions in Expressions ,Operator Precedence and Associativity ,Mathematical Functions,programming in c,simple c concepts,thiruvalluvar university c syllabus,c syllabus,c notes
2. • Overview of C: History – Importance – Sample
Programs – Basic Structure – Programming Style –
Executing – Unix System – MS-DOS System -
Constants, Variables, and Data Types: Character Set
– C Token – Keyword and Identifiers – Constants –
Variables – Data Types – Declaration of Storage Class –
Assigning Values to Variables – Defining Symbolic
Constants – Declaration – Overflow and Underflow of
Data - Operators and Expressions: Arithmetic,
Relational, Logical, Assignment, Increment and
Decrement, Conditional, Bitwise, Special Operators –
Arithmetic Expressions, Evaluation of Expressions –
Precedence of Arithmetic Operators – Some
Computational Problems – Type Conversions in
Expressions – Operator Precedence and Associativity –
Mathematical Functions .
3. INTRODUCTION C
• C is one of the most popular computer language
today because it is a structured, high-level,
machine independent language.
• C is general-purpose procedural programming
language developed by Dennis Ritchie at AT&T’s
Bell laboratories in 1972.
• It is a high-level programming language.
However, often referred as a middle-level
programming language.
4. INTRODUCTION C
• since it provides rich support to low-level
programming constructs.
• C is also called as the mother of all programming
languages.
• Because most of the modern computer
programming languages directly or indirectly
influenced from C (such as C++, Java, C#, PHP,
Perl, and JavaScript etc.).
5. ALGOL
• Algol programming language
• Algol is a computer programming language
• Developed in 1958.
• Named for the algorithmic process of
definition of a programming problem.
• Short for Algorithmic Language.
• Uses words to bracket blocks and was the first
to use begin end pairs.
6. BCPL
• Full form: Basic Combined Programming
Language.
• Developed in 1996.
• Developed by Martin Richards
• Its having high portability.
• BCPL is the successor to the CPL
programming language.
• Type less system programming language.
7. B
• Developed in 1969
• Designed by D. M. Ritchie and K. L.
Thompson
• Developed at Bell Labs
• B was derived from BCPL
• Designed for primarily non-numeric
applications.
• Type less system programming language.
8. Traditional C
• C was evolved from ALGOL, BCPL and B by
dennis ritchie at the Bell Laboratories in 1972.
• C uses many concepts from these languages
and added the concept of data types and other
powerful features.
9. K & R C
• The language became more popular after
publication of the book “The C Programming
Language” by Brian Kerningham and Dennis
Titchie in 1978.
• The book was so popular that the language
came to be known as K &R C
10. ANSI C / ISO
• To assure that the C language remains standard, in
1983, American National Standards
Institute(ANSI) appointed a technical committed
to define a standard for C.
• The committee approved a version of C in
December 1989 which is known as ANSI C.
• It was then approved by ISO in 1990 which is
known as ISO C.
11. C99
• C99 is improved version of c(C++ and Java)
12.
13. IMPORTANCE OF C
• It is a robust language whose rich set of build in
functions and operators can be used to write any
complex program.
• Rich set of built-in functions
• Operators can be used to write any complex program.
• The C compiler combines the capabilities of an
assembly language with the features of a high-level
language.
14. IMPORTANCE OF C
• It is well sited for writing both system software
and business packages.
• Program written in C are efficient and fast Due
to variety of data types and powerful operators
programs written in C are efficient and fast.
• There are only 32 keywords in C and its
strength lies in its built in functions.
15. IMPORTANCE OF C
• Ability to extend itself.
• A C program is basically a collection of function that are
supported by the C library.
• C is a Structured Programming Language (requiring the
user to think of a problems in terms of function modules or
blocks).
• C is highly portable. This means that C program written for
one computer can be run on another with little or no
modification.
16. SAMPLE PROGRAM
• main() //main function
• { /*…….printing begins……..*/
• printf(“I see, I remember”);
• /*……printing end…………*/ }
18. SAMPLE PROGRAM
• Stdio.h is a header file which is included in our
program by writing
• #include<stdio.h> in first line of program.
• main() is function where execution begins.
• Every program must have exactly one main
function.
• “{” opening brace & “}” closing brace.
• printf(“ ”); is the only executable instruction.
• C is case sensitive.
19. SAMPLE PROGRAM
• The main() is a part of every C program.
• C permits different forms of main statements
• main()
• int main()
• void main()
• main(void)
• void main(void)
• int main(void)
20. SAMPLE PROGRAM
• The main() is a part of every C program.
• C permits different forms of main statements
• main()
• int main()
• void main()
• main(void)
• void main(void)
• int main(void)
22. BASIC STRUCTURE
Documentation Section
• This section consists of comment lines which include
the name of programmer, the author and other details
like time and date of writing the program.
• Documentation section helps anyone to get an overview
of the program.
Link Section
• The link section consists of the header files of the
functions that are used in the program. It provides
instructions to the compiler to link functions from the
system library.
23. BASIC STRUCTURE
Definition Section
• All the symbolic constants are written in
definition section. Macros are known as symbolic
constants.
Global Declaration Section
• The global variables that can be used anywhere in
the program are declared in global declaration
section. This section also declares the user
defined functions.
24. BASIC STRUCTURE
main() Function Section
• It is necessary have one main() function section in every C program.
This section contains two parts, declaration and executable part.
• The declaration part declares all the variables that are used in
executable part. These two parts must be written in between the
opening and closing braces.
• Each statement in the declaration and executable part must end with
a semicolon (;). The execution of program starts at opening braces
and ends at closing braces.
Subprogram Section
• The subprogram section contains all the user defined functions that
are used to perform a specific task. These user defined functions are
called in the main() function.
25. PROGRAMMING STYLE
• C program statement are written in lower case letter
uppercase letters are used for symbolic constant.
• braces group program statement together and mark the
beginning of the end of the function.
• proper identification of braces and statement would
make a program easier to read and debug.
• Command lines are very important for debugging and
testing the program.
26. • C is a free-form language we can group
together on one line.
• Ex:
– a=b;
– x=y+1;
– z=a+x;
• Can be written on one line
– a=b; x=y+1; z=a+x;
27. – main()
– {
– printf(“CS”);
– }
• May be written in one line
• main(){printf(“CS”);}
• However this style make the program more
difficult to understand and should not be used.
28. EXECUTING
• 1.Creating the program.
• 2.Compiling the program.
• 3.Linking the program with functions that are
needed from the C library.
• 4.Executing the program.
– Operating system is a program that controls the entire
operation in computer system.
– The operating system which is interface between the
hardware and user, handles the execution of user
programs.
29.
30. TRUE OR FALSE:
• (a) Any valid printable ANSII character can be used in an identifier.
• (b) All variables must be given a type when they are declared.
• (c) Declarations can appear anywhere in a program.
• (d) ANSI C treats the variable name and Name to be same.
• (e) The underscore can be used anywhere in an identifier.
• (f) The keyword void is a data type in C.
• (g) Floating point data constants, by default, denote float type values.
• (h) Like variables, constants have a type.
• (i) Character constants are coded using double quotes.
• (j) Initialization is the process of assigning a value to a variable at
the time of declaration.
• (k) All static variables are automatically initialized to zero.
• (l) The scanf function can be used to read only one value at a time.
31. CHARACTER SET
• The characters that can be used to form words,
numbers and expressions depend upon the
computer on which the program is run.
• The characters in C are grouped into the
following categories:
– Letters
– Digits
– Special Characters
– White Spaces
32. CHARACTER SET
• The compiler ignores white spaces unless they
are a part of string constant.
• White spaces may be used to separate words,
but are prohibited between the characters of
keywords and identifiers.
34. CHARACTER SET
• ~ tilde
• % percent sign
• | vertical bar
• @ at symbol
• + plus sign
• < less than
• _ underscore
• - minus sign
• > greater than
• ^ caret
• # number sign
• = equal to
• & ampersand
• $ dollar sign
35. CHARACTER SET
•
• / slash
• ( left parenthesis
• * asterisk
• back slash
• ) rightparenthesis
• ′ apostrophe
• : colon
• [ left bracket
• " quotation mark
• ; semicolon
• ] right bracket
• ! exclamation mark
• , comma
• { left flower brace
• ? Question mark
• . dot operator
• } right flower brace
36. CHARACTER SET
• b blank space
• t horizontal tab
• v vertical tab
• r carriage return
• f form feed
• n new line
• Back slash
• ’ Single quote
• " Double quote
• ? Questionmark
• 0 Null
• a Alarm (bell)
37. TRIGRAPH CHARCTERS
• Some of the characters like {}, [], , |, ~ and ^ are
missing in the above keyboard. Hence practically it
may not be possible to write a C program using this
keyboard.
• To solve this problem C suggested to use combination
of 3 characters to produce a single character called
trigraph character.
• A trigraph is a sequence of three characters, the first
two of which are question marks
• C supports the following 9 trigraph characters.
41. KEYWORD
• Every C word is classified as either a keyword
and identifier.
• All keywords have fixed meaning and these
meaning cannot be changed.
• Keywords serve as basic building blocks for
program statements.
• 32 keywords.
43. IDENTIFIERs
• Identifiers refer to the names of variables, functions and
arrays.
• These are user-defined names and consist of a sequence
of letters and digits, with a letter as a first character.
• Both uppercase and lowercase letters are permitted.
• The underscore character is also permitted.(_)it is link
between two words.
45. CONSTANTS
• Constant in C refer to fixed values do not
change during the execution of a program.
46. INTEGER CONSTANT
• IT IS REFERS TO A SEQUNCE OF DIGITS
• THERE ARE THREE TYPES :-
– (1) DECIMAL INTEGER
– (2) OCTAL INTEGER
– (3) HEXADECIMAL INTEGER
47. DECIMAL INTEGERS
• Decimal integers consist of a set of digits, o
through 9, preceded by an optional - or + sign.
• Ex= 123 -321 0 654321 +78
• Embedded spaces, commas, and non digit
characters are not permitted between digts.
• Ex= 15 750 20000 $1000
48. OCTAL AND HEXA DECIMAL
• An octal integer constant consist of any combination of
digits from the set 0 through 7, with a leading 0.
• Ex = 037 0 0435 o551 •
• a sequence of digits preceded by 0x is considerd as
hexadecimal integer.
• They may also include alphabets a through f or a
through f represent the numbers 10 through 15…
• EX = 0X2 OX9F oxbcd 0x
• we rarely use octal and hexadecimal numbers in
programming
49. REAL CONSTANT
• Integer number are inadequate to represent
quantites that very continuously ,such as as
distances,heights,tempertures,prices and so
on..Such numbers are called real constants..
• Ex= 0.0083 -o.75 435.36 +247.0
• the mantissa is either a real number expressed in
exponential notation an integer.
• The letter separating the mantissa and the
exponent can be written in either lower case or
upper case .
• Ex= 0.65e4 12e-2 1.5e+
50. • Example of numeric consatant
• Constant valid?? Remarks
• 698354L yes represent long integer
• 25,00 no comma is not allowed
• +5.0E3 yes (ANSI C supports unary
plus)
51. SINGLE CHARACTER CONSTANT
• A single character constant cotains a single character
enclosed within a pair of single quote marks .
• Ex= ‘5’ ‘x’ ‘;’ ‘ ’
• Note that the character constant 5 is not the same as the
number 5
• Printf(“%d”, ’a’ );
• Would print the number 9, the ascii value of the letter a.
• Since each character constant represents an integer
value it is also possible perform arithmetic operations
52. STRING CONSTANT
• A sting constant is a sequence of characters
enclosed in double quotes.
• The character may be letters , numbers, special
character and blank space are
• ex =“hello!” “1987 “
• Remember that a constant is not equivalent to
the single string constant .
• Character strings are often used in programs to
build meaningful programs
53. BACKSLASH CHARACTER
• C supports some special backslash character
that are used in output function .
• For example ,the symbol ‘n’ stands for newline
character.
• Note that each one of them represents one
character , although they consist of two
characters.
• these characters combinations are know as
escape sequence
54. VARIABLES
• A variable is a data name that may be used to
store data value.
• Unlike constant that remain unchanged during the
execution of a program ,a variable take different
values at different times during execution.
• a variable name can be chosen by the programmer
in a meaningful way so as to reflect its function or
nature in the program ..
• some example
average counter_1 height class_strength total
55. VARIABLES
• As mentioned earlier ,variable , names may consist of
letters ,digits and the underscore (_) character , subject
to the following conditions;
• (1) They must begin with a letter .some systems permit
underscore as the first character.
• (2) ANSI standard recognizes a length should not be
normally more than eight characters since only the first
eight characters are treated as significant by many
compilers
• (3) Uppercase and lowercase are significant. That is
,the variable Total is not the same as total or TOTAL
56. VARIABLES
• (4) It should not be a keyword.
• (5) White space is not allowed
• Examples of variable names
• variable name valid ?? Remark
• first_tag valid
• char not valid chair is a keyword
• Int _type valid
57. VARIABLES
• If only the first eight characters are recognized by
a compiler ,then the two names
– average_height
– average_weight
• mean the same thing to the computer such
names can be rewritten as
– avg_height and
– avg_weight
– ht_average and
– wt_average
• With out changing their meanings
59. DATA TYPES
• char: The most basic data type in C. It stores a
single character and requires a single byte of
memory in almost all compilers.
• int: As the name suggests, an int variable is used
to store an integer.
• float: It is used to store decimal numbers
(numbers with floating point value) with single
precision.
• double: It is used to store decimal numbers
(numbers with floating point value) with double
precision.
64. VOID
• The void type has no values.
• This is usually used to specify the type of
functions.
• The type of function is said to be void when it
does not return any value to the calling
function.
65. DECLARATION OF VARIALE
• It tells the compiler what the variable name is.
• It specifies what type of data the variable will
hold.
Syntax: Datatype v1,v2……vn;
Example: int count;
double ratio;
66. USER DEFINED DATA TYPE
• typedef
• Typedef type identifier;
• Ex: typedef int count;
• Count b1,b2;
• Enum
• Enum identifier {value1…..valuen};
67. TYPEDEF
• In c, it is possible to redefine the built-in as well as user defined data
types. This task is accomplished by the typedef statement.
• Syntax typedef type new_type;
• where typedef is keyword, type is either built-in data type or user-
defined data type, and new_type is the new name of the type.
• Example: - typedef float real;
• This statement redefines the data types float to real. Then to declare
x,y and z of type float, we can also write
• Real x,y,z;
• The compiler will still recognize the statement
• float x,y,z; as correct.
68. ENUM
• Enumerated Data Type ( enum )
• An Enumerated data type consists of an ordered
set of distinct constant values defined in a data
type in a program.
• The format of en-um is:-
• enum name {value-1,value-2,value-3,…….,value-
4};
• where., name is the name of the enumerated data
type, also known as tag and value-1,value-
2,value-3,…….,value-n are values that variable of
type name can take.
69. ENUM
• Example : -
• Part-1 enum days{ mon,tue,wed,thu,fri,sat,sun; };
Part-2 days holiday,wdays;
• The first part defines an enumeration named days
.
• The second part declares the variables holiday
and wdays to be enumeration variable of type
days.
• Thus each variable can be assigned any one of the
constants mon,tue,wed,thu,fri,sat,sun.
70. ENUM
• Enumeration constants are automatically assigned
equivalent integer values, beginning with 0 for the first
constant, with each successive constant increasing by 1.
• Therefore in example-1, the enumeration constants will
represent the following integer values: -
– Mon 0
– Tue 1
– Wed 2
– Thu 3
– Fri 4
– Sat 5
– Sun 6
days holiday=mon,wdays=wed;
71. DECLARATION OF STORAGE CLASS
#include<stdio.h>
int number; // global variable
void main()
{
number = 10;
printf("I am in main function. My value is %dn", number);
fun1();
}
fun1()
{ number = 20;
printf("I am in function fun1. My value is %d", number); }
72. DECLARATION OF STORAGE
CLASS
• auto – local variable known only to the
function in which it is declared.(Default).
• static – local variable which exists and retains
its value even after the control is transferred to
the calling function.
• extern- global variable known to all functions
in the file.
• register- local variable which is stored in the
register.
73. ASSIGNMENT STATMENT
• Value=amount + inrate * amount;
• The result of (amount+inrate*amount) calculation
stored in value variable.
• The variable value is called target variable.
• All variable declared for their type, the variable
used in expression(on the right sideof
equal(=)sign of the computational statement)must
be assigned values before use in program.
75. READING DATA FROM KEYBOARD
• Syntax of scanf function is
• scanf (“control string”, &variable);
• The format string must be a text enclosed in
double quotes. It contains the information for
interpreting the entire data for connecting it
into internal representation in memory.
• Example: scanf(“%d”,&number);
76. SYBOLIC CONSTANT
• Two problem
– Problem in modification of program.
– Problem in understanding the program.
• Modifiability
– Change the value Pi=3.142 to 3.14159 for accuracy but
left unchanged the program disastrous output.
• Understandability
– When same value means different things in different
places.example number of students.
77. SYBOLIC CONSTANT
• Syntax
#define symbolic_name value_of_constant
– #define printf print
#define MAX 50
#define TRUE 1
#define FALSE 0
#define SIZE 15
• Symbolic names are sometimes called
constant identifiers.
78. RULES FOR SYM.CONST.,
• The rules for constructing the name part are the same as that for
constructing identifiers. However, typically symbolic names are
written in uppercase letters
• # must be the first character in the line
• No blank space between # and define
• #define statements can appear anywhere within the program but
before the symbolic constant is referenced in the program. Normally
they are placed at the beginning of the program
• A blank space between #define, symbolic name and constant.
• #define statements must not end with a semicolon, since they are
preprocessor directives (like #include) and not executable C
statements
• Symbolic constant names are NOT declared for data types.
• No assignment statement needed (=).
79. DECLARING - CONSTANT
• Variables can be declared as constants by using
the “const” keyword before the datatype of the
variable.
• The constant variables can be initialized once
only. The default value of constant variables
are zero.
• const int a=12;
80. DECLARING - VOLATILE
• volatile int a;
• The volatile keyword tells the compiler that
the value of the variable may change at any
time as a result of external conditions.
• Value may change at any time.
81. OVERFLOW AND UNDERFLOW
• Assigning a value which is more than its upper
limit is called overflow and less than its lower
limit is called underflow.
• C does not provide any warning or indication of
overflow. it produce incorrect results.
• In case of integer types overflow results wrapping
towards negative side and underflow results
wrapping towards positive.
82. OPERATORS
• An operator is a symbol that tells the computer
to perform certain mathematical or logical
manipulations.
• These operators are used in programs to
manipulate data and variables.
84. ARITHMETIC OPERATORS
• Arithmetic operators are used to perform
numerical calculations among the values.
OPERATOR MEANING
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
85. EXAMPLE
#include <stdio.h>
int main()
{
int a=14,b=4;
printf("ARITHMETIC OPERATORn");
printf("ADDITION:%dn",a+b);
printf("SUBTRACTION:%dn",a-b);
printf("MULTIPLICATION:%dn",a*b);
printf("DIVISION:%dn",a/b);
printf("MODULO DIVISION:%d",a%b);
return 0;
}
87. INTEGER ARITHMETIC
• Both operands in a single arithmetic expression
such as a+b are integers, the expression called an
integer expression, and the operation is called
integer arithmetic.
• Example a=14 b=4
a-b = 10
a+b = 18
a*b =56
a/b = 3(decimal part truncated)
a%b = 2
88. REAL ARITHMETIC
• An arithmetic operation involving only real
operands is called real arithmetic.
• Example :
• X=6.0/7.0=0.857143
• Y=1.0/3.0=0.333333
• Z=-20/3.0=-0.666667
89. MIXED MODE ARITHMETIC
• When one of the operands is real and the other
is integer, the expression is called a mixed
mode arithmetic expression.
• Example: 15/10.0=1.5
90. RELATIONAL OPERATORS
• Relational Operators are used to compare two
quantities and take certain decision depending
on their relation.
• If the specified relation is true it returns one.
• If the specified relation is false it returns zero.
91. RELATIONAL OPERATORS
OPERATOR MEANING
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal to
== Is equal to
!= Is not equal
92. EXAMPLE
#include <stdio.h>
int main()
{
int a=14,b=4;
printf("RELATIONAL OPERATORn");
printf("LESS THAN:%dn",a<b);
printf("GREATER THAN:%dn",a>b);
printf("LESS THAN EQUAL TO:%dn",a<=b); printf("GREATER
THAN EQUAL TO:%dn",a>=b); printf("EQUAL
TO:%dn",a==b);
printf("NOT EQUAL TO:%dn",a!=b);
return 0;
}
94. LOGICAL OPERATORS
• these operators are used for testing more
than one condition and making decisions.
• 'c' has three logical operators they are:
OPERATOR MEANING
&& Logical AND
|| Logical OR
! Logical NOT
95. EXAMPLE
#include <stdio.h>
int main()
{
int a=14,b=4;
printf("LOGICAL OPERATORn");
printf("LOGICAL AND:%dn",(a>b)&&(a==b));
printf("LOGICAL OR:%dn",(a>b)||(a==b));
printf("LOGICAL NOT:%d",!(a==b));
return 0;
}
97. ASSIGNMENT OPERATORS
• These operators are used for assigning the result of an
expression to a variable.
• b=a;
OPERATORS:
==
+=
-=
*= short hand asssignment OPERATOR
/=
%=
100. Increment and decrement operators
• Two most useful operators which are present in 'c'
are increment and decrement operators.
• Operators: ++ and –
m++; or ++m
m--; or –m;
• The operator ++ adds one to the operand The
operator -- subtracts one from the operand.
• Both are unary operators and can be used as pre
or post increment/decrement.
101. Increment and decrement operators
• Increment and decrement operators are unary operators and they
require variable as their operands.
• When postfix ++(or--)is used with a variable in an expression, the
expression is evaluated first using the original value of the variable
and then the variable is incremented (or decremented) by one.
• When prefix ++ (or--)is used an expression, the variable is
incremented(or decremented)first and then the expression is
evaluated using the new value of the variable.
• The precedence and associatively of ++ and – operators are the same
as those of unary + and unary -.
102. EXAMPLE
#include <stdio.h>
int main()
{
int a=14,b=4,c;
printf("INCREMENT/DECREMENT OPERATORn");
printf("BEFORE INCREMENT:%dn",a);
c=a++;
printf("AFTER INCREMENT:%dn",a);
printf("BEFORE DECREMENT:%dn",b);
c=b--;
printf("AFTER DECREMENT:%dn",b);
return 0;
}
104. CONDITIONAL OPERATORS
• These conditional operator are used to
construct conditional expressions of the form.
• Syntax: exp1?exp2:exp3
• where exp1,exp2,exp3 are expressions.
• Operator: ?: (ternary operator)
107. BITWISE OPERATORS
• These operators works on bit level
• Applied to Integers only
OPERATOR MEANING
& Bitwise AND
| Bitwise OR
^ Bitwise Exclusive OR
<< Shift Left
>> Shift Right
108. SPECIAL OPERATORS
• special operators such as comma operator,
sizeof operator.
• Comma operator: the comma operator is used
to combine related expressions.
• A comma linked list of expressions are
evaluated left to right and the value of right
most expression is the value of combined
expression..
• Example: value=(x=10, y=5, x+y);
109. SPECIAL OPERATORS
• Sizeof Operator: Sizeof is an operator used to
return the number of bytes the operand
occupies.
• Syntax: m=sizeof(sum);
• k=sizeof(2351);
110. ARITHMETIC EXPRESSIONS
• An arithmetic expression is a combination of
variable, constants, and operators arranged as
per the syntax of the language.
• No operator for exponentiation.
• C can handle any complex mathematical
expressions.
112. EVALUATION OF EXPRESSIONS
• Expressions are eveluated using an assignment
statement of the form
Varaiable =expression;
• Variable is any valid C variable name.
• When the statement is executed the expression is
evaluated first and the result then replaces the
previous value of the variable of the left hand
side.
• All variable used in the expression must be
assigned values before evaluation is attempted.
113. EXAMPLE
• x=a*b-c;
• Y=b/c*a;
• Z=a-b/c+d;
– The blank spaces around an operator is optional
and adds only to improve readability.
114. Precedence of Arithmetic Operators
• an arithmetic expression without parentheses
will be evaluated from left to right using the
rules of precedence of operators.
• There are two distinct priority levels of
arithmetic operators in C:
High priority * / %
Low priority +-
115. Precedence of Arithmetic Operators
• The basic evaluation procedure includes ‘two’
left-to-right passes through the expression.
• During the irst pass, the high priority operators
are applied as they are encountered.
• During the second pass, the low priority
operators are applied as they are encountered.
116. EXAMPLE
• a=9,b=12 and c=3
x=a-b/3+c*2-1
• Becomes x=9-12/3+3*2-1
Evaluation
First pass
• Step 1: x=9-4+3*2-1
• Step 2:x=9-4+6-1
Second pass
• Step 3:x=5+6-1
• Step 4:x=11-1
• Step 5:x=10
119. Some Computational Problems
• When expressions include real values, then it is important to
take necessary precautions to guard against certain
computational errors.
• Computer gives approximation values for real numbers and
the errors due to such approximations may lead to serious
problems.
a=1.0/3.0
b=a*3.0
• Answer is 1 but there is no guarantee that the value of b
computed in a program will equal 1.
• Divide by zero
• Overflow and underflow
120. Type Conversions in Expressions
Implicit type conversion
• C performs automatic conversions of type in
order to evaluate the expression. This is called
implicit type conversion.
• For example, if we have an integer data type
value and a double data type value in an
expression then C will automatically convert
integer type value to double in order to
evaluate the expression.
121. Type Conversions in Expressions
Explicit type conversion
• In explicit type conversion we decide what
type we want to convert the expression.
• Syntax of explicit type conversion is:
• (type) expression
• Where, type is any of the type we want to
convert the expression into.
122. Example
• #include <stdio.h>
• int main(void)
• {
• float x = 24.5, y = 7.2;
• int result = (int) x / (int) y;
• printf("Result = %dn", result);
• Printf("End of coden");
• return 0;
• }