C Session 2.pptx for engninering students

nraj02252 4 views 37 slides Jun 07, 2024
Slide 1
Slide 1 of 37
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37

About This Presentation

For engineering students


Slide Content

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 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

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 T he 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 x th   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