Tuesday 23 August 2022

Introduction to c

 

Unit 2. Introduction to c

# C-Programming :-

·       C is very robust machine independent, general purpose, concise programming language.

·       C-Programming language was at first developed by Dennis Ritchie at AT & T Bell Labs, USA in 1972.

·       Famous operating system such as UNIX, LINUX, MINIX and many system and application program are written in c.

·       C consist of very few number of keywords (reserved words) in comparison to other programming languages.

·       C is general purpose programming languages which features economy of expressions, modern control flow, and data structure a rich set of operators.

·       C is typical programming language for academic process.

 

2.1 History of C program :-

1.      By 1960, FORTRAN (Formula Translation) and COBOL( Common Business Oriented Language) were developed with specific use and specific purposes.

2.      In 1960, a committee was developed ALGOL (Algorithmic Oriented Language). But it was too abstract and general.

3.      In 1963, Cambridge University developed CPL (Combined Programming Language). But it was too big has too many feature.

4.      In 1967, Martin Richards (at Cambridge University) developed BCPL. But it become less powerful and too much specific.

5.      In 1970, Ken Thompson developed a programming language B. But it was too, like other programming language.

6.      Taking all the good points of BCPL, CPL and B programming language was developed in 1972 bu Dennis Ritchie at AT and T Bell Lab, USA.

7.       

2.3 Character set, Token and Comments :-

2.3.1 character set:

A character denotes any alphabets, digits or special symbol used to represent information. The table given below shows the valid alphabets, numbers and special symbols allowed in c.

Alphabets

Uppercase (i.e. A, B,….,Y, Z)or

Lowercase (i.e. a, b,……,y, z)

Digits

0,1,2,…….,9

Special symbols

+ - * / = () {} [] < > ‘ “ ! # % & | ~ \ . , ; : ?

White space characters

Blank, new line, tab etc

 

2.3.2. Token:

Tokens are collection of different characters, symbols, operators or puntuators. The first job of compiler is to test the validity of these token. There are six types of tokens as prescribed by C:

i.                    Keywords

ii.                   Identifiers

iii.                 Constants

iv.                 String constants

v.                   Operators

vi.                 Punctuators

Keywords:

Keywords are predefine words that have strict meaning. For example run has meaning in English but urn is senseless. So run can can be considered as keywords in English where as urn is not keyword. There are 32 keywords in ANSI C.

auto

double

If

static

break

else

int

struct

case

enum

long

switch

char

extern

near

typedef

const

float

register

union

continue

far

return

unsigned

default

for

short

void

do

goto

signed

while

Basics usage of these keywords –

if, else, switch, case, default – Used for decision control programming structure.

break – Used with any loop OR switch case.

int, float, char, double, long – These are the data types and used during variable declaration.

for, while, do – types of loop structures in C.

void – One of the return type.

goto – Used for redirecting the flow of execution.

auto, signed, const, extern, register, unsigned – defines a variable.

return – This keyword is used for returning a value.

continue – It is generally used with for, while and dowhile loops, when compiler encounters this statement it performs the next iteration of the loop, skipping rest of the statements of current iteration.

enum – Set of constants.

sizeof – It is used to know the size.

struct, typedef  – Both of these keywords used in structures (Grouping of data types in a single record).

union – It is a collection of variables, which shares the same memory location and memory storage.

 

Identifiers:

Identifiers can be defined as programmer defined elements like the name of variables, functions, array, structure etc.

Following are some valid identifiers names:

LenghtOFBox

Length_of_box

Length2

LemGth

l

 

 

 

 

constants:

Constant are the entities that never change. All the integer value can be considered as integer constants (i.e. 5, 97 etc.) and all the fractional parts can be thought as floating constants (i.e. 5.7,3.340 etc). Character are also constants which are single alphabets enclosed by single quotes (i.e. ‘a’, ‘x’,’y’ etc.)

 

 String constants:

1.      A string constant is a sequence of alphanumeric characters enclosed in double quotation marks whose maximum length is 255 characters.

2.      String constants are considered to as array of characters and are enclosed within double quotes. So ‘a’ and “a” are considered different.

3.      For the double quote or single quote to be the part of the string, they should be proceded by back slash (\). For example

For string

String to be written

“Ram is a boy”

“\”Ram is a boy\””

Ram’s wife

“Ram\’s wife”

He said “ health is wealth”

“He said \”health is wealth\””

 

 

Operator:

Operators are special symbols which cause to perform mathematical or logical operations. Following are the symbols used for arithmetic operators in C.

Operators

Meaning

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Remainder

 

 

 

 

 

                                                                    Operands

 

A     +    B

 

 


                                                                                                             Operation

 

 

                                                                      Operators

Punctuators:

The special symbols which act like punctuation marks are called punctuators. Following are the punctuators used in C.

Parenthesis   ()

Braces            {}

Commas         ,

Semicolons    ;

2.3.2. Comments:

1.  comments are the remarks or arbitrary string written by programmer into programming codes and are written in between the character set/* and */.

2. It is used by programmer for documentation purpose.

3. it is non-executable part of program.

4. It is ignored by the compiler and user is free to write his comment within those symbol.

5. Following are some example of writing comments on C.

/* this program calculates area of a tringle*/

/*** test is***/

/**********************

*put your comment in between*

*these line and boxes, we are free*

************************/

 

 

 

 

 

 

 

 

 

 

 

 

No comments:

Post a Comment