Unit-2 CPPM
Unit-2 CPPM
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()
#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
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.
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.
1.printf()
The printf() function is used for output. It prints the given statement to the console.
Syntax
printf("format string",argument_list);
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.
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.
2.3 Operators:
Operators are symbols that can be used to perform mathematical, relational, bitwise,
conditional, or logical manipulations.
The C programming language has a lot of built-in operators to perform various tasks as per the
need of the program.
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;
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.
Syntax
B = --A;
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.
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.
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
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 |.
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.
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.
1.Strlen
The strlen() function calculates the length of a given string.
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‟.
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.
3.Strcpy
The strcpy(destination, source) function copies the source string in destination.
char str1[20] = "SDJ";
char str2[20];
4.strcat
The strcat(first_string, second_string) function concatenates two strings and result is returned to
first_string.
strcat() function takes two arguments:
The strcat() function concatenates the destination string and the source string, and the result is
stored in the destination string.
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.
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.
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.
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.
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.
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);
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);
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.