SlideShare a Scribd company logo
1 of 30
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Basic Concepts of a C Program (Structure of a C Program):
Comments: Comments are one of the most important parts of any program. Comment is a
Programming construct which contains the short descriptions explaining the purpose of each
statement or set of statements in a program. The shorthand descriptions are very significant to the
programmers to understand the logic of the program but are ignored by the compiler. The main
purposes of using the comments are shown below.
 It is very easy to read and understand the logic of the program.
 Helps us to maintain and modify the program easily.
Any comment must start with two characters /* and end with */ and the comments can appear
anywhere in the program. The symbols /* mark the beginning of the program and the symbols */ mark
the end of the comment. Comments in can be represented in two ways. Single line comments and
multiline comments.
a) Single Line comment is represented as follows:
c=a + b; // Addition of two numbers.
b) Multiline comments are represented as follows:
/*
Problem : The program arranges the number in ascending order
Limitations’: It arranges only integers in ascending order.
*/
Preprocessor directives: The Preprocessor statements start with symbol # symbol. These Statements
instruct the compiler to include some of the header files in the beginning of the program.
For Example:
#include<stdio.h>
#include<math.h>
stdio.h and math.h are header files and hence the extension “.h”
[Comments] //Optional
[Preprocessor directives]
[Global Declarations]
[Function Declarations]
void main() //Program Header
{
[Declaration Section]
[Executable Section]
}
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Using the preprocessor directive the user can define constants also.
For Example:
#define MAX 1024
#define PI 3.1417
Here MAX and PI are called symbolic constants.
Global declarations and definitions: The variables that are declared before all the functions are called
global variables. The global variables can be accessed by all the functions. We can also declare functions
also as global declarations. Immediately after the function declarations functions can be defined.
The Program header: In C Program, the execution always starts from the function main and it can be
written as shown below.
void main()
The above line tells the compiler that execution always starts from function main is called program
header. Pair of parenthesis is denoted by “( )” is must immediately after the main.
Body of the program: A series of actions that are performed will be enclosed within braces { and } and
present immediately after the program header is called body of the program. The body of the program
contains two essential parts:
1. Declaration part: The variables that are used inside the function should be declared in the
declaration section. The variables can also be initialized. For Example, the declarations shown
below:
int sum=0;
int a;
float b;
char ch;
Here, the variable sum is declared as an integer variable and it is initialized to zero. The variable
a is declared as an integer b is declared as float and ch is declared as character variable.
2. Executable Part: These are the building blocks of a program. They represent the instructions
given to the computer to perform a specific task. An instruction may represent an expression to be
evaluated, input/output statement etc. Each executable statement ends with semicolon (;). The
instructions can be input/output statements, simple assignment statements, control statements
such as if statement, for statements etc.
Program Example:
#include<stdio.h>
#define PI 3.1417
int x; // Global Variable x Declaration
void show(); // Global Function Declaration
/*
The Below code is function definition
*/
void show(){
printf("Hellon");
}
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
void main(){
int z,c;
z=10;
x=20;
c=z+x;
printf("%dn",x);
printf("%fn",PI);
printf("Result:%dn",c);
show(); //function call
}
Output: 20
3.1417
30
Hello
C Tokens: A token is a smallest basic unit of a C program. One or more characters are grouped in
sequence to form meaningful words. These meaningful words are called tokens. The tokens in C language
are broadly classified as shown below.
Keywords: The keywords also known as reserved words. Keywords have predefined meaning in C
language. They are reserved for specific purpose in C language.
The rules to be followed while using keywords in Program
 The keywords should not be used as variables, function names, array names etc.
 The meaning of the keywords cannot be changed by the user.
 All keywords should be written in lowercase letters.
List of Keywords used in C language is as follows:
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Identifiers: An identifier is a word consisting of sequence of one or more letters or digits along with “_”
(read as underscore). The letters are the characters from ‘A’ to ‘Z’ or ‘a’ to z’. The digits are characters
from ‘0’ to ‘9’. Using the identifiers we can give names to program elements such as variables, constants,
function names, array names etc.
The rules to be followed to frame an identifier are:
a. The first character in the identifier should be a letter or underscore ( _ ) and can be followed
by any number of letters or digits or underscores.
b. No extra symbols are allowed other than letters, digits and underscores.
c. The length of the identifier can be up to a maximum of 31 characters for external names
such as function names and global variables that can be shared between source files. The
length of the identifier can be up to maximum of 63 characters for internal names such as
local variables.
d. Reserved words (Keywords) cannot be used as identifiers.
e. Identifiers can be case sensitive. It means that the words such as sum, SUM and Sum are all
different identifiers.
The following table gives valid identifiers and invalid identifiers and reasons for invalidity.
S.NO Identifiers Valid/Invalid Reasons for invalidity
1 for Invalid It is a Keyword
2 3_factorial Invalid Should not start with a digit
3 Sum,1 Invalid Comma is not allowed
4 Sum-of-digits Invalid Minus(-) sign is not allowed
5 Sum= Invalid = is not allowed
6 If Invalid It is a Keyword
7 One+two Invalid + is not allowed
8 Sum! Invalid ! is not allowed
9 Int Invalid It is a Keyword
10 $roll no Invalid $ is not allowed
11 I Class Invalid Space is not allowed between words
12 James Bond Invalid Space is not allowed between words
13 _name1 Valid No Reason
14 _abc_name Valid No Reason
15 Abc123 Valid No Reason
16 _ Valid No Reason
Difference between Identifier and Keyword
S.NO Keywords Identifier
1 Keywords have pre-defined meaning Identifiers do not have a pre-defined meaning
2 Keywords are the instructions given to the
compiler
Identifiers are not the instructions given to the
compiler. They are the names to variables, functions.
3 Keywords in C Language: int, float, char, do,
while etc.
Identifiers in C Language :sum, length, A1,a etc.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Constants: A constant is a value which will not change during the execution of a program. The constants
cannot be modified in the program. For Example:
- 10 is a integer constant.
- 12.5 is a floating point constant.
- “SVIT” is a string constant.
- ‘A’,’B’ etc are character constants.
The Constants in C language are classified as shown below:
Integer Constant: An integer is a whole number without any decimal point or a fraction part. No extra
characters are allowed other than ‘+’ and ‘-‘ sign. If ‘+’ sign and ‘-‘ sign are present, they should precede
the number. An Integer constant can be classified into three types as shown below.
 Decimal Integer: A Decimal integer can be any combination of digits from ‘0’ to ‘9’. In other
words is a base 10 number. No extra characters are allowed other than ‘+’ and ‘-‘ sign. If ‘+’ and
‘-‘ sign are present, they should precede the number. For Example:- 10,140,-140 are valid.
 Octal Integer: A Octal integer can be any combination of digits from ‘0’ to ‘7’ with a prefix
zero(0). In other words is a base 8 number. No extra characters are allowed other than ‘+’ and ‘-‘
sign. If ‘+’ and ‘-‘ sign are present, they should precede the number. For Example:- 010,0777,-
065 are valid.
 Hexadecimal Integer: A Hexadecimal integer can be any combination of digits from ‘0’ to ‘9’
along with the letters ‘A’ to ‘F’ or ‘a’ to ‘f’. In other words is a base 16 number. The constant has
to be preceded by 0x or 0X. For Example: - 0x8A, 0XAB, 0XA123 etc are valid.
Floating Point Constants: The floating point constants are base 10 numbers with fraction part such as
10.5. All negative numbers should have a prefix ‘-‘. A positive number can have an optional ‘+’ sign. No
Extra characters are allowed. The floating point constants can be represented using two forms as shown
below.
 Fractional Form: A floating point number represented using fractional form has an integer part
followed by a dot and a fractional part. For Example: = 0.5, -.99, -.6 etc.
 Exponent Form: The floating point number represented using scientific notation has three parts
namely: mantissa ,e/E ,exponent .Example:= 6.69e2 means 6.69x102
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Character Constants: A character constant contains one single character enclosed within single
quotes. Examples of valid character constants ‘a’ , ‘Z’, ‘5’ It should be noted that character
constants have numerical values known as ASCII values, for example, the value of ‘A’ is 65
which is its ASCII value. C allows us to have certain non graphic characters in character
constants. Non graphic characters are those characters that cannot be typed directly from
keyboard, for example, tabs, carriage return, etc.
Escape
Sequence
Description
a Audible alert(bell)
b Backspace
f Form feed
n New line
r Carriage return
t Horizontal tab
v Vertical tab
 Backslash
“ Double quotation mark
‘ Single quotation mark
? Question mark
String Constants: String constants are sequence of characters enclosed within double quotes.
For example:= “hello”, “abc” ,“hello911”. Every sting constant is automatically terminated
with a special character ‘’ called the null character which represents the end of the string. For
example, “hello” will represent “hello” in the memory. Thus, the size of the string is the total
number of characters plus one for the null character.
Special Symbols: The following special symbols are used in C having some special meaning
and thus, cannot be used for some other purpose. For Example: [] () {} , ; : * … = #
 Braces {}: These opening and ending curly braces marks the start and end of a block of
code containing more than one executable statement.
 Parentheses (): These special symbols are used to indicate function calls and function
parameters.
 Brackets [ ]: Opening and closing brackets are used as array element reference. These
indicate single and multidimensional subscripts.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Data Types: The data type defines the type of data stored in a memory location. The data
type determines how much memory is should be allocated for a variable. Various types of
data such as integer constant, floating point constant, character constant etc. can be stored in
memory during program execution. Data types in c are classified into 2 types.
 Primitive Data types: The data types which can be manipulated by machine
instructions are called primitive data types. They are also called as basic data
types or simple data types or fundamental data types. The various primitive data
types supported by C language are: = int, char, float, double etc.
 Derived Data types: The various data types that are available in c are int, char,
float, double etc. Using these primitives we can derive some other data types.
The data types which are derived from basic data types are called Derived data
types. For Example: = Arrays, Pointers, Enumerated, Structures, Unions.
Data Type: int An int is a keyword in C which is used to define integers in C Language. Using
int keyword, Programmer can inform the compiler that the data associated with keyword should
be treated as integer. The size of int can be 2 or 4 or 8 bytes and is machine dependent. C
supports three types of integer data type namely: short int, int, long int.
sizeof(short int)<=sizeof(int)<=sizeof(long int). The range of values varies from machine
to machine and compiler to compiler as shown below.
S.NO Type Storage size Value range
1 short int 2 bytes -32,768 to 32,767
2 unsigned short int 2 bytes 0 to 65,535
3 Int 2 or 4 Bytes -32,768 to 32,767 or
-2,147,483,648 to 2,147,483,647
4 unsigned int 2 or 4 Bytes 0 to 65,535 or 0 to 4,294,967,295
5 long int 4 bytes -2,147,483,648 to 2,147,483,647
6 unsigned long int 4 bytes 0 to 4,294,967,295
Advantages of Integer: Since the integer constants are exact quantities, they are used for
counting and indexing etc.
Disadvantage: Higher range numbers cannot be represented.
The numbers with fraction part cannot be represented and stored.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Data type: float
 A float is a keyword which is used to define floating point numbers in C language. Using
float keyword, the programmer can inform the compiler that the data associated with this
keyword should be treated as floating point number. The size of float can range from
machine to machine as shown below.
S.NO Type Storage size Value range
1 float 4 bytes(16 bit
Machine)
3.4E-38 to 3.4E38
2 Float 8 bytes(32 bit
Machine)
1.7E-308 to 1.7E308
Advantages: The Number with fractional part or decimal points can be stored. Higher
precision and range compared to integers.
Disadvantages: Since they are not exact quantities, they are not used for counting,
indexing etc.
Data type: double
 A double is a keyword which is used to define long floating point numbers in C language.
Using double keyword, the programmer can inform the compiler that the data associated
with this keyword should be treated as long floating point numbers. The size range of
double varies from machine to machine as shown below.
S.NO Type Storage size Value range
1 double 8 bytes(16 bit
Machine)
1.7E-308 to 1.7E308
2 Double 16 bytes(32 bit
Machine)
3.4E-4932 to 1.1E4932
Advantage: Higher precision and range when compared to floating point numbers.
Data type: char
 The char is a keyword which is used to define single character or sequence of characters
(called string) in C language. Using char keyword, the programmer can inform the
compiler that the data associated with this keyword should be treated as character. Each
character stored in the memory is associated with a unique value called as ASCII
(American Code For Information Interchange) value. The size of the char range is fixed
in all the machines as shown below.
S.NO Type Storage size Value range
1 char 1 bytes(16/32
bit Machine)
-128 to 127
2 unsigned char 1 byte 0 to 255
Data type: void
 The keyword void is used in functions. When function does not return any value, we use
void keyword. It (void) is a empty data type. It does not occupy any space in the memory.
The keyword is not associated with any variable except pointers.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Variable
A variable is a name given to a memory location where the data can be stored. Using the
variable name, the data can be stored in a memory location and can be accessed or
manipulated very easily.
Rules for defining a variable:
 The first character in the variable should be a letter (from A to Z or a to z) or underscore.
 The first character can be followed by any number of letters or digits(0 to 9) or
underscores.
 No extra symbols are allowed other than letters, digits and underscore.
 The length of an identifier can be up to a maximum of 31 characters for external names
such as function names and global variables.
 C keywords cannot be used as variable names.
Valid and invalid variables are shown below
S.NO Variable Valid/Invalid Reasons for invalidity
1 For Invalid It is a Keyword
2 3_factorial Invalid Should not start with a digit
3 Sum,1 Invalid Comma is not allowed
4 Sum-of-digits Invalid Minus(-) sign is not allowed
5 Sum= Invalid = is not allowed
6 If Invalid It is a Keyword
7 One+two Invalid + is not allowed
8 Sum! Invalid ! is not allowed
9 Int Invalid It is a Keyword
10 $roll no Invalid $ is not allowed
11 I Class Invalid Space is not allowed between words
12 Space1999 Valid No Reason
13 Principle_amount Valid No Reason
14 A Valid No Reason
15 _ _ a_ _ _ b Valid No Reason
16 For1 Valid No Reason
Declaring and defining a variable: Giving name to a memory location is called declaring a
variable. Declaration of a variable will not reserve the space in the memory. Reserving the
required memory space to store the data using the declared Variable is called defining a variable.
Syntax:
type variable1,variable2………variable
 Where variable1, variable2 are variable names. All the variables should
be separated by commas.
 Type is a type of variables variable1,variable2 …. variableN the type of
variable may be int, float, char, double etc.
 Semicolon is must at the end of the declaration.
Example: int a; float b; char c; int a1,a2,a3,a4;
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Variable declaration can be done in the following way:
 All the variables of the same data type can be defined separately using multiple
statements in multiple lines for better readability as shown below.
int a; // a is an integer variable.
int b; // b is an integer.
int c; //c is an integer
 Multiple declarations are allowed in the same line whenever the declaration statements
end with semicolon as follows.
 The variables with different data types can be defined in different lines.
Variable Initialization/Assignment: Variables are not initialized when are declared and
defined. Hence, variables normally contain garbage values (meaningless values) and hence they
have to be initialized with valid data. The method of giving the initial values for the variables
before they are processed is called initialization of variables. The variable initialization can be
done using 3 ways.
 Initializing only one variable: The syntax to initialize only one variable is shown below.
Type var_name = data;
Where type : It can be int, float, char, double etc.
var_name : must be name of a variable.
= : is an Assignment operator
Data: is the value to be stored in memory.
Example: int a=10; //a is assigned the value 10.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
 Initializing more than variable while defining: All the variables defined must be
separately initialized. For Example
int count=0,sum=10; //count is assigned the value 10 and sum is assigned the value 10
 Initializing at appropriate place: To avoid most of the Programming errors, it is always
better to initialize the variables at the appropriate place in the body of the program as
shown below.
Input and Output Functions
Definition of Input Functions: The input functions accept the data from the keyboard and store
in memory locations. These functions that help the user to input the data from input devices such
as keyboard and transfer the data to memory locations are called input functions. For Example,
scanf(), getch(), getche(), getchar() etc.
Definition of output Functions: The output functions receive the data stored in the memory and
send the data to output devices. These functions that help the user to send the data stored in
memory to the output devices such as monitor or printer are called output functions. The monitor
is the standard output device and printf() is the standard output function. So by using printf(), the
data can be displayed on the monitor. For Example, printf(), puts(), putch(), putchar() etc..
Input output functions are classified into formatted and un-formatted input/output functions.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Unformatted I/O functions: There are mainly six unformatted I/O functions discussed as
follows:
getchar():This function is an Input function. It is used for reading a single character from the
keyborad. It is buffered function. Buffered functions get the input from the keyboard and store it
in the memory buffer temporally until you press the Enter key.
The general syntax is as: v = getchar();
where v is the variable of character type.
putchar(): This function is an output function. It is used to display a single character on the
screen.
The general syntax is as: putchar(v);
where v is the variable of character type.
/*Program illustrate the use of getchar() and putchar() functions*/
#include<stdio.h>
void main()
{
char n;
n = getchar();
putchar(n);
}
Output: Hello
H
getch():This is also an input function. This is used to read a single character from the keyboard
like getchar() function. But getchar() function is a buffered is function, getchar() function is a
non-buffered function. The character data read by this function is directly assign to a variable
rather it goes to the memory buffer, the character data directly assign to a variable without the
need to press the Enter key. Another use of this function is to maintain the output on the screen
till you have not press the Enter Key. The general syntax is as:
v = getch();
where v is the variable of character type.
putch(): This function is an output function. It is used to display a single character on the
screen. The general syntax is as: putch(v);
where v is the variable of character type.
/*Program to explain the use of getch() function*/
#include<stdio.h>
void main()
{
char n;
puts("Enter the Char");
n = getch();
puts("Char is :");
putch(n);
} OUTPUT: Enter the Char
Char is L
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
getche(): All are same as getch() function execpt it is an echoed function. It means when you
type the character data from the keyboard it will visible on the screen. The general syntax is as:
v = getche(); // where v is the variable of character type.
A simple C program to illustrate the use of getch() function:
/*Program to explain the use of getch() function*/
#include<stdio.h>
void main()
{
char n;
puts("Enter the Char");
n = getche();
puts("Char is :");
putch(n);
}
OUTPUT IS:
Enter the Char L
Char is L
gets(): This function is an input function. It is used to read a string from the keyboard. It is also
buffered function. It will read a string, when you type the string from the keyboard and press the
Enter key from the keyboard. It will mark nulll character ('0') in the memory at the end of the
string, when you press the enter key. The general syntax is as:
gets(v); // where v is the variable of character type.
puts(): This is an output function. It is used to display a string inputted by gets() function. It is
also used to display an text (message) on the screen for program simplicity. This function
appends a newline ("n") character to the output. The general syntax is as:
puts(v);
or
puts("text line");
where v is the variable of character type. A simple C program to illustrate the use of puts()
function:
/*Program to illustrate the concept of puts() with gets() functions*/
#include<stdio.h>
void main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}
OUTPUT IS: Enter the Name
Vijay
Name is:
Vijay
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Formatted I/O functions: Formatted I/O functions which refers to an Input or Ouput data that
has been arranged in a particular format. There are mainly two formatted I/O functions discussed
as follows:
 scanf()
 printf()
scanf(): The scanf() function is an input function. It used to read the mixed type of data from
keyboard. You can read integer, float and character data by using its control codes or format
codes. The general syntax is as:
scanf("Format strings",&v1,&v2,&v3,................&vn);
Where arg1,arg2,..........argn are the arguments for reading and v1,v2,v3,........vn all are the
variables. The scanf() format code (specifier) is as shown in the below table:
Format String Meaning
%d Scan or print an integer as signed decimal number
%f Scan or print a floating point number
%c To scan or print a character
%s To scan or print a character string. The scanning ends at whitespace.
printf(): This ia an output function. It is used to display a text message and to display the
mixed type (int, float, char) of data on screen. The general syntax is as:
printf("control strings",&v1,&v2,&v3,................&vn);
or
printf("Message line or text line"); // Where v1,v2,v3,........vn all are the variables.
The control strings use some printf() format codes or format specifiers or conversion characters.
Format String Meaning
%d Scan or print an integer as signed decimal number
%f Scan or print a floating point number
%c To scan or print a character
%s To scan or print a character string. The scanning ends at whitespace.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
/*Below the program which show the use of printf() and scanf() function*/
#include<stdio.h>
void main()
{
int a;
float b;
char c;
printf("Enter the mixed type of data");
scanf("%d,%f,%c",&a,&b,&c);
printf("%d%f%c ",a,b,c);
}
Output: Enter the mixed type of data
6 10.5 K
6 10.5 K
Operators and Expressions:
Operator: An operator is a symbol that specifies the operation to be performed on various types
of operands. For Example,
 The symbol + indicates add operation
 The symbol - indicates subtraction operation
 The symbol * indicates multiplication operation
 The symbol / indicates division operation
Operand: A constant or a variable or a function which returns a value is an operand. An operator
may have one or two or three operands.
Expression: A sequence of operands and operators that reduces to a single value is an
expression.
In the above expressions, the identifiers a and b are operands and the symbols ‘+’,‘-‘,‘*’,‘/’ are
operators.
Operators are classified in C language based on
 The Number of operands an operator has.
 The type of operation being performed.
Classification based on the number of operands: The operators are classified in to four major
categories based on the number of operands as shown below.
Unary operators, Binary operators, Ternary Operator, Special operators
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Unary operators: An operator that operates on one operand to produce a result is called unary
operator. In the expressions involving unary operators, the operators precede the operand.
Ex: -10, -1, a*b etc.
Binary operators: An operator that operates on two operands in an expression to produce a
result is called binary operator. In an expression involving binary operators, the operator is in
between two operands.
Ex: a + b, a * b, a - b, a / b, a % b.
Ternary operator: An operator that operates on three operands to produce a result is called a
ternary operator. Ternary operator is also called conditional operator.
Ex: a ? b : c
Since there are three operands a, b and c that are associated with operators ? and : It is called
Ternary operator(?:) and Conditional operator.
Classification based on the type of operation: Operators are classified based on the type of
operation being performed as shown below:
 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
 Special Operators
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Arithmetic Operators: The operators those are usedto perform addition,subtraction, multiplication, division
and modulus operators are called arithmetic operators. These operators perform operations on two operands and
hence they are called Binary operators. The following table shows all the arithmetic operators
supported by the C language. Assume variable A holds 10 and variable B holds 20 then
Operator Description Example
+ Adds two operands. A + B = 30
− Subtracts second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B / A = 2
% Modulus Operator and remainder of after an integer division. B % A = 0
ConversionofExpressions:
S.NO Mathematical Expressions C Equivalent Expressions
1 𝒂
𝒃
a/b
2
𝐬 =
𝒂 + 𝒃 + 𝒄
𝟐
s=(a+b+c)/2
3 𝒂𝒓𝒆𝒂 = √𝐬(𝐬 − 𝐚)(𝐬 − 𝐛)(𝐬 − 𝐜) area=sqrt(s*(s-a)*(s-b)*(s-c))
4
sin (
𝒃
√a + 𝑏2
)
sin(b/sqrt(a*a+b*b))
5 𝒂𝒙𝟐
+ 𝒃𝒙 + 𝒄 a*x*x+b*x+c
6
𝜏1 = √(
𝜎𝑥 − 𝜎𝑦
𝟐
) + 𝜏𝑥𝑦2
tow1= sqrt((rowx-rowy)/2+tow*x*y*y)
7
𝜏1 = √(
𝜎𝑥 − 𝜎𝑦
𝟐
)
2
+ 𝜏𝑥𝑦2
tow1= sqrt(pow((rowx-rowy)/2,2)+tow*x*y*y)
8
𝑥 =
−𝑏 + √𝑏2 − 4𝑎𝑐
2𝑎
x=(-b+sqrt(b*b-4*a*c)/(2*a)
9 𝑒|𝑎|+𝑏
𝒙+𝒚
(2x+3)
exp(abs(a)+b)/(x+y)*(2*x+3)
10
Y=
(𝛼+𝛽)
𝒔𝒊𝒏𝜽
+|x|
Y=(alpha+beta)/sin(theta*3.1416/180)+abs(x)
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Type Conversion: InC language,theprogrammercan instruct thecompilertoconvert the datafrom onedata
type to another datatype .Some timethecompileritselfconverts thedata from onedatatype to another datatype.
The process of converting the data from one data type to another data type is called type conversion. Type
conversioncanbeoftwotypes:
 Implicit type conversion: The process of conversion of data from lower rank to higher
rank automatically by the C compiler is called implicit type conversion. If both operands
are of same type for addition, multiplication and subtraction no conversion takes place. If
one operand is int type and another operand is float type then type int is promoted to
float. Promotion hierarchy is shown as follows:
Size of(char)< Size of(short int)<Size of(int)< Size of(unsigned int)< Size of(long int) <
Size of(unsigned long int)< Size of(float)< Size of(double).
Advantages: Compiler does conversion automatically from lower rank to higher rank.
Programmer need not worry about the type conversion procedure or syntax.
Disadvantages: If both operands are of the same data type, implicit type conversion is
not possible when we want to perform division operation. The conversion of higher rank
to lower rank is not done automatically by the compiler.
 Explicit type conversion: If the operands are of the same type no conversion takes place
by the compiler. Sometimes, the type conversion is required to get the desired results. In
such case the programmer can instruct the compiler to change the type of the operand
from one data type to another data type. The forcible conversion from one data type to
another is called explicit type conversion.
The syntax is shown
(type) expression Ex: (int) 9.43 converted to 9 only
Where type is the required type of the expression.
Expression can be an operand such as variable or a constant.
Example: 1
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Example: 2
Modes of Arithmetic Expressions: There are three types of arithmetic expressions.
 Integer Expression: If all the operands in an expression are integers, the expression is
called integer expression. An integer expression always yields an integer result.
Example: 4/2 =2
4/3=1 (Note: answer is 1 not 1.3333)
3/4=0 (Note: answer is 0 not 0.750)
 Floating point Expression: If all the operands in an expression are floating point
numbers or double values, the expression is called floating point expression. A floating
point expression always yields a floating point result.
Example: 4.0/2.0 =2.000000
4.0/3.0= 1.333333
3.0/4.0= 0.750000
 Mixed Mode (type) expressions: An expression that has operands of different data
types is called mixed mode expression or mixed type expression. During evaluation, data
conversion takes place from lower rank to higher rank so that type of operands remains
same.
Example: 4.0/3 =2.000000
4./3= 1.333333
Arithmetic operators along with priority and associativity as follows
Arithmetic
Operators
Description Operator Priority Associativity Precedence
Multiplication * 1 Left to Right Highest
Precedence
Division / 1 Left to Right
Modulus % 1 Left to Right
Addition + 2 Left to Right Lower
Precedence
Subtraction - 2 Left to Right
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Relational Operators: The operators that are used to find the relationship between two
operands are called relational operators. The two operands may be constants, variables or
expressions. The relationship between two operand a values results in true (whose value is 1) or
false (whose value is 0). The relational operators and the meaning associated with them are
shown in the table below.
Relational
Operators
Description Operator Priority Associativity
Less than < 1 Left to Right
Greater than > 1 Left to Right
Less than/equal <= 1 Left to Right
Greater than/equal >= 1 Left to Right
Equal = = 2 Left to Right
Not Equal != 2 Left to Right
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
= = Checks if the values of two operands are equal or not. If yes, then
the condition becomes true.
(A == B)
is not true.
!= Checks if the values of two operands are equal or not. If the values
are not equal, then the condition becomes true.
(A != B) is
true.
> Checks if the value of left operand is greater than the value of right
operand. If yes, then the condition becomes true.
(A > B) is
not true.
< Checks if the value of left operand is less than the value of right
operand. If yes, then the condition becomes true.
(A < B) is
true.
>= Checks if the value of left operand is greater than or equal to the
value of right operand. If yes, then the condition becomes true.
(A >= B)
is not 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.
(A <= B)
is true.
Logical Operators: The operators that are used to combine two or more relational expressions
are called logical operators. Since the output of the relational expression is true or false, the
output of the logical expression is also true(whose value is 1) or false(whose value is 0). The
logical operators and the meaning associated with them are shown in the table below.
Logical
Operators
Description Operator Priority Associativity
Not(unary operator) ! 1 Left to Right
and(binary operator) && 2 Left to Right
or(binary operator) || 3 Left to Right
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Logical and operation how it works: Result of the logical && operator is true if and only if
both the relational expression is true otherwise it is false. The logical && table is as follows.
Operand1 Operator(&&) Operand2 Result
TRUE(1) && TRUE(1) TRUE(1)
TRUE(1) && FALSE(0) FALSE(0)
FALSE(0) && TRUE(1) FALSE(0)
FALSE(0) && FALSE(0) FALSE(0)
Example: int a=3,b=5,c=10;
a<b && c>b
1 && 1
1
Logical OR operation how it works: Result of the logical || operator is true at least any one of
the relational expression is true otherwise it is false. The logical && table is as follows.
Operand1 Operator(||) Operand2 Result
TRUE(1) || TRUE(1) TRUE(1)
TRUE(1) || FALSE(0) TRUE(1)
FALSE(0) || TRUE(1) TRUE(1)
FALSE(0) || FALSE(0) FALSE(0)
Example: int a=3,b=5,c=10;
a>b || c>b
0 || 1
1
Logical not operation how it works: Result of the logical not operator is true if the operand is
false and the result is false if the operand is true. The logical not table is as follows.
Operand1 !Operand
TRUE(1) FALSE(0)
FALSE(0) TRUE(1)
The precedence rules to be followed while evaluating the expression is shown below.
 Parenthesis
 Unary Expressions
 Arithmetic expressions
 Relational expressions
 Logical Expressions
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Problem 1: Evaluate the expression a+2 > b && !c || a!=d && a-2 <=e where a=11,
b=6, c=0, d=7 and e=5
Solution:
Problem 2: Evaluate the expression a+2 > b || !c && a==d || a-2 <=e where a=11,
b=6, c=0, d=7 and e=5
Solution:
Assignment Operators: An assignment operator which is used to assign the data or result of an
expression into a variable is called an assignment operator. Copying a value into memory
location is called assigning and hence the name .The assignment operator is denoted by ‘=’ sign.
There are three types of assignment statements are shown below.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
 Simple Assignment: Ex: a=a+10; The syntax of simple assignment is as follows
Where
 The expression can be a constant or a variable or any expression. The symbol ‘=’
is an assignment operator. The expression on right side of assignment operator is
evaluated and the result is converted into type of variable on the left hand side of
assignment operator. The converted data is copied into a variable which is
present on the LHS of the operator.
Ex:1 a = 10 ; //RHS is a constant
Ex:2 a = b ; //RHS is a variable
Ex:1 a = x + y ; //RHS is a expression
 Shorthand assignment statement: The operators such as +=, - =, *=, /=, %= are called
shorthand assignment operators. The assignment statements that use these operators are
called shorthand assignment statements. These statements are also called compound
statements. Examples of shorthand assignment and their meaning is as shown below:
Shorthand statement Meaning Explanation
a+=2 a=a+2 Evaluate a+2 and store the result in a
a- =2 a=a-2 Evaluate a-2 and store the result in a
a*=2 a=a*2 Evaluate a*2 and store the result in a
a%=2 a=a%2 Evaluate a%2 and store the result in a
a/=2 a=a/2 Evaluate a/2 and store the result in a
 Multiple assignment statement: A statement using which a value or set of values are
assigned to different variables is called multiple assignment statement. The assignment
operator ‘=’ can be used to assign a single value to more than one variable.
i=10; j=10; k=10;
the above simple assignment can be written using multiple assignment is :
i = j = k = 10;
assignment operator uses right to left associativity.
Simplify the expression: x*=y+3 when x=10 and y=5
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Simplify the expression: a+=b*=c- =5 when a=1 and b=3 and c=7
Increment and decrement operator
Increment Operator: ++ is an increment operator. This is a unary operator. It increments the
value of a variable by one. The increment operator is classified into two categories.
 Post increment: If the increment operator ++ is placed immediately after the operand,
then the operator is called post increment. As the name indicates, use the operand value
first and then increment the operand value by 1.
 Pre increment: If the increment operator ++ is placed before the operand, then the
operator is called pre increment. As the name indicates, first increment the operand value
by 1 and then use the operand value later.
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Sample Program to demonstrate post increment
#include<stdio.h>
void main(){
int a=20;
int b;
b = a++;
printf("%dn", a);
printf("%dn", b);
}
Output: 21
20
Sample Program to demonstrate pre increment
#include<stdio.h>
void main(){
int a=20;
int b;
b = ++ a;
printf("%dn", a);
printf("%dn", b);
}
Output: 21
21
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Decrement Operator: - - is an decrement operator. This is a unary operator. It decrements the
value of a variable by one. The decrement operator is classified into two categories.
 Post decrement: If the decrement operator - - is placed immediately after the operand,
then the operator is called post decrement. As the name indicates, use the operand value
first and then decrement the operand value by 1.
 Pre decrement: If the decrement operator - - is placed before the operand, then the
operator is called pre decrement. As the name indicates, first decrement the operand
value by 1 and then use the operand value later.
Sample Program to demonstrate post decrement
#include<stdio.h>
void main(){
int a=20;
int b;
b = a--;
printf("%dn", a);
printf("%dn", b);
}
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Output: 19
20
Sample Program to demonstrate pre decrement
#include<stdio.h>
void main(){
int a=20;
int b;
b = - - a;
printf("%dn", a);
printf("%dn", b);
}
Output: 19
19
Conditional Operator: This is also called as ternary operator. As the name indicates, an
operator that operates on three operands is called ternary operator. The ternary operators are ?
and : The syntax is as follows:
Exp1 ? Exp2 : Exp3 ;
 Exp1 is an expression evaluated to true or false.
 If Exp1 is evaluated to true, Exp2 is executed.
 If Exp1 is evaluated to false, Exp3 is executed.
Program to demonstrate largest of two numbers using Ternary or conditional operator
#include<stdio.h>
void main(){
int a,b,large;
printf("Enter two numbersn");
scanf("%d%d",&a,&b);
large=(a>b)?a:b;
printf("Largest of two numbers=%dn",large);
}
Output: Enter Two numbers
10 20
Largest of two numbers=20
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Program to demonstrate largest of two numbers using Ternary or conditional operator
#include<stdio.h>
void main(){
int a,b,c,large;
printf("Enter three integer numbersn");
scanf("%d%d%d",&a,&b,&c);
large = (a>b) ? (a>c) ? a : c : (b>c) ? b : c;
printf("Largest of two numbers=%dn",large);
}
Output: Enter Three integer numbers
10 20 30
Largest of three numbers=30
Bitwise Operators: Bitwise operator works on bits and performs bit-by-bit operation. The
truth tables for &, |, and ^ is as follows –
P Q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume A = 60 and B = 13 in binary format, they will be as follows −
A = 0011 1100
B = 0000 1101
-------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and
variable 'B' holds 13, then –
Show Examples
Operator Description Example
& Binary AND Operator copies a bit to the result if
it exists in both operands.
(A & B) = 12, i.e.,
0000 1100
| Binary OR Operator copies a bit if it exists in
either operand.
(A | B) = 61, i.e.,
0011 1101
^ Binary XOR Operator copies the bit if it is set in
one operand but not both.
(A ^ B) = 49, i.e.,
0011 0001
~ Binary Ones Complement Operator is unary and
has the effect of 'flipping' bits.
(~A ) = -61, i.e,.
1100 0011 in 2's
complement form.
<< Binary Left Shift Operator. The left operands
value is moved left by the number of bits specified
by the right operand.
A << 2 = 240 i.e.,
1111 0000
>> Binary Right Shift Operator. The left operands
value is moved right by the number of bits
specified by the right operand.
A >> 2 = 15 i.e.,
0000 1111
Special operators:
 Comma operator: Comma (’,’) is a special operator. To declare and initialize more than
one variable we use comma operator. For Example
int a, b, c; // a b c are variables
int a=10, b=20, c=30; // initialize a b c variables
b=(a=12,345) // initialize 12 in to a and 345 into b
 Size of operator: This operator is used to determine the size of each different data types
used in C language.
#include<stdio.h>
void main(){
printf("Size of char=%dn",sizeof(char));
printf("Size of int=%dn",sizeof(int));
printf("Size of long int=%dn",sizeof(long int));
printf("Size of float=%dn",sizeof(float));
printf("Size of double=%dn",sizeof(double));
}
Programming In C & Datastructure 17PCD13/23
DEPARTMENT OF CSE,NIT RAICHUR
Output: Size of char=1
Size of int=4
Size of long int=4
Size of float=4
Size of double=8
Pseudocode: Pseudocode is nothing but a series of steps to solve a given problem written
using a mixture of English language and C like language. It is very easy for the programmer to
convert the pseudocode into actual C program. So, pseudocode acts a problem-solving tool.
Writing a pseudocode is the first step in writing a Program.
Problem: Display list of numbers from 5 to 10 along with their squares in the following format
n n2
Pseudocode solution to the above problem:
“Start with first number i,e 5 and compute its square. Print the number along with
its square. The process is repeated for each of the other numbers 6 to 10”
Solution 2:
Get the First Number
Compute square of the number
Print the number and its square.
Do the above activities for remaining numbers.

More Related Content

Similar to Module 1 PCD.docx

datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c languageRai University
 
Diploma ii cfpc u-2 datatypes and variables in c language
Diploma ii  cfpc u-2 datatypes and variables in c languageDiploma ii  cfpc u-2 datatypes and variables in c language
Diploma ii cfpc u-2 datatypes and variables in c languageRai University
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageRai University
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageRai University
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdfAdiseshaK
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)indrasir
 
Chapter3
Chapter3Chapter3
Chapter3Kamran
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to cHattori Sidek
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centrejatin batra
 
(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.pptatulchaudhary821
 
C presentation book
C presentation bookC presentation book
C presentation bookkrunal1210
 

Similar to Module 1 PCD.docx (20)

C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Diploma ii cfpc u-2 datatypes and variables in c language
Diploma ii  cfpc u-2 datatypes and variables in c languageDiploma ii  cfpc u-2 datatypes and variables in c language
Diploma ii cfpc u-2 datatypes and variables in c language
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
 
Bsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c languageBsc cs i pic u-2 datatypes and variables in c language
Bsc cs i pic u-2 datatypes and variables in c language
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdf
 
C programming notes
C programming notesC programming notes
C programming notes
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
fds unit1.docx
fds unit1.docxfds unit1.docx
fds unit1.docx
 
Ch02
Ch02Ch02
Ch02
 
Chapter3
Chapter3Chapter3
Chapter3
 
C programming
C programmingC programming
C programming
 
C language
C language C language
C language
 
Basics of c
Basics of cBasics of c
Basics of c
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt
 
C presentation book
C presentation bookC presentation book
C presentation book
 
C Language
C LanguageC Language
C Language
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 

Module 1 PCD.docx

  • 1. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Basic Concepts of a C Program (Structure of a C Program): Comments: Comments are one of the most important parts of any program. Comment is a Programming construct which contains the short descriptions explaining the purpose of each statement or set of statements in a program. The shorthand descriptions are very significant to the programmers to understand the logic of the program but are ignored by the compiler. The main purposes of using the comments are shown below.  It is very easy to read and understand the logic of the program.  Helps us to maintain and modify the program easily. Any comment must start with two characters /* and end with */ and the comments can appear anywhere in the program. The symbols /* mark the beginning of the program and the symbols */ mark the end of the comment. Comments in can be represented in two ways. Single line comments and multiline comments. a) Single Line comment is represented as follows: c=a + b; // Addition of two numbers. b) Multiline comments are represented as follows: /* Problem : The program arranges the number in ascending order Limitations’: It arranges only integers in ascending order. */ Preprocessor directives: The Preprocessor statements start with symbol # symbol. These Statements instruct the compiler to include some of the header files in the beginning of the program. For Example: #include<stdio.h> #include<math.h> stdio.h and math.h are header files and hence the extension “.h” [Comments] //Optional [Preprocessor directives] [Global Declarations] [Function Declarations] void main() //Program Header { [Declaration Section] [Executable Section] }
  • 2. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Using the preprocessor directive the user can define constants also. For Example: #define MAX 1024 #define PI 3.1417 Here MAX and PI are called symbolic constants. Global declarations and definitions: The variables that are declared before all the functions are called global variables. The global variables can be accessed by all the functions. We can also declare functions also as global declarations. Immediately after the function declarations functions can be defined. The Program header: In C Program, the execution always starts from the function main and it can be written as shown below. void main() The above line tells the compiler that execution always starts from function main is called program header. Pair of parenthesis is denoted by “( )” is must immediately after the main. Body of the program: A series of actions that are performed will be enclosed within braces { and } and present immediately after the program header is called body of the program. The body of the program contains two essential parts: 1. Declaration part: The variables that are used inside the function should be declared in the declaration section. The variables can also be initialized. For Example, the declarations shown below: int sum=0; int a; float b; char ch; Here, the variable sum is declared as an integer variable and it is initialized to zero. The variable a is declared as an integer b is declared as float and ch is declared as character variable. 2. Executable Part: These are the building blocks of a program. They represent the instructions given to the computer to perform a specific task. An instruction may represent an expression to be evaluated, input/output statement etc. Each executable statement ends with semicolon (;). The instructions can be input/output statements, simple assignment statements, control statements such as if statement, for statements etc. Program Example: #include<stdio.h> #define PI 3.1417 int x; // Global Variable x Declaration void show(); // Global Function Declaration /* The Below code is function definition */ void show(){ printf("Hellon"); }
  • 3. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR void main(){ int z,c; z=10; x=20; c=z+x; printf("%dn",x); printf("%fn",PI); printf("Result:%dn",c); show(); //function call } Output: 20 3.1417 30 Hello C Tokens: A token is a smallest basic unit of a C program. One or more characters are grouped in sequence to form meaningful words. These meaningful words are called tokens. The tokens in C language are broadly classified as shown below. Keywords: The keywords also known as reserved words. Keywords have predefined meaning in C language. They are reserved for specific purpose in C language. The rules to be followed while using keywords in Program  The keywords should not be used as variables, function names, array names etc.  The meaning of the keywords cannot be changed by the user.  All keywords should be written in lowercase letters. List of Keywords used in C language is as follows:
  • 4. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Identifiers: An identifier is a word consisting of sequence of one or more letters or digits along with “_” (read as underscore). The letters are the characters from ‘A’ to ‘Z’ or ‘a’ to z’. The digits are characters from ‘0’ to ‘9’. Using the identifiers we can give names to program elements such as variables, constants, function names, array names etc. The rules to be followed to frame an identifier are: a. The first character in the identifier should be a letter or underscore ( _ ) and can be followed by any number of letters or digits or underscores. b. No extra symbols are allowed other than letters, digits and underscores. c. The length of the identifier can be up to a maximum of 31 characters for external names such as function names and global variables that can be shared between source files. The length of the identifier can be up to maximum of 63 characters for internal names such as local variables. d. Reserved words (Keywords) cannot be used as identifiers. e. Identifiers can be case sensitive. It means that the words such as sum, SUM and Sum are all different identifiers. The following table gives valid identifiers and invalid identifiers and reasons for invalidity. S.NO Identifiers Valid/Invalid Reasons for invalidity 1 for Invalid It is a Keyword 2 3_factorial Invalid Should not start with a digit 3 Sum,1 Invalid Comma is not allowed 4 Sum-of-digits Invalid Minus(-) sign is not allowed 5 Sum= Invalid = is not allowed 6 If Invalid It is a Keyword 7 One+two Invalid + is not allowed 8 Sum! Invalid ! is not allowed 9 Int Invalid It is a Keyword 10 $roll no Invalid $ is not allowed 11 I Class Invalid Space is not allowed between words 12 James Bond Invalid Space is not allowed between words 13 _name1 Valid No Reason 14 _abc_name Valid No Reason 15 Abc123 Valid No Reason 16 _ Valid No Reason Difference between Identifier and Keyword S.NO Keywords Identifier 1 Keywords have pre-defined meaning Identifiers do not have a pre-defined meaning 2 Keywords are the instructions given to the compiler Identifiers are not the instructions given to the compiler. They are the names to variables, functions. 3 Keywords in C Language: int, float, char, do, while etc. Identifiers in C Language :sum, length, A1,a etc.
  • 5. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Constants: A constant is a value which will not change during the execution of a program. The constants cannot be modified in the program. For Example: - 10 is a integer constant. - 12.5 is a floating point constant. - “SVIT” is a string constant. - ‘A’,’B’ etc are character constants. The Constants in C language are classified as shown below: Integer Constant: An integer is a whole number without any decimal point or a fraction part. No extra characters are allowed other than ‘+’ and ‘-‘ sign. If ‘+’ sign and ‘-‘ sign are present, they should precede the number. An Integer constant can be classified into three types as shown below.  Decimal Integer: A Decimal integer can be any combination of digits from ‘0’ to ‘9’. In other words is a base 10 number. No extra characters are allowed other than ‘+’ and ‘-‘ sign. If ‘+’ and ‘-‘ sign are present, they should precede the number. For Example:- 10,140,-140 are valid.  Octal Integer: A Octal integer can be any combination of digits from ‘0’ to ‘7’ with a prefix zero(0). In other words is a base 8 number. No extra characters are allowed other than ‘+’ and ‘-‘ sign. If ‘+’ and ‘-‘ sign are present, they should precede the number. For Example:- 010,0777,- 065 are valid.  Hexadecimal Integer: A Hexadecimal integer can be any combination of digits from ‘0’ to ‘9’ along with the letters ‘A’ to ‘F’ or ‘a’ to ‘f’. In other words is a base 16 number. The constant has to be preceded by 0x or 0X. For Example: - 0x8A, 0XAB, 0XA123 etc are valid. Floating Point Constants: The floating point constants are base 10 numbers with fraction part such as 10.5. All negative numbers should have a prefix ‘-‘. A positive number can have an optional ‘+’ sign. No Extra characters are allowed. The floating point constants can be represented using two forms as shown below.  Fractional Form: A floating point number represented using fractional form has an integer part followed by a dot and a fractional part. For Example: = 0.5, -.99, -.6 etc.  Exponent Form: The floating point number represented using scientific notation has three parts namely: mantissa ,e/E ,exponent .Example:= 6.69e2 means 6.69x102
  • 6. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Character Constants: A character constant contains one single character enclosed within single quotes. Examples of valid character constants ‘a’ , ‘Z’, ‘5’ It should be noted that character constants have numerical values known as ASCII values, for example, the value of ‘A’ is 65 which is its ASCII value. C allows us to have certain non graphic characters in character constants. Non graphic characters are those characters that cannot be typed directly from keyboard, for example, tabs, carriage return, etc. Escape Sequence Description a Audible alert(bell) b Backspace f Form feed n New line r Carriage return t Horizontal tab v Vertical tab Backslash “ Double quotation mark ‘ Single quotation mark ? Question mark String Constants: String constants are sequence of characters enclosed within double quotes. For example:= “hello”, “abc” ,“hello911”. Every sting constant is automatically terminated with a special character ‘’ called the null character which represents the end of the string. For example, “hello” will represent “hello” in the memory. Thus, the size of the string is the total number of characters plus one for the null character. Special Symbols: The following special symbols are used in C having some special meaning and thus, cannot be used for some other purpose. For Example: [] () {} , ; : * … = #  Braces {}: These opening and ending curly braces marks the start and end of a block of code containing more than one executable statement.  Parentheses (): These special symbols are used to indicate function calls and function parameters.  Brackets [ ]: Opening and closing brackets are used as array element reference. These indicate single and multidimensional subscripts.
  • 7. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Data Types: The data type defines the type of data stored in a memory location. The data type determines how much memory is should be allocated for a variable. Various types of data such as integer constant, floating point constant, character constant etc. can be stored in memory during program execution. Data types in c are classified into 2 types.  Primitive Data types: The data types which can be manipulated by machine instructions are called primitive data types. They are also called as basic data types or simple data types or fundamental data types. The various primitive data types supported by C language are: = int, char, float, double etc.  Derived Data types: The various data types that are available in c are int, char, float, double etc. Using these primitives we can derive some other data types. The data types which are derived from basic data types are called Derived data types. For Example: = Arrays, Pointers, Enumerated, Structures, Unions. Data Type: int An int is a keyword in C which is used to define integers in C Language. Using int keyword, Programmer can inform the compiler that the data associated with keyword should be treated as integer. The size of int can be 2 or 4 or 8 bytes and is machine dependent. C supports three types of integer data type namely: short int, int, long int. sizeof(short int)<=sizeof(int)<=sizeof(long int). The range of values varies from machine to machine and compiler to compiler as shown below. S.NO Type Storage size Value range 1 short int 2 bytes -32,768 to 32,767 2 unsigned short int 2 bytes 0 to 65,535 3 Int 2 or 4 Bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 4 unsigned int 2 or 4 Bytes 0 to 65,535 or 0 to 4,294,967,295 5 long int 4 bytes -2,147,483,648 to 2,147,483,647 6 unsigned long int 4 bytes 0 to 4,294,967,295 Advantages of Integer: Since the integer constants are exact quantities, they are used for counting and indexing etc. Disadvantage: Higher range numbers cannot be represented. The numbers with fraction part cannot be represented and stored.
  • 8. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Data type: float  A float is a keyword which is used to define floating point numbers in C language. Using float keyword, the programmer can inform the compiler that the data associated with this keyword should be treated as floating point number. The size of float can range from machine to machine as shown below. S.NO Type Storage size Value range 1 float 4 bytes(16 bit Machine) 3.4E-38 to 3.4E38 2 Float 8 bytes(32 bit Machine) 1.7E-308 to 1.7E308 Advantages: The Number with fractional part or decimal points can be stored. Higher precision and range compared to integers. Disadvantages: Since they are not exact quantities, they are not used for counting, indexing etc. Data type: double  A double is a keyword which is used to define long floating point numbers in C language. Using double keyword, the programmer can inform the compiler that the data associated with this keyword should be treated as long floating point numbers. The size range of double varies from machine to machine as shown below. S.NO Type Storage size Value range 1 double 8 bytes(16 bit Machine) 1.7E-308 to 1.7E308 2 Double 16 bytes(32 bit Machine) 3.4E-4932 to 1.1E4932 Advantage: Higher precision and range when compared to floating point numbers. Data type: char  The char is a keyword which is used to define single character or sequence of characters (called string) in C language. Using char keyword, the programmer can inform the compiler that the data associated with this keyword should be treated as character. Each character stored in the memory is associated with a unique value called as ASCII (American Code For Information Interchange) value. The size of the char range is fixed in all the machines as shown below. S.NO Type Storage size Value range 1 char 1 bytes(16/32 bit Machine) -128 to 127 2 unsigned char 1 byte 0 to 255 Data type: void  The keyword void is used in functions. When function does not return any value, we use void keyword. It (void) is a empty data type. It does not occupy any space in the memory. The keyword is not associated with any variable except pointers.
  • 9. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Variable A variable is a name given to a memory location where the data can be stored. Using the variable name, the data can be stored in a memory location and can be accessed or manipulated very easily. Rules for defining a variable:  The first character in the variable should be a letter (from A to Z or a to z) or underscore.  The first character can be followed by any number of letters or digits(0 to 9) or underscores.  No extra symbols are allowed other than letters, digits and underscore.  The length of an identifier can be up to a maximum of 31 characters for external names such as function names and global variables.  C keywords cannot be used as variable names. Valid and invalid variables are shown below S.NO Variable Valid/Invalid Reasons for invalidity 1 For Invalid It is a Keyword 2 3_factorial Invalid Should not start with a digit 3 Sum,1 Invalid Comma is not allowed 4 Sum-of-digits Invalid Minus(-) sign is not allowed 5 Sum= Invalid = is not allowed 6 If Invalid It is a Keyword 7 One+two Invalid + is not allowed 8 Sum! Invalid ! is not allowed 9 Int Invalid It is a Keyword 10 $roll no Invalid $ is not allowed 11 I Class Invalid Space is not allowed between words 12 Space1999 Valid No Reason 13 Principle_amount Valid No Reason 14 A Valid No Reason 15 _ _ a_ _ _ b Valid No Reason 16 For1 Valid No Reason Declaring and defining a variable: Giving name to a memory location is called declaring a variable. Declaration of a variable will not reserve the space in the memory. Reserving the required memory space to store the data using the declared Variable is called defining a variable. Syntax: type variable1,variable2………variable  Where variable1, variable2 are variable names. All the variables should be separated by commas.  Type is a type of variables variable1,variable2 …. variableN the type of variable may be int, float, char, double etc.  Semicolon is must at the end of the declaration. Example: int a; float b; char c; int a1,a2,a3,a4;
  • 10. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Variable declaration can be done in the following way:  All the variables of the same data type can be defined separately using multiple statements in multiple lines for better readability as shown below. int a; // a is an integer variable. int b; // b is an integer. int c; //c is an integer  Multiple declarations are allowed in the same line whenever the declaration statements end with semicolon as follows.  The variables with different data types can be defined in different lines. Variable Initialization/Assignment: Variables are not initialized when are declared and defined. Hence, variables normally contain garbage values (meaningless values) and hence they have to be initialized with valid data. The method of giving the initial values for the variables before they are processed is called initialization of variables. The variable initialization can be done using 3 ways.  Initializing only one variable: The syntax to initialize only one variable is shown below. Type var_name = data; Where type : It can be int, float, char, double etc. var_name : must be name of a variable. = : is an Assignment operator Data: is the value to be stored in memory. Example: int a=10; //a is assigned the value 10.
  • 11. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR  Initializing more than variable while defining: All the variables defined must be separately initialized. For Example int count=0,sum=10; //count is assigned the value 10 and sum is assigned the value 10  Initializing at appropriate place: To avoid most of the Programming errors, it is always better to initialize the variables at the appropriate place in the body of the program as shown below. Input and Output Functions Definition of Input Functions: The input functions accept the data from the keyboard and store in memory locations. These functions that help the user to input the data from input devices such as keyboard and transfer the data to memory locations are called input functions. For Example, scanf(), getch(), getche(), getchar() etc. Definition of output Functions: The output functions receive the data stored in the memory and send the data to output devices. These functions that help the user to send the data stored in memory to the output devices such as monitor or printer are called output functions. The monitor is the standard output device and printf() is the standard output function. So by using printf(), the data can be displayed on the monitor. For Example, printf(), puts(), putch(), putchar() etc.. Input output functions are classified into formatted and un-formatted input/output functions.
  • 12. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Unformatted I/O functions: There are mainly six unformatted I/O functions discussed as follows: getchar():This function is an Input function. It is used for reading a single character from the keyborad. It is buffered function. Buffered functions get the input from the keyboard and store it in the memory buffer temporally until you press the Enter key. The general syntax is as: v = getchar(); where v is the variable of character type. putchar(): This function is an output function. It is used to display a single character on the screen. The general syntax is as: putchar(v); where v is the variable of character type. /*Program illustrate the use of getchar() and putchar() functions*/ #include<stdio.h> void main() { char n; n = getchar(); putchar(n); } Output: Hello H getch():This is also an input function. This is used to read a single character from the keyboard like getchar() function. But getchar() function is a buffered is function, getchar() function is a non-buffered function. The character data read by this function is directly assign to a variable rather it goes to the memory buffer, the character data directly assign to a variable without the need to press the Enter key. Another use of this function is to maintain the output on the screen till you have not press the Enter Key. The general syntax is as: v = getch(); where v is the variable of character type. putch(): This function is an output function. It is used to display a single character on the screen. The general syntax is as: putch(v); where v is the variable of character type. /*Program to explain the use of getch() function*/ #include<stdio.h> void main() { char n; puts("Enter the Char"); n = getch(); puts("Char is :"); putch(n); } OUTPUT: Enter the Char Char is L
  • 13. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR getche(): All are same as getch() function execpt it is an echoed function. It means when you type the character data from the keyboard it will visible on the screen. The general syntax is as: v = getche(); // where v is the variable of character type. A simple C program to illustrate the use of getch() function: /*Program to explain the use of getch() function*/ #include<stdio.h> void main() { char n; puts("Enter the Char"); n = getche(); puts("Char is :"); putch(n); } OUTPUT IS: Enter the Char L Char is L gets(): This function is an input function. It is used to read a string from the keyboard. It is also buffered function. It will read a string, when you type the string from the keyboard and press the Enter key from the keyboard. It will mark nulll character ('0') in the memory at the end of the string, when you press the enter key. The general syntax is as: gets(v); // where v is the variable of character type. puts(): This is an output function. It is used to display a string inputted by gets() function. It is also used to display an text (message) on the screen for program simplicity. This function appends a newline ("n") character to the output. The general syntax is as: puts(v); or puts("text line"); where v is the variable of character type. A simple C program to illustrate the use of puts() function: /*Program to illustrate the concept of puts() with gets() functions*/ #include<stdio.h> void main() { char name[20]; puts("Enter the Name"); gets(name); puts("Name is :"); puts(name); } OUTPUT IS: Enter the Name Vijay Name is: Vijay
  • 14. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Formatted I/O functions: Formatted I/O functions which refers to an Input or Ouput data that has been arranged in a particular format. There are mainly two formatted I/O functions discussed as follows:  scanf()  printf() scanf(): The scanf() function is an input function. It used to read the mixed type of data from keyboard. You can read integer, float and character data by using its control codes or format codes. The general syntax is as: scanf("Format strings",&v1,&v2,&v3,................&vn); Where arg1,arg2,..........argn are the arguments for reading and v1,v2,v3,........vn all are the variables. The scanf() format code (specifier) is as shown in the below table: Format String Meaning %d Scan or print an integer as signed decimal number %f Scan or print a floating point number %c To scan or print a character %s To scan or print a character string. The scanning ends at whitespace. printf(): This ia an output function. It is used to display a text message and to display the mixed type (int, float, char) of data on screen. The general syntax is as: printf("control strings",&v1,&v2,&v3,................&vn); or printf("Message line or text line"); // Where v1,v2,v3,........vn all are the variables. The control strings use some printf() format codes or format specifiers or conversion characters. Format String Meaning %d Scan or print an integer as signed decimal number %f Scan or print a floating point number %c To scan or print a character %s To scan or print a character string. The scanning ends at whitespace.
  • 15. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR /*Below the program which show the use of printf() and scanf() function*/ #include<stdio.h> void main() { int a; float b; char c; printf("Enter the mixed type of data"); scanf("%d,%f,%c",&a,&b,&c); printf("%d%f%c ",a,b,c); } Output: Enter the mixed type of data 6 10.5 K 6 10.5 K Operators and Expressions: Operator: An operator is a symbol that specifies the operation to be performed on various types of operands. For Example,  The symbol + indicates add operation  The symbol - indicates subtraction operation  The symbol * indicates multiplication operation  The symbol / indicates division operation Operand: A constant or a variable or a function which returns a value is an operand. An operator may have one or two or three operands. Expression: A sequence of operands and operators that reduces to a single value is an expression. In the above expressions, the identifiers a and b are operands and the symbols ‘+’,‘-‘,‘*’,‘/’ are operators. Operators are classified in C language based on  The Number of operands an operator has.  The type of operation being performed. Classification based on the number of operands: The operators are classified in to four major categories based on the number of operands as shown below. Unary operators, Binary operators, Ternary Operator, Special operators
  • 16. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Unary operators: An operator that operates on one operand to produce a result is called unary operator. In the expressions involving unary operators, the operators precede the operand. Ex: -10, -1, a*b etc. Binary operators: An operator that operates on two operands in an expression to produce a result is called binary operator. In an expression involving binary operators, the operator is in between two operands. Ex: a + b, a * b, a - b, a / b, a % b. Ternary operator: An operator that operates on three operands to produce a result is called a ternary operator. Ternary operator is also called conditional operator. Ex: a ? b : c Since there are three operands a, b and c that are associated with operators ? and : It is called Ternary operator(?:) and Conditional operator. Classification based on the type of operation: Operators are classified based on the type of operation being performed as shown below:  Arithmetic Operators  Relational Operators  Logical Operators  Bitwise Operators  Assignment Operators  Special Operators
  • 17. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Arithmetic Operators: The operators those are usedto perform addition,subtraction, multiplication, division and modulus operators are called arithmetic operators. These operators perform operations on two operands and hence they are called Binary operators. The following table shows all the arithmetic operators supported by the C language. Assume variable A holds 10 and variable B holds 20 then Operator Description Example + Adds two operands. A + B = 30 − Subtracts second operand from the first. A − B = -10 * Multiplies both operands. A * B = 200 / Divides numerator by de-numerator. B / A = 2 % Modulus Operator and remainder of after an integer division. B % A = 0 ConversionofExpressions: S.NO Mathematical Expressions C Equivalent Expressions 1 𝒂 𝒃 a/b 2 𝐬 = 𝒂 + 𝒃 + 𝒄 𝟐 s=(a+b+c)/2 3 𝒂𝒓𝒆𝒂 = √𝐬(𝐬 − 𝐚)(𝐬 − 𝐛)(𝐬 − 𝐜) area=sqrt(s*(s-a)*(s-b)*(s-c)) 4 sin ( 𝒃 √a + 𝑏2 ) sin(b/sqrt(a*a+b*b)) 5 𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 a*x*x+b*x+c 6 𝜏1 = √( 𝜎𝑥 − 𝜎𝑦 𝟐 ) + 𝜏𝑥𝑦2 tow1= sqrt((rowx-rowy)/2+tow*x*y*y) 7 𝜏1 = √( 𝜎𝑥 − 𝜎𝑦 𝟐 ) 2 + 𝜏𝑥𝑦2 tow1= sqrt(pow((rowx-rowy)/2,2)+tow*x*y*y) 8 𝑥 = −𝑏 + √𝑏2 − 4𝑎𝑐 2𝑎 x=(-b+sqrt(b*b-4*a*c)/(2*a) 9 𝑒|𝑎|+𝑏 𝒙+𝒚 (2x+3) exp(abs(a)+b)/(x+y)*(2*x+3) 10 Y= (𝛼+𝛽) 𝒔𝒊𝒏𝜽 +|x| Y=(alpha+beta)/sin(theta*3.1416/180)+abs(x)
  • 18. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Type Conversion: InC language,theprogrammercan instruct thecompilertoconvert the datafrom onedata type to another datatype .Some timethecompileritselfconverts thedata from onedatatype to another datatype. The process of converting the data from one data type to another data type is called type conversion. Type conversioncanbeoftwotypes:  Implicit type conversion: The process of conversion of data from lower rank to higher rank automatically by the C compiler is called implicit type conversion. If both operands are of same type for addition, multiplication and subtraction no conversion takes place. If one operand is int type and another operand is float type then type int is promoted to float. Promotion hierarchy is shown as follows: Size of(char)< Size of(short int)<Size of(int)< Size of(unsigned int)< Size of(long int) < Size of(unsigned long int)< Size of(float)< Size of(double). Advantages: Compiler does conversion automatically from lower rank to higher rank. Programmer need not worry about the type conversion procedure or syntax. Disadvantages: If both operands are of the same data type, implicit type conversion is not possible when we want to perform division operation. The conversion of higher rank to lower rank is not done automatically by the compiler.  Explicit type conversion: If the operands are of the same type no conversion takes place by the compiler. Sometimes, the type conversion is required to get the desired results. In such case the programmer can instruct the compiler to change the type of the operand from one data type to another data type. The forcible conversion from one data type to another is called explicit type conversion. The syntax is shown (type) expression Ex: (int) 9.43 converted to 9 only Where type is the required type of the expression. Expression can be an operand such as variable or a constant. Example: 1
  • 19. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Example: 2 Modes of Arithmetic Expressions: There are three types of arithmetic expressions.  Integer Expression: If all the operands in an expression are integers, the expression is called integer expression. An integer expression always yields an integer result. Example: 4/2 =2 4/3=1 (Note: answer is 1 not 1.3333) 3/4=0 (Note: answer is 0 not 0.750)  Floating point Expression: If all the operands in an expression are floating point numbers or double values, the expression is called floating point expression. A floating point expression always yields a floating point result. Example: 4.0/2.0 =2.000000 4.0/3.0= 1.333333 3.0/4.0= 0.750000  Mixed Mode (type) expressions: An expression that has operands of different data types is called mixed mode expression or mixed type expression. During evaluation, data conversion takes place from lower rank to higher rank so that type of operands remains same. Example: 4.0/3 =2.000000 4./3= 1.333333 Arithmetic operators along with priority and associativity as follows Arithmetic Operators Description Operator Priority Associativity Precedence Multiplication * 1 Left to Right Highest Precedence Division / 1 Left to Right Modulus % 1 Left to Right Addition + 2 Left to Right Lower Precedence Subtraction - 2 Left to Right
  • 20. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Relational Operators: The operators that are used to find the relationship between two operands are called relational operators. The two operands may be constants, variables or expressions. The relationship between two operand a values results in true (whose value is 1) or false (whose value is 0). The relational operators and the meaning associated with them are shown in the table below. Relational Operators Description Operator Priority Associativity Less than < 1 Left to Right Greater than > 1 Left to Right Less than/equal <= 1 Left to Right Greater than/equal >= 1 Left to Right Equal = = 2 Left to Right Not Equal != 2 Left to Right 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 = = Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (A == B) is not true. != Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true. > Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (A > B) is not true. < Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (A < B) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (A >= B) is not 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. (A <= B) is true. Logical Operators: The operators that are used to combine two or more relational expressions are called logical operators. Since the output of the relational expression is true or false, the output of the logical expression is also true(whose value is 1) or false(whose value is 0). The logical operators and the meaning associated with them are shown in the table below. Logical Operators Description Operator Priority Associativity Not(unary operator) ! 1 Left to Right and(binary operator) && 2 Left to Right or(binary operator) || 3 Left to Right
  • 21. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Logical and operation how it works: Result of the logical && operator is true if and only if both the relational expression is true otherwise it is false. The logical && table is as follows. Operand1 Operator(&&) Operand2 Result TRUE(1) && TRUE(1) TRUE(1) TRUE(1) && FALSE(0) FALSE(0) FALSE(0) && TRUE(1) FALSE(0) FALSE(0) && FALSE(0) FALSE(0) Example: int a=3,b=5,c=10; a<b && c>b 1 && 1 1 Logical OR operation how it works: Result of the logical || operator is true at least any one of the relational expression is true otherwise it is false. The logical && table is as follows. Operand1 Operator(||) Operand2 Result TRUE(1) || TRUE(1) TRUE(1) TRUE(1) || FALSE(0) TRUE(1) FALSE(0) || TRUE(1) TRUE(1) FALSE(0) || FALSE(0) FALSE(0) Example: int a=3,b=5,c=10; a>b || c>b 0 || 1 1 Logical not operation how it works: Result of the logical not operator is true if the operand is false and the result is false if the operand is true. The logical not table is as follows. Operand1 !Operand TRUE(1) FALSE(0) FALSE(0) TRUE(1) The precedence rules to be followed while evaluating the expression is shown below.  Parenthesis  Unary Expressions  Arithmetic expressions  Relational expressions  Logical Expressions
  • 22. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Problem 1: Evaluate the expression a+2 > b && !c || a!=d && a-2 <=e where a=11, b=6, c=0, d=7 and e=5 Solution: Problem 2: Evaluate the expression a+2 > b || !c && a==d || a-2 <=e where a=11, b=6, c=0, d=7 and e=5 Solution: Assignment Operators: An assignment operator which is used to assign the data or result of an expression into a variable is called an assignment operator. Copying a value into memory location is called assigning and hence the name .The assignment operator is denoted by ‘=’ sign. There are three types of assignment statements are shown below.
  • 23. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR  Simple Assignment: Ex: a=a+10; The syntax of simple assignment is as follows Where  The expression can be a constant or a variable or any expression. The symbol ‘=’ is an assignment operator. The expression on right side of assignment operator is evaluated and the result is converted into type of variable on the left hand side of assignment operator. The converted data is copied into a variable which is present on the LHS of the operator. Ex:1 a = 10 ; //RHS is a constant Ex:2 a = b ; //RHS is a variable Ex:1 a = x + y ; //RHS is a expression  Shorthand assignment statement: The operators such as +=, - =, *=, /=, %= are called shorthand assignment operators. The assignment statements that use these operators are called shorthand assignment statements. These statements are also called compound statements. Examples of shorthand assignment and their meaning is as shown below: Shorthand statement Meaning Explanation a+=2 a=a+2 Evaluate a+2 and store the result in a a- =2 a=a-2 Evaluate a-2 and store the result in a a*=2 a=a*2 Evaluate a*2 and store the result in a a%=2 a=a%2 Evaluate a%2 and store the result in a a/=2 a=a/2 Evaluate a/2 and store the result in a  Multiple assignment statement: A statement using which a value or set of values are assigned to different variables is called multiple assignment statement. The assignment operator ‘=’ can be used to assign a single value to more than one variable. i=10; j=10; k=10; the above simple assignment can be written using multiple assignment is : i = j = k = 10; assignment operator uses right to left associativity. Simplify the expression: x*=y+3 when x=10 and y=5
  • 24. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Simplify the expression: a+=b*=c- =5 when a=1 and b=3 and c=7 Increment and decrement operator Increment Operator: ++ is an increment operator. This is a unary operator. It increments the value of a variable by one. The increment operator is classified into two categories.  Post increment: If the increment operator ++ is placed immediately after the operand, then the operator is called post increment. As the name indicates, use the operand value first and then increment the operand value by 1.  Pre increment: If the increment operator ++ is placed before the operand, then the operator is called pre increment. As the name indicates, first increment the operand value by 1 and then use the operand value later.
  • 25. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Sample Program to demonstrate post increment #include<stdio.h> void main(){ int a=20; int b; b = a++; printf("%dn", a); printf("%dn", b); } Output: 21 20 Sample Program to demonstrate pre increment #include<stdio.h> void main(){ int a=20; int b; b = ++ a; printf("%dn", a); printf("%dn", b); } Output: 21 21
  • 26. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Decrement Operator: - - is an decrement operator. This is a unary operator. It decrements the value of a variable by one. The decrement operator is classified into two categories.  Post decrement: If the decrement operator - - is placed immediately after the operand, then the operator is called post decrement. As the name indicates, use the operand value first and then decrement the operand value by 1.  Pre decrement: If the decrement operator - - is placed before the operand, then the operator is called pre decrement. As the name indicates, first decrement the operand value by 1 and then use the operand value later. Sample Program to demonstrate post decrement #include<stdio.h> void main(){ int a=20; int b; b = a--; printf("%dn", a); printf("%dn", b); }
  • 27. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Output: 19 20 Sample Program to demonstrate pre decrement #include<stdio.h> void main(){ int a=20; int b; b = - - a; printf("%dn", a); printf("%dn", b); } Output: 19 19 Conditional Operator: This is also called as ternary operator. As the name indicates, an operator that operates on three operands is called ternary operator. The ternary operators are ? and : The syntax is as follows: Exp1 ? Exp2 : Exp3 ;  Exp1 is an expression evaluated to true or false.  If Exp1 is evaluated to true, Exp2 is executed.  If Exp1 is evaluated to false, Exp3 is executed. Program to demonstrate largest of two numbers using Ternary or conditional operator #include<stdio.h> void main(){ int a,b,large; printf("Enter two numbersn"); scanf("%d%d",&a,&b); large=(a>b)?a:b; printf("Largest of two numbers=%dn",large); } Output: Enter Two numbers 10 20 Largest of two numbers=20
  • 28. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Program to demonstrate largest of two numbers using Ternary or conditional operator #include<stdio.h> void main(){ int a,b,c,large; printf("Enter three integer numbersn"); scanf("%d%d%d",&a,&b,&c); large = (a>b) ? (a>c) ? a : c : (b>c) ? b : c; printf("Largest of two numbers=%dn",large); } Output: Enter Three integer numbers 10 20 30 Largest of three numbers=30 Bitwise Operators: Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and ^ is as follows – P Q p & q p | q p ^ q 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0 0 1 1 Assume A = 60 and B = 13 in binary format, they will be as follows − A = 0011 1100 B = 0000 1101 ------------- A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011
  • 29. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then – Show Examples Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, i.e., 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, i.e., 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i.e., 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = -61, i.e,. 1100 0011 in 2's complement form. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240 i.e., 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15 i.e., 0000 1111 Special operators:  Comma operator: Comma (’,’) is a special operator. To declare and initialize more than one variable we use comma operator. For Example int a, b, c; // a b c are variables int a=10, b=20, c=30; // initialize a b c variables b=(a=12,345) // initialize 12 in to a and 345 into b  Size of operator: This operator is used to determine the size of each different data types used in C language. #include<stdio.h> void main(){ printf("Size of char=%dn",sizeof(char)); printf("Size of int=%dn",sizeof(int)); printf("Size of long int=%dn",sizeof(long int)); printf("Size of float=%dn",sizeof(float)); printf("Size of double=%dn",sizeof(double)); }
  • 30. Programming In C & Datastructure 17PCD13/23 DEPARTMENT OF CSE,NIT RAICHUR Output: Size of char=1 Size of int=4 Size of long int=4 Size of float=4 Size of double=8 Pseudocode: Pseudocode is nothing but a series of steps to solve a given problem written using a mixture of English language and C like language. It is very easy for the programmer to convert the pseudocode into actual C program. So, pseudocode acts a problem-solving tool. Writing a pseudocode is the first step in writing a Program. Problem: Display list of numbers from 5 to 10 along with their squares in the following format n n2 Pseudocode solution to the above problem: “Start with first number i,e 5 and compute its square. Print the number along with its square. The process is repeated for each of the other numbers 6 to 10” Solution 2: Get the First Number Compute square of the number Print the number and its square. Do the above activities for remaining numbers.