34
Basic Elements of C Staff Incharge: S.Sasirekha

Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Basic Elements of C

Staff Incharge: S.Sasirekha

Page 2: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Basic Elements of C

• Character Set

• Identifiers & Keywords

• Constants

• Variables

• Data Types

• Declaration

• Expressions & Statements

Page 3: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

C Character Set

• Letters– Uppercase A to Z

– Lowercase a to z• E.g.:- 'A', 'B',‘a', 'b’

• Digits– 0 to 9

• E.g.:- '0','1’

• Special Characters– Constants, Variables, Operators & Expressions• E.g.:- ‘+’ , ‘?’ , ‘<’ , ‘_’ , ‘%’

Page 4: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

• White Spaces– White Spaces are ignored by the compiler until they are a part of string constant.

– White Space may be used to separate words, but are strictly prohibited while using between characters of keywords or identifiers.

– Escape Sequences• Combination Of Characters

– E.g.:-1. Blank Space2. Horizontal Tab3. Carriage Return4. New Line5. Form Feed

• Note:@ & $ are included with strings and Comments

• Characters are small integers (0-255)

• Character constants are integers that denote corresponding characters

• ASCII code maps characters to integers– A – Z (65-90)– a – z (97-122)

Page 5: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Identifiers & Keyword• Identifiers refers to the name of user-defined variables, array and

functions.

• Identifies consists of Letter & digits, in any order, except that 1st

character must be a letter.

• Both upper and lower case are allowed

• Underscore(_) can also be included & considered as a letter

• A identifier should not contain a space.

• Some implementation can recognize only 8 characters, typically 31 characters are accepted.

• E.g.– Valid

• X,name,area,SUM_1

– Invalid • “x” 4th

Page 6: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

• There are certain reserved words called Keywords that have standard predefined meaning in c.

• Used only for the purpose it is intended for.

• Cannot be used as identifier

• Note: Keywords are lowercase, Uppercase keywords can be used as identifiers.

Page 7: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Keyword Description auto Storage class specifier break Statement case Statement char Type specifier const Storage class modifier continue Statement default Label do Statement double Type specifier else Statement enum Type specifier

extern Storage class specifier float Type specifier for Statement goto Statement

Keywords Description

Page 8: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

if Statement int Type specifier long Type specifier register Storage class specifier return Statement short Type specifier signed Type specifier sizeof Operator static Storage class specifier struct Type specifier switch Statement typedef Statement Union Type specifier unsigned Type specifier void Type specifier volatile Storage class modifier while Statement

Keywords Description

Page 9: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Constants

• A constant value is the one which does not change during the execution of a program.

• C supports several types of constants.• Numeric Constants

1. Integer Constants2. Real Constants

• Character Constants3. Single Character Constants4. String Constants

• Numeric Type Constants Follow Rules to be applied– Commas & blank spaces cannot be included

– The Constants can be preceded by a minus(-) sign if desired

– The value of the Const cannot exceed specific Min & Max Bounds

Page 10: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Integer Constants– An integer constant is a sequence of digits & integer valued number.

– There are 3 types of integers namely• Decimal integer (base 10)• Octal integers (base 8)

• Hexadecimal integer (base 16)

• Decimal Integers

– consists of a set of digits 0 to 9 preceded by an optional + or -sign.

– Spaces, commas and non digit characters are not permitted between digits.

– Example for valid decimal integer constants are » 123» -31» 0» 562321» + 78

– Some examples for invalid integer constants are» 15 750» 20,000» Rs. 1000

Page 11: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

• Octal Integers– Constant consists of any combination of digits from 0 through 7.

– First digit must begin with a O.( for identification– Some examples of octal integers are

– O26– O– O347– O676

– Some Invalid example– 743– 05280– 077.77

• Hexadecimal integer – Constant is preceded by OX or Ox, – They may contain alphabets from A to F or a to f. – The alphabets A to F refers to 10 to 15 in decimal digits. – Example of valid hexadecimal integers are

– OX2– OX8C– OXbcd– Ox

Page 12: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Real Constants

– Real Constants consists of a fractional part in their representation.

– Integer constants are inadequate to represent quantities that vary continuously.

– These quantities are represented by numbers containing fractional parts like 26.082.

– Example of real constants are– 0.0026– -0.97– 435.29– +487.0

– Real Numbers can also be represented by exponential notation.

– The general form for exponential notation is– mantissa e exponent

• The mantissa is either a real number expressed in decimal notation or an integer.

• ex mean multiply by 102

• The exponent is an integer number with an optional plus or minussign.(+ means shift right,- means shift left)

Page 13: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Character Constants

Single Character– Constant represent a single character which is enclosed in a pair of quotation symbols.

– Example for character constants are» '5‘» 'x‘» ';‘» ' '

– All character constants have an equivalent integer value which are called ASCII Values

String Constants– A string constant is a set of characters enclosed in double quotation marks.

– The characters in a string constant sequence may be a alphabet, number, special character and blank space.

– Example of string constants are» "VISHAL“» "1234“» "God Bless“» "!.....?"S

Page 14: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

• Backslash Character Constants [Escape Sequences]– Backslash character constants are special characters used in output functions.

– Although they contain two characters they represent only one character.

– Given below is the table of escape sequence and their meanings.

Produces a null character.\0

Produces a single backslash.\\

Produces a question mark.\?

Produces a double quote.\"

Produces a single quote.\'

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

Moves the cursor to the next horizontal tabular position.Horizontal

Tab

\t

Moves the cursor to the first position of the current line.Carriage

Return

\r

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

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

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

Produces an audible or visible alert.Alert\a

MeaningNameEscape Sequence

Page 15: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Example

/* Special Characters */ #include <stdio.h>main () { printf ("Beep! \7 \n"); printf ("ch = \'a\' \n"); printf (" <- Start of this line!! \r"); }

Output of this program is: Beep! (and the BELL sound )ch = 'a' <- Start of this line!! and the text cursor is left where the arrow points.

Page 16: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Variables

– A variable is a value that can change any time.

– It is a memory location used to store a data value.

– A variable name should be carefully chosen by the programmer so that its use is reflected in a useful way in the entire program.

– Variable names are case sensitive.

– Example of variable names are» Sun» Number» Salary» Emp_name» Average1

Page 17: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

– Any variable declared in a program should confirm to the following

1.They must always begin with a letter, although some systems permit underscore as the first character.

2.The length of a variable must not be more than 8 characters.

3.White space is not allowed and

4.A variable should not be a Keyword

5.It should not contain any special characters.

– Examples of Invalid Variable names are

» 123» (area)» 6th

»%abc

Page 18: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Data Types

• C Support different types of data, each of which may represented differently within the computer memory

• C language data types can be broadly classified as – Primary data type– Derived data type– User-defined data type

Page 19: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Primary data type • All C Compilers accept the following fundamental data types

• The size and range of each data type is given in the table below

Page 20: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

• The basic data types can be augmented by the use of data type qualifiers (Storage)

– Short• Half or same the amount of storage as regular int numbers

– Long• To increase the range of values( double the amount)

– These can be:• Signed

– ordinary data type

• Unsigned– All the bits for the magnitude of the numbers are always positive

• E.g.– Integer qualifiers:

• Short int• Long int (4 bytes)• Signed int (2 bytes)• Unsigned int(2 bytes)

– Won’t use left most bit for sign(0 to 65536)

Page 21: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Void Type :

• Using void data type, we can specify the type of a function.

• It is a good practice to use a void functions that does not return any values to the calling function

Page 22: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Declaration of Variables

• Every variable used in the program should be declared to the compiler.

• The declaration does two things. 1. Tells the compiler the variables name. 2. Specifies what type of data the variable will hold.

The general format of any declaration

datatype v1, v2, v3, ……….. vn;

Where v1, v2, v3 are variable names.• Variables are separated by commas. A declaration statement must end with a semicolon.

Example:Int sum; Int number, salary; Double average, mean;

Page 23: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

User defined type declaration

• In C language a user can define an identifier that represents anexisting data type.

• The user defined datatype identifier can later be used to declare variables.

• The general syntax is

typedef type identifier;

here type represents existing data type and ‘identifier’ refers to the ‘row’ name given to the data type.

Example:

typedef int salary; typedef float average;

Here salary symbolizes int and average symbolizes float. They can be later used to declare variables as follows:

salary dept1, dept2; Average section1, section2;

Therefore dept1 and dept2 are indirectly declared as integer datatype and section1 and section2 are indirectly float data type.

Page 24: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

• The second type of user defined datatype is enumerated data type which is defined as follows.

Enum identifier {value1, value2 …. Value n};

• The identifier is a user defined enumerated datatype which can be used to declare variables that have one of the values enclosed within the braces.

• After the definition we can declare variables to be of this ‘new’ type as below.

enum identifier V1, V2, V3, ……… Vn

• The enumerated variables V1, V2, ….. Vn can have only one of the values value1, value2 ….. value n

Page 25: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

The enum Data Type

• enum is short for enumerated. The enumerated data type can be used to declare named integer constants.

• Syntax of the enum data type declaration

enum tag_name {enumeration_list} variable_list;

• tag_name � name of the enumeration.

variable_list � list of variable names that are of the enum data type.

enumeration_list � enumerated names that are used to represent integer constants. (Both tag_name and variable_list are optional.)

Page 26: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

The enum Data Type - Example

• enum automobile {sedan, pick_up, sport_utility};

• Given this, you can define enum variables like this:enum automobile domestic, foreign;

• Here the two enum variables, domestic and foreign, are defined.

• You can always declare and define a list of enumvariables in a single statement, as shown in the general form of the enum declaration. enum automobile {sedan, pick_up, sport_utility} domestic, foreign;

Page 27: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

The enum Data Type

• Assigning Values to enum Names

By default, the integer value associated with the leftmost name in the enumeration list field, surrounded by the braces ({ and }), starts with 0, and the value of each name in the rest of the list increases by one from left to right. Therefore, in the previous example, sedan, pick_up, and sport_utilityhave the values of 0, 1, and 2,

respectively.

• You can assign integer values to enum names. Considering the previous example, you can initialize the enumerated names like this:

enum automobile {sedan = 60, pick_up = 30, sport_utility = 10};

Page 28: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Example program for enum

/* Defining enum data types */#include <stdio.h>main() /* main() function */{enum language {human=100,animal=50,computer};

enum days{SUN, MON,TUE,WED,THU,FRI,SAT};

printf("human: %d, animal: %d, computer: %d\n", human, animal, computer);printf("SUN: %d\n", SUN);printf("MON: %d\n", MON);printf("TUE: %d\n", TUE);printf("WED: %d\n", WED);printf("THU: %d\n", THU);printf("FRI: %d\n", FRI);printf("SAT: %d\n", SAT);return 0;

}

Page 29: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Example program for enum

• OUTPUThuman: 100, animal: 50, computer: 51

SUN: 0

MON: 1

TUE: 2

WED: 3

THU: 4

FRI: 5

SAT: 6

Page 30: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Declaration of Storage Class

• Variables in C have not only the data type but also storage class that provides information about their location and visibility.

• The storage class divides the portion of the program within which the variables are recognized.

auto : It is a local variable known only to the function in which it is declared. Auto is the default storage class.

static : Local variable which exists and retains its value even after the control is transferred to the calling function.

extern : Global variable known to all functions in the file

register : Social variables which are stored in the register.

Page 31: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Defining Symbolic Constants

• A symbolic constant value can be defined as a preprocessor statement and used in the program as any other constant value. The general form of a symbolic constant is

# define symbolic_name value of constant

Valid examples of constant definitions are :

# define marks 100 # define total 50 # define pi 3.14159

• These values may appear anywhere in the program, but must come before it is referenced in the program.

• It is a standard practice to place them at the beginning of the program.

Page 32: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Example

#define PASSPERCENT 65

int main(int argc, char **argv)

{

/* PASSPERCENT macro being used in code */

printf (“Pass percentage is: %d\n”, PASSPERCENT);

/* Return a success code to Operating System */

return 0;

}

A preprocessed code looks like this:

int main(int argc, char **argv)

{

printf (“Pass percentage is: %d\n”, 65);

return 0;}

Page 33: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Declaring Variable as Constant

• The values of some variable may be required to remain constant through-out the program.

• We can do this by using the qualifier const at the time of initialization.

Example:Const int class_size = 40;

– The const data type qualifier tells the compiler that the value of the int variable class_size may not be modified in the program

Page 34: Basic Elements of C(230908) - STUD NOTES · 2012. 2. 29. · Basic Elements of C •Character Set •Identifiers & Keywords •Constants •Variables •Data Types •Declaration

Volatile Variable

• A volatile variable is the one whose values may be changed at any time by some external sources.

Example:volatile int num;

• The value of data may be altered by some external factor, even if it does not appear on the left hand side of the assignment statement.

• When we declare a variable as volatile the compiler will examine the value of the variable each time it is encountered to see if an external factor has changed the value.