Presentation is loading. Please wait.

Presentation is loading. Please wait.

Powerpoint Templates Page 1 Programming in C/C++ PS04CINS02.

Similar presentations


Presentation on theme: "Powerpoint Templates Page 1 Programming in C/C++ PS04CINS02."— Presentation transcript:

1 Powerpoint Templates Page 1 Programming in C/C++ PS04CINS02

2 Powerpoint Templates Page 2 History Levels of programming Language –Machine Level –Assembly Level –High Level 1960- Cambridge University –BCPL(Basic Computer Programming Language) 1972- Bell Laboratory –C coded by Dennis Ritchie –Associated with Unix (Unix written in C)

3 Powerpoint Templates Page 3 Importance of C Language Robust Rich set of Functions, Operators and Data Types Efficient codingKey-words: 32 onlySeveral library functions

4 Powerpoint Templates Page 4 Importance of C Language Portability Compatible from one PC to another PC Capability of Assembly and Higher Languages Suitable for System and Comm. Softwares Supports Structured Programming Suitable for modular programming Self extension capability

5 Powerpoint Templates Page 5 Basic Structure of C Program Subroutine Section Main function Section Global Declaration Section Definition Section Link Section Document Section /* comments */ # include # define variables or constants Declaration of global variables Declaration functions Execution

6 Powerpoint Templates Page 6 Basic structure of C Program Documentation: Comments about program details, author etc. Link: Link functions from library Definition: symbolic constant definition Global Var: declaration of global variables Main: two parts –Declaration of local variables. –Executable expressions Functions: Subroutines

7 Powerpoint Templates Page 7 Programming Style Free form Lowercase writing {,},#include<>,#define,; Single line or multiple lines each for one expression commments

8 Powerpoint Templates Page 8 Program Compilation and Execution System Ready Link with System Library Correct Output Stop System Library Execute Object Code C Compiler Compile Source Program Edit Source Program Program Code Enter Program Syntax Errors? Input Data Logic and Data Errors? Y N No Error Logic Error Data Error

9 Powerpoint Templates Page 9

10 Powerpoint Templates Page 10 Characters Letters: A-Z & a-z Digits: 0-9 Special characters: White Spaces: –Space, Horizontal and Vertical Tabs, Carriage Return, New Line, Form feed,.;:?‘ “!|/\~ _$%#&^ *-+<>( )[]{}=

11 Powerpoint Templates Page 11 Tokens Smallest individual unit –6 types C Tokens Keywords Float while Identifiers Main fact Constants 100 -3.29, ‘a’ Strings “ABCDEF” “hello!” Special Symbols [], {}, <> Operators +, -, /, * =,&&,||

12 Powerpoint Templates Page 12 Keywords & Identifiers Keywords: –Have fixed meaning –Serve as basic building blocks Identifiers: –Names of variable, constants, functions, arrays etc. –User defined with letters and digits; beginning with a letter

13 Powerpoint Templates Page 13 C Keywords

14 Powerpoint Templates Page 14 Constants Not variable during execution of program ConstantsNumericIntegerDecimalOctalHexadecimalRealCharacter Single Character String

15 Powerpoint Templates Page 15 Constants 0 to 9 and +or – sign e.g. 124, -345, 0, +52, -327857 Valid Space, comma, symbols e.g. 10 277 | 23,356 | Rs.458 | $852 Invalid Starting with 0 using digits 0 to 7 e.g. 056, 0246 Octal Starting with 0x or 0X using digits 0 to 9 and A to F or a to f e.g. 0x56, 0XA246, 0xabcd Hexadecimal Unsigned, Long and unsigned long integers e.g. 45682U, 99999999L, 502045484UL Larger integers Contains fractions e.g. 0.025, 2.025, -3.24, +5.741, 324.,.241, -.21, +.8, 0.52e8, - 1.25E-12 Real

16 Powerpoint Templates Page 16 Character Constants Single character constant: ‘e’, ‘W’, ‘2’,’.’ etc. Srting constants: “hello”, “17”, “12+23” etc. Backslash constants:

17 Powerpoint Templates Page 17 Data Types C Supported Data Types Primary User- defined DerivedEmpty

18 Powerpoint Templates Page 18 Primary Data Types Integers Signed short int int long int Unsigned unsigned short int unsigned int unsigned long int Real float double long double Character signed char unsigned char

19 Powerpoint Templates Page 19 Range of data types

20 Powerpoint Templates Page 20 Declaration of Variables Declaration defines: –Variable name and its type of data Primary Type Declaration –e.g. The type of variable is represented by corresponding keyword

21 Powerpoint Templates Page 21 Keywords for data-type CharacterCharUnsigned character Unsigned char Signed character Signed char/ char Signed integerSigned int / int Signed short integer Signed short int/ short int / short Signed long integer Signed long int / long int / long unsigned short integer unsigned short int / unsigned short unsigned long integer unsigned long int / unsigned long Unsigned integer Unsigned int / unsigned Floating pointFloat Double precision floating point DoubleExtended double precision floating point Long double

22 Powerpoint Templates Page 22 User defined type declaration User defined identifier enum typedef int whole; typedef float real whole no1, no2; real no3,no4; typedef int whole; typedef float real whole no1, no2; real no3,no4; typedef type identifier enum day {sun, mon,tue, wed, thu, fri, sat}; enum currency {1,2,5,10,20,50,100,500,1000}; enum day wday; wday = sun; enum currency note; note=100; enum day {sun, mon,tue, wed, thu, fri, sat}; enum currency {1,2,5,10,20,50,100,500,1000}; enum day wday; wday = sun; enum currency note; note=100; enum identifier { value1, value2,….valuen};

23 Powerpoint Templates Page 23 Storage Class Local and global –Auto: local; known to function in which defined –Static: local; retains its value even after control is transferred to function –Extern: global; known to all functions –Register: local; stored into registers auto int n; static char str1; extern float value; register int n1;

24 Powerpoint Templates Page 24 Variable Name of data used to store some data value Rules for naming: –Must begin with a letter –Maximum 31 characters –Uppercase and Lowercase are significant –Must not be same as keyword –Space not allowed Valid:John, alpha, a_1, B_pack Invalid: 123, (area), ast%, 1alpha

25 Powerpoint Templates Page 25 Assignment statement var-name=value; no1=47; str1=“hello”; a=b=c=3.4; int n=47,sum=0; Declaration as constant const data-type var-name = value; const int cmax=35; const float pi=3.14; Declaration as Volatile volatile data-type var-name; volatile const data-type var-name; volatile int n; volatile const int cstr=35;

26 Powerpoint Templates Page 26 scanf(“%d”,&a); scanf(“%f”,&b); scanf(“%d %d”,&a, &b); scanf(“%d %f %d”,&a, &b, &c); scanf(“contol string”,&var1,&var2….); printf(“%d”,a); printf(“%f”,b); printf(“%d %d”,a, b); printf(“%d %f %d”,a, b, c); printf(“contol string”,var1,var2….);

27 C Operators ArithmeticRelationalLogicalAssignmentIncrementDecrementConditionalBitwiseSpecial

28 Powerpoint Templates Page 28 Arithmetic Operators +Addition or unary plus -Subtraction or unary minus *Multiplication /Division %Modulo Division Integer Arithmetic Real Arithmetic Mixed Arithmetic –Casting (float) a, (int) area

29 Powerpoint Templates Page 29 Relational Operators <Less than >Greater than <=Less than or equal to >=Greater than or equal to ==Equal to !=Not equal to Result is either TRUE or FALSE Used in decision making statements like –if, while, for etc.

30 Powerpoint Templates Page 30 Logical Operators &&Logical AND ||Logical OR !Logical NOT Result is either TRUE or FALSE Used in decision making statements when more than one condition is to be verified. e.g. –a>b && b<c –a==b || a<d –!(a<b)

31 Powerpoint Templates Page 31 Assignment Operators Left hand operand is not required to be repeated on right side of expression. Concise Efficient Sum +=a; Short hand assignment operators a=a+1a+=1 a=a-1a-=1 a=a*(n+1)a*=(n+1) a=a/(b+c)a/=(b+c) a=a%(x*y)a%=(x*y)

32 Powerpoint Templates Page 32 Increment and Decrement Operators ++Increment --Decrement a=b++; –a=b; –b=b+1; a=++b; –b=b+1; –a=b;

33 Powerpoint Templates Page 33 Bitwise Operators &Bitwise AND |Bitwise OR ^Bitwise XOR <<Shift left >>Shift right ~Bitwise NOT Applied to integers only. Used for testing bits Possible operations: –Masking –Setting –complement

34 Powerpoint Templates Page 34 Conditional Operator exp1? exp2: exp3 –a=10; –b=12; –x=(a>b)? a : b; Used for either or type situations

35 Powerpoint Templates Page 35 Special Operators Comma Operator Used to link related expressions together. a=(b=5,c=2,b+c); –b=5; –c=2; –a=b+c; & Operator –To indicate address Sizeof Operator Returns number of bytes required to store the variable. –a=sizeof(int); –b=sizeof(area); –c=sizeof(2.3); * Operator –To indicate value for given address

36 Powerpoint Templates Page 36 Arithmetic Expressions Evaluation Precedence of Operators –High priority* / % –Low priority+ - Computational Problems –a=1.0/3.0; –b=a*3.0 a x b – ca*b-c (m+n) (x-y)(m+n)*(x-y) (ab)/ca*b/c ax 2 + bxy + ca*x*x + b*x*y + c

37 Powerpoint Templates Page 37 Type Conversion Automatic int i,x; float f; double d; long int l; x=l / i + i * f – d; Casting a Value –(type-name) expression Examples int long x = l / i + i * f – d; long float double

38 Powerpoint Templates Page 38 Exercise Find errors in the exp. A= b = c = 12.., 23, 34; X = ++y*z; P* = x/y; S = /4; A = b++ -c * 2; If a = 5, b = 10, c = -6 –A>b && a<c –a<


Download ppt "Powerpoint Templates Page 1 Programming in C/C++ PS04CINS02."

Similar presentations


Ads by Google