Thursday, December 17, 2020

Type conversion, Precedence & Associativity of Operators:

 C provides the facility of mixing different types of variable and constant in an expression.


Implicit Type Conversion:





Type Conversion:



Explicit Type Conversion:

Syntax:   (Data type)expression

e.g  z=float(x/y)





Precedence & Associativity of Operators:


 Let a=8, b=4, c=2, d=1, e=5, f=20





Operators in 'C':

 An operator specifies an operation to be performed that yields a value.

1.Arithmetic operators

2. Assignment operators .

3. Increment and Decrement operators

4. Relational  operators.

5. Logical operators

6. Conditional operator

7. Comma operator

8. Sizeof operator

9. Bitwise operators

      10. Other operators


1. Arithmetic operators:  It is used for numeric calculations.

They are of two types:

1. Unary Arithmetic Operators

2. Binary Arithmetic Operators

Unary Arithmetic Operators: Unary operators require only one operand. For e.g +x -y

Binary Arithmetic Operators: Binary operators require two operands.  There are five binary arithmetic operators.



Integer Arithmetic:  In which both operands are integer



Floating-Point Arithmetic:  In which both operands are integer type.



2. Assignment Operator :  A value can be stored in a variable with the use of assignment operator. 

The operator ‘=’ is used in assignment operator & statement.



Compound Assignment


3. Increment/ Decrement  Operators: These are the unary operators because they operate on a single operand. Increment(++) and decrement(--) used to increase and decrease the value by 1.


1. Prefix Increment/ Decrement:




2. Postfix Increment/ Decrement:





4. Relational Operators:

It is used to compare values of two expressions depending on their relationship.


Let us take two variables a=9 and b=5 . Evaluate the value.





5. Logical Operators:  An expression that combines  two or more expressions in terms of logical expression. The operator returns 0 for false and 1 for true.



1.AND(&&) Operator: This  gives true if both conditions are true, otherwise the result is false.





2. OR(||) Operators: This operator gives results false , if both conditions are false, otherwise the result is true.



3. Not(!) Operator: This is a unary operator and it negates the value of the condition . If the value of the condition is false  then it gives the value true.


6. Conditional Operator: Conditional operator is a ternary (? And :) which requires three expressions as operands.

Syntax:   TestExpression ? Expression1: Expression2

e.g.  a > b ? a : b

Suppose a=5 and b=8 then,

Max=a > b ? a : b 



7. Comma Operator: Expressions are separated using comma operator. The expressions are evaluated from left to right and the type and value of the compound expression.

e.g.   a=8, b=7, c=9, a+b+c

a=8, b=7, c=9


sum=a+b+c






Sizeof Operator: It is a unary operator . This operator gives the size of its operand in terms of bytes . The operand can be variable, constant or any data types( int, float, char etc)



Bitwise Operator: C has the ability to support the manipulation of data at the bit level. Bitwise operators are used for the operations on individual bits. It operates only on integers. 











Tuesday, December 8, 2020

Algorithms,Flowcharts and Pseudo code:

Algorithm:  an algorithm is a procedure or formula for solving a problem.  The word and derived from the name of the mathematician. An algorithm is a step-wise procedure written in a simple language. If equation solution of any problem that written in human language is called as algorithm.

Algorithm is a first step of the solution process, after the analysis of the problem, program for write the algorithm of that problem.

Write an algorithm to find a number is even or odd.

Step 1: Start

Step 2: Input a number

Step 3: rem= number mod 2

Step 4: if rem= 0 then

                         print “even number”

            else

                        print “odd number” 

            end if

Step 5: Stop

Flowchart:  chat is a means of visually presenting the flow of the data through an information processing system, the operations performed within the system and the sequence in which they are performed.  in this lesson,  we shall concern yourself with a program flowchart,  which describe what operations  and in what sequence are required to solve a given problem.  The program flowchart that can be likened  to the blueprint of a building.  As we know a designer draws a blueprint before starting construction on a building.  Similarly a program word brief was to draw a flowchart prior to writing a computer program.  As in the case of the growing of a blueprint, the flowchart is drawn according to define rule and using standard for chat symbol prescribed by the ANSI.

Meaning of flowchart:  a flowchart is a diagrammatic representation that illustrates the sequence of operation to be performed to get a better solution of problem.  who chart are generally draw in the early stages of formulating Computer Solutions. Flowchart facilitates communication between the program office and business people. These four charges play a vital role in programming of the problem and are quite helpful in understanding the logic of the complicated and then the problem.  Once the flowchart is drawn in become easy to write the program in any high level language.  Often we see how the Charter helpful in explaining the program to others.  is it is correct to say that flow chart is a must for better documentation of complex program.

Symbols used in flowchart:


Guidelines of  flowchart drawing:


  1. In drawing a proper flow chart, all necessary requirements should be listed out in logical order.
  2. The future should be clear, Neat and easy to follow.  There should not be any room for ambiguity in understanding the flowchart.
  3. The usual direction of the flowchart of a procedure is from left to right or top to bottom
  4. Only one flow line should come out from the process symbol
  5. Only one flow line should we enter a decision symbol, but two or more flow lines, one for each possible answer, should leave the decision symbol.
  6. Only one flow line is used in conjunction with the terminal symbol
  7. Write within the standard symbol briefly.  as necessary you can use a notation symbol to describe data or computational steps more clearly
  8. If the future become Complex it is better to use connector symbol to reduce the number of flow lines over the intersection of flow lines if you want to make it more effective and better  way of communication.
  9.  Ensure that the flowchart has a logical start and finish.
  10.  It is useful to test the validity of the future by passing through it within a simple test data 











Pseudo code: It is an informal way of programming description that does not require any strict programming language syntax or underlying technology considerations. It is used for creating an outline or a rough draft of a program. Pseudo code summarizes a program’s flow, but excludes underlying details. System designers write pseudo code to ensure that programmers understand a software project's requirements and align code accordingly.

Description: Pseudo code is not an actual programming language. So it cannot be compiled into an executable program. It uses short terms or simple English language syntax's to write code for programs before it is actually converted into a specific programming language. This is done to identify top level flow errors, and understand the programming data flows that the final program is going to use. This definitely helps save time during actual programming as conceptual errors have been already corrected. Firstly, program description and functionality is gathered and then pseudo code is used to create statements to achieve the required results for a program. Detailed pseudo code is inspected and verified by the designer’s team or programmers to match design specifications. Catching errors or wrong program flow at the pseudo code stage is beneficial for development as it is less costly than catching them later. Once the pseudo code is accepted by the team, it is rewritten using the vocabulary and syntax of a programming language. The purpose of using pseudo code is an efficient key principle of an algorithm. It is used in planning an algorithm with sketching out the structure of the program before the actual coding takes place.

Advantages of pseudo code –

• Pseudo code is understood by the programmers of all types.

•It enables the programmer to concentrate only on the algorithm part of the code development.




Enumerations:

Enumeration:  An enumeration type is a user defined data type, which can take values only from a user defined list or named integer constants called enumerators.

Syntax:

enum tag

{

            member1;

member2;

…………;

………...

} ;

Here enum is a keyboard, tag is an identifier that specifies the name of the new enumeration type of being defined, member1, member2 are identifiers which represent integer constant  are called enumerator constant or enumerator. The list of these enumerators is called as enumerator list.

After the definition, we can declare variable of these new data type as -

enum tag var1, var2, var3;

Here var1, var2, var3 are variables of type enum tag. This variable can take value from the enumerator list.

The variable can also be declared with the definition as-

enum tag

{

            member1;

member2;

…………;

………...

} var1, var2, var3;

Example:

enum month {Jan, Feb, Mar, Apr, May, Jun};

Here a new data type month is define and the enumerator list contains six enumerators. internally the compiler treat the numerator as integer constant.  these are automatically assign integer values beginning from 0,1,2..

Jan      0

Feb      1

Mar      2

Apr      3

.

Desired for values assigned to enumerator.  it is also possible to explicitly assign any value to enumerator.

enum month {Jan, Feb=4, Mar, Apr, May=11, Jun};

Jan      0

Feb      4

Mar      5

Apr      6

May     11

Jun      12

We can assign any signed integer value to the enumerator.

enum boolean{true, false};

enum switch{off, on};

enum subject{Phy, Chem, Math, Cs, Ele}

Input & Output in C:

Format Specifiers: 


Reading Input Data: Input data can be entered into the memory from a standard input device (keyboard). C provides the scanf( ) library function for entering input data. This function can take all types of values (numeric, character, string) as input.

The scanf( ) function can be written as

scanf("control string" , address1, address2, ....);

This function should have at least two parameters. First parameter is a control string, which contains conversion specification characters. It should be within double quotes.

The address of a variable is found by preceding the variable name by an ampersand (&) sign. This sign is called the address operator and it gives the starting address of the variable name in memory

 Some examples of scanf( ) function are as-







Writing Output Data: Output data can be written from computer memory to the standard output device (monitor) usIng printf() library function. With this function all type of values (numeric, character or string) can be written as output.

The printf( ) function can be written as

printf("control string", variable 1, variable 2, );

In this function the control string contains conversion specification characters and text. It should be enclosed within'double quotes. The name of variables snould not be preceded by an ampersand(&) sign.

Some example of printf( ) function are as-