KEMBAR78
Module 4 - 2 Operators and IO Statements | PDF | Computer Engineering | Computer Programming
0% found this document useful (0 votes)
8 views49 pages

Module 4 - 2 Operators and IO Statements

Uploaded by

2301710
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 views49 pages

Module 4 - 2 Operators and IO Statements

Uploaded by

2301710
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/ 49

CMPRG

C Programming

Operators and
I/O Statements
Learning Objectives:
At the end of the session, students should be
able to:
1. Enumerate the operators in C
2. To use operators in C
Operators in C Language

C has a large number of built in operators. An


operator is a symbol which acts on operands to
produce certain result as output. The data
items on which operators act upon are called
„operands‟ For example in the expression a+b;
+ is an operator, a and b are operands. The
operators are fundamental to any mathematical
computations.
Operators in C Language

C language is rich in built-in operators and


provides the following types of operators:

A. Arithmetic Operators
B. Relational Operators
C. Logical Operators
D. The sizeof Operator
E. Assignment Operators
Arithmetic Operators

Arithmetic operators are used to perform


numerical operators in C.
They are divided into two classes namely,
Unary and Binary arithmetic operators.
Unary operators:
Operators Action
- Unary minus
++ Increment
-- Decrement
Arithmetic Operators
Binary operators:
Operator Action
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus Division
Unary Minus ( - )
This symbol is used to indicate the negative sign of the value.
For example:
a = -10; b = -a;
Here the value -10 is assigned to a and the value-a to b.
Increment and Decrement Operator

The increment operator ( + + ) adds 1 to its operand


and decrement operator ( - - ) subtracts one from its
operand.

To be precise:
y = y + 1 is written as y = y + +
These are two types of increment and decrement
operators:
a. Prefix increment (++i)
b. Postfix increment (i++)
c. Prefix decrement (- -i)
d. Postfix decrement (i - -)
Increment and Decrement Operator

In the prefix increment, first increment and


then do operation. In postfix increment, first do
the operation and then increment. In prefix
decrement first decrement and then do
operation. In postfix decrement, first do the
operation and then decrement.
Relational Operator
The following table shows all the relational operators supported
by C. Assume variable A holds 10 and variable B holds 20,
then:
Operator Description Example
== equal to (A == B) is
Checks if the values of two operands are not true.
equal or not. If yes, then the condition
becomes true.
!= not equal to (A != B) is
Checks if the values of two operands are true.
equal or not. If the values are not equal,
then the condition becomes true.
Relational Operator
Operator Description Example
> Greater than (A > B) is not
Checks if the value of left operand is greater than the true.
value of right operand. If yes, then the condition becomes
true.
< Less than (A < B) is true.
Checks if the value of left operand is less than the value
of right operand. If yes, then the condition becomes true.

>= Greater than or equal to (A >= B) is not


Checks if the value of left operand is greater than or equal true.
to the value of right operand. If yes, then the condition
becomes true.
<= Less than or equal to (A <= B) is true.
Checks if the value of left operand is less than or equal to
the value of right operand. If yes, then the condition
becomes true.
Logical Operator

C has three types of logical operators.

Operator Action
&& and
|| or
! not
Logical Operator

In logical operation the operands are


themselves logical. The individual logical
expression is compared, using logical
operators. The result evaluates which is
represented by integer values 0 or 1. The
logical AND evaluates to true condition only if
both of the operands are true. Logical OR
evaluates to true condition if any one of the
condition is true. The logical expression can
be changed from false to true or from true to
false with negation operator.
Logical Operator

Logical AND ( && )

Expression Interpretation Value


false && false false 0
false && true false 0
true && false false 0
true && true true 1
Logical Operator

Logical OR ( | | )
Expression Interpretation Value
false || false false 0
false || true true 1
true || false true 1
true || true true 1

Logical Negation Operator ( ! )


Expression Interpretation Value
! (true) false 0
! (false) true 1
Logical Operator
The sizeof Operator
The sizeof operator is one of the special operator. This operator
returns the size of its operand in bytes. The sizeof operator always
precedes its operand.
This is as shown in the example:
Let int i ; float b; double d; char c; be the variables:
printf(“integer requires: %d bytes \n”, sizeof i) ;
printf(“float requires: %d bytes \n”, sizeof b) ;
printf(“double requires: %d bytes \n”, sizeof d) ;
printf(“character requires: %d byte \n”, sizeof c) ;
Output:
Integer requires : 2 bytes
Float requires : 4 bytes
Double requires : 8 bytes
Character requires : 1 byte
Assignment Operator
Operator Description Example
Simple assignment operator. Assigns C = A + B
=
values from right side operands to left will assign
side operand. the value of
A + B to C
Add AND assignment operator. It adds the C += A is
+=
right operand to the left operand and equivalent
assigns the result to the left operand. to C = C +
A
Subtract AND assignment operator. It C -= A is
-=
subtracts the right operand from the left equivalent
operand and assigns the result to the left to C = C - A
operand.
Assignment Operator
Operator Description Example
Multiply AND assignment operator. It C *= A is
*=
multiplies the right operand with the left equivalent
operand and assigns the result to the left to C = C * A
operand.
Divide AND assignment operator. It C /= A is
/=
divides the left operand with the right equivalent
operand and assigns the result to the left to C = C / A
operand.
Modulus AND assignment operator. It C %= A is
%=
takes modulus using two operands and equivalent
assigns the result to the left operand. to C = C %
A
Precedence of Operator

Operator precedence determines the grouping


of terms in an expression and decides how an
expression is evaluated. Certain operators
have higher precedence than others; for
example, the multiplication operator has a
higher precedence than the addition operator.
Precedence of Operator
Operator category Operators Associativity

unary operators - ++ ! sizeof (type) Right to Left


arithmetic multiply, divide and */% Left to Right
remainder

arithmetic add and subtract +- Left to Right


relational operators < <= > >= Left to Right
equality operators != Left to Right
logical and && Left to Right
logical or || Left to Right
assignment operators = += -= *= /= %= Right to Left
I/O Statements
Learning Objectives:
At the end of the session, students should be
able to:
1. Identify the formatted and unformatted I/O
functions
2. Write simple input and output statements
Input / Output Statements

In C language input and output functions are


accomplished through library function. The
header file for I/O function is <stdio.h> In C
there are two types of I/O functions. They are
console I/O and file I/O. The console Input /
Output functions are also called as Standard
input / output functions.
Input / Output Statements
Formatted I/O Functions

Formatted I/O means reading and writing data


in formats which are desired by the user. The
function used to input action is scanf() and
output action is printf().
Formatted I/O Functions

The printf() Function

Printf() is used to print or display data on the console


in a formatted form. The format of printf() is printf(
“control string”, list of arguments); Control string
contains the formatting instructions within quotes.

This can contain:


• characters that are simply printed
• conversion specifications with begins with format
specifier (%) sign.
• escape sequences.
Formatted I/O Functions

Arguments values get substituted in the place


of appropriate format specifications.
Arguments can be variables, constants, arrays
or complex expressions. The percentage (%)
followed by conversion character is called
format specifier. This indicates the type of the
corresponding data item.
Formatted I/O Functions
Most frequently used format specifiers:

Format specifier Meaning


% d or % i decimal integers
%u unsigned decimal integer
%x unsigned hexadecimal (lower
case letter)
%X unsigned hexadecimal (upper
case letter)
%o octal
%c character
%f floating point
Formatted I/O Functions
Format specifier Meaning
%s strings
% lf double
% ld long signed integer
% lu long unsigned integer
%p displays pointer
%% prints a % sign
%e scientific notation (e
lower case)
%E scientific notation (e
upper case)
printf() Function
Application of printf() Function
The printf() function is used to print the different types of output.
This is given below.
1. Printing given data
The printf() statement can be used without format specifier, just
to print the given data. Example:
#include <stdio.h>
main()
{
clrscr();
printf(“C programming is easy”);
getch();
}
Output: C programming is easy
printf() Function
2. Printing numbers
To print integers %d or %i is used, to print floating point %f is used.
Example:
#include <stdio.h>
main()
{
int a = 5;
float d = 10.3;
clrscr();
printf(“the value of integer a is %d”, a);
printf(“the value of float d is %f”, d);
getch();
}
printf() Function
Output: The value of integer a is 5
The value of float d is 10.3

In the above instance instead of using two printf()


statements only one printf() can be used.

printf( “the value of integer a is %d \n, the value of float


d is %f”, a,d);

Output: The value of integer a is 5


The value of float d is 10.3
printf() Function
3. Printing character / string data
To print character %c used and to print a string %s format specifier is used.

Example:
#include <stdio.h>
main()
{
clrscr();
printf( “This is %c %s”, „a‟, “book”);
getch();
}

Output: a book

The characters are always given in single quote and strings are enclosed in
quote (double).
Escape Sequence in printf() Function

In addition to format specification, escape


sequence can also be used in printf(). These are
specified in the control string portion of the printf()
and are used mainly for screen formatting. All
escape sequence are provided with slash ( \ ).
Since back slash is considered as an “escape”
character. These sequences are called escape
sequences. These sequences cause an escape
from the normal interpretation of a string. So that
the next character (after blank slash) is recognized
as having a special meaning.
Escape Sequence in printf() Function
Escape Sequence in printf() Function
Formatted I/O Functions
The scanf() Function
The scanf() reads the input data from standard input
device. i.e. keyboard. The general format of the
scanf() function is scanf( “format string”, &list of
arguments ); where format string consists of format
specifiers and arguments consists of address of
variables. To this corresponding address the data
read from keyboard is sent. The address of the
variable is denoted by ampersand symbol „&‟ (it is
called as „‟address of the operator).
scanf() Function
Note that the values that are supplied through keyboard
must be separated by either blank tabs or newlines. Escape
sequences are not included in scanf() function.
To read integer data:
int i ;
scanf(“%d”, &i);

To read floating point data:


float f;
scanf(“%f”, &f);
scanf() Function
To read character data:
char sam, john;
scanf(“%c %c”, &sam, &john);

To read more than one data types at a time:


int i;
float b;
char c;
char s[20];
scanf(“%d %f %c %s”, &i, &b, &c, &s );
Sample I/O statement programs
1. Adding two integer numbers.
#include <stdio.h>
main()
{
int num1, num2, sum;
clrscr();
printf( “Enter the value of num1:”);
scanf(“%d”, &num1);
printf( “\nEnter the value of num2:”);
scanf(“%d”, &num2);
sum = num1 + num2;
printf( “\nThe sum of %d and %d is %d”, num1,num2,sum);
getch();
}
Sample I/O statement programs
2. Fahrenheit scale to Celsius scale
#include <stdio.h>
main()
{
float fahr, celsius;
clrscr();
printf( “Enter Fahrenheit value:”);
scanf(“%f”, &fahr);
celsius = ( fahr - 32.0 ) / 1.8;
printf( “\n Fahrenheit %.2f is %.2f degree Celsius”, fahr,
celsius);
getch();
}
Sample I/O statement programs
3. Display the input characters
#include <stdio.h>
main()
{
char name[20];
clrscr();
printf( “Enter your name:”);
scanf(“%s”, &name);
printf( “\n Your name is: %s”, name);
getch();
}
Unformatted I/O Functions

A simple reading of data from keyboard and


writing to I/O device, without any format is
called unformatted I/O functions. This is
classified as string I/O and character I/O.
Unformatted I/O Functions

Character Input/Output(I/O)
In order to read and output a single character,
character I/O functions are used.
The functions under character I / O are:
• getchar()
• putchar()

String Input/Output(I/O)
• gets()
• puts()
Character Input/Output(I/O)

The getchar() and putchar() Functions

The int getchar(void) function reads the next available


character from the screen and returns it as an integer. This
function reads only single character at a time. You can use
this method in the loop in case you want to read more than
one character from the screen. The int putchar(int c)
function puts the passed character on the screen and
returns the same character. This function puts only single
character at a time. You can use this method in the loop in
case you want to display more than one character on the
screen.
Character Input/Output(I/O)
#include <stdio.h>
main( )
{
int c;
printf( "Enter a value :");
c = getchar();
printf( "\nYou entered: ");
putchar( c );
return 0;
}
When the above code is compiled and executed, it waits for you to input
some text. When you enter a text and press enter, then the program
proceeds and reads only a single character.
Output: Enter a value : Engineering
You entered: E
String I/O Unformatted Function

The gets() and puts() Functions


The char *gets(char *s) function reads a line
from stdin into the buffer pointed to by s until
either a terminating newline or EOF (End of
File). The int puts(const char *s) function
writes the string „s‟ and „a‟ trailing newline to
stdout.
gets() and puts() Functions
#include <stdio.h>
main( )
{
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou entered: ");
puts( str );
return 0;
}
gets() and puts() Functions

When the above code is compiled and


executed, it waits for you to input some text.
When you enter a text and press enter, then the
program proceeds and reads the complete line
till end, and displays it as follows:

Enter a value: Engineering


You entered: Engineering

You might also like