Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to...

33
Prepared by: Shraddha Modi

Transcript of Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to...

Page 1: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Prepared by:Shraddha Modi

Page 2: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Importance/Features of C Robust language built in functions and operators.

C has set of data types and operators so C is efficient and fast.

C is highly portable means the program written for one computer

can be run on another computer.

C has only 32 keywords which are easy to remember.

C has set of built in functions and standard functions.

C has system library.

And We can add our own functions to C library it means C can be

extended itself.

Page 3: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Documentation SectionLink SectionDefinition Section

Global Declaration Section

Main Function Section

Local Declaration PartExecutable Code Part

Sub Program Section

Function1()Function2()……………FunctionN()

#include<stdio.h>#define PI 3.1415float radius;float area();int main(){

float a;printf(“Enter radius : “);scanf(“%f”,&radius);a = area();printf(“Area of Circle :

%f”,a);}float area(){

return (PI * radius * radius);}

Page 4: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Character Set Characters are used to write tokens. These characters are

defined by the Unicode character set.

The C character set includes letters, digits, special

characters and white spaces as building blocks to form

basic programming elements (tokens).

Page 5: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

C Character SetLetters DigitsUppercase A……ZLowercase a……z

Decimal digits 0……9

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

Back space \bHorizontal tab \tCarriage return \rNew line \nForm feed \f

Page 6: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Trigraph charactersTrigraph sequence Translation??=??(??)??<??>??!??\??/??-

#[]{}|\^~

Page 7: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

#include<stdio.h>void main(){clrscr();

printf("\nSOCET");printf("\bCampus");printf("\rHello");

getch();}

OUTPUT:

Helloampus

Page 8: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Tokens Smallest individual units in a program are known as tokens.

C has six types of tokens as given below:

1. Keywords Ex: float, int, while, if

2. Identifier Ex: amount, sum, area

3. Constants Ex: -15.5,100

4. String Ex: “year”, ”hello”

5. Special Symbols Ex: [ ],{ }

6. Operators. Ex : +, - ,*

Page 9: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Keywords

Predefined word is known as keywords.

C language has 32 reserved keywords.

Since keywords have specific meaning, we cannot use

them as identifiers.

All keywords are to be written in lower – case letters.

Page 10: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Keywordsauto extern sizeof break float static

case for struct char goto switch

const if typedef continue int union

default long unsigned do register void

double return signed else short while

enum signed

Page 11: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Identifier They are used for naming variables, functions and arrays.

Identifiers follow the below listed rules:

Alphabets, digits, underscores are permitted.

They must not begin with a digit.

First character must be a letter. They may also begin with anunderscore.

Uppercase and lower case letters are distinct.

They can be of any length however the first 8 characters aretreated as significant by the C compiler.

Declared keywords cannot be used as a variable.

Page 12: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Constant Constant means fixed value.

The value of the constant cannot change during

execution of program.

C supports following types of constant:

CONSTANTS

Numeric Character

Integer Real Single Character

String

Page 13: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Integer Constant Decimal

123 -321 0 654321 +78

Hexadecimal0X2 0x9F 0Xbcd

Octal037 0 0435 0551

Embedded space , commas and non-digits are not permittedbetween digits

Page 14: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Real Constant 0.0083 -0.75 435.36 +247.0

215. .95 -.71 +.5

Mantisaa e exponent

215.65 2.1565e2

7500000000 7.5 E9 or 75E8

White space , commas , special characters are not permitted

Page 15: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Character Constant A character constant contains a single character enclosed

within a pair of single quote marks . Examples:

‘5’ ‘X’ ‘;’ ‘ ’

The character constants have integer values known as ASCIIvalues

Printf(“%d” , ‘a’); o/p 97Printf(“%c” , ‘97’); o/p a

Page 16: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

String Constant A string constant Is a sequence of characters enclosed in

double quotes.. The characters may be letters, numbers , special characters

and blank space. Example s are:

“Hello!” “1987” “WELL DONE” “?...!” “5+3” “X”

“X” (string constant) and ‘X’ (character constant)are not same

Page 17: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Backslash character ConstantEscape sequences

Data type Name Meanning

\a Alert Produces an audible or visible alert.

\b Backspace Moves the cursor back one position (non-destructive).

\f Form Feed Moves the cursor to the first position of the next page.

\n New Line Moves the cursor to the first position of the next line.

\r Carriage Return Moves the cursor to the first position of the current line.

\t Horizontal Tab Moves the cursor to the next horizontal tabular position.

\v Vertical Tab Moves the cursor to the next vertical tabular position.

\' Single quote Produces a single quote.

\" Double quote Produces a double quote.

\? Question mark Produces a question mark.

\\ backslash Produces a single backslash.

‘\0’ null Produces a null character.

Page 18: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Data types in C A Data Type is a type of data. A data type is a data storage format that can contain a specific type or range

of values.

ANSI C Supports Three classes of data types.1. Primary data type(fundamental)2. Derived data types3. User defined data types

All “C” compiler supports 5 fundamental data types:1. Integer (int)2. Character (char)3. floating point (float)4. double-precision (double)5. void

Page 19: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Size and range of data typeData type Bits in RAM

(1 byte = 8 bits)Range of data type

Char or signed char 8 -128 to 127

Unsigned char 8 0 to 255

Int or signed int 16 -32,768 to 32,767

Unsigned int 16 0 to 65535

Short int or signed short int 8 -128 to 127

Unsigned short int 8 0 to 255

Long int or signed long int 32 -2,147,483,648 to 2,147,483,647

Unsigned long int 32 0 to 4,294,967,295

Float 32 3.4E-38 to 3.4E+38 (6-digitprecision)

Double 64 1.7E-308 to 1.7E+308 (14-digitprecision)

Long double 80 3.4E-4932 to 1.1E+4932 (80-digitprecision)

Page 20: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character
Page 21: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

User defined Data types Typedef : Syntax: typedef type identifier

# include < stdio.h>main ( ){typedef int amt ;amt Rupees = 20;printf (“ Rupees %d“, Rupees);}

Output : Rupees 20.

Advantage: We can create meaningful data type names for increasing the readability of the program.

Page 22: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

User defined Data types enum: Syntax: enum identifier {value1, value2, … valuen}; The idea is that instead of using an int to represent a set of values, a

type with a restricted set of values is used instead. For example if we use the colors of the rainbow, which are

Red, Orange, Yellow, Green, Blue, Indigo, Violet If enum didn't exist, you might use a #define to specify these values.

For example: #define red 1#define orange 2

Using Enum:enum rainbowcolors { red, orange, yellow, green, blue, indigo, violet }enum rainbowcolors trafficlights;trafficlights= red;

Now internally, the compiler will use an int to hold these and if no valuesare supplied, red will be 0, orange is 1 etc.

Page 23: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Storage Class in C Location and Visibility of variables. Example:

int m;void main(){int i;float balance;….function();}function(){int i;float sum;…}

1) Global or External Variable

2) Local Variable

Page 24: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Storage Class Storage class is used to declare explicitly scope and lifetime of variables.

A variable storage class tells us

1) Where the variable would be stored.

2) What will be the initial value of the variable, if the initial value is not

specifically assigned (i.e, the default initial value).

3) What is the scope of the variable, i.e., in which functions the value of the

variable would be available.

4) What is the life of the variables; i.e., how long would the variable exist.

Page 25: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Storage Class

1) auto (automatic)

Keyword : auto

Storage Location : Main memory

Initial Value : Garbage Value

Life : Control remains in a block where it is defined.

Scope : Local to the block in which variable is declared.

Syntax : auto [data_type] [variable_name];

Example : auto int a;

It is not required to use the keyword auto because by default, storage class

within a block is auto.

auto static extern register

Page 26: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Example 1:main(){auto int i=10;printf(“%d”,i);}Output:10Example 2:main(){auto int i;printf(“%d”,i);}Output:1002

Page 27: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Storage Class2) static

Keyword : static

Storage Location : Main memory

Initial Value : Zero and can be initialize once only.

Life : depends on function calls and the whole application or program.

Scope : Local to the block.

Syntax : static [data_type] [variable_name];

Example : static int a;

Static storage class can be used only if we want the value of a variable to persist between different function calls.

auto static extern register

Page 28: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Example :void main(){add();add();}void add(){static int i=10;printf(“\n%d”,i);i= i + 1;}Output:1011

Example :void main(){add();add();}void add(){int i=10;printf(“\n%d”,i);i= i + 1;}Output:1010

Page 29: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Storage Class3) extern

Keyword : extern (global or external variables)

Storage Location : Main memory

Initial Value : Zero

Life : Until the program ends.

Scope : Global to the program.

Syntax : extern [data_type] [variable_name];

Example : extern int a;

They are declared outside the functions and can be invoked at anywhere in a program.

auto static extern register

Page 30: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

#include <stdio.h> #include <conio.h> extern int i=10; void main() { int i=20; clrscr(); printf("\n\t %d",i); show(); getch(); }void show() { printf("\n\n\t %d",i); }

Output:

20

10

Page 31: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

Storage Class

4) register

Keyword : register

Storage Location : CPU Register

Initial Value : Garbage

Life : Local to the block in which variable is declared.

Scope : Local to the block.

Syntax : register [data_type] [variable_name];

Example : register int a;

auto static extern register

Page 32: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

No. Local Variables Global Variables

1. Exist inside the function

that creates them.

Accessible through out the

program

2. Are not initialized

automatically.

Are normally initialized to 0

by default.

Page 33: Prepared by: Shraddha Modi - WordPress.com · 2016-03-02 · Character Set Characters are used to write tokens. These characters are defined by the Unicode character set. The C character

No. Auto Static1. Auto variables persists till the control

remains within the block where it is

defined.

Static variables persist between different

function calls.

2. The auto variables are created when the

function is called and destroyed

automatically when the function is

exited.

The variables persist in the main memory.

3. auto is the default storage class for

local variables.

static is the default storage class for global

variables.4. Default initial value: Garbage value. Default initial value: Zero.5. Auto declaration is valid only for

variables.

Static declaration is valid for variables and

functions.6. Declaration: auto int x; Declaration: static int x;