SlideShare une entreprise Scribd logo
1  sur  10
OPERATOR
Operator is a symbol that is used to combine data items such as constant, variable,
function reference and array element to form an expression. The data item that acts
upon is called Operand.
e.g.
c = a + b
in this expression, = and + are the operators which combines variables a, b and c.
There are 7 basic types of operators in C.
UNARY OPERATOR
Operator that acts upon a single operand is known as Unary Operator. Some unary
operators are -, ++, --, sizeof, type
e.g.
-x; i++; sizeof(a); (float) 5/3;
Program 1
/* Program to differentiate use of unary operator as prefix and suffix */
#include<stdio.h>
void main()
{
int x=5;
printf("%d",x);
printf("%d",x++);
printf("%d",x);
printf("%d",++x);
printf("%d",x);
}
Program 2
/* Program to illustrate use of sizeof and type operators */
#include<stdio.h>
void main()
{
int x=5,y=4;
float z;
printf("size of x is %d bytes",sizeof(x));
printf("%d",x/y);
printf("size of z is %d bytes",sizeof(z));
printf("%f",(float)x/y);
}
ARITHMETIC OPERATOR
The operator which is used for general mathematical operations, is called Arithmetic
operator. The five arithmetic operators are +, -, *, /, %.
Here, % is known as modulus operator which returns reminder after integer division.
Program 3
/* to differentiate /(division) and % (modulus operator)*/
void main()
{
int x=5,y=2, z1,z2;
z1=x/y;
z2=x%y;
printf("n Quotient : %d",z1);
printf("n Remainder : %d",z2);
}
RELATIONAL OPERATOR
Relational operators are used to compare two data items. There are four relational
operators, they are >, <, >=, <=.
e.g.
(x>y)
Program 4
/* Program to check whether user can or cannot vote*/
main()
{
int age=25;
if(age>=18)
printf("n You can vote.");
else
printf("n You cannot vote.");
}
EQUALITY OPERATOR
Equality operator is also used to compare two data items whether they are equal or not.
There are two equality operators, ==, !=.
Program 5
/* Program to show whether input number is positive, zero or negative*/
void main()
{
int n;
if(n>0)
printf("n %d is positive.",n);
else if(n==0)
printf("n %d is zero.",n);
else
printf("n The number %d is negative");
}
LOGICAL OPERATOR
The logical operator acts upon operands those are themselves logical expression. Such
logical expressions are combined together to form more complex expression. The two
logical operators are logical and (&&) and Logical or (||).
Program 6
/* Program to find highest number among three input integer numbers */
void main()
{
int x,y,z;
printf("n enter three integer numbers");
scanf("%d%d%d",&x,&y,&z);
if(x>y && x>z)
printf("%d is the highest number",x);
else if(y>x && y>z)
printf("%d is the highest number",y);
else
printf("%d is the highest number",z);
}
Program 7
/* Program to check whether user can or cannot apply for DV */
#include<stdio.h>
void main()
{
int age;
printf("n enter your age");
scanf("%d",&age);
printf("n enter your qualification (no. of years)");
scanf("%d",&qual);
printf("n enter your experience (no. of years)");
scanf("%d",&exp);
if(age >= 18 && (qual >= 12 || exp >= 2)
printf(" You can apply");
else
printf("You cannot apply ");
}
CONDITIONAL OPERATOR
Simple conditional operator can be carried out with conditional operator (? :). It can be
written in place of if...else statement. It can be used in this form.
expression1 ? expression2 : expression3;
e.g.
(age>=18) ? printf("can vote") : printf("cannot vote");
/* Program to check whether user can or cannot vote using conditional
operator*/
ASSIGNMENT OPERATOR
This type of operator assigns some value to left of operator after executing expression
to its right. Some assignment operators are =, +=, -=, *=, /=, %=.
e.g.
x+=2 equivalent to x=x+2
x-=3 equivalent to x=x-3
x*=2 equivalent to x=x*2
x/=2 equivalent to x=x/2
x%=2 equivalent to x=x%2
Operator precedence
Precedence is the hierarchical order of operators. Operation with higher
precedence group is carried out before lower precedence group.
Another important consideration is the order in which consecutive operations
within the same precedence group is carried out. This is known as associativity.
Type Operators Associativity
Unary -, ++, --, sizeof, type R  L
Arithmetic *, /, % L  R
+, - L  R
Relational <, <=, >, >= L  R
Equality ==, != L  R
Logical && L  R
| | L  R
Conditional ? : R  L
Assignment =, +=, -=, *=, /=, %= R  L
e.g. a – b/c * d
First, it evaluates b/c then multiplies with d and finally result is subtracted from a.
c+= (a>0 && a<=10) ? ++a : a/b;
if a, b & c have values 1, 2 & 3 respectively, then result will be c=5;
if a, b & c have values 50, 10 & 20 respectively, then result will be c=25;
Library Functions
Library functions are pre-defined module in header files which can easily be accessed
anywhere in the program by including its respective header file. Library function is the
one of the important feature of C language. Because of huge collection of library
function, it makes very easy to programmers for programming.
Library functions are defined for
 commonly used operations such as sqrt to find square root, pow to find power of
any base etc.
 machine dependent standard input/output operations.
A library function is accessed simply by writing the function name, followed with list of
arguments that represent information being passed to the function. The arguments must
be enclosed in parentheses and separated by commas. The argument can be constant,
or variable. But the parentheses must be present, even if there is no argument.
Some commonly used library functions:
abs(i) - returns absolute value of i
clrscr() - to clear screen
getch() - to input a character without echo
getche() - to input a character with echo
tolower(c) – to convert a letter to lowercase
toupper(c) – to convert a letter to uppercase
sin(d) - return sine of d
cos(d) - return cosine of d
tan(d) - return tangent of d
sqrt(d) - return square root of d
pow(d1,d2) - return d1 raised to power d2
log(d) - return natural logarithm to d
exp(d) - return e raise to power d
Program 8
/*To convert an input char to capital letter*/
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void main()
{
char c;
c=getche();
putch(toupper(c));
}
Program 9
/*To find value of sine of angle 30 degree*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PI 3.141593
void main()
{
float x=30;
clrscr();
printf("%f",sin(x*(PI/180)));
getch();
}
Program 10
/*To find value of antilog of log of specified no.*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x=1;
clrscr();
printf("%f",log(x));
printf("%f",exp(log(x)));
getch();
}
Some more about Escape Sequence:
Character Escape Sequence
bell a
backspace b
horizontal tab t
new line n
form feed f
quotation mark (") "
apostrophe (') '
question mark ?
backslash 
null 0
Input data using Scanf Function
This function can be used to enter any combination of numerical values, single
characters and strings. The function returns the number of data items that have been
entered successfully.
scanf(control string, arg1,arg2,...argn)
where control string refers to a string containing certain required formatting
information. arg1, arg2, ... argn represents the individual input data items.
Within the control string comprises individual groups of characters, with one character
group of each input data item. Each character group must begin with a percent sign (%)
followed with a conversion character which indicates the type of the corresponding
data items.
Conversion
character Meaning
c data item is a single character
d data item is an integer without decimal
e data item is a floating point value in exponent form
f data item is a floating point value
g data item is a floating point value without trailing zeros.
h data item is a short integer
i data item is a signed integer number
o data item is an octal integer
s data item is a string followed by a whitespace character
u data item is unsigned integer
[...] data item is a string which may include white space
/* To print a line of input text without white
space */
#include<stdio.h>
#include<conio.h>
void main()
{
char x[20];
printf("n Enter a string");
scanf("%s",x);
printf("%s",x);
getch();
}
/* To print a line of input text with white space
*/
#include<stdio.h>
#include<conio.h>
void main()
{
char x[20];
printf("n Enter a string");
scanf("%[^n]",x);
printf("%s",x);
getch();
}
/*displaying a floating point number with diff format*/
#include<stdio.h>
#include<conio.h>
void main()
{
float x=123.456;
printf("%f %.2f ",x,x);
printf("%e %.2e",x,x);
printf("%g %.2g",x,x);
getch();
}
Output:
123.456000 123.46
1.234560e+02 1.23e+02
123.456 1.23e+02
Program 11
/* To convert an integer number into hexadecimal number */
#include<stdio.h>
#include<conio.h>
void main()
{
int x=65;
clrscr();
printf("%x",x);
printf("%o",x);
}
Program 12
/* To print unsigned integer and long integer number */
#include<stdio.h>
#include<conio.h>
void main()
{
unsigned int x=65535;
long int y=2000000000;
printf("n%u",x);
printf("n%ld",y);
getch();
}
Program 13
/* To print long integer number */
#include<stdio.h>
#include<conio.h>
void main()
{
long int x=2000000000;
printf("n%ld",x);
getch();
}

Contenu connexe

Tendances (20)

7. input and output functions
7. input and output functions7. input and output functions
7. input and output functions
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
 
What is c
What is cWhat is c
What is c
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Operators in C Programming
Operators in C ProgrammingOperators in C Programming
Operators in C Programming
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Data Input and Output
Data Input and OutputData Input and Output
Data Input and Output
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Input Output Management In C Programming
Input Output Management In C ProgrammingInput Output Management In C Programming
Input Output Management In C Programming
 
Learning the C Language
Learning the C LanguageLearning the C Language
Learning the C Language
 
Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Input And Output
 Input And Output Input And Output
Input And Output
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 
functions in C
functions in Cfunctions in C
functions in C
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 

Similaire à 2. operator

2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2Don Dooley
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structuresPradipta Mishra
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures Pradipta Mishra
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Hazwan Arif
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptxManojKhadilkar1
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSivakumar R D .
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariTHE NORTHCAP UNIVERSITY
 
Operators in c language
Operators in c languageOperators in c language
Operators in c languageAmit Singh
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxKrishanPalSingh39
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 

Similaire à 2. operator (20)

2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
Introduction to programming c and data structures
Introduction to programming c and data structuresIntroduction to programming c and data structures
Introduction to programming c and data structures
 
Introduction to programming c and data-structures
Introduction to programming c and data-structures Introduction to programming c and data-structures
Introduction to programming c and data-structures
 
Functions
FunctionsFunctions
Functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Array Cont
Array ContArray Cont
Array Cont
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
C Programming
C ProgrammingC Programming
C Programming
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
Operators in c language
Operators in c languageOperators in c language
Operators in c language
 
7 functions
7  functions7  functions
7 functions
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Tut1
Tut1Tut1
Tut1
 
Function in c program
Function in c programFunction in c program
Function in c program
 

Plus de Shankar Gangaju (20)

Tutorial no. 8
Tutorial no. 8Tutorial no. 8
Tutorial no. 8
 
Tutorial no. 7
Tutorial no. 7Tutorial no. 7
Tutorial no. 7
 
Tutorial no. 6
Tutorial no. 6Tutorial no. 6
Tutorial no. 6
 
Tutorial no. 3(1)
Tutorial no. 3(1)Tutorial no. 3(1)
Tutorial no. 3(1)
 
Tutorial no. 5
Tutorial no. 5Tutorial no. 5
Tutorial no. 5
 
Tutorial no. 4
Tutorial no. 4Tutorial no. 4
Tutorial no. 4
 
Tutorial no. 2
Tutorial no. 2Tutorial no. 2
Tutorial no. 2
 
Tutorial no. 1.doc
Tutorial no. 1.docTutorial no. 1.doc
Tutorial no. 1.doc
 
What is a computer
What is a computerWhat is a computer
What is a computer
 
Pointer
PointerPointer
Pointer
 
Array
ArrayArray
Array
 
9.structure & union
9.structure & union9.structure & union
9.structure & union
 
6.array
6.array6.array
6.array
 
5.program structure
5.program structure5.program structure
5.program structure
 
4. function
4. function4. function
4. function
 
3. control statement
3. control statement3. control statement
3. control statement
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
Ads lab
Ads labAds lab
Ads lab
 
Electromagnetic formula
Electromagnetic formulaElectromagnetic formula
Electromagnetic formula
 
Electromagnetic formula
Electromagnetic formulaElectromagnetic formula
Electromagnetic formula
 

Dernier

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Dernier (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

2. operator

  • 1. OPERATOR Operator is a symbol that is used to combine data items such as constant, variable, function reference and array element to form an expression. The data item that acts upon is called Operand. e.g. c = a + b in this expression, = and + are the operators which combines variables a, b and c. There are 7 basic types of operators in C. UNARY OPERATOR Operator that acts upon a single operand is known as Unary Operator. Some unary operators are -, ++, --, sizeof, type e.g. -x; i++; sizeof(a); (float) 5/3; Program 1 /* Program to differentiate use of unary operator as prefix and suffix */ #include<stdio.h> void main() { int x=5; printf("%d",x); printf("%d",x++); printf("%d",x); printf("%d",++x); printf("%d",x); } Program 2 /* Program to illustrate use of sizeof and type operators */ #include<stdio.h> void main() { int x=5,y=4; float z; printf("size of x is %d bytes",sizeof(x)); printf("%d",x/y); printf("size of z is %d bytes",sizeof(z)); printf("%f",(float)x/y); }
  • 2. ARITHMETIC OPERATOR The operator which is used for general mathematical operations, is called Arithmetic operator. The five arithmetic operators are +, -, *, /, %. Here, % is known as modulus operator which returns reminder after integer division. Program 3 /* to differentiate /(division) and % (modulus operator)*/ void main() { int x=5,y=2, z1,z2; z1=x/y; z2=x%y; printf("n Quotient : %d",z1); printf("n Remainder : %d",z2); } RELATIONAL OPERATOR Relational operators are used to compare two data items. There are four relational operators, they are >, <, >=, <=. e.g. (x>y) Program 4 /* Program to check whether user can or cannot vote*/ main() { int age=25; if(age>=18) printf("n You can vote."); else printf("n You cannot vote."); } EQUALITY OPERATOR Equality operator is also used to compare two data items whether they are equal or not. There are two equality operators, ==, !=. Program 5 /* Program to show whether input number is positive, zero or negative*/
  • 3. void main() { int n; if(n>0) printf("n %d is positive.",n); else if(n==0) printf("n %d is zero.",n); else printf("n The number %d is negative"); } LOGICAL OPERATOR The logical operator acts upon operands those are themselves logical expression. Such logical expressions are combined together to form more complex expression. The two logical operators are logical and (&&) and Logical or (||). Program 6 /* Program to find highest number among three input integer numbers */ void main() { int x,y,z; printf("n enter three integer numbers"); scanf("%d%d%d",&x,&y,&z); if(x>y && x>z) printf("%d is the highest number",x); else if(y>x && y>z) printf("%d is the highest number",y); else printf("%d is the highest number",z); } Program 7 /* Program to check whether user can or cannot apply for DV */ #include<stdio.h> void main() { int age; printf("n enter your age");
  • 4. scanf("%d",&age); printf("n enter your qualification (no. of years)"); scanf("%d",&qual); printf("n enter your experience (no. of years)"); scanf("%d",&exp); if(age >= 18 && (qual >= 12 || exp >= 2) printf(" You can apply"); else printf("You cannot apply "); } CONDITIONAL OPERATOR Simple conditional operator can be carried out with conditional operator (? :). It can be written in place of if...else statement. It can be used in this form. expression1 ? expression2 : expression3; e.g. (age>=18) ? printf("can vote") : printf("cannot vote"); /* Program to check whether user can or cannot vote using conditional operator*/ ASSIGNMENT OPERATOR This type of operator assigns some value to left of operator after executing expression to its right. Some assignment operators are =, +=, -=, *=, /=, %=. e.g. x+=2 equivalent to x=x+2 x-=3 equivalent to x=x-3 x*=2 equivalent to x=x*2 x/=2 equivalent to x=x/2 x%=2 equivalent to x=x%2 Operator precedence Precedence is the hierarchical order of operators. Operation with higher precedence group is carried out before lower precedence group. Another important consideration is the order in which consecutive operations within the same precedence group is carried out. This is known as associativity.
  • 5. Type Operators Associativity Unary -, ++, --, sizeof, type R  L Arithmetic *, /, % L  R +, - L  R Relational <, <=, >, >= L  R Equality ==, != L  R Logical && L  R | | L  R Conditional ? : R  L Assignment =, +=, -=, *=, /=, %= R  L e.g. a – b/c * d First, it evaluates b/c then multiplies with d and finally result is subtracted from a. c+= (a>0 && a<=10) ? ++a : a/b; if a, b & c have values 1, 2 & 3 respectively, then result will be c=5; if a, b & c have values 50, 10 & 20 respectively, then result will be c=25; Library Functions Library functions are pre-defined module in header files which can easily be accessed anywhere in the program by including its respective header file. Library function is the one of the important feature of C language. Because of huge collection of library function, it makes very easy to programmers for programming. Library functions are defined for  commonly used operations such as sqrt to find square root, pow to find power of any base etc.  machine dependent standard input/output operations. A library function is accessed simply by writing the function name, followed with list of arguments that represent information being passed to the function. The arguments must be enclosed in parentheses and separated by commas. The argument can be constant, or variable. But the parentheses must be present, even if there is no argument. Some commonly used library functions: abs(i) - returns absolute value of i clrscr() - to clear screen getch() - to input a character without echo getche() - to input a character with echo tolower(c) – to convert a letter to lowercase toupper(c) – to convert a letter to uppercase sin(d) - return sine of d cos(d) - return cosine of d
  • 6. tan(d) - return tangent of d sqrt(d) - return square root of d pow(d1,d2) - return d1 raised to power d2 log(d) - return natural logarithm to d exp(d) - return e raise to power d Program 8 /*To convert an input char to capital letter*/ #include<stdio.h> #include<ctype.h> #include<conio.h> void main() { char c; c=getche(); putch(toupper(c)); }
  • 7. Program 9 /*To find value of sine of angle 30 degree*/ #include<stdio.h> #include<conio.h> #include<math.h> #define PI 3.141593 void main() { float x=30; clrscr(); printf("%f",sin(x*(PI/180))); getch(); } Program 10 /*To find value of antilog of log of specified no.*/ #include<stdio.h> #include<conio.h> #include<math.h> void main() { float x=1; clrscr(); printf("%f",log(x)); printf("%f",exp(log(x))); getch(); } Some more about Escape Sequence: Character Escape Sequence bell a backspace b horizontal tab t new line n form feed f quotation mark (") " apostrophe (') ' question mark ? backslash null 0
  • 8. Input data using Scanf Function This function can be used to enter any combination of numerical values, single characters and strings. The function returns the number of data items that have been entered successfully. scanf(control string, arg1,arg2,...argn) where control string refers to a string containing certain required formatting information. arg1, arg2, ... argn represents the individual input data items. Within the control string comprises individual groups of characters, with one character group of each input data item. Each character group must begin with a percent sign (%) followed with a conversion character which indicates the type of the corresponding data items. Conversion character Meaning c data item is a single character d data item is an integer without decimal e data item is a floating point value in exponent form f data item is a floating point value g data item is a floating point value without trailing zeros. h data item is a short integer i data item is a signed integer number o data item is an octal integer s data item is a string followed by a whitespace character u data item is unsigned integer [...] data item is a string which may include white space
  • 9. /* To print a line of input text without white space */ #include<stdio.h> #include<conio.h> void main() { char x[20]; printf("n Enter a string"); scanf("%s",x); printf("%s",x); getch(); } /* To print a line of input text with white space */ #include<stdio.h> #include<conio.h> void main() { char x[20]; printf("n Enter a string"); scanf("%[^n]",x); printf("%s",x); getch(); } /*displaying a floating point number with diff format*/ #include<stdio.h> #include<conio.h> void main() { float x=123.456; printf("%f %.2f ",x,x); printf("%e %.2e",x,x); printf("%g %.2g",x,x); getch(); } Output: 123.456000 123.46 1.234560e+02 1.23e+02 123.456 1.23e+02 Program 11 /* To convert an integer number into hexadecimal number */ #include<stdio.h> #include<conio.h> void main()
  • 10. { int x=65; clrscr(); printf("%x",x); printf("%o",x); } Program 12 /* To print unsigned integer and long integer number */ #include<stdio.h> #include<conio.h> void main() { unsigned int x=65535; long int y=2000000000; printf("n%u",x); printf("n%ld",y); getch(); } Program 13 /* To print long integer number */ #include<stdio.h> #include<conio.h> void main() { long int x=2000000000; printf("n%ld",x); getch(); }