INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE...

32
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)- FIETE, FIE Director Academics, Bharat Institute Of Engineering And Technology Mangalpally Village, Ibrahimpatnam Rangareddy District

description

A SAMPLE ‘C’ PROGARM #include void main() { /* This program prints 1 to 10 one number on each line */ int i; clrscr(); for(i=0;i

Transcript of INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE...

Page 1: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

INTRODUCTION TO ‘C’ PROGRAMMING

BYProf. P. PADMANABHAM

M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIEDirector Academics,

Bharat Institute Of Engineering And TechnologyMangalpally Village, Ibrahimpatnam

Rangareddy District

Page 2: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

TOPICS COVERED

• A Sample ‘C’ Program• Entities of a ’C’ Program• Variables • Constants• Data Types• Identifiers• Expressions

Page 3: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

A SAMPLE ‘C’ PROGARM#include<stdio.h>#include<conio.h>void main(){/* This program prints 1 to 10 one number on each line */

int i;clrscr();for(i=0;i<10;i++) { printf(“%d\n”, i+1);}getch();

}

Page 4: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

DIFFERENT ENTITIES IN ‘C’ PROGARAM

• “#include<stdio.h> , #include<conio.h>” are compiler directives

• “void main(){ }” is a function written by the program writer

• “printf()” and “clrscr()” are library functions• “int i;” is a declaration and “i” is an integer variable• “for( i=0;i<10;i++){ }” is a control structure

• “i=0 ; i<10 ; i++” are Expressions • “/* This program prints 1 to 10 one number on each line

*/” is a comment

Page 5: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Variables• Variables are memory locations in computer's

memory to store data.• To indicate the memory location, each variable

should be given a unique name .• Variable names are just the symbolic

representation of a memory location. • Examples of variable name: a,x, sum,

no_of_students, count1 etc.

Page 6: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Rules for writing variable name in C• Variable name can be composed of letters (both uppercase and

lowercase ), digits and underscore '_' only.• The first letter of a variable name should be either a letter or an

underscore. But, it is discouraged to start variable name with an underscore though it is legal.

• There is no rule for the length of a variable. However, the first 31 characters of a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.

• A variable name should not be a reserve ( key) word.• In C programming, declare a variable before using it in the

program

Page 7: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Keywords:• Keywords are the reserved words used in

programming. Each keyword has fixed meaning and that cannot be changed by user. For example:• int money;• Here, int is a keyword that indicates, 'money'

is of type integer. • As, C programming is case sensitive, all

keywords must be written in lowercase.

Page 8: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Key(Reserve)words in C Language

Auto double int structBreak else long switchCase enum register typedefChar extern return unionContinue for signed voidDo if static whileDefault goto sizeof volatileConst float short unsigned

Page 9: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

constants• Constants are the entities that can't be changed

during the execution of a program. For example: 1, 2.5, "Programming is easy." etc.

• In C, constants can be classified as:1.Integer constants2.Floating point constants3.Character Constants4.String constants5.Enumeration Constants

Page 10: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

INTEGER CONSTANTS

• Integer constants are the numeric constants(constant associated with number) without any fractional part or exponential part. There are three types of integer constants in C language: decimal constant(base 10), octal constant(base 8) and hexadecimal constant(base 16) .

• Decimal digits: 0 1 2 3 4 5 6 7 8 9• Octal digits: 0 1 2 3 4 5 6 7• Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F.

Page 11: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

EXAMPLES FOR INTEGER CONSTANTS• You can use lowercase a, b, c, d, e, f instead of

uppercase letters while writing a hexadecimal constant.

• Every octal constant starts with 0 and hexadecimal constant starts with 0x in C programming.

• Decimal constants: 0, -9, 22 etc• Octal constants: 021, 077, 033 etc• Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

Page 12: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

FLOATING POINT CONSTATNS• Floating point constants are the numeric

constants are in fractional and/or in exponential form.

For example:• -2.0• 0.0000234• -0.22E-5• Here, E-5 represents 10-5. Thus, -0.22E-5 = -

0.0000022. We can use either “E” or “e”.

Page 13: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

CHARACTER CONSTANTS

• Character constants are the constant which use single quotation around characters. For example: 'a', 'l', 'm', 'F' etc.

• However , all character constants are internally represented in ASCII.

• Sometimes, it is necessary to use newline(enter), tab, quotation mark etc. in the program which either cannot be typed or has special meaning in C programming.

• In such cases, escape sequence are used

Page 14: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Different Escape Sequences

Page 15: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

String constants

Page 16: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Enumeration constants

Page 17: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Identifiers• In C programming, identifiers are names given to C entities, such

as variables, functions, structures etc. • Identifier are created to give unique name to C entities to

identify it during the execution of program. For example:• int money;• int gcd(int n1,int n2);• Here, money is a identifier which denotes a variable of type

integer. • gcd is an identifier which is a function name which returns an

integer and takes two integers as paramaters. • The rules for choosing an identifier name are same as applicable

to variable name (As discussed earlier)

Page 18: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

• In C, Different data types have different storage allocation and /or different format of storage in order to store different data used in a program.

• Variables that hold data should be declared before they can be used in program along with their data type by using the keywords(such as int,float,double,char etc.,) which are used for assigning a data type to a variable.

DATA TYPES

Page 19: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Data types in C

1. Fundamental Data Types – Integer types– Floating Type– Character types

2. Derived Data Types – Arrays– Pointers– Structures– Enumeration

• Syntax for declaration of a variable• data_type variable_name;

Page 20: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Integer data types• Keyword int is used for declaring the variable with integer type.

For example:• int var1;• Here, var1 is a variable of type integer.• The size of int is either 2 bytes or 4 bytes. • If you consider an integer having size of 4 byte( equal to 32 bits),

it has a range as: -231 to 231-1• Similarly, int of 2 bytes, it has a range as: -215 to 215-1.• If you try to store numbers larger than the ranges specified

above , program will not run correctly.• Some compilers refer 4 byte int as long and 2 byte int as short• If “unsigned” qualifier is used before the declaration of int it

stores 0 to 232 -1 or 0 to 216 depending on int being 2 bytes or 4 bytes

Page 21: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Floating point types• Variables of floating points types can hold real

values(numbers) such as: 2.34, -9.382 etc. • Keywords either float or double is used for declaring

floating type variable. For example:• float var2; double var3;• Here, both var2 and var3 are floating type variables.• In C, floating values can be represented in

exponential form as well. For example:• float var3=22.442e2

Page 22: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Difference between float and double• Generally the size of float(Single precision

float data type) is 4 bytes and that of double(Double precision float data type) is 8 bytes.

• Floating point variables has a precision of 6 digits whereas the precision of double is 14 digits.

• Precision describes the number of significant decimal places that a floating values carries.

Page 23: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Character types• Keyword char is used for declaring the variable of

character type. For example:• char var4='h';• Here, var4 is a variable of type character which is

storing a character 'h'.• The size of char is 1 byte.• The character data type is represented in ASCII

internally. • Each character is given a specific ASCII value• For example: 'a’ =97,’b’ =98, 'A’=65, ‘0’=48, '2’=50

Page 24: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

EXPRESSIONS

• In general an expression in C is constructed using variables and/or constants For Example:

• x+y*3.5 where x,y are variables +,* are operators and 3.5 is a floating point constant.

• The type of expression depends on variables and operators chosen. The above is an example of arithmetic expression.

• For example x>y is a relational expression

Page 25: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

TYPES OF OPERATORS IN C

• Arithmetic• Assignment • Relational • Logical• Increment and Decrement• Conditional operators• Bitwise and others

Page 26: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

HOW EXPRESSIONS ARE EVALUATED

• All expressions in C language are evaluated based on 1. Precedence of the operator2. AssociativityThe operator with higher precedence is evaluated first and with in the same precedence operators are evaluated based on associativity

Page 27: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

operator Meaning of operator Associativity

()[]->.

Braces to indicate higher priorityArray element reference indirect member selection ( through pointer)Direct member selection

Left to right

!~+-

++--&*

sizeof(type)

Logical negationBitwise(1 's) complementUnary plusUnary minusIncrementDecrementDereference Operator(Address)Pointer referenceReturns the size of an objectType cast(conversion)

Right to left(unary operators)

*/%

MultiplyDivideRemainder

Left to right

Page 28: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Operator Meaning of operator Associativity

+-

Binary plus(Addition)Binary minus(subtraction)

Left to right

<<>>

Left shiftRight shift

Left to right

<<=>

>=

Less thanLess than or equal

Greater thanGreater than or equal

Left to right

==!=

Equal toNot equal to

Left to right

& Bitwise AND Left to right

^ Bitwise exclusive OR Left to right

| Bitwise OR Left to right

&& Logical AND Left to right

|| Logical OR Left to right

?: Conditional Operator Left to right

Page 29: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Operator Meaning of operator Associativity

= Simple assignment Right to left

*=Assign product

/= Assign quotient

%= Assign remainder

-= Assign difference

&= Assign bitwise AND

^= Assign bitwise XOR

|= Assign bitwise OR

<<= Assign left shift

>>= Assign right shift

, Separator of expressions

Page 30: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

EXAMPLES OF EXPRESSIONS

1. 2+3*5 is evaluated as 17 and not as 252. (2+3)*5 is evaluated as 25 3. x+y>z*3 is evaluated to 1 if x+y is grater than

z*3 else to 04. x=y+=3 current value of y is incremented by

3 and that is assigned to x5. x<<=3 the current value of x is multiplied by

86. ~x+1 is evaluated to -x

Page 31: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

More examples

• 8>6>5• X=a>b ? a :b• X=a>b&&a>c?a:b>c?b:c• X=a<0?-a:a

Page 32: INTRODUCTION TO C PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.

Thank youFor further doubts send your question to

my [email protected]