SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Downloaden Sie, um offline zu lesen
!
"
What is a Flow Chart?
A flow chart is a graphical or symbolic representation of a process.
Each step in the process is represented by a different symbol and
contains a short description of the process step. The flow chart
symbols are linked together with arrows showing the process flow
direction.
What is an Algorithm? What are the steps of algorithm?
Step-by-step method of solving a problem or making decisions, as
in making a diagnosis.
Steps of the algorithm
) Finiteness: - an algorithm terminates after a finite numbers of
steps.
2) Definiteness: - each step in algorithm is unambiguous. This
means that the action specified by the step cannot be interpreted
(explain the meaning of) in multiple ways & can be performed
without any confusion.
3) Input: - an algorithm accepts zero or more inputs
4) Output:- it produces at least one output.
5) Effectiveness:- it consists of basic instructions that are
realizable. This means that the instructions can be performed by
using the given inputs in a finite amount of time
Difference between algorithm and flowchart?
An algorithm is a precise rule (or set of rules) specifying how to
solve some problem while A flowchart is a diagram of the sequence
of operations (pictorial representation of algorithm) in a computer
program
!
"
Keywords
The following keywords are reserved and may not be used as an
identifier for any other purpose.
auto double int long
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Formatted String:
It specified to read which type of data.
%c CHARACTER
%d INTEGER
%f FLOAT
%lf DOUBLE
%u UNSIGNED
%s STRING
%Lf LONG DOUBLE
%ld LONG INT
!
"
Preprocessor directives in c
In c all the preprocessor directives start with # character except
define () operator. All the directives have different task which
executes just before the actual execution of c program which makes
the program more portable. These directives are used only to
instruct compilers.
Why preprocessor in c programming language?
Answer:
1. It improves the readability of program.
2. It makes easy to modify
3. Portability
#include directive in c
Syntax:
#include <filename>
or
#include “filename”
Task of include directive is to include or import any type file. There
are two form of include directive.
Form 1:
#include<filename>: This statement will search the file in include
directory (Default location of include directory is c:tcinclude). If
that file is present then #include statement is replaced by content of
file. If that file is not present then it will cause of compilation error.
//test.c
#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
printf("HOW large is this file?");
!
"
getch();
}
C Programming - Data Types
C language data types can be broadly classified as
1. Primary data type
2. Derived data type
3. User-defined data type
Primary data type
All C Compilers accept the following fundamental data types
!
"
Secondary Data Types
• Array in C programming
An array in C language is a collection of similar data-type,
means an array can hold value of a particular data type for
which it has been declared. Arrays can be created from any of
the C data-types int,...
• Pointers in C Programming
In this tutorial I am going to discuss what pointer is and how
to use them in our C program. Many C programming learner
thinks that pointer is one of the difficult topic in C language
but its not...
• Structure in C Programming
We used variable in our C program to store value but one
variable can store only single piece information (an integer can
!
"
hold only one integer value) and to store similar type of values
we had to declare...
User defined type declaration
C language supports a feature where user can define an identifier
that characterizes an existing data type. This user defined data type
identifier can later be used to declare variables. In short its purpose
is to redefine the name of an existing data type.
Syntax: typedef <type> <identifier>; like
typedef int number;
Now we can use number in lieu of int to declare integer variable.
For example: “int x1” or “number x1” both statements declaring an
integer variable. We have just changed the default keyword “int” to
declare integer variable to “number
A data type defines a set of values that a variable can store along
with a set of operations that can be performed on that variable.
Common data types are integer, character, and floating-point.
Main ( ): All C programs consist of one or more functions. As a
general rule, the only function that must be present is called
main ( ), which is the first function called when program execution
begins
!
"
The general form of a C program:
Global declarations
int main(parameter list)
{
statement sequence
}
return-type f1(parameter list)
{
statement sequence
}
return-type f2(parameter list)
{
statement sequence
}...
return-type fN(parameter list)
{
statement sequence
Review of Terms
.
• Source code: The text of a program that a user can read,
commonly thought of as the program. The source code is input into
the C compiler.
• Object code: Translation of the source code of a program into
machine code, which the computer can read and execute directly.
Object code is the input to the linker.
• Linker: A program that links separately compiled modules into
one program. It also combines the functions in the Standard C
library with the code that you wrote. The output of the linker is an
executable program.
!
"
• Library: The file containing the standard functions that your
program can use. These functions include all I/O operations as well
as other useful routines.
• Compile time: The time during which your program is being
compiled.
• Run time: The time during which your program is executing
NOTE: The type void either explicitly declares a function as
returning no value or creates generic pointers
All Data Types Defined by the C Standard
Type Typical Size in
char 8 –127 to 127
unsigned char 8 0 to 255
signed char 8 –127 to 127
int 16 or 32 –32,767 to 32,767
unsigned int 16 or 32 0 to 65,535
signed int 16 or 32 Same as int
short int 16 –32,767 to 32,767
unsigned short int 16 0 to 65,535
signed short int 16 Same as short int
long int 32 –2,147,483,647 to 2,147,483,647
long long int 64 –(263 –1) to 263 –1 (Added by C99)
signed long int 32 Same as long int
unsigned long int 32 0 to 4,294,967,295
unsigned long long int 64 264 –1 (Added by C99)
float 32 1E–37 to 1E+37 with six digits of
precision
double 64 1E–37 to 1E+37 with ten digits of
precision
long double 80 1E–37 to 1E+37 with ten digits of
precision
!
"
Variables:
As you probably know, a variable is a named location in memory
that is used to hold a value that can be modified by the program. All
variables must be declared before they can be used. The general
form of a declaration is:
type variable_list;
Here, type must be a valid data type plus any modifiers, and
variable_list may consist of one or more identifier names separated
by commas. Here are some declarations:
Eg: int i, j, l;
Eg: short int si;
Eg: unsigned int ui;
Eg: double balance, profit, loss;
Remember:
Where Variables Are Declared Variables can be declared in three
places: inside functions, in the definition of function parameters,
and outside of all functions. These positions correspond to local
variables, formal parameters, and global variables, respectively.
Local Variables:
Variables that are declared inside a function are called local
variables Local variables exist only while the block of code in which
they are declared is executing. That is, a local variable is created
upon entry into its block and destroyed upon exit. Furthermore, a
variable declared within one code block has no bearing on or
!
"
relationship to another variable with the same name declared
within a different code block.
Global Variables:
Unlike local variables, global variables are known throughout the
program and may be used by any piece of code. Also, they will hold
their value throughout the program's execution. You create global
Variables by declaring them outside of any function. Any expression
may access them, regardless of what block of code that expression
is in.
Variable Initializations:
You can give variables a value as you declare them by placing an
equal sign and a constant after the variable name. The general form
of initialization is type variable_name = constant;
Some examples are
char ch = 'a';
int first = 0;
double balance = 123.23;
Constants:
Constants refer to fixed values that the program may not alter.
Constants can be of any of the basic data types. The way each
constant is represented depends upon its type. Constants are also
called literals.
!
"
Escape Sequences:
Code Meaning
b Backspace
f Form feed
n new line
r Carriage return
t Horizontal tab
" Double quote
' Single quote
  Backslash
v Vertical tab
a Alert
? Question mark
N octal constant (where N is an octal constant)
xN Hexadecimal constant (where N is a hexadecimal constant)
Operators
C is very rich in built-in operators. In fact, it places more significance on operators
than do most other computer languages. There are four main classes of operators:
arithmetic, relational, logical, and bitwise. In addition, there are some special
operators, such as the assignment operator, for particular tasks
1) The Assignment Operator
You can use the assignment operator within any valid expression the general form
of the assignment operator is: variable name = expression; where an expression
may be as simple as a single constant or as complex as you require.
a) Multiple Assignments
You can assign many variables the same value by using multiple assignments in a
single statement. For example, this program fragment assigns x, y, and z the value
0:
!
"
x = y = z = 0; in professional programs, variables are frequently assigned common
values using this method.
b) Compound Assignments
There is a variation on the assignment statement, called compound assignment. that
simplifies the coding of a certain type of assignment operations. For example,
x = x+10; can be written as x += 10;
The operator += tells the compiler to assign to x the value of x plus 10.
Compound assignment operators exist for all the binary operators (those that
require two operands).In general, statements like
a) var = var operator expression
Can be rewritten as
a) var operator = expression
For another example,
x = x-100;
is the same as x -= 100;
Because compound assignment is more compact than the corresponding =
equivalent, compound assignment is also sometimes referred to as shorthand
assignment. Compound assignment is widely Used in professionally written C
programs; you should be familiar with it.
2) Arithmetic Operators
Table: lists C's arithmetic operators. The operators +, –, *, and / work as they do in
most other computer languages. You can apply them to almost any built-in data
type. When you apply / to an integer or character, any remainder will be truncated.
For example, 5/2 will equal 2 in integer division. The modulus operator % also
works in C as it does in other languages, yielding the remainder of an integer
division. However, you cannot use it on floating-point types. The following code
fragment illustrates %:
Operator Action
– Subtraction, also unary minus
+ Addition
* Multiplication
/ Division
!
"
% Modulus
–– Decrement
++ Increment
3) Increment and Decrement Operators
C includes two useful operators that simplify two common operations. These are
the increment and decrement operators, ++ and ––. The operator ++ adds 1 to its
operand, and ––subtracts 1. In other words:
x = x+1; is the same as ++x;
And x = X–1; is the same as x– –;
Both the increment and decrement operators may either precede (prefix) or follow
(postfix) the operand. For example,
x = x+1; can be written ++x; or x++;
There is, however, a difference between the prefix and postfix forms when you use
these operators in a larger expression. When an increment or decrement operator
precedes its operand, the increment or decrement operation is performed before
obtaining the value of the operand for use in the expression. If the operator follows
its operand, the value of the operand is obtained before incrementing or
decrementing it. For instance.
x = 10;
y = ++x;
Sets y to 11. However, if you write the code as
x = 10;
y = x++;
y is set to 10. Either way, x is set to 11; the difference is in when it happens. Most
C compilers produce very fast, efficient object code for increment and decrement
operations code that is better than that generated by using the equivalent
assignment statement. For this reason, you should use the increment and decrement
operators when you can. Here is the precedence of the arithmetic operators: Here is
the precedence of the arithmetic operators:
Highest ++ ––
– (unary minus)
!
"
* / %
Lowest + –
Operators on the same level of precedence are evaluated by the compiler from left
to right. Of course, you can use parentheses to alter the order of evaluation. C
treats parentheses in the same way as virtually all other computer languages.
Parentheses force an operation, or set of operations, to have a higher level of
precedence.
4) Logical Operators
In the term relational operator, relational refers to the relationships that values can
have with one another. In the term logical operator, logical refers to the ways these
relationships can be connected. Because the relational and logical operators often
work together, they are discussed together here. The idea of true and false
underlies the concepts of relational and logical operators. In C, true is any value
other than zero. False is zero. Expressions that use relational or logical operators
return 0 for false and 1 for true.
Table shows the relational and logical operators. The truth table for the logical
operators is shown here using 1's and 0's.
p q p && q p || q !p
0 0 0 0 1
0 1 0 1 1
1 1 1 1 0
1 0 0 1 0
5) Relational Operators
Operator Action
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
= = Equal
!
"
!= Not equal
Logical Operators
Operator Action
&& AND
| | OR
! NOT
Table: Relational and Logical Operators
The following table shows the relative precedence of the relational and logical
operators:
Highest!
> >= < <=
= = !=
&&
Lowest | |
As with arithmetic expressions, you can use parentheses to alter the natural order
of evaluation in a relational and/or logical expression. For example,! 0&&0 | | 0
Is false. However, when you add parentheses to the same expression, as shown
here, the result is true.
Remember, all relational and logical expressions produce a result of either 1 or 0.
Therefore, the following program fragment is not only correct, but will print the
number 1.
The following table shows the relative precedence of the relational and logical
operators:
As with arithmetic expressions, you can use parentheses to alter the natural order
of evaluation in a relational and/or logical expression. For example, is false.
However, when you add parentheses to the same expression, as shown here, the
result is true. Remember, all relational and logical expressions produce a result of
either 1 or 0. Therefore, the following program fragment is not only correct, but
will print the number 1.
6) Bitwise operators :
!
"
Unlike many other languages, C supports a full complement of bitwise operators.
Since C was designed to take the place of assembly language for most
programming tasks, it needed to be able to support many operations that can be
done in assembler, including operations on bits. Bitwise operation refers to testing,
setting, or shifting the actual bits in a byte or word, which correspond to the
standard char and int data types and variants.
Highest!
> >= < <=
= = !=
&&
The bitwise AND, OR, and NOT (one's complement) are governed by the same
truth table as their logical equivalents, except that they work bit by bit. The
exclusive OR has the truth table shown here:
p q p ^q
0 0 0
1 0 1
1 1 0
0 1 1
- As the table indicates, the outcome of an XOR is true only if exactly one of
the operands is true; otherwise, it is false.
- Bitwise operations most often find application in device drivers— such as
modem programs, disk file routines, and printer routines— because the
bitwise operations can be used to mask off certain bits, such as parity. (The
parity bit confirms that the rest of the bits in the byte are unchanged. It is
often the high-order bit in each byte.)
-
Operator Action
& AND
| OR
^ Exclusive OR (XOR)
~ One's complement (NOT)
!
"
>> Shift right
<< Shift left
Table: Bitwise Operators
Example:
#include<stdio.h>
#include<conio.h>
Void main ()
{
Char ch;
Int x=127, y;
Y=ch&x;
Printf(“%d”,y);}
Output:
Parity is often indicated by the eighth bit, which is set to 0 by ANDing it with a
byte that has bits 1through 7 set to 1 and bit 8 set to 0. The expression ch & 127
means to AND together the bits in ch with the bits that make up the number 127.
The net result is that the eighth bit of ch is set to 0. In the following example,
assume that ch had received the character A and had the parity bit set:
The bitwise OR, as the reverse of AND, can be used to set a bit. Any bit that is set
to 1 in either operand causes the corresponding bit in the outcome to be set to 1.
For example, the following is 128 | 3:
An exclusive OR, usually abbreviated XOR will set a bit on, if and only if the bits
being compared are different. For example, 127 ^120 is
!
"
Remember, relational and logical operators always produce a result that is either
true or false, whereas the similar bitwise operations may produce any arbitrary
value in accordance with the specific operation. In other words, bitwise operations
may produce values other than 0 or 1, while logical operators will always evaluate
to 0 or 1.The bit-shift operators, >> and <<, move all bits in a variable to the right
or left as specified. The general form of the shift-right statement is
- variable >> number of bit positions
The general form of the shift-left statement is
- variable << number of bit positions
- As bits are shifted off one end, zeroes are brought in the other end. (In the
case of a signed, negative integer, a right shift will cause a 1 to be brought in
so that the sign bit is preserved.) Remember, a shift is not a rotate. That is,
the bits shifted off one end do not come back around to the other. The bits
shifted off are lost.
- Notice that a sequence of two complements in a row always produces the
original number. Hence, the first complement represents the coded version
of that byte. The second complement decodes the byte to its original value.
- unsigned char x; x as each statement executes value of x
- x = 7; 00000111 7
- x = x<<l; 00001110 14
- x = x<<3; 01110000 112
- x = x<<2; 11000000 192
- x = x>>l; 01100000 96
- x = x>>2; 00011000 24
- Each left shift multiplies by 2. Notice that information has been lost after
x<<2 because a bit was shifted off the end.
- Each right shift divides by 2. Notice that subsequent divisions do not bring
back any lost bits.
7) Ternary operator:
!
"
C contains a powerful and convenient operator that replaces certain statements of
the if-then-else form. The ternary operator? Takes the general form
Syntax: Exp1? Exp2: Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the
Colon.
- The? Operator works like this: Exp1 is evaluated. If it is true, Exp2 is
evaluated and becomes the value of the expression. If Exp1 is false, Exp3 is
evaluated, and its value becomes the value of the expression.
For example, in x = 10;
y = x>9? 100: 200;
Y is assigned the value 100. If x had been less than 9, y would have received the
value 200. The same code written using the if-else statement is
x = 10;
If (x>9) y = 100;
Else y = 200;
The? Operator will be discussed more fully in Chapter 3 in relationship to the other
conditional statements.
8) The Comma Operator
The comma operator strings together several expressions. The left side of the
comma operator is always evaluated as void. This means that the expression on the
right side becomes the value of the total comma-separated expression. For
example, x = (y=3, y+1);
First assigns y the value 3 and then assigns x the value 4. The parentheses are
necessary because the comma operator has a lower precedence than the assignment
operator.
Essentially, the comma causes a sequence of operations. When you use it on the
right side of an assignment statement, the value assigned is the value of the last
!
"
expression of the comma-separated list. The comma operator has somewhat the
same meaning as the word "and" in English, as used in the phrase "do this and this
and this."
Order of Evaluation
Precedence of C Operators
C does not specify the order in which the sub expressions of an expression are
evaluated. This leaves the compiler free to rearrange an expression to produce
more
Highest ( ) [ ] –>.
! ~ ++ –––(type) * & size of
* / %
+ –
<< >>
< <= > >=
== !=
&
^
|
&&
| |
?: = += –= *= /= etc.
Control Statement
There are four types of control statements
1) Selection Control Statement (Switch-Case)
2) Decision Making Control Statement (if, if-else, if-else-if ladder)
3) Iteration Control Statement (While, Do-While, For)
4) Jump Control Statement (Go to, Continue, Break)
!
"
I. Selection Control Statement: C supports two selection
statements: if and switch. In addition, the ? Operator is an alternative to if
in certain circumstances.
If:
The general form of the if statement is
if (expression) statement;
else statement;
Switch- Case Statement:
C has a built-in multiple-branch selection statement, called switch, which
successively tests the value of an expression against a list of integer or character
constants. When a match is found, the statements associated with that constant are
executed. The general form of the switch statement is
switch (expression)
{
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
default
statement sequence
}
- Note: The expression must evaluate to an integer type. Thus, you can use
character or integer values, but floating-point expressions, for example, are
!
"
not allowed. The value of expression is tested against the values, one after
another, of the constants specified in the case statements. When a match is
found, the statement sequence associated with that case is executed until the
break statement or the end of the switch statement is reached. The default
statement is executed if no matches are found. The default is optional, and if
it is not present, no action takes place if all matches fail.
- The break statement is one of C's jump statements. You can use it in loops
as well as in the switch statement (see the section ''Iteration Statements").
When break is encountered in a switch, program execution "jumps" to the
line of code following the switch statement.
There are three important things to know about the switch statement:
I. The switch differs from the if in that switch can only test for equality,
whereas if can evaluate any type of relational or logical expression.
II. No two case constants in the same switch can have identical values. Of
course, a switch statement enclosed by an outer switch may have case
constants that are in common.
III. If character constants are used in the switch statement, they are
automatically converted to integers (as is specified by C's type conversion
rules).
II. Decision Making Statement:
A) Nested ifs
A nested if is an, if that is the target of another if or else. Nested ifs are very
common in programming. In a nested if, an else statement always refers to the
nearest if statement that is within the same block as the else and that is not already
associated with an else
SYNTAX:
!
"
if(i)
{
if(j) dosomething1();
if(k) dosomething2(); /* this if */
else dosomething3(); /* is associated with this else */
}
else dosomething4(); /* associated with if(i) */
B) The if-else-if Ladder
A common programming construct is the if-else-if ladder, sometimes called the if-
else-if staircase because of its appearance. Its general form is
if (expression) statement;
else
if (expression) statement;
else
if (expression) statement;
.
.
.
else statement;
III. Iteration Statements:
In C, and all other modern programming languages, iteration statements (also
called loops) allow a set of instructions to be repeatedly executed until a certain
condition is reached. This condition may be predetermined (as in the for loop) or
open ended (as in the while and do-while loops).
A) The for Loop
The general design of the for loop is reflected in some form or another in all
procedural programming languages. However, in C, it provides unexpected
flexibility and power. The general form of the for statement is
SYNTAX: For (initialization; condition; increment) statement;
!
"
Q. In the following program, a for loop is used to print the numbers 1 through 100
on the screen:
Example: #include <stdio.h>
int main(void)
{
int x;
for(x=1; x <= 100; x++)
printf("%d ", x);
return 0;
}
Explanation: In the loop, x is initially set to 1 and then compared with 100. Since
x is less than 100, printf ( ) is called and the loop iterates. This causes x to be
increased by 1 and again tested to see if it is still less than or equal to 100. If it is,
printf ( ) is called. This process repeats until x is greater than 100, at which point
the loop terminates. In this example, x is the loop control variable, which is
changed and checked each time the loop repeats.
B) The while Loop
The second loop available in C is the while loop.
Its general form is:
While (condition)
{
Statement;
}
Where statement is an empty statement, a single statement, or a block of
statements. The condition may be any expression, and true is any nonzero value.
The loop iterates while the condition is true. When the condition becomes false,
program control passes to the line of code immediately following the loop.
C) The do-while Loop
!
"
Unlike for and while loops, which test the loop condition at the top of the loop, the
do-while loop checks its condition at the bottom of the loop. This means that a do-
while loop always executes at least once.
The general form of the do-while loop is:
Do
{
Statement;
}
While (condition);
Although the curly braces are not necessary when only one statement is present,
they are usually used to avoid confusion (to you, not the compiler) with the while.
The do-while loop iterates until condition becomes false.
Q. The following do-while loop will read numbers from the keyboard until it finds
a number less than or equal to 100:
Do
{
scanf(''%d", &num);
}
while(num > 100);
IV: Jump Statements
C has four statements that perform an unconditional branch: return, go to, break,
and continue. Of these, you can use return and go to anywhere inside a function.
You can use the break and continue statements in conjunction with any of the loop
statements. As discussed earlier in this chapter, you can also use break with switch.
The return Statement
The return statement is used to return from a function. It is categorized as a jump
statement because it causes execution to return (jump back) to the point at which
the call to the function was made. A return may or may not have a value associated
with it. A return with a value can be used only in a function with a non-void return
!
"
type. In this case, the value associated with return becomes the return value of the
function. A return without a value is used to return from a void function.
The general form of the return statement is:
Return expression;
The go to statement:
It requires a label for operation. (A label is a valid identifier followed by a
colon.)Furthermore, the label must be in the same function as the go to that uses
it— you cannot jump between functions.
The general form of the go to statement is
Go to label;
...
Label:
Where label is any valid label either before or after go to.
For example, you could create a loop from 1 to 100 using the go to and a label, as
shown here:
x = 1;
loop1:
x++;
if(x <= 100) go to loop1;
The break Statement:
The break statement has two uses. You can use it to terminate a case in the switch
statement (covered in the section on switch earlier in this chapter). You can also
use it to force immediate termination of a loop, bypassing the normal loop
conditional test. When the break statement is encountered inside a loop, the loop is
immediately terminated, and program control resumes at the next statement
following the loop. For example,
#include <stdio.h>
int main (void)
{
int t;
for(t=0; t < 100; t++) {
!
"
printf(''%d ", t);
if(t == 10) break;
}
return 0;}
The continue Statement
The continue statement works somewhat like the break statement. Instead of
forcing termination, however, continue forces the next iteration of the loop to take
place.
Block Statements
Block statements are simply groups of related statements that are treated as a unit.
The statements that make up a block are logically bound together. Block
statements are also called compound statements. A block is begun with a {and
terminated by its matching}. Programmers use block statements most commonly
to create a multi statement target for some other statement, such as if. However,
you may place a block statement anywhere you would put any other statement. For
Example, this is perfectly valid (although unusual) C code:

Weitere ähnliche Inhalte

Was ist angesagt?

01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
Tareq Hasan
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C language
Sachin Verma
 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
SINGH PROJECTS
 

Was ist angesagt? (20)

Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
C fundamental
C fundamentalC fundamental
C fundamental
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
C language
C languageC language
C language
 
C language ppt
C language pptC language ppt
C language ppt
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C programming notes
C programming notesC programming notes
C programming notes
 
OpenGurukul : Language : C Programming
OpenGurukul : Language : C ProgrammingOpenGurukul : Language : C Programming
OpenGurukul : Language : C Programming
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
Basic of the C language
Basic of the C languageBasic of the C language
Basic of the C language
 
Programming in C Presentation upto FILE
Programming in C Presentation upto FILEProgramming in C Presentation upto FILE
Programming in C Presentation upto FILE
 
C material
C materialC material
C material
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
 
Programming in C- Introduction
Programming in C- IntroductionProgramming in C- Introduction
Programming in C- Introduction
 
C notes for exam preparation
C notes for exam preparationC notes for exam preparation
C notes for exam preparation
 
C tokens
C tokensC tokens
C tokens
 
Declaration of variables
Declaration of variablesDeclaration of variables
Declaration of variables
 
C++ Basics introduction to typecasting Webinar Slides 1
C++ Basics introduction to typecasting Webinar Slides 1C++ Basics introduction to typecasting Webinar Slides 1
C++ Basics introduction to typecasting Webinar Slides 1
 

Andere mochten auch

Andere mochten auch (19)

Co question 2008
Co question 2008Co question 2008
Co question 2008
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Storage class
Storage classStorage class
Storage class
 
Rdbms2010
Rdbms2010Rdbms2010
Rdbms2010
 
Progr3
Progr3Progr3
Progr3
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Expected Questions TC
Expected Questions TCExpected Questions TC
Expected Questions TC
 
Preeprocessor
PreeprocessorPreeprocessor
Preeprocessor
 
Functions
FunctionsFunctions
Functions
 
Oops model question
Oops model questionOops model question
Oops model question
 
Iwt module 1
Iwt  module 1Iwt  module 1
Iwt module 1
 
Progr2
Progr2Progr2
Progr2
 
Dbms module i
Dbms module iDbms module i
Dbms module i
 
Btech 2nd ds_2005.ppt
Btech 2nd ds_2005.pptBtech 2nd ds_2005.ppt
Btech 2nd ds_2005.ppt
 
Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
Image recogonization
Image recogonizationImage recogonization
Image recogonization
 
System programming note
System programming noteSystem programming note
System programming note
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 

Ähnlich wie Pc module1

Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
valarpink
 
C programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfC programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdf
ComedyTechnology
 

Ähnlich wie Pc module1 (20)

unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
venkatesh.pptx
venkatesh.pptxvenkatesh.pptx
venkatesh.pptx
 
C intro
C introC intro
C intro
 
c#.pptx
c#.pptxc#.pptx
c#.pptx
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
C programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdfC programming language tutorial for beginers.pdf
C programming language tutorial for beginers.pdf
 
over all view programming to computer
over all view programming to computer over all view programming to computer
over all view programming to computer
 
Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptx
 
Interview Questions For C Language .pptx
Interview Questions For C Language .pptxInterview Questions For C Language .pptx
Interview Questions For C Language .pptx
 
Lecture 01 2017
Lecture 01 2017Lecture 01 2017
Lecture 01 2017
 
Computer programming questions
Computer programming questionsComputer programming questions
Computer programming questions
 
Computer programming questions
Computer programming questionsComputer programming questions
Computer programming questions
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 

Mehr von SANTOSH RATH

Lesson plan proforma database management system
Lesson plan proforma database management systemLesson plan proforma database management system
Lesson plan proforma database management system
SANTOSH RATH
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
SANTOSH RATH
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questions
SANTOSH RATH
 
( Becs 2208 ) database management system
( Becs 2208 ) database management system( Becs 2208 ) database management system
( Becs 2208 ) database management system
SANTOSH RATH
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
SANTOSH RATH
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
SANTOSH RATH
 
Operating system notes
Operating system notesOperating system notes
Operating system notes
SANTOSH RATH
 
Btech 2nd ds_2008.ppt
Btech 2nd ds_2008.pptBtech 2nd ds_2008.ppt
Btech 2nd ds_2008.ppt
SANTOSH RATH
 
Btech 2nd ds_2007.ppt
Btech 2nd ds_2007.pptBtech 2nd ds_2007.ppt
Btech 2nd ds_2007.ppt
SANTOSH RATH
 

Mehr von SANTOSH RATH (20)

Lesson plan proforma database management system
Lesson plan proforma database management systemLesson plan proforma database management system
Lesson plan proforma database management system
 
Lesson plan proforma progrmming in c
Lesson plan proforma progrmming in cLesson plan proforma progrmming in c
Lesson plan proforma progrmming in c
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questions
 
2011dbms
2011dbms2011dbms
2011dbms
 
2006dbms
2006dbms2006dbms
2006dbms
 
( Becs 2208 ) database management system
( Becs 2208 ) database management system( Becs 2208 ) database management system
( Becs 2208 ) database management system
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
 
Operating system notes
Operating system notesOperating system notes
Operating system notes
 
Os notes
Os notesOs notes
Os notes
 
OS ASSIGNMENT 2
OS ASSIGNMENT 2OS ASSIGNMENT 2
OS ASSIGNMENT 2
 
OS ASSIGNMENT-1
OS ASSIGNMENT-1OS ASSIGNMENT-1
OS ASSIGNMENT-1
 
OS ASSIGNMENT 3
OS ASSIGNMENT 3OS ASSIGNMENT 3
OS ASSIGNMENT 3
 
Ds using c 2009
Ds using c 2009Ds using c 2009
Ds using c 2009
 
Btech 2nd ds_2008.ppt
Btech 2nd ds_2008.pptBtech 2nd ds_2008.ppt
Btech 2nd ds_2008.ppt
 
Btech 2nd ds_2007.ppt
Btech 2nd ds_2007.pptBtech 2nd ds_2007.ppt
Btech 2nd ds_2007.ppt
 
Ds using c 2011
Ds using c 2011Ds using c 2011
Ds using c 2011
 
Co question 2009
Co question 2009Co question 2009
Co question 2009
 
Co question 2006
Co question 2006Co question 2006
Co question 2006
 

Kürzlich hochgeladen

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Krashi Coaching
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Kürzlich hochgeladen (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
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
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
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...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
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
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
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
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
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"
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 

Pc module1

  • 1. ! " What is a Flow Chart? A flow chart is a graphical or symbolic representation of a process. Each step in the process is represented by a different symbol and contains a short description of the process step. The flow chart symbols are linked together with arrows showing the process flow direction. What is an Algorithm? What are the steps of algorithm? Step-by-step method of solving a problem or making decisions, as in making a diagnosis. Steps of the algorithm ) Finiteness: - an algorithm terminates after a finite numbers of steps. 2) Definiteness: - each step in algorithm is unambiguous. This means that the action specified by the step cannot be interpreted (explain the meaning of) in multiple ways & can be performed without any confusion. 3) Input: - an algorithm accepts zero or more inputs 4) Output:- it produces at least one output. 5) Effectiveness:- it consists of basic instructions that are realizable. This means that the instructions can be performed by using the given inputs in a finite amount of time Difference between algorithm and flowchart? An algorithm is a precise rule (or set of rules) specifying how to solve some problem while A flowchart is a diagram of the sequence of operations (pictorial representation of algorithm) in a computer program
  • 2. ! " Keywords The following keywords are reserved and may not be used as an identifier for any other purpose. auto double int long break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while Formatted String: It specified to read which type of data. %c CHARACTER %d INTEGER %f FLOAT %lf DOUBLE %u UNSIGNED %s STRING %Lf LONG DOUBLE %ld LONG INT
  • 3. ! " Preprocessor directives in c In c all the preprocessor directives start with # character except define () operator. All the directives have different task which executes just before the actual execution of c program which makes the program more portable. These directives are used only to instruct compilers. Why preprocessor in c programming language? Answer: 1. It improves the readability of program. 2. It makes easy to modify 3. Portability #include directive in c Syntax: #include <filename> or #include “filename” Task of include directive is to include or import any type file. There are two form of include directive. Form 1: #include<filename>: This statement will search the file in include directory (Default location of include directory is c:tcinclude). If that file is present then #include statement is replaced by content of file. If that file is not present then it will cause of compilation error. //test.c #include<stdio.h> #include<conio.h> void main(){ clrscr(); printf("HOW large is this file?");
  • 4. ! " getch(); } C Programming - Data Types C language data types can be broadly classified as 1. Primary data type 2. Derived data type 3. User-defined data type Primary data type All C Compilers accept the following fundamental data types
  • 5. ! " Secondary Data Types • Array in C programming An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int,... • Pointers in C Programming In this tutorial I am going to discuss what pointer is and how to use them in our C program. Many C programming learner thinks that pointer is one of the difficult topic in C language but its not... • Structure in C Programming We used variable in our C program to store value but one variable can store only single piece information (an integer can
  • 6. ! " hold only one integer value) and to store similar type of values we had to declare... User defined type declaration C language supports a feature where user can define an identifier that characterizes an existing data type. This user defined data type identifier can later be used to declare variables. In short its purpose is to redefine the name of an existing data type. Syntax: typedef <type> <identifier>; like typedef int number; Now we can use number in lieu of int to declare integer variable. For example: “int x1” or “number x1” both statements declaring an integer variable. We have just changed the default keyword “int” to declare integer variable to “number A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common data types are integer, character, and floating-point. Main ( ): All C programs consist of one or more functions. As a general rule, the only function that must be present is called main ( ), which is the first function called when program execution begins
  • 7. ! " The general form of a C program: Global declarations int main(parameter list) { statement sequence } return-type f1(parameter list) { statement sequence } return-type f2(parameter list) { statement sequence }... return-type fN(parameter list) { statement sequence Review of Terms . • Source code: The text of a program that a user can read, commonly thought of as the program. The source code is input into the C compiler. • Object code: Translation of the source code of a program into machine code, which the computer can read and execute directly. Object code is the input to the linker. • Linker: A program that links separately compiled modules into one program. It also combines the functions in the Standard C library with the code that you wrote. The output of the linker is an executable program.
  • 8. ! " • Library: The file containing the standard functions that your program can use. These functions include all I/O operations as well as other useful routines. • Compile time: The time during which your program is being compiled. • Run time: The time during which your program is executing NOTE: The type void either explicitly declares a function as returning no value or creates generic pointers All Data Types Defined by the C Standard Type Typical Size in char 8 –127 to 127 unsigned char 8 0 to 255 signed char 8 –127 to 127 int 16 or 32 –32,767 to 32,767 unsigned int 16 or 32 0 to 65,535 signed int 16 or 32 Same as int short int 16 –32,767 to 32,767 unsigned short int 16 0 to 65,535 signed short int 16 Same as short int long int 32 –2,147,483,647 to 2,147,483,647 long long int 64 –(263 –1) to 263 –1 (Added by C99) signed long int 32 Same as long int unsigned long int 32 0 to 4,294,967,295 unsigned long long int 64 264 –1 (Added by C99) float 32 1E–37 to 1E+37 with six digits of precision double 64 1E–37 to 1E+37 with ten digits of precision long double 80 1E–37 to 1E+37 with ten digits of precision
  • 9. ! " Variables: As you probably know, a variable is a named location in memory that is used to hold a value that can be modified by the program. All variables must be declared before they can be used. The general form of a declaration is: type variable_list; Here, type must be a valid data type plus any modifiers, and variable_list may consist of one or more identifier names separated by commas. Here are some declarations: Eg: int i, j, l; Eg: short int si; Eg: unsigned int ui; Eg: double balance, profit, loss; Remember: Where Variables Are Declared Variables can be declared in three places: inside functions, in the definition of function parameters, and outside of all functions. These positions correspond to local variables, formal parameters, and global variables, respectively. Local Variables: Variables that are declared inside a function are called local variables Local variables exist only while the block of code in which they are declared is executing. That is, a local variable is created upon entry into its block and destroyed upon exit. Furthermore, a variable declared within one code block has no bearing on or
  • 10. ! " relationship to another variable with the same name declared within a different code block. Global Variables: Unlike local variables, global variables are known throughout the program and may be used by any piece of code. Also, they will hold their value throughout the program's execution. You create global Variables by declaring them outside of any function. Any expression may access them, regardless of what block of code that expression is in. Variable Initializations: You can give variables a value as you declare them by placing an equal sign and a constant after the variable name. The general form of initialization is type variable_name = constant; Some examples are char ch = 'a'; int first = 0; double balance = 123.23; Constants: Constants refer to fixed values that the program may not alter. Constants can be of any of the basic data types. The way each constant is represented depends upon its type. Constants are also called literals.
  • 11. ! " Escape Sequences: Code Meaning b Backspace f Form feed n new line r Carriage return t Horizontal tab " Double quote ' Single quote Backslash v Vertical tab a Alert ? Question mark N octal constant (where N is an octal constant) xN Hexadecimal constant (where N is a hexadecimal constant) Operators C is very rich in built-in operators. In fact, it places more significance on operators than do most other computer languages. There are four main classes of operators: arithmetic, relational, logical, and bitwise. In addition, there are some special operators, such as the assignment operator, for particular tasks 1) The Assignment Operator You can use the assignment operator within any valid expression the general form of the assignment operator is: variable name = expression; where an expression may be as simple as a single constant or as complex as you require. a) Multiple Assignments You can assign many variables the same value by using multiple assignments in a single statement. For example, this program fragment assigns x, y, and z the value 0:
  • 12. ! " x = y = z = 0; in professional programs, variables are frequently assigned common values using this method. b) Compound Assignments There is a variation on the assignment statement, called compound assignment. that simplifies the coding of a certain type of assignment operations. For example, x = x+10; can be written as x += 10; The operator += tells the compiler to assign to x the value of x plus 10. Compound assignment operators exist for all the binary operators (those that require two operands).In general, statements like a) var = var operator expression Can be rewritten as a) var operator = expression For another example, x = x-100; is the same as x -= 100; Because compound assignment is more compact than the corresponding = equivalent, compound assignment is also sometimes referred to as shorthand assignment. Compound assignment is widely Used in professionally written C programs; you should be familiar with it. 2) Arithmetic Operators Table: lists C's arithmetic operators. The operators +, –, *, and / work as they do in most other computer languages. You can apply them to almost any built-in data type. When you apply / to an integer or character, any remainder will be truncated. For example, 5/2 will equal 2 in integer division. The modulus operator % also works in C as it does in other languages, yielding the remainder of an integer division. However, you cannot use it on floating-point types. The following code fragment illustrates %: Operator Action – Subtraction, also unary minus + Addition * Multiplication / Division
  • 13. ! " % Modulus –– Decrement ++ Increment 3) Increment and Decrement Operators C includes two useful operators that simplify two common operations. These are the increment and decrement operators, ++ and ––. The operator ++ adds 1 to its operand, and ––subtracts 1. In other words: x = x+1; is the same as ++x; And x = X–1; is the same as x– –; Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand. For example, x = x+1; can be written ++x; or x++; There is, however, a difference between the prefix and postfix forms when you use these operators in a larger expression. When an increment or decrement operator precedes its operand, the increment or decrement operation is performed before obtaining the value of the operand for use in the expression. If the operator follows its operand, the value of the operand is obtained before incrementing or decrementing it. For instance. x = 10; y = ++x; Sets y to 11. However, if you write the code as x = 10; y = x++; y is set to 10. Either way, x is set to 11; the difference is in when it happens. Most C compilers produce very fast, efficient object code for increment and decrement operations code that is better than that generated by using the equivalent assignment statement. For this reason, you should use the increment and decrement operators when you can. Here is the precedence of the arithmetic operators: Here is the precedence of the arithmetic operators: Highest ++ –– – (unary minus)
  • 14. ! " * / % Lowest + – Operators on the same level of precedence are evaluated by the compiler from left to right. Of course, you can use parentheses to alter the order of evaluation. C treats parentheses in the same way as virtually all other computer languages. Parentheses force an operation, or set of operations, to have a higher level of precedence. 4) Logical Operators In the term relational operator, relational refers to the relationships that values can have with one another. In the term logical operator, logical refers to the ways these relationships can be connected. Because the relational and logical operators often work together, they are discussed together here. The idea of true and false underlies the concepts of relational and logical operators. In C, true is any value other than zero. False is zero. Expressions that use relational or logical operators return 0 for false and 1 for true. Table shows the relational and logical operators. The truth table for the logical operators is shown here using 1's and 0's. p q p && q p || q !p 0 0 0 0 1 0 1 0 1 1 1 1 1 1 0 1 0 0 1 0 5) Relational Operators Operator Action > Greater than >= Greater than or equal < Less than <= Less than or equal = = Equal
  • 15. ! " != Not equal Logical Operators Operator Action && AND | | OR ! NOT Table: Relational and Logical Operators The following table shows the relative precedence of the relational and logical operators: Highest! > >= < <= = = != && Lowest | | As with arithmetic expressions, you can use parentheses to alter the natural order of evaluation in a relational and/or logical expression. For example,! 0&&0 | | 0 Is false. However, when you add parentheses to the same expression, as shown here, the result is true. Remember, all relational and logical expressions produce a result of either 1 or 0. Therefore, the following program fragment is not only correct, but will print the number 1. The following table shows the relative precedence of the relational and logical operators: As with arithmetic expressions, you can use parentheses to alter the natural order of evaluation in a relational and/or logical expression. For example, is false. However, when you add parentheses to the same expression, as shown here, the result is true. Remember, all relational and logical expressions produce a result of either 1 or 0. Therefore, the following program fragment is not only correct, but will print the number 1. 6) Bitwise operators :
  • 16. ! " Unlike many other languages, C supports a full complement of bitwise operators. Since C was designed to take the place of assembly language for most programming tasks, it needed to be able to support many operations that can be done in assembler, including operations on bits. Bitwise operation refers to testing, setting, or shifting the actual bits in a byte or word, which correspond to the standard char and int data types and variants. Highest! > >= < <= = = != && The bitwise AND, OR, and NOT (one's complement) are governed by the same truth table as their logical equivalents, except that they work bit by bit. The exclusive OR has the truth table shown here: p q p ^q 0 0 0 1 0 1 1 1 0 0 1 1 - As the table indicates, the outcome of an XOR is true only if exactly one of the operands is true; otherwise, it is false. - Bitwise operations most often find application in device drivers— such as modem programs, disk file routines, and printer routines— because the bitwise operations can be used to mask off certain bits, such as parity. (The parity bit confirms that the rest of the bits in the byte are unchanged. It is often the high-order bit in each byte.) - Operator Action & AND | OR ^ Exclusive OR (XOR) ~ One's complement (NOT)
  • 17. ! " >> Shift right << Shift left Table: Bitwise Operators Example: #include<stdio.h> #include<conio.h> Void main () { Char ch; Int x=127, y; Y=ch&x; Printf(“%d”,y);} Output: Parity is often indicated by the eighth bit, which is set to 0 by ANDing it with a byte that has bits 1through 7 set to 1 and bit 8 set to 0. The expression ch & 127 means to AND together the bits in ch with the bits that make up the number 127. The net result is that the eighth bit of ch is set to 0. In the following example, assume that ch had received the character A and had the parity bit set: The bitwise OR, as the reverse of AND, can be used to set a bit. Any bit that is set to 1 in either operand causes the corresponding bit in the outcome to be set to 1. For example, the following is 128 | 3: An exclusive OR, usually abbreviated XOR will set a bit on, if and only if the bits being compared are different. For example, 127 ^120 is
  • 18. ! " Remember, relational and logical operators always produce a result that is either true or false, whereas the similar bitwise operations may produce any arbitrary value in accordance with the specific operation. In other words, bitwise operations may produce values other than 0 or 1, while logical operators will always evaluate to 0 or 1.The bit-shift operators, >> and <<, move all bits in a variable to the right or left as specified. The general form of the shift-right statement is - variable >> number of bit positions The general form of the shift-left statement is - variable << number of bit positions - As bits are shifted off one end, zeroes are brought in the other end. (In the case of a signed, negative integer, a right shift will cause a 1 to be brought in so that the sign bit is preserved.) Remember, a shift is not a rotate. That is, the bits shifted off one end do not come back around to the other. The bits shifted off are lost. - Notice that a sequence of two complements in a row always produces the original number. Hence, the first complement represents the coded version of that byte. The second complement decodes the byte to its original value. - unsigned char x; x as each statement executes value of x - x = 7; 00000111 7 - x = x<<l; 00001110 14 - x = x<<3; 01110000 112 - x = x<<2; 11000000 192 - x = x>>l; 01100000 96 - x = x>>2; 00011000 24 - Each left shift multiplies by 2. Notice that information has been lost after x<<2 because a bit was shifted off the end. - Each right shift divides by 2. Notice that subsequent divisions do not bring back any lost bits. 7) Ternary operator:
  • 19. ! " C contains a powerful and convenient operator that replaces certain statements of the if-then-else form. The ternary operator? Takes the general form Syntax: Exp1? Exp2: Exp3; Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the Colon. - The? Operator works like this: Exp1 is evaluated. If it is true, Exp2 is evaluated and becomes the value of the expression. If Exp1 is false, Exp3 is evaluated, and its value becomes the value of the expression. For example, in x = 10; y = x>9? 100: 200; Y is assigned the value 100. If x had been less than 9, y would have received the value 200. The same code written using the if-else statement is x = 10; If (x>9) y = 100; Else y = 200; The? Operator will be discussed more fully in Chapter 3 in relationship to the other conditional statements. 8) The Comma Operator The comma operator strings together several expressions. The left side of the comma operator is always evaluated as void. This means that the expression on the right side becomes the value of the total comma-separated expression. For example, x = (y=3, y+1); First assigns y the value 3 and then assigns x the value 4. The parentheses are necessary because the comma operator has a lower precedence than the assignment operator. Essentially, the comma causes a sequence of operations. When you use it on the right side of an assignment statement, the value assigned is the value of the last
  • 20. ! " expression of the comma-separated list. The comma operator has somewhat the same meaning as the word "and" in English, as used in the phrase "do this and this and this." Order of Evaluation Precedence of C Operators C does not specify the order in which the sub expressions of an expression are evaluated. This leaves the compiler free to rearrange an expression to produce more Highest ( ) [ ] –>. ! ~ ++ –––(type) * & size of * / % + – << >> < <= > >= == != & ^ | && | | ?: = += –= *= /= etc. Control Statement There are four types of control statements 1) Selection Control Statement (Switch-Case) 2) Decision Making Control Statement (if, if-else, if-else-if ladder) 3) Iteration Control Statement (While, Do-While, For) 4) Jump Control Statement (Go to, Continue, Break)
  • 21. ! " I. Selection Control Statement: C supports two selection statements: if and switch. In addition, the ? Operator is an alternative to if in certain circumstances. If: The general form of the if statement is if (expression) statement; else statement; Switch- Case Statement: C has a built-in multiple-branch selection statement, called switch, which successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed. The general form of the switch statement is switch (expression) { case constant1: statement sequence break; case constant2: statement sequence break; case constant3: statement sequence break; . . . default statement sequence } - Note: The expression must evaluate to an integer type. Thus, you can use character or integer values, but floating-point expressions, for example, are
  • 22. ! " not allowed. The value of expression is tested against the values, one after another, of the constants specified in the case statements. When a match is found, the statement sequence associated with that case is executed until the break statement or the end of the switch statement is reached. The default statement is executed if no matches are found. The default is optional, and if it is not present, no action takes place if all matches fail. - The break statement is one of C's jump statements. You can use it in loops as well as in the switch statement (see the section ''Iteration Statements"). When break is encountered in a switch, program execution "jumps" to the line of code following the switch statement. There are three important things to know about the switch statement: I. The switch differs from the if in that switch can only test for equality, whereas if can evaluate any type of relational or logical expression. II. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constants that are in common. III. If character constants are used in the switch statement, they are automatically converted to integers (as is specified by C's type conversion rules). II. Decision Making Statement: A) Nested ifs A nested if is an, if that is the target of another if or else. Nested ifs are very common in programming. In a nested if, an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else SYNTAX:
  • 23. ! " if(i) { if(j) dosomething1(); if(k) dosomething2(); /* this if */ else dosomething3(); /* is associated with this else */ } else dosomething4(); /* associated with if(i) */ B) The if-else-if Ladder A common programming construct is the if-else-if ladder, sometimes called the if- else-if staircase because of its appearance. Its general form is if (expression) statement; else if (expression) statement; else if (expression) statement; . . . else statement; III. Iteration Statements: In C, and all other modern programming languages, iteration statements (also called loops) allow a set of instructions to be repeatedly executed until a certain condition is reached. This condition may be predetermined (as in the for loop) or open ended (as in the while and do-while loops). A) The for Loop The general design of the for loop is reflected in some form or another in all procedural programming languages. However, in C, it provides unexpected flexibility and power. The general form of the for statement is SYNTAX: For (initialization; condition; increment) statement;
  • 24. ! " Q. In the following program, a for loop is used to print the numbers 1 through 100 on the screen: Example: #include <stdio.h> int main(void) { int x; for(x=1; x <= 100; x++) printf("%d ", x); return 0; } Explanation: In the loop, x is initially set to 1 and then compared with 100. Since x is less than 100, printf ( ) is called and the loop iterates. This causes x to be increased by 1 and again tested to see if it is still less than or equal to 100. If it is, printf ( ) is called. This process repeats until x is greater than 100, at which point the loop terminates. In this example, x is the loop control variable, which is changed and checked each time the loop repeats. B) The while Loop The second loop available in C is the while loop. Its general form is: While (condition) { Statement; } Where statement is an empty statement, a single statement, or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line of code immediately following the loop. C) The do-while Loop
  • 25. ! " Unlike for and while loops, which test the loop condition at the top of the loop, the do-while loop checks its condition at the bottom of the loop. This means that a do- while loop always executes at least once. The general form of the do-while loop is: Do { Statement; } While (condition); Although the curly braces are not necessary when only one statement is present, they are usually used to avoid confusion (to you, not the compiler) with the while. The do-while loop iterates until condition becomes false. Q. The following do-while loop will read numbers from the keyboard until it finds a number less than or equal to 100: Do { scanf(''%d", &num); } while(num > 100); IV: Jump Statements C has four statements that perform an unconditional branch: return, go to, break, and continue. Of these, you can use return and go to anywhere inside a function. You can use the break and continue statements in conjunction with any of the loop statements. As discussed earlier in this chapter, you can also use break with switch. The return Statement The return statement is used to return from a function. It is categorized as a jump statement because it causes execution to return (jump back) to the point at which the call to the function was made. A return may or may not have a value associated with it. A return with a value can be used only in a function with a non-void return
  • 26. ! " type. In this case, the value associated with return becomes the return value of the function. A return without a value is used to return from a void function. The general form of the return statement is: Return expression; The go to statement: It requires a label for operation. (A label is a valid identifier followed by a colon.)Furthermore, the label must be in the same function as the go to that uses it— you cannot jump between functions. The general form of the go to statement is Go to label; ... Label: Where label is any valid label either before or after go to. For example, you could create a loop from 1 to 100 using the go to and a label, as shown here: x = 1; loop1: x++; if(x <= 100) go to loop1; The break Statement: The break statement has two uses. You can use it to terminate a case in the switch statement (covered in the section on switch earlier in this chapter). You can also use it to force immediate termination of a loop, bypassing the normal loop conditional test. When the break statement is encountered inside a loop, the loop is immediately terminated, and program control resumes at the next statement following the loop. For example, #include <stdio.h> int main (void) { int t; for(t=0; t < 100; t++) {
  • 27. ! " printf(''%d ", t); if(t == 10) break; } return 0;} The continue Statement The continue statement works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place. Block Statements Block statements are simply groups of related statements that are treated as a unit. The statements that make up a block are logically bound together. Block statements are also called compound statements. A block is begun with a {and terminated by its matching}. Programmers use block statements most commonly to create a multi statement target for some other statement, such as if. However, you may place a block statement anywhere you would put any other statement. For Example, this is perfectly valid (although unusual) C code: