100% found this document useful (1 vote)
569 views12 pages

Unit-2 CPPM

Uploaded by

adobephoto0011
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
100% found this document useful (1 vote)
569 views12 pages

Unit-2 CPPM

Uploaded by

adobephoto0011
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/ 12

104-CPPM Unit-2 SDJ International College

UNIT-2: Input/Output Statements and Operators:


2.1 Input/Output statements:
2.1.1 Concepts of Header files (STDIO,CONIO)
2.1.1.1 Concepts of pre-compiler directives.
2.1.1.2 Use of #inlcude and #define
2.2 Input/Output Statements:
2.2.1 Input statements : scanf(), getc(), getch(), gets(), getchar()
2.2.2 Output Statements: printf(), putc(),puts(), putchar()
2.2.3 Type specifiers (formatting strings) : %d, %ld, %f, %c, %s, %lf
2.3 Operators:
2.3.1 Arithmetic operators ( +, -, *, /, %, ++, --, )
2.3.2 Logical Operators ( &&, ||, ! )
2.3.3 Relational Operators ( >, <, ==, >=, <=, != )
2.3.4 Bit-wise operators ( &, |, ^ , <<, >>)
2.3.5 Assignment operators ( =, +=, -=, *=, /=, %=)
2.3.6 Ternary Operator and use of sizeof() function.
2.4 Important Built-in functions:
2.4.1 Use of <string.h> : ( strlen, strcmp, strcpy, strcat, strrev)
2.4.2 Use of <math.h> : (abs(), floor(), round(), ceil(), sqrt(), exp(), log(), sin(), cos(),
tan(), pow() and trunc())

2.1 Input/Output statements:

2.1.1 Concepts of Header files (STDIO,CONIO)


In C language, header files contain the set of predefined standard library functions. The
“#include” preprocessing directive is used to include the header files with “.h” extension in the
program.

1.stdio
The stdio.h header file declares functions that deal with standard input and output.
It contains function like getc(),putc(),gets(),puts(),printf(), scanf().

2.conio
conio.h is a header file used for functions related to console input/output. conio.h has many
inbuilt library functions that are used to perform input and output from a c program. Most C
programs use this header file.
It contains function like getch(),clrscr(),getche()

2.1.1.1 Concepts of pre-compiler directives.


The C Preprocessor is not a part of the compiler, but is a separate step in the compilation
process.
it instructs the compiler to do required pre-processing before the actual compilation. All
preprocessor commands begin with a hash symbol (#).
#define , #include , #undef , #ifdef are example of preprocessor.

Prof. Nainesh Gathiyawala Page 1


104-CPPM Unit-2 SDJ International College

2.1.1.2 Use of #inlcude and #define

#include is a way of including a standard or user-defined file in the program and is mostly
written at the beginning of any C program. It helps to insert a certain header from another file.

Syntax:
#include <header_file>

Example
#include <stdio.h>

#define: The #define preprocessor directive is used to define constant or micro substitution. It
can use any basic data type.

Syntax:
#define token value

Example:
#define PI 3.14

2.2 Input/Output Statements:

2.2.1 Input statements : scanf(), getc(), getch(), gets(), getchar()

1.scanf()

In C programming, scanf() is one of the commonly used function to take input from the user. The
scanf() function reads formatted input from the standard input such as keyboards.

2.getc()
C getc() function is a C library function, which reads a character from a file that has been opened
in read mode by the fopen() function.

3.getch()
The getch() is a predefined non-standard function that is defined in conio.h header file.
We use a getch() function in a C/ C++ program to hold the output screen for some time until the
user passes a key from the keyboard to exit the console screen.
It can also be used to read a single byte character or string from the keyboard and then print.

4.gets()
The gets() function enables the user to enter some characters followed by the enter key. All the
characters entered by the user get stored in a character array.

Prof. Nainesh Gathiyawala Page 2


104-CPPM Unit-2 SDJ International College

5.getchar()
getchar is a function in C programming language that reads a single character from the
standard input stream stdin, regardless of what it is, and returns it to the program.

2.2.2 Output Statements: printf(), putc(),puts(), putchar()

1.printf()
The printf() function is used for output. It prints the given statement to the console.

Syntax
printf("format string",argument_list);

The format string can be %d (integer), %c (character), %s (string), %f (float) etc.

2.putc()
The putc function in C writes a character to any output stream and returns an integer value. It is
a standard function in C and can be used by including the <stdio.h> header file.

3.puts()
The puts() function in C is used to write a line or string to the output(stdout) stream.

char Mystr[] = "SDJ International College";


puts(Mystr); //writing the string to stdout

4.putchar()
The putchar(int char) method in C is used to write a character, of unsigned char type, to stdout.
The putchar() function is used to write a single character to the standard output stream, writes a
single character to the standard output stream, stdout.

2.2.3 Type specifiers (formatting strings) : %d, %ld, %f, %c, %s, %lf
The format specifiers are used in C for input and output purposes. Using this concept the
compiler can understand that what type of data is in a variable during taking input using the
scanf() function and printing using printf() function. Here is a list of format specifiers.

Format Specifier Type


%c Character
%d Signed integer
%f Float values
%ld Long
%s String
%lf Double

Prof. Nainesh Gathiyawala Page 3


104-CPPM Unit-2 SDJ International College

2.3 Operators:
Operators are symbols that can be used to perform mathematical, relational, bitwise,
conditional, or logical manipulations.

An operator is a symbol that operates on a value or a variable. For example: + is an operator to


perform addition.

The C programming language has a lot of built-in operators to perform various tasks as per the
need of the program.

2.3.1 Arithmetic operators ( +, -, *, /, %, ++, --, )


An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).

Increment and Decrement Operators


The increment operator ++ increases the value of a variable by 1. Similarly, the decrement
operator -- decreases the value of a variable by 1.

C programming has two operators increment ++ and decrement -- to change the value of an
operand (constant or variable) by 1.

Increment Operator
Increment Operators are the unary operators used to increment or add 1 to the operand value.
The Increment operand is denoted by the double plus symbol (++). It has two types, Pre
Increment and Post Increment Operators.

Pre-increment Operator
The pre-increment operator is used to increase the original value of the operand by 1 before
assigning it to the expression.

Syntax
X = ++A;

Prof. Nainesh Gathiyawala Page 4


104-CPPM Unit-2 SDJ International College

Post increment Operator


The post-increment operator is used to increment the original value of the operand by 1 after
assigning it to the expression.

Syntax
X = A++;

Decrement Operator
Decrement Operator is the unary operator, which is used to decrease the original value of the
operand by 1. The decrement operator is represented as the double minus symbol (--). It has two
types, Pre Decrement and Post Decrement operators.

Pre Decrement Operator


The Pre Decrement Operator decreases the operand value by 1 before assigning it to the
mathematical expression. In other words, the original value of the operand is first decreases, and
then a new value is assigned to the other variable.

Syntax
B = --A;

Post decrement Operator


Post decrement operator is used to decrease the original value of the operand by 1 after assigning
to the expression.
Syntax
B = A--;

2.3.2 Logical Operators ( &&, ||, ! )


They are used to combine two or more conditions/constraints or to complement the evaluation of
the original condition under consideration. They are described below:

Following table shows all the logical operators supported by C language. Assume variable A
holds 1 and variable B holds 0, then –
Operator Description Example
Called Logical AND operator. If both the
&& operands are non-zero, then the condition becomes (A && B) is false.
true.
Called Logical OR Operator. If any of the two
|| operands is non-zero, then the condition becomes (A || B) is true.
true.
Called Logical NOT Operator. It is used to
reverse the logical state of its operand. If a
! !(A && B) is true.
condition is true, then Logical NOT operator will
make it false.

Prof. Nainesh Gathiyawala Page 5


104-CPPM Unit-2 SDJ International College

Logical AND Operator


The ‘&&’ operator returns true when both the conditions under consideration are satisfied.
Otherwise, it returns false. For example, a && b returns true when both a and b are true (i.e.
non-zero). An expression containing logical operator returns either 0 or 1 depending upon
whether expression results true or false. Logical operators are commonly used in decision
making in C programming.

Logical OR operator
The ‘||’ operator returns true even if one (or both) of the conditions under consideration is
satisfied. Otherwise, it returns false. For example, a || b returns true if one of a or b, or both are
true (i.e. non-zero). Of course, it returns true when both a and b are true.

Logical NOT operator:


The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise, it returns
false. For example, !a returns true if a is false, i.e. when a=0.

2.3.3 Relational Operators ( >, <, ==, >=, <=, != )


Relational Operators are the operators used to create a relationship and compare the values of
two operands. Relational operators are used in decision making and loops.

For example, there are two numbers, 5 and 15, and we can get the greatest number using the
greater than operator (>) that returns 15 as the greatest or larger number to the 5.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
> Greater than 5 > 3 is evaluated to 1
< Less than 5 < 3 is evaluated to 0
!= Not equal to 5 != 3 is evaluated to 1
>= Greater than or equal to 5 >= 3 is evaluated to 1
<= Less than or equal to 5 <= 3 is evaluated to 0

2.3.4 Bit-wise operators ( &, |, ^ , <<, >>)


During computation, mathematical operations like: addition, subtraction, multiplication, division,
etc are converted to bit-level which makes processing faster and saves power.

Bitwise operators are used in C programming to perform bit-level operations.


Operators Meaning of operators
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement

Prof. Nainesh Gathiyawala Page 6


104-CPPM Unit-2 SDJ International College

Operators Meaning of operators


<< Shift left
>> Shift right

Bitwise AND Operator &


The output of bitwise AND is 1 if the corresponding bits of two operands is 1. If either bit of an
operand is 0, the result of corresponding bit is evaluated to 0.
In C Programming, the bitwise AND operator is denoted by &.

Bitwise OR Operator |
The output of bitwise OR is 1 if at least one corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.

Bitwise XOR (exclusive OR) Operator ^


The result of bitwise XOR operator is 1 if the corresponding bits of two operands are opposite. It
is denoted by ^.

Bitwise Complement Operator ~


Bitwise complement operator is a unary operator (works on only one operand). It changes 1 to 0
and 0 to 1. It is denoted by ~.

Right Shift Operator


Right shift operator shifts all bits towards right by certain number of specified bits. It is denoted
by >>.

Left Shift Operator


Left shift operator shifts all bits towards left by a certain number of specified bits. The bit
positions that have been vacated by the left shift operator are filled with 0. The symbol of the left
shift operator is <<.

2.3.5 Assignment operators ( =, +=, -=, *=, /=, %=)


An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as
= 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

Prof. Nainesh Gathiyawala Page 7


104-CPPM Unit-2 SDJ International College

2.3.6 Ternary Operator and use of sizeof() function.

Ternary Operator
We use the ternary operator in C to run one code when the condition is true and another code
when the condition is false.

Syntax of Ternary Operator


testCondition ? expression1 : expression 2;

The testCondition is a Boolean expression that results in either true or false. If the condition is
 true - expression1 (before the colon) is executed
 false - expression2 (after the colon) is executed

The ternary operator takes 3 operands (condition, expression1 and expression2). Hence, the
name ternary operator.

Example
(age >= 18) ? printf("Can Vote") : printf("Cannot Vote");

Here, when the age is greater than or equal to 18, Can Vote is printed. Otherwise, Cannot Vote is
printed.

sizeof()
It is a compile-time unary operator and used to compute the size of its operand. It returns the size
of a variable. It can be applied to any data type, float type, pointer type variables.

When sizeof() is used with the data types such as int, float, char… etc it simply returns the
amount of memory is allocated to that data types.

printf("%lu\n", sizeof(char)); //1


printf("%lu\n", sizeof(int));//4
printf("%lu\n", sizeof(float));//4
printf("%lu", sizeof(double));//8

NOTE: sizeof() may give different output according to machine.

2.4 Important Built-in functions:


2.4.1 Use of <string.h> : ( strlen, strcmp, strcpy, strcat, strrev)

The C <string.h> header file declares a set of functions to work strings.

1.Strlen
The strlen() function calculates the length of a given string.

Prof. Nainesh Gathiyawala Page 8


104-CPPM Unit-2 SDJ International College

The strlen() function calculates the length of a given string.The strlen() function is defined in
string.h header file. It doesn‟t count null character „\0‟.

char str[]= "Princy";


printf("Length of string is: %d", strlen(str));

Output:
Length of string is: 6

2.Strcmp
The strcmp(first_string, second_string) function compares two string and returns 0 if both strings
are equal. The strcmp() function is defined in the string.h header file.

The function takes two parameters:


 str1 - a string
 str2 - a string
Return Value from strcmp()

Return Value Remarks


0 if strings are equal
>0 if the first non-matching character in str1 is greater (in ASCII) than that of str2.
<0 if the first non-matching character in str1 is lower (in ASCII) than that of str2.

3.Strcpy
The strcpy(destination, source) function copies the source string in destination.
char str1[20] = "SDJ";
char str2[20];

// copying str1 to str2


strcpy(str2, str1);
puts(str2); // SDJ

4.strcat
The strcat(first_string, second_string) function concatenates two strings and result is returned to
first_string.
strcat() function takes two arguments:

destination - destination string


source - source string

The strcat() function concatenates the destination string and the source string, and the result is
stored in the destination string.

Prof. Nainesh Gathiyawala Page 9


104-CPPM Unit-2 SDJ International College

char str1[100] = "This is ", str2[] = "SDJ International College";


strcat(str1, str2);
concatenates str1 and str2
// the resultant string is stored in str1.
strcat(str1, str2);
puts(str1); // This is SDJ International College

5.strrev
The strrev() function is a built-in function in C and is defined in string.h header file. The
strrev() function is used to reverse the given string.
char str[50] = "Princy";
printf("After reversing string is =%s",strrev(str));

Output
After reversing string is =ycnirP

2.4.2 Use of <math.h> : (abs(), floor(), round(), ceil(), sqrt(), exp(), log(), sin(), cos(), tan(),
pow() and trunc())

The C <math.h> header file declares a set of functions to perform mathematical operations such
as: sqrt() to calculate the square root, log() to find natural logarithm of a number etc.

1.abs()
The abs() function always returns a positive number. Even if the given number is either negative
or positive.

This function returns the absolute value of x.

Int b = abs(-10);
printf("value of b = %d\n", b);

output: 10

2.floor()
The floor() is a library function in C defined in the <math.h> header file. This function returns
the nearest integer value, which is less than or equal to the floating point number.

The floor() function in C is used to convert a floating point number to its immediately smaller
integer (for eg, 3.6 to 3). The floor() function only accepts floating point numbers and returns an
integer.

Prof. Nainesh Gathiyawala Page 10


104-CPPM Unit-2 SDJ International College

3.round()
The C round function is one of the Math Functions used to return the nearest value (rounded
value) of a given number.
The math round Function allows you to find the closest integer value of a given number.

float var = 37.66666;


// Directly print the number with .2f precision
printf("%.2f", var);

4.ceil()
The ceil() function computes the nearest integer greater than the argument passed.
The ceil() function takes a single argument and returns a value of type int.

For example: If 2.3 is passed to ceil(), it will return 3.

5.sqrt()
The sqrt() function computes the square root of a number.
The sqrt() function takes a single argument (in double) and returns its square root (also in
double).
The sqrt() function is defined in math.h header file.

double number=9, squareRoot;


squareRoot = sqrt(number);

output:3

6.exp()
The exp() function computes e (2.71828) raised to the power of the given argument.
double x = 12.0, result;
result = exp(x);
printf("Exponential of %.2lf = %.2lf", x, result);

output: Exponential of 12.00 = 162754.79

Prof. Nainesh Gathiyawala Page 11


104-CPPM Unit-2 SDJ International College

7.log()
The log() function computes the natural logarithm of an argument. It is defined in <math.h>
header file.
double num = 5.6, result;
result = log(num);
printf("log(%.1f) = %.2f", num, result);

Output: log(5.6) = 1.72

8.sin()
The sin() function returns the sine of a number. It is defined in math.h header file.

9.cos()
The cos() function computes the cosine of an argument. The value returned by cos() is always in
the range: -1 to 1. It is defined in <math.h> header file.

10.tan()
The tan() function returns tangent of the argument passed. It is defined in math.h header file.

11.pow()
The pow() function computes the power of a number.

The pow() function takes two arguments (base value and power value) and, returns the power
raised to the base number. For example,

int base = 3;
int power = 5;
result = pow(base,power);

12.trunc()
Truncates a double value after the decimal point and gives the integer part as the result. The
return value and the arguments are of the type double.

Disclaimer: The study material is compiled by Prof. Nainesh J. Gathiyawala. The basic
objective of this material is to supplement teaching and discussion in the classroom in the
subject. Students are required to go for extra reading in the subject through library work.

Prof. Nainesh Gathiyawala Page 12

You might also like