complete c programming notes according to syllbus

February 21, 2017 | Author: sharma_rajwinder | Category: N/A
Share Embed Donate


Short Description

Download complete c programming notes according to syllbus...

Description

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

Introduction C is a High Level Programming Language. It is the most popular General Purpose Language. This Language was first developed by Dennis Ritchie at AT&T Bell Labs in 1972. Before this C language low level language was used for the computer which is very difficult for human to understand and there are so many problems in that type of language. So C language is developed which is a High Level Language which is very closer to human languages. C language has also many features like other high level languages like Readability, Maintainability, Portability, Usability etc.

Character Set Character sets means the characters and symbols that can be understand and accepted by the C language. These are grouped to form the commands, expressions, c-statements and other tokens for C Language. There are mainly four categories of the character set as given below.

1. Letter or Alphabet: These are represented by A-Z or a-z. C language is case sensitive so it takes different meaning for small case and upper case letters. There are total 26 letters used in the CProgramming.

2. Digit: These are represented by 0-9 or by combination of these digits, there are total 10 digits used in the C-Programming. Characters: There are some special symbols and punctuation marks used for some special 3. Special Characters:

 purpose. Here are total 30 special characters used in the C-Programming. Special symbols used under this category are like, +, -, *, /, @, #, $, %, ^ etc.

4. Empty space characters or white spaces: White spaces has blank space, new line return, Horizontal tab space, vertical tab space, etc.

Identifiers Identifiers are used for naming variables, methods, classes, labels and other programming elements. Identifiers must enforce following rules a) They can have have alphabets alphabets (a to z), digits (0 to 9), and and underscore. underscore. sal1

Valid

sal# sal#

Inva Invali lid d (# (# is is not not a val valid id char charac acte ter) r)

 b) They They must must not begin begin with with a digit digit.. sal1

Valid

_sal1 Valid 1sal 1sal

Inva Invali lid d (S (Start tart from from digi digit) t)

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 1

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

c) Uppercas Uppercasee and lower lowercase case lett letters ers are are distinc distinctt #include void main() {

int a=2;  printf(“%d”,A) ;

} O/P- This program generates compile error: Undefined Variable A because in our program we have declared a not A d) Keywor Keywords ds cannot cannot be used used as identifi identifiers. ers. int float

invalid (f (float is a keyword)

Keywords The keywords are also called reserved words. Keywords can not be used as variables names. These are mainly 40 keywords among which 32 are used by many compilers for high level programming, whereas remaining 8 reserve words are used by the programmer for low level programming. Following are the 32 Keywords of C. auto

do

typedef

for break

return double

go t o

start

un i on

case

else

if

sizeof

unsigned

char

in t

static

void

continue

extern

l on g

struct

while

volatile signed

constant switch

default double

 

register   enum

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 2

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

Constants/Literals Constant is one that has a fixed value throughout the program. Constants are assigned to variables in a  program. Constants can be divided into two categories 1) Numeric Constant 2) Non-Numeric Constant Constants

Numeric Constant

Integer

Non-Numeric Constant

Real or float

Decimal Octal Hexadecimal

Single Character String Character Ba Backslash character  

with

without

Exponent

Exponent

1. Numeric Constant:

These have numeric value having combination of sequence of digits i.e. from 0-9 as alone or  combination of 0-9 with decimal points having positive or negative sign. a. Integer Constant:

Integer numeric constant have integer data combination of 0-9 without any decimal point. These are further subdivided into three parts:

i.

Decimal Constant: These have no decimal point in it and is either be alone or the combination of 0-9 digits. These have either +ve or –ve sign.

For example 124, -345, +45 etc.

ii.

Octal Constant: These consist of combination of digits from 0-7 with positive or  negative sign. It has leading with O or o. For example O37, -O87, O0 etc.

iii.

Hexadecimal Constant: These consist of combination of digits from 0-9 and A-F. It has leading with Ox or ox. For example Ox37, ox87, OX0 etc.

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 3

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

b. Real or Float Constant:

Real numeric constant have data combination of 0-9 with decimal point. It may have positive or negative values. It is also called floating point constant. These are further subdivided into two  parts:

i.

Without exponent: Real numeric constant without exponent is a simple numeric value having combination of 0-9 with decimal point.

For example: 3.54, 34.56, -987.56 etc.

ii.

With exponent: Real numeric constant with exponent have two parts in the constant value. One part is mantissa and other part is exponent part.

For example: 3.2e-04 (0.00032), 65.74e01(657.4) etc. 2. Non-Numeric Constant:

These Non-numeric constant have values only from alphanumeric characters. There are not any negative values in these constants.

a.

Single Character Constant: This type of constant contains only single character from alphanumeric character sets. This single character is written in single quotation marks.

For example: ‘a’, ‘A’, ‘2’ valid constants,

‘sd’ invalid constants.

 b. String Character Constant: This type of constant contains more than one character in it. It contains multiple characters according to our requirements. These characters are written in double quotation marks. For example: “Welcome to IIMT”etc.

c.

Backslash Character Constant: These type of constant contains some characters for some special purposes. Following are some of the backslash character constants. These are also known as Escape sequences. Esca Escape pe Sequ Sequen ence ce Purp Purpos osee

\a

Alert/ bell/ beep

\b

Backspace

\f

Form feed

\n

New line

\t

Tab

\\

Backslash

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 4

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

\’

Single quotation mark  

\”

Double quote

Punctuators These are symbols used for grouping and separating code. They are also known as separators or Delimiters. Separators are given below



Parentheses () Braces { } Brackets [ ]



Semicolon (;)



Colon (:)



Comma (,)



Period (.) etc.

• •

Data Types Data types are used to declare variables. Variable declaration tells us 1) Ty Type pe of valu valuee a varia variabl blee can hol hold d 2) Name Name of vari variab able le 3) Range There are mainly five types of data type used in C 1. Primar Primary y or scalar scalar or standar standard d or fundame fundamental ntal or simpl simplee data type type 2. Secon Seconda dary ry or or deri derived ved dat dataa type type 3. Enumerat Enumerated ed Data Data types types or User User define defined d data type typess 4. Empty Empty data data typ typee or voi void d data data typ typee 5. Poin Pointe terr data data ty type representing a single value only. Scalar Scalar data type is further  1. Primary or Scalar Data type: it is used for representing divided into three types 1. Integer types 2. Floating types 3. Character 

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 5

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

Integer type: Integer type can store integer constant i.e. numerical value without decimal points.  Numerical value can be positive as well as negative. It supports 4 types of integers as shown in table

Type

Size

Range

In t

2 bytes -32768 to 32767

Unsigned int

2 bytes 0 to 65535

Long

4 bytes -2 31 to 2 31-1

Unsigned long 4 bytes 0 to 2 32 -1 Floating Data Type: Floating data type can store real constants i.e. numerical value with decimal  points. Values can be positive as well as negative.

Type

Size

Range

Float

4 bytes

3.4 * 10  –38 to 3.4 * 1038

D o ub l e

8 bytes

1.7 * 10  –308 to 1.1 * 10308

Long double

10 bytes

3.4 * 10  –4932 to 1.1 * 10 4932

Character data type can hold only a single alphabet (A-Z) or digit (0-9) or special Character Type: Character symbol. The character data type assumes a size of one byte. It has been designed to hold 8 bit ASCII code. Its range is from 0 to 255. 2. Secondary or Derived data type:

Secondary data types are derived from the scalar data types. Secondary data type may be used for  representing single value or multiple values. Secondary data types are divided into three categories 1. Arra Array y or or Str Strin ings gs 2. Struct ucture ures 3. Unions 3. Enumerated Data types or User defined data types:

It provides us a way to define our own data types. This is used when you know in advance the finite number of values a variable can take in a program. ‘Enumerated’ means that all values are listed. These data types are internally treated as integers. By default first member gets value 0, second 1 and so on.

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 6

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

#include enum dow{sun=1,mon,tue,wed,thu,fri,sat,sun}; void main() {

dow d1,d2; d1=mon; d2=thu;  printf(“%d”,d2-d1);

} Output: 3 4. Void or Empty Data Type:

Void or empty data type is used in the user defined functions. This is used when function returns nothing. This is also used when function has no arguments. 5. Pointer Data Type:

Pointer data types are used to store memory addresses instead of values.

White spaces White space is defined as spaces, carriage return, line feeds, tabs, vertical tabs and form feeds. These are ignored by the compiler. But there are some exceptions: 1. The string string const constant ant can not be split. split. 2. #include #include must must be writ written ten on a single single line line.. 3. // symbols symbols can be used used to show comments. comments. These are are valid only only for a single line; line; no white white space should be used between these slashes.

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 7

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

C operators An operator is a symbol that tells the compiler to perform a certain mathematical or logical manipulators. C supports a rich set of operators. C operators can be classified into following categories: 1. Arithmetic operators

Arithmetic operators are used for arithmetic operations like Addition, Subtraction, Multiplication, Division & Modulus. These can operate on any built in numeric type. Following are the list of Arithmetic Operators. Operator

Meaning

+

Addition

-

Subtraction or unary minus

*

Multiplication

/

Division

%

Modulus division

E.g. If a=14 and b=4 we have following results a-b=10

a+b=18

a*b=56

a/b=3( decimal part trncated )

a%b=2 (Remainder (Remainder of the division) Here a, b are known as operands. Operator is called binary operator if it requires two operands and unary operator if it requires one operand. Type Conversions: When two operands of different types are encountered in the same expression low type variable is converted to the type of the higher type variable. The conversion takes place (implicitly) invisible to the user.

Long Lo ng Doub Double le High Highes estt orde order  r  Double Float Long Int

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 8

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

Char

E.g.

(Lowest order)

1. a = 11/5= 2

2. a = 11/5.0=2.2

Casting: The term applies to the data conversions by the programmer as opposed to the automatic data conversion. It is also known as explicit conversion.

E.g.

a = 11/( int ) (5.0 ) =2

Expressions: An expression is a combination of operands and operators. In an expression there may be multiple operands and multiple operators depends upon the situation. An operand may be a variable or a constant value. Expression are of five types. 1. Arithmetic Expression 2. Relational Expression 3. Logical Expression 4. Assignment Expression 5. Conditional Expression 1. Arithmetic Expression: Arithmetic Expressions are the expressions which contains only the Arithmetic Operators. These are of three types. a) Integer Arithmetic Expression: When all the operands are of Integer type then the Expression is known as Integer Arithmetic Expression. For Example: 5 +2, 35/3 etc. b) Real Arithmetic Expression: When all the operands are of real type then the expression is known as Real Arithmetic Expression. For Example: 5.4*3.2, 567.7/45.7 etc. c) Mixed Mixed Mode Arithmetic Arithmetic Expressi Expression: on: When all the operands are of mixed type i.e. some operands are of real type and some operands are of Integer type then the Expression is known as Mixed Mode Arithmetic Expression. For example: 3+5.6, 567* 4.5 etc. 2. Relational Operators: The relational operators are symbols that are used to test the relation between two expressions. expressions. These operators return true or false i.e. 1 or 0.Relational operators operators are binary operators because they required two operands. There are mainly six type of relational operators used in C-Language. Oper Op era ator tor Meaning ning

==

Equal to >

Greater than

<

Less than

!=

Not equal to

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 9

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

>=

Greater than or equal to

3 etc. b) Real Relational Expression: When all the operands are of real type then the expression is known as Real Relational Expression. For Example: 5.4= =3.2, 567.7!=45.7 etc. c) Mixed Mode Relational Expression: When all the operands are of mixed type i.e. some operands are of real type and some operands are of Integer type then the Expression is known as Mixed Mode Relational Expression. For example: 345< 34.5, 23.45 >= 34 etc. 3. Logical operators: Logical operators are used to combine two or more relations. These are used in decision making statement because they return true or false values only i.e. 1 or 0. There are mainly three type of Logical Operators used in the C-Language. Opera perattor Meani eaning ng

&&

AND

||

OR

!

NOT

 

For example: void main() {

int a=2,b=0,c; if( a>=2 && a 3) etc. b) Real Logical Expression: When all the operands are of real type then the expression is known as Real Logical Expression. For Example: (5.4= =3.2) || (567.7!=45.7) etc. c) Mixed Mode Logical Expression: When all the operands are of mixed type i.e. some operands are of real type and some operands are of Integer type then the Expression is known as Mixed Mode Logical Expression. For example: (345< 34.5)&& (23.45 >= 34) etc. 4. Assignment operator: Assignment operators are used for assigning an expression or value to a variable. It is the short hand symbol for arithmetic & bitwise operators. Assignment operators are binary operators  because they required two operands. Some of the commonly used assignment operators are given below

Stat Statem ement ent usi using ng Assi Assignm gnmen entt Opera Operator tor

Equiv Eq uival alent ent stat statem ement ent usi using ng arith arithme meti ticc operato operator  r 

a+=1

a=a+1

a- =1

a=a-1

a*=5

a= a* 5

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 11

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

a\=5

a=a/5

a%=b

a=a%b

4. Assignment Expression: Assignment Expressions are the expressions which contains the Assignment Operators. These are of three types. a) Integer Assignment Expression: When all the operands are of Integer type then the Expression is known as Integer Assignment Expression. For Example: a = 5+2, b= 35*3 etc. b) Real Assignment Expression: When all the operands are of real type then the expression is known as Real Assignment Expression. For Example: c=5.4/3.2, d=567.7*5.7 etc. 5. Conditional operator or Ternary operator: This operator is compressed version of if-else statement. This is called ternary operator because it requires three operands. The syntax of the statement is

C=( a > b) ? a : b; This above statement returns the greatest number as a value of C variable i.e if a is greater than b then the value of a is assigned to c otherwise the value of b is assigned to c. 6. Increment/Decrement Increment/Decrement operator: The ++ (increment) operator adds 1 to the operand and – (decrement) subtrac subtracts ts 1 from the operand. operand. Both are unary unary operator operatorss because because they required required single single operand. operand. These operators can be used in two ways:

1. Prefix operator  2. Post Postfi fix x oper operat ator  or  1. Prefix Prefix Incremen Increment/Dec t/Decreme rement nt Oper Operator ator:: In this first of all value will be incremented or decremented (according to operators ++ & --) and then that new value will be assigned to a variable. 2. Postfix Postfix Incremen Increment/De t/Decrem crement ent Oper Operator ator:: In this first of all value will be assigned to a variable then its is incremented or decremented according to the operator used. //Program to explain Postfix and Prefix Increment Operator

#include void main ( ) {

int a=2, b=2, c, d; c= ++a; d= b++;  printf(“\na=%d\tc=%d”,a,c);  printf(“\nb=%d\td=%d”,b,d);

} Output:

a=3

c=3

b=3

d=2

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 12

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

7. Bitwise Operators:

These operators are mainly used for the operation of Binary bits i.e. 0 and 1. So these are mainly used for  low level programming. Bitwise operations are the testing, setting or shifting of the actual bit in a byte. These operators should not be of float or double type due to binary version. There are mainly six Bitwise Operators used in C programming.

Bitwise Operator

Meaning

&

Bitwise AND

|

Bitwise OR

 

^

Bitwise XOR

 

>

Bitwise Right

~

One’s Complement

8. Special Operators: These are used for special purposes in C-Language. These operators are used in Pointers, Structures and Unions etc. Six type of Special Operators are given below.

a. Unary Operator

b. Comma Operator  

c. Sizeof Operator

d. Type Operator  

e. Pointer Operator

f. Member Selection Operator  

Sizeof Operator This operator returns the number of bytes occupied by operand. The operand may be a variable, a constant or a data type. For Example a= sizeof(int)

//a=2

a=sizeof(float)

//a=4

a=sizeof (char)

//a=1

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 13

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

Precedence of operators: In an expression operators with higher precedence are evaluated before lower   precedence operator. Operators having equal precedence are evaluated on the basis of associatively.

Desc.

Operator

Associatively

Higher ( ), [ ], ->, .

Left to right

-, ++, --, ~, !

Right to left

& (address of) * ( value of address) ( Type ) casting Size of  *,/,%

Left to right

+-

Left to right

> ( Right shift ) < , , >=

Left to right

== , !=

Left to right

& (Bitwise AND)

Left to right

^ Bitwise XOR

Left to right

| Bitwise OR

Left to right

&&

Left to right

||

Left to right

? : ( cond condit ition ional al oper operat ator) or) Right Right to left left Lower

=

Right to left

*=

|=

%=

Right to left

+=

-=

&=

Right to left

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 14

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

^=

|=

Right to left

==

Right to left

Control Statements or Flow Controls A program consists of some statements which will execute in some sequiential manner but if we want to override that flow of execution of statements then we need some type of Flow Control Statements. So there are mainly three different categories to control the flow of execution. 1. Branching 2. Looping 3. Jumping

1. Branching To override the sequential flow of execution branching is must. At the same time arbitrary unconditional  branches are not healthy for programming. Branching must be done on a test. C supports following control or decision making statements 1. If st statement 2. swit switch ch stat statem emen entt 3. condit conditio ional nal oper operato atorr statem statemen entt If statement

If statement is used to control the flow of execution of statements. It is a two way decision statement and used in conjunction with an expression. If statement is of further four types. a) Simple If Statement: When there is only one statement in our program, then we need only one  block of statement. At that time we need only Simple if statement. The syntax of if statement is like If (condition) {true statement block;} other statements;

In this type of statement condition will be checked. If condition is true then true statement  block will be executed and after that other statements block will be executed. If the condition is false only other statements bock will be executed. b) If-else Statement: This statement also contains single condition but with two different blocks one for the true condition and one for the false condition. The syntax of if statement is like: If (condition) {True Statement- block;} else {False Statement- block;} Statement – n;

Expressi on ?

False

True

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 15

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

In this type of statement condition will be checked. If condition is true then true statement  block will be executed and after that statement - n block will be executed. If the condition is false then the block of false statement will be executed and after that statement – n block will be executed. c) Nested If Statement: When one if statement occurs within another if statement then that type of if  statements statements are known as nested if statements. statements. These statements statements are used to solve some complex type of problems. The syntax of this statement is given below: If(condition1) { If (condition2) {Statement1;} else {Statement2;} } else {Statement3;} Statement – n; }

In this type of statement condition1 will be checked. If condition1 is true then next condition2 will be checked. If condition2 is true then Statement1 will be executed and Statement – n will be executed and if condition2 will be false then Statement2 will be executed and then Statement – n will  be executed. If condition1 becomes false then Statement3 will be executed and after that Statement –  n will executed. d) Ladder if-else statement: This type of statement is used only when there are many number of  conditions to be there to check. The program showing the structure of this statement are given below:

//Program to print the division of a student on the basis of following criteria: Marks>=60

First

Marks>=50 AND Marks=40 AND Marks=50)  printf(”Second”); else if(marks >=40)  printf(”Third”);

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 16

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

else  printf(”Fail”); } 2. Switch statement:

Switch statement is a multi way decision statement which tests the value of a given variable (expression) against a list of case values and when a match is found a block of statements associated with that case is executed. The general form of switch statement is as shown below Switch (expression) { case value1: block-1; break; case value 2: block-2;break ; default: default block; } //Program to check if the given character is vowel

void main( ) {

char ch;  printf (”\n Enter any character”); scanf (“%c”, &ch) ; switch(ch) {

case ‘A’ : case ‘a’ : printf(”Vowel”); break; case ‘E’ : case ‘e’ : printf(”Vowel”); break; case ‘I’ : case ‘i’ : printf(”Vowel”); break; case ‘O’ : case ‘o’ : printf(”Vowel”); break; case ‘U’ : case ‘u’ : printf(”Vowel”); break; default : printf(”\n Not Vowel”) ;

} }

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 17

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

Conditional Operator (? :) :

Conditional operator is compressed version of if statement. It is also called ternary operator because it requires three operands. The general form of use of the conditional operator is as follows Conditional Expression? exp1: exp2 The conditional expression is evaluated first if the result is true exp1 is evaluated and is returned otherwise exp2 is evaluated and its value is returned. For Example

void main( ) {

int x=2, y ; y= ( x>2) ? ( 2 * x * 5) : (3 * x +1);  printf(“%d”,y) ;

} Output : 7

VPO Manuke Gill, Distt Moga, Barnala-Baghapurana Road 18

Affiliated With

Punjab Technical University Ideal Institute of Management &Technology

Er. Rajwinder Sharma +9193168-11444

2. Looping: Loops Loops are used to repeat repeat the some some portion portion of a program program either either a specified specified number number of times times or until until a  particular condition is being satisfied. Mainly loops are of two types. One is Entry Controlled Loop and another is Exit Controlled Loop. firstly conditi condition on is checked checked if it is true then body of the a) Entry Control Loop: In this type of loop firstly loop is executed otherwise body of the loop is not executed. b) Exit Control Loop: In this type of loop firstly body of the loop is executed then condition is checked, now if condition is true then body executed again until the condition becomes true otherwise the  program goes out of the loop.

After the above types, there are three ways by which we can repeat a part of the program. 1. For statement 2. While statement 3. Do-while statement

1. For loop: For loop is an entry control loop. It is one step loop, which initialize, check the condition and increment/decrement the step in the loop in a single step. For loop is used where the loop will be traversed a fixed number of times. The general form of for statement is as under  for( initialization; test condition; modifier expression) { body of the loop; } For Example

void main( ) {

int i; for( i=1;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF