SlideShare a Scribd company logo
Constants , Variables and Data types
A constant is a value that doesn't change throughout the
execution of a program.
A variable is an identifier which is used to store a value.
There are four commonly used data types such as int, float,
char and a void.
Each data type differs in size and range from one another.
Constants , Variables and Data
types
• Data Types
• There are 4 data types in C language.
• They are:-
• int – This data type is used to define an integer
number (-….-3,-2,-1,0,1,2,3….). A single integer
occupies 2 bytes.
• char – Used to define characters. A single character
occupy 1 byte.
• float – Used to define floating point
numbers (single precision). Occupies 4 bytes.
• double – Used for double precision floating point
numbers(double precision). Occupies 8 bytes.
C Session 2.pptx for engninering students
C Data Types
• Primitive data types are the first form – the basic data
types (int,char,float,double).
• Derived data types are a derivative of primitive data
types known as arrays, pointer and function.
• User defined data types are those data types which are
defined by the user/programmer himself.
C Data Types
• Data Type Qualifiers
• Each of these data type has got qualifiers. The purpose of a
qualifier is to manipulate the range of a particular data type or
its size. The 4 qualifiers in C are long,short,signed and
unsigned.First two long and short are called size
qualifiers and the other two signed and unsigned are
called sign qualifiers.
C Data Types
• Basic Types
• They are arithmetic types and are further classified into: (a)
integer types and (b) floating-point types.
• Enumerated types
• They are again arithmetic types and they are used to define
variables that can only assign certain discrete integer values
throughout the program.
• The type void
• The type specifier void indicates that no value is available.
• Derived types
• They include (a) Pointer types, (b) Array types, (c)
Structure types, (d) Union types and (e) Function types.
C Data Types
• Integer Types
• The following table provides the details of standard integer types
with their storage sizes and value ranges −
• Char
• unsigned char
• signed char
• unsigned int
• Short
• unsigned short
• Long
• unsigned long
C Data Types
• Floating-Point Types
• Float
• Double
• long double
• The void Type
• The void type specifies that no value is
available. It is used in three kinds of situations
C Data Types
• Function returns as void
• There are various functions in C which do not return any value
or you can say they return void. A function with no return
value has the return type as void. For example, void exit (int
status);
• Function arguments as void
• There are various functions in C which do not accept any
parameter. A function with no parameter can accept a void.
For example, int rand(void);
• Pointers to void
• A pointer of type void * represents the address of an
object, but not its type. For example, a memory
allocation function void *malloc( size_t size ); returns a
pointer to void which can be casted to any data type.
Constants
• There are 4 types of constants in C.
• Integer constants
• Character constants
• Real/Floating point constants
• String constants
C Session 2.pptx for engninering students
Constants
• Integer Constants
• An integer constant is an integer quantity which contains a
sequence of digits.It should not have a decimal point. Blanks and
commas are not allowed within an integer constant.An integer
constant can be either +ve or -ve. The constant must lie within
the range of the declared data type (including qualifiers
long,short etc.).
• Example of valid integer constants:- 976 8987 5 -25 etc.
Constants
• Floating Point Constants
• They are also known as real constants. A real constant
contains a decimal point or an exponent. It can be either +ve
or -ve. Commas and blank space are not allowed within a real
constant.
• Examples:– 254.175, -16.47 -0.5e-7 +4.1e8
• Character Constant
• A character constant is a character which is enclosed in single
quotes. A character constant is of size 1byte and can contain
only 1 character. A character can be an alphabet like a,b,A,C
etc or a special character like &,^, $, #,@ etc or a single digit
from 0 through 9. It can also be an escape sequence
character like space ‘ ‘ or a null character ‘o’ or a new line
‘n’ etc.
• Example: ‘A’ ‘a’ ‘ b’ ‘8’ ‘#’ etc.
Constants
• String Constants
– A string constant is a collection of characters
enclosed in double quotations “”
– It may contain alphabets, digits, special characters
and blank space.
• Example: “Circuits Today123”
variable
• variable in simple terms is a storage place which has some memory
allocated to it. Basically, a variable used to store some form of data.
Different types of variables require different amounts of memory and
have some specific set of operations which can be applied to them.
• Variable Declaration:
A typical variable declaration is of the form:
• type variable_name;
• or for multiple variables:
• type variable1_name, variable2_name, variable3_name;
• A variable name can consist of alphabets (both upper and lower case),
numbers and the underscore ‘_’ character. However, the name must not
start with a number.
variable
• Variable Name may consist of letters , digits and
underscore() character ,subject to the following
conditions
• 1.They must begin with a letter . Some systems permit
underscore as the first character.
• 2.Ansi standard recognizes a length of 31 characters.
• 3.Uppercase and lowercase are significant.
• 4.It should not be keyword.
• 5.white space not allowed.
Example Program
• #include <stdio.h>
• int main()
• {
• // declaration and definition of variable 'a123'
• char a123 = 'a';
•
• // This is also both declaration
• // and definition as 'b' is allocated
• // memory and assigned some garbage value.
• float b;
•
• // multiple declarations and definitions
• int _c, _d45, e;
•
• // Let us print a variable
• printf("%c n", a123);
•
• return 0;
• }
OPERATORS IN C
• An operator is a symbol that tells the compiler to perform specific
mathematical or logical functions. C language is rich in built-in operators
and provides the following types of operators −
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Increment and decrement operators
• Conditional operator
• Special operator
Arithmetic Operators
• + Adds two operands.
• − Subtracts second operand from the first.
• * Multiplies both operands.
• / Divides numerator by de-numerator.
• % Modulus Operator and remainder of after an integer
division.
• ++ Increment operator increases the integer value by
one.
• -- Decrement operator decreases the integer value by one.
Relational Operators
• Relational Operators
• The following table shows all the relational operators
supported by C
• == Checks if the values of two operands are equal or
not. If yes, then the condition becomes true.
• != Checks if the values of two operands are equal or
not. If the values are not equal, then the condition
becomes true.
• > Checks if the value of left operand is greater than the
value of right operand. If yes, then the condition
becomes true.
Relational Operators
• < Checks if the value of left operand is less than the
value of right operand. If yes, then the condition
becomes 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.
• <= Checks if the value of left operand is less than or
equal to the value of right operand. If yes, then the
condition becomes true.
Logical Operators
• && Called Logical AND operator. If both the operands are non-zero,
then the condition becomes true.
• || Called Logical OR Operator. If any of the two operands is non-zero,
then the condition becomes true.
• ! Called Logical NOT Operator. It is used to reverse the logical state of
its operand. If a condition is true, then Logical NOT operator will make it
false.
Bitwise Operators
• & Binary AND Operator copies a bit to the result if it
exists in both operands.
• | Binary OR Operator copies a bit if it exists in either
operand.
• ^ Binary XOR Operator copies the bit if it is set in one
operand but not both.
• ~ Binary One's Complement Operator is unary and has
the effect of 'flipping' bits.
• << Binary Left Shift Operator. The left operands value is
moved left by the number of bits specified by the right
operand.
• >> Binary Right Shift Operator. The left operands value
is moved right by the number of bits specified by the
right operand.
Assignment Operators
• = Simple assignment operator. Assigns values from right
side operands to left side operand
• += Add AND assignment operator. It adds the right
operand to the left operand and assign the result to the
left operand.
• -= Subtract AND assignment operator. It subtracts the
right operand from the left operand and assigns the
result to the left operand.
• *= Multiply AND assignment operator. It multiplies the
right operand with the left operand and assigns the result
to the left operand.
Assignment Operators
• /= Divide AND assignment operator. It divides the left
operand with the right operand and assigns the result to
the left operand.
• %= Modulus AND assignment operator. It takes modulus
using two operands and assigns the result to the left
operand.
• <<= Left shift AND assignment operator.
• >>= Right shift AND assignment operator.
• &= Bitwise AND assignment operator.
• ^= Bitwise exclusive OR and assignment operator.
• |= Bitwise inclusive OR and assignment operator.
Increment and Decrement Operators in C
• C has two special unary operators called increment (++)
and decrement (--) operators. These operators
increment and decrement value of a variable by 1.
• ++x is same as x = x + 1 or x += 1
--x is same as x = x - 1 or x -= 1
• Increment and decrement operators can be used only
with variables. They can't be used with constants or
expressions.
• Increment/Decrement operators are of two types:
• Prefix increment/decrement operator.
• Postfix increment/decrement operator.
Prefix increment/decrement
operator
• The prefix increment/decrement operator immediately
increases or decreases the current value of the variable.
This value is then used in the expression. Let's take an
example:
• y = ++x;
• Here first, the current value of x is incremented by 1. The
new value of x is then assigned to y.
• y = --x;
• the current value of x is decremented by 1. The new
value of x is then assigned to y.
Postfix Increment/Decrement
operator
• The postfix increment/decrement operator causes the
current value of the variable to be used in the
expression, then the value is incremented or
decremented. For example:
• y = x++;
• y = x--;
• the current value of x is assigned to y then x is
decremented.
Conditional Operator
• The conditional operator (? and :) is a special operator
which requires three operands. Its syntax is as follows:
• Syntax: expression1 ? expression2 : expression3
• The first expression1 is evaluated, if it is true then the
value of expression2 becomes the result of the overall
expression. On the other hand, if expression1 is false,
then the value of expression3 becomes the result of the
overall expression.
• Example
• int a = 5, b = 3;
• a > b ? a : b
Special Operators in C
• Comma operator
• The Comma operator allows us to place one or more
expression where C syntax allows only one expression.
Each expression must be separated using the comma ( ,
) and are evaluated from left to right. The value of
rightmost expression becomes the value of the overall
expression. An example will make everything clear.
• a=2, a++, a+10
• sizeof operator
• The sizeof is a unary operator used to determine the size
of its operand. The general form of sizeof operator is:
Special Operators in C
• sizeof(object)
• where object can be data type keywords like int, float,
double or expression or variable.
• For example, sizeof(int) gives the size occupied by an int
data type. The sizeof operator returns size in bytes.
Arithmetic expressions
• An arithmetic expression is a combination of
variables ,constants and operators arranged as
per the syntax of the language.
• Example
• (a*b)-c
Evaluation of expressions
• Expressions are evaluated using an
assignment statement of the form:
• variable = expression;
• Example:
• x=a * b-c;
Operator Precedence and Associativity in C
• The operators at higher level of precedence are
evaluated first .The operators of the same
Precedence are evaluated either from ‘left to
right’ or from ‘right to left’, depending on the
level. This is known as the associativity property
of an operator.
Mathematical Functions
• Function
• Description
• floor ( ) This function returns the nearest integer which is less than
or equal to the argument passed to this function.
• round ( ) This function returns the nearest integer value of the
float/double/long double argument passed to this function. If decimal
value is from “.1 to .5”, it returns integer value less than the
argument. If decimal value is from “.6 to .9”, it returns the integer
value greater than the argument.
• ceil ( ) This function returns nearest integer value which is greater
than or equal to the argument passed to this function.
• sin ( ) This function is used to calculate sine value.
• cos ( ) This function is used to calculate cosine.
• cosh ( ) This function is used to calculate hyperbolic cosine.
• exp ( ) This function is used to calculate the exponential “e” to the
xth power.
Mathematical Functions
• tan ( ) This function is used to calculate tangent.
• tanh ( ) This function is used to calculate hyperbolic
tangent.
• sinh ( ) This function is used to calculate hyperbolic sine.
• log ( ) This function is used to calculates natural
logarithm.
• log10 ( ) This function is used to calculates base 10
logarithm.
• sqrt ( ) This function is used to find square root of the
argument passed to this function.
• pow ( ) This is used to find the power of the given
number. trunc.(.) This function truncates the decimal
value from floating point value and returns integer value.
Type conversions in expressions
• Implicit type conversion
• C permits mixing of constants and variables of different
types in an expression.
• C automatically converts any intermediate values to the
proper type sp that the expression can be evaluated
without losing any significance .This automatic
conversion is known as implicit type conversion.
• Explicit type conversion
• The process of such a local conversion is known as
explicit conversion or casting a value . The general form
of a cast is: (type-name)expression

More Related Content

PPTX
C Language Part 1
PPT
Introduction to c
PPTX
Variable declaration
PDF
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
PPT
Unit i intro-operators
PPT
Token and operators
PPTX
introduction to c programming and C History.pptx
PPT
C language Unit 2 Slides, UPTU C language
C Language Part 1
Introduction to c
Variable declaration
Module 2_PPT_P1 POP Notes module 2 fdfd.pdf
Unit i intro-operators
Token and operators
introduction to c programming and C History.pptx
C language Unit 2 Slides, UPTU C language

Similar to C Session 2.pptx for engninering students (20)

PDF
Lamborghini Veneno Allegheri #2004@f**ck
PDF
C programming | Class 8 | III Term
PPTX
Learn C LANGUAGE at ASIT
DOC
C language
PPTX
Fundamentals of C Programming Language
PPTX
Module 1:Introduction
PDF
Introduction to programming c and data-structures
PPTX
Fundamentals of computers - C Programming
PPTX
introductory concepts
PDF
Introduction to programming c and data structures
PDF
02 - Data Types and Expressions using C.pdf
PPTX
Lecture 02 Programming C for Beginners 001
PPTX
Basic Of C language
PPTX
Introduction to c
PPTX
Introduction to C Programming - R.D.Sivakumar
PPTX
CHAPTER 2
PPTX
Structure of c_program_to_input_output
PDF
C language
PPTX
C programming(Part 1)
Lamborghini Veneno Allegheri #2004@f**ck
C programming | Class 8 | III Term
Learn C LANGUAGE at ASIT
C language
Fundamentals of C Programming Language
Module 1:Introduction
Introduction to programming c and data-structures
Fundamentals of computers - C Programming
introductory concepts
Introduction to programming c and data structures
02 - Data Types and Expressions using C.pdf
Lecture 02 Programming C for Beginners 001
Basic Of C language
Introduction to c
Introduction to C Programming - R.D.Sivakumar
CHAPTER 2
Structure of c_program_to_input_output
C language
C programming(Part 1)
Ad

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Well-logging-methods_new................
PPTX
Internet of Things (IOT) - A guide to understanding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Sustainable Sites - Green Building Construction
PPT
Mechanical Engineering MATERIALS Selection
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Lecture Notes Electrical Wiring System Components
Well-logging-methods_new................
Internet of Things (IOT) - A guide to understanding
CYBER-CRIMES AND SECURITY A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
Sustainable Sites - Green Building Construction
Mechanical Engineering MATERIALS Selection
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
CH1 Production IntroductoryConcepts.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Structs to JSON How Go Powers REST APIs.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
additive manufacturing of ss316l using mig welding
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Ad

C Session 2.pptx for engninering students

  • 1. Constants , Variables and Data types A constant is a value that doesn't change throughout the execution of a program. A variable is an identifier which is used to store a value. There are four commonly used data types such as int, float, char and a void. Each data type differs in size and range from one another.
  • 2. Constants , Variables and Data types • Data Types • There are 4 data types in C language. • They are:- • int – This data type is used to define an integer number (-….-3,-2,-1,0,1,2,3….). A single integer occupies 2 bytes. • char – Used to define characters. A single character occupy 1 byte. • float – Used to define floating point numbers (single precision). Occupies 4 bytes. • double – Used for double precision floating point numbers(double precision). Occupies 8 bytes.
  • 4. C Data Types • Primitive data types are the first form – the basic data types (int,char,float,double). • Derived data types are a derivative of primitive data types known as arrays, pointer and function. • User defined data types are those data types which are defined by the user/programmer himself.
  • 5. C Data Types • Data Type Qualifiers • Each of these data type has got qualifiers. The purpose of a qualifier is to manipulate the range of a particular data type or its size. The 4 qualifiers in C are long,short,signed and unsigned.First two long and short are called size qualifiers and the other two signed and unsigned are called sign qualifiers.
  • 6. C Data Types • Basic Types • They are arithmetic types and are further classified into: (a) integer types and (b) floating-point types. • Enumerated types • They are again arithmetic types and they are used to define variables that can only assign certain discrete integer values throughout the program. • The type void • The type specifier void indicates that no value is available. • Derived types • They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.
  • 7. C Data Types • Integer Types • The following table provides the details of standard integer types with their storage sizes and value ranges − • Char • unsigned char • signed char • unsigned int • Short • unsigned short • Long • unsigned long
  • 8. C Data Types • Floating-Point Types • Float • Double • long double • The void Type • The void type specifies that no value is available. It is used in three kinds of situations
  • 9. C Data Types • Function returns as void • There are various functions in C which do not return any value or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status); • Function arguments as void • There are various functions in C which do not accept any parameter. A function with no parameter can accept a void. For example, int rand(void); • Pointers to void • A pointer of type void * represents the address of an object, but not its type. For example, a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.
  • 10. Constants • There are 4 types of constants in C. • Integer constants • Character constants • Real/Floating point constants • String constants
  • 12. Constants • Integer Constants • An integer constant is an integer quantity which contains a sequence of digits.It should not have a decimal point. Blanks and commas are not allowed within an integer constant.An integer constant can be either +ve or -ve. The constant must lie within the range of the declared data type (including qualifiers long,short etc.). • Example of valid integer constants:- 976 8987 5 -25 etc.
  • 13. Constants • Floating Point Constants • They are also known as real constants. A real constant contains a decimal point or an exponent. It can be either +ve or -ve. Commas and blank space are not allowed within a real constant. • Examples:– 254.175, -16.47 -0.5e-7 +4.1e8 • Character Constant • A character constant is a character which is enclosed in single quotes. A character constant is of size 1byte and can contain only 1 character. A character can be an alphabet like a,b,A,C etc or a special character like &,^, $, #,@ etc or a single digit from 0 through 9. It can also be an escape sequence character like space ‘ ‘ or a null character ‘o’ or a new line ‘n’ etc. • Example: ‘A’ ‘a’ ‘ b’ ‘8’ ‘#’ etc.
  • 14. Constants • String Constants – A string constant is a collection of characters enclosed in double quotations “” – It may contain alphabets, digits, special characters and blank space. • Example: “Circuits Today123”
  • 15. variable • variable in simple terms is a storage place which has some memory allocated to it. Basically, a variable used to store some form of data. Different types of variables require different amounts of memory and have some specific set of operations which can be applied to them. • Variable Declaration: A typical variable declaration is of the form: • type variable_name; • or for multiple variables: • type variable1_name, variable2_name, variable3_name; • A variable name can consist of alphabets (both upper and lower case), numbers and the underscore ‘_’ character. However, the name must not start with a number.
  • 16. variable • Variable Name may consist of letters , digits and underscore() character ,subject to the following conditions • 1.They must begin with a letter . Some systems permit underscore as the first character. • 2.Ansi standard recognizes a length of 31 characters. • 3.Uppercase and lowercase are significant. • 4.It should not be keyword. • 5.white space not allowed.
  • 17. Example Program • #include <stdio.h> • int main() • { • // declaration and definition of variable 'a123' • char a123 = 'a'; • • // This is also both declaration • // and definition as 'b' is allocated • // memory and assigned some garbage value. • float b; • • // multiple declarations and definitions • int _c, _d45, e; • • // Let us print a variable • printf("%c n", a123); • • return 0; • }
  • 18. OPERATORS IN C • An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators − • Arithmetic Operators • Relational Operators • Logical Operators • Bitwise Operators • Assignment Operators • Increment and decrement operators • Conditional operator • Special operator
  • 19. Arithmetic Operators • + Adds two operands. • − Subtracts second operand from the first. • * Multiplies both operands. • / Divides numerator by de-numerator. • % Modulus Operator and remainder of after an integer division. • ++ Increment operator increases the integer value by one. • -- Decrement operator decreases the integer value by one.
  • 20. Relational Operators • Relational Operators • The following table shows all the relational operators supported by C • == Checks if the values of two operands are equal or not. If yes, then the condition becomes true. • != Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. • > Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true.
  • 21. Relational Operators • < Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes 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. • <= 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.
  • 22. Logical Operators • && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. • || Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. • ! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false.
  • 23. Bitwise Operators • & Binary AND Operator copies a bit to the result if it exists in both operands. • | Binary OR Operator copies a bit if it exists in either operand. • ^ Binary XOR Operator copies the bit if it is set in one operand but not both. • ~ Binary One's Complement Operator is unary and has the effect of 'flipping' bits. • << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. • >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
  • 24. Assignment Operators • = Simple assignment operator. Assigns values from right side operands to left side operand • += Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. • -= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. • *= Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.
  • 25. Assignment Operators • /= Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand. • %= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. • <<= Left shift AND assignment operator. • >>= Right shift AND assignment operator. • &= Bitwise AND assignment operator. • ^= Bitwise exclusive OR and assignment operator. • |= Bitwise inclusive OR and assignment operator.
  • 26. Increment and Decrement Operators in C • C has two special unary operators called increment (++) and decrement (--) operators. These operators increment and decrement value of a variable by 1. • ++x is same as x = x + 1 or x += 1 --x is same as x = x - 1 or x -= 1 • Increment and decrement operators can be used only with variables. They can't be used with constants or expressions. • Increment/Decrement operators are of two types: • Prefix increment/decrement operator. • Postfix increment/decrement operator.
  • 27. Prefix increment/decrement operator • The prefix increment/decrement operator immediately increases or decreases the current value of the variable. This value is then used in the expression. Let's take an example: • y = ++x; • Here first, the current value of x is incremented by 1. The new value of x is then assigned to y. • y = --x; • the current value of x is decremented by 1. The new value of x is then assigned to y.
  • 28. Postfix Increment/Decrement operator • The postfix increment/decrement operator causes the current value of the variable to be used in the expression, then the value is incremented or decremented. For example: • y = x++; • y = x--; • the current value of x is assigned to y then x is decremented.
  • 29. Conditional Operator • The conditional operator (? and :) is a special operator which requires three operands. Its syntax is as follows: • Syntax: expression1 ? expression2 : expression3 • The first expression1 is evaluated, if it is true then the value of expression2 becomes the result of the overall expression. On the other hand, if expression1 is false, then the value of expression3 becomes the result of the overall expression. • Example • int a = 5, b = 3; • a > b ? a : b
  • 30. Special Operators in C • Comma operator • The Comma operator allows us to place one or more expression where C syntax allows only one expression. Each expression must be separated using the comma ( , ) and are evaluated from left to right. The value of rightmost expression becomes the value of the overall expression. An example will make everything clear. • a=2, a++, a+10 • sizeof operator • The sizeof is a unary operator used to determine the size of its operand. The general form of sizeof operator is:
  • 31. Special Operators in C • sizeof(object) • where object can be data type keywords like int, float, double or expression or variable. • For example, sizeof(int) gives the size occupied by an int data type. The sizeof operator returns size in bytes.
  • 32. Arithmetic expressions • An arithmetic expression is a combination of variables ,constants and operators arranged as per the syntax of the language. • Example • (a*b)-c
  • 33. Evaluation of expressions • Expressions are evaluated using an assignment statement of the form: • variable = expression; • Example: • x=a * b-c;
  • 34. Operator Precedence and Associativity in C • The operators at higher level of precedence are evaluated first .The operators of the same Precedence are evaluated either from ‘left to right’ or from ‘right to left’, depending on the level. This is known as the associativity property of an operator.
  • 35. Mathematical Functions • Function • Description • floor ( ) This function returns the nearest integer which is less than or equal to the argument passed to this function. • round ( ) This function returns the nearest integer value of the float/double/long double argument passed to this function. If decimal value is from “.1 to .5”, it returns integer value less than the argument. If decimal value is from “.6 to .9”, it returns the integer value greater than the argument. • ceil ( ) This function returns nearest integer value which is greater than or equal to the argument passed to this function. • sin ( ) This function is used to calculate sine value. • cos ( ) This function is used to calculate cosine. • cosh ( ) This function is used to calculate hyperbolic cosine. • exp ( ) This function is used to calculate the exponential “e” to the xth power.
  • 36. Mathematical Functions • tan ( ) This function is used to calculate tangent. • tanh ( ) This function is used to calculate hyperbolic tangent. • sinh ( ) This function is used to calculate hyperbolic sine. • log ( ) This function is used to calculates natural logarithm. • log10 ( ) This function is used to calculates base 10 logarithm. • sqrt ( ) This function is used to find square root of the argument passed to this function. • pow ( ) This is used to find the power of the given number. trunc.(.) This function truncates the decimal value from floating point value and returns integer value.
  • 37. Type conversions in expressions • Implicit type conversion • C permits mixing of constants and variables of different types in an expression. • C automatically converts any intermediate values to the proper type sp that the expression can be evaluated without losing any significance .This automatic conversion is known as implicit type conversion. • Explicit type conversion • The process of such a local conversion is known as explicit conversion or casting a value . The general form of a cast is: (type-name)expression