'C' Notes

January 23, 2017 | Author: Vivek Rawat | Category: N/A
Share Embed Donate


Short Description

Download 'C' Notes...

Description

Notes on ‘C’ Language By-Umesh Bhardwaj

1

‘C’ Language [A.] There are four type of number system as following(1) Decimal number system

(Total no. Used - 10 nos.) (0-9)

(2) Octal number system

(Total no. Used - 8 nos.)

(3) Hexadecimal number system

(Total no. Used - 16 nos.) (0-9, A-F)

(4) Binary number system

(Total no. Used - 2 nos.) (0, 1)

(0-7)

We all use Decimal Number System in general whereas Computer uses Binary Number System.

[B.] There are basically three types of languages that continue language generation(1)

Machinery language (Binary language)

(2)

Assembly language

(3)

High level language

[C.] TranslatorDefinition:-

A special type of software or a program which converts any other language into the machinery language & vice-versa, is known as translator.

There are basically three type of translator as following (1) Assembler (2) Interpreter (3) Compiler

Assembler:- A s/w or a program which convert the assembly language into the machinery language & vice-versa.

Interpreter Both are S/W or program which convert the HLL into the machinery language & & vice-versa. Compiler:-

Notes on ‘C’ Language By-Umesh Bhardwaj

2 Difference between Interpreter & Compiler

Interpreter

Compiler

It translates the source program line-by-line. (It means it would not execute the next line unless the first one has no error). So it is slower.

It translate the source program as whole, so it is faster in speed.

a

At the time of using interpreter each & every time, to obtain the output the source program is needed.

Whereas a compiler creates an unique object program, so there is no source code every time because out put can obtain by that object code.

Note: - For each high level language the machine require a separate Compiler or Interpreter. Example:- Interpreter based language- Cobol, Pascal, FORTRAN, HTML Compiler based language - C, C++ ,Visual Basic,Java.. (D) History of C In 1960’s “Basic combined programming language“ (BCPL) called ‘B’ language was developed at Cambridge University. It was not fully satisfied language. ‘B’ language was modified by Denies Ritchie and was implemented at bell laboratories in 1977,was named ‘C’. Turbo C- (Compiler made by Turbo company.) Borland C- (Compiler name by Borland company.) ANSI C – (Compiler made by American National Standard Institute)

(E)

Features of ‘C’ language:(1) (2) (3) (4)

There are only 32 keywords. Rich library of in-built function. Fastest execution speed, Because of Compiler based language. Open system- its ability to extend itself. We can developed our own function & add them to the libraries. (5) It is the basis for “C++” & “Java”. (6) Structure programming approach . (7) This is highly portable language. This means that C program written for one computer can be run on another with little or on modification.

Notes on ‘C’ Language By-Umesh Bhardwaj

3

(F) Character set:The Characters in C are grouped into the following categories.:Letter A-Z & a-z. Digits 0-9. Special Characters-~,!,=,-,( ),*,&,^,%,$,#,@ ,--------. White Spaces.- blank space, tab, return, new line.

   

(G) Trigraph characters:-See in Chapter 2 of Balagurusamy page-19. (H) Tokens:In a ‘C’ program the smallest individual units are knows as ‘Tokens’. ‘C’ program are written using these token and the syntax of the language.

‘C’ Tokens

Keywords

Constants

Identifiers

Keyword Identifier Constant String Operator

Special Symbols Strings

Operators

- The words which are pre defined by the ‘C’ language. - User defined variable names such as a, b, i, j, marks etc. - A DataName whose value cannot be change during the execution of the program - Collection of Characters - A symbol which is used to perform specific operation

Constants

Numeric Constant

Integer Constant

Real Constant

Character Constant

Single Character Constant

String Constant

Note:- ‘C’ also support some special backslash charcter constant that are use in out put function. (page-25) (\a,\b,\n,\t)

Notes on ‘C’ Language By-Umesh Bhardwaj

4

(I) Variable & Constant A variable & a constant is a data name that may be used to store a data value. A value of Constant can not be changed during the executing of program. Whereas value of Variable may take different value at different time during execution . Variable name may consists of letter, digits, and Linder line (-) with following the below rules :1) They must being with a letter. 2) ANSI standard recognigs the length of 31 characters 3) Upper case & lover case are significant mean total is differ. 4) Variable name should not be a key word.

(J) Data types: ‘C’ supports four classes of data type as following1) Primary data type. 2) User defined data type. 3) Derived data type. 4) Empty data type.

1) Primary data type:- int, float, char int (2 byte) Range(-32768 to +32767) Signed

short int (1 byte ) Range(-128 to +127) long int (4 byte) Range(- 2,147,483,648 to +2,147,483,647 )

Integer int (2 byte) Range(0 to +65535) Unsigned

short int (1 byte ) Range(0 to 255) long int (4 byte) Range( 2,147,483,648 to +2,147,483,647)

Notes on ‘C’ Language By-Umesh Bhardwaj

5

float (4 byte) Range(3.4E-38 to 3.4E+38) Float

double (8 byte ) Range(1.7E-308 to 1.7E+308) long double (10 byte) Range(3.4E-4932 to 1,1E+4932 )

signed char (1 byte) Range(-128 to +127) char unsigned char (1 byte) Range( 0 to 255 )

2) User defined data type:a.)

b.)

typedef ; Ex:- typedef int umesh; umesh a,b,c; enum {value1, value2, value3, value4,………, value n}; enum v1, v2, v3, ……….., vn;

Ex:- enum days {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}; enum days d1, d2, d3;

3) Derived data types:Derived datatypes such as arrays, functions, structures & union and pointers will discuss in further topics.

4) Empty data set :The empty data set is discussed in the chaptor on functions.

Notes on ‘C’ Language By-Umesh Bhardwaj

6

( K) Storage classes :‘C ’provides a variety of storage class specifiers that can be used to declare explicitly the scope & lifetime of variables. There are four storage class specifies as following –

Storage class Auto

Static

Extern Register

Meaning Local variable known to only to that function in which it is declared. Bydefult auto is set. Local variable which exists and retain its value even after the control is transferred to the calling the function. Global variable known to all function in the files . Local variable which is stored in the register .

Notes on ‘C’ Language By-Umesh Bhardwaj

7

Note:- Static & Extern variable are automatically intllized to zero. Auto variable contain garbage value unless they are initialize explicitly.

( L)

Defining symbolic constant:[#define ] Valid exampled of constant definition are:-

(M)

# define STRENGTH

1000

# define PASS-MARKS

50

# define MAX

200

# define PI

3.14154

Operators & Expression:An operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. ‘C’ operator are as following categories:1) Arithmetic operator - +,-,*,/,% . 2) Relational operator - >,>=, b) ? a : b ;

7)

Bitwise Operators:- See at page no.55 in E. Balagurusamy.

8)

Special Operators:Comma - , Sizeof operator. Pointer operator- & - ‘address of ’ Operator * - ‘ value at ‘ Operator Member selection operator – (.) and (->) Preprocessor operator – String izing - # Token Pasting - ##

Note:- Type conversion in Expression – (a) Auto type conversion (b) Forcly type conversion (Type casting)

Notes on ‘C’ Language By-Umesh Bhardwaj

9 (typename) Expression;

(N)

Operator Precedence and Associativity :Each operator in C has a precedence associated with it. This precedence is used to determine how an expression involving more than one operator is evaluated. There are distinct levels of precedence and an operators may belong to one of the levels. 

The operator at the higher level of precedence is evaluated first.



The operator of same precedence is evaluated either from L-R or R-L, depending on the level, this is known as associativity of an operator.

See the table at page no. 66 in Balagurusamy.

Documentation Section

(Comment Staement) (//) (/*…*/)

Linking Secton

(#Include Section)

Definition Section

(Constant Declaration Section)

Global Declaration Section Main( ) function Section {

BasicPart Structure of ‘C’ Programs Local Declaration Executable Statement Part }

Function1( ) Function2( ) SubFunction3( Program Section ) . . . FunctionN( )

User Defined Functions

Notes on ‘C’ Language By-Umesh Bhardwaj

10

Important points about ‘C’ program 1. 2. 3. 4. 5. 6. 7. 8.

Each ‘C’ program is written in lower case only. Each ‘C’ program must have at least 1 main function. All executable statement in ‘C’ must end written a semi colon (;). Anything given within “ “ is printed as it is. All variables in ‘C’ must be declared of it’s data types before they are used. All ‘C’ programs must be saved with (.c) extension. All header files are included before the main function starts All opening ‘ braces ‘ “parenthesis “,” brackets “, “quotations “ must have a closing one.

9.

Use fflush(stdin) to flush keyboard buffer between two scanf’s provided to second scanf accept the character . 10. All single characters in ‘C’ must be enclosed within single quotes & strings are enclosed in double quotes. 11. #define Statement should not end with semicolon.

Notes on ‘C’ Language By-Umesh Bhardwaj 12. 13.

11 goto statement cannot jump between functions. exit(0) forcefully terminates the program .

Special Note:Reading a character -> getchar( ) function char y =getchar ( ) ; isalpha( ), isdigit( ) function islower( ) , isuppar( ) , tolower( ) , toupper( ) isprint( ), ispunct( ), isspace( ), isalnum( ) . Writing a characther -> putchar ( ) function

I

printf( ) & scanf( ) function . % Formats -

I

%c for %f for float %c for characters %s for string %[ characters] %[^ characters] %e – read a floating value ( exponent form) %g – read a floating value(e-type or f-type) %I – Decimal, hexadecimal, Octal %o – Octal %u – unsigned integer %x – hexadecimal integer

Use as prefix – h for short int l for long int or double L for long double

“Decision Making & Branching “ (There are also known as control or decision making statements .) 1) 2) 3) 4)

if statement switch statement conditional operator statement go to statement

if statement (a) simple if statement SYNTAX – if(condition)



Notes on ‘C’ Language By-Umesh Bhardwaj

12

{ statement block ; } statement – x ;

Is Condition ?

False

Statement Block

Statement - X

Next statement (b)The if else statement :SYNTAX – If ( condition ) { True –block statements ; } else { False- block statements ; } Statement X;

True

True Block Statement

Is Condition ? False

Statement - X

Next statement

False

False Block Statement

Notes on ‘C’ Language By-Umesh Bhardwaj

13

Flow Chart for If..Else Statement (C ) Nested of if else statement :SYNTAX :If ( test condition ) { If ( test condition ) { Statement 1; } else { Statement 2; } } else { Statement 3; } Statement X;

(d) The else if ladder :SYNTAX :if ( test condition 1) { Statement 1; } else if (test condition 2) { Statement 2; } else if ( test condition 3) { Statement 3; } . . . .

Notes on ‘C’ Language By-Umesh Bhardwaj

14 else if ( test condition n) { Statement n; } else { default Statement; } Statement X;

Note :-

Finding a leap Year A leap Year is one that is evenly divisible by 4, other than century year(such as 1900) ; or a century year(such as 2000) . main( ) { int year; printf(“Please enter a year:-“); scanf(“%d”,&year); /* If year is divisible by 4 and not divisible by 100, or is divisible by 400, it is leap year */ if (year%4==0 && year % 100 != 0 || year % 400 == 0) { printf(“%d is a leap year \n”,year); } else { printf(“%d is not a leap year\n”,year); } }

Not leap year – 1986, 1900 Leap Year - 1948, 2000

The Switch Statement The general form of the switch statement is as shown below – switch(expression) { case value 1: block statement 1; break; case value 2: block statement 2; break; . . . . case value n: block statement n; break;

Notes on ‘C’ Language By-Umesh Bhardwaj

15 default : Default block statement ;

} statement x; Here the “Expression” is an integer expression or characters. value-1, value-2, are constants or constant expressions and are known as case labels. First the integer expression following the keyword switch is evaluated. The value if gives is then matched, one by one, against the constant values, that follow the case statement when a match is found, the program executes the statement following that case, and all subsequent case and default statement as well. If no match is found with any of the case statement, only the statement following the default are executed If he wants to execute only to that case which is matched than we can do this by using a “break” statement there is no need for a break statement after the default, since the control comes to the end anyway. Note: (i) even if they are multiple statement to be execute in each can there is no need to enclose there within a pair of braces. (Unlike if, and else.) (ii) Is switch a replacement of y? (Yes or no) Yes, because it offers a better way of writing programs as compared to it, and no because in certain situation we are left with no choice but to use if. (iii) The disadvantage of switch is that one cannot have a case in a switch, which looks like case K
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF