Ete105 fall2014 lec3 (East West University)

6
EAST WEST UNIVERSITY Department of Electronics & Communications Engineering Semester: Fall 2014 ETE 105: Computer Fundamentals and Programming Language Lecture 3: C Fundamentals THE C CHARACTER SET A character denotes any alphabet, digit or special symbol used to represent information. Figure 1: Valid alphabets, digits and special symbols allowed in C CONSTANTS AND VARIABLES A constant is an entity that doesn‟t change whereas a variable is an entity that may change. In the above example, since the location whose name is x can hold different values at different times, x is known as a variable. As against this, 3 or 5 do not change, hence are known as constants. TYPES OF C CONSTANTS

description

East West University ETE lecture for all.

Transcript of Ete105 fall2014 lec3 (East West University)

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

Semester: Fall 2014

ETE 105: Computer Fundamentals and Programming Language

Lecture 3: C Fundamentals

THE C CHARACTER SET

A character denotes any alphabet, digit or special symbol used to represent information.

Figure 1: Valid alphabets, digits and special symbols allowed in C

CONSTANTS AND VARIABLES

A constant is an entity that doesn‟t change whereas a variable is an entity that may change.

In the above example, since the location whose name is x can hold different values at different

times, x is known as a variable. As against this, 3 or 5 do not change, hence are known as

constants.

TYPES OF C CONSTANTS

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

INTEGER CONSTANTS

An integer constant is an integer-valued number. Thus, it consists of a sequence of digits. Integer

constants can be written in three different number systems: decimal (base 10), octal (base 8) and

hexadecimal (base 16).

A decimal integer constant can consist of any combination of digits taken from the set through 0

to 9. If the constant contains two or more digits, the first digit must be something other than 0.

Rules for Integers Constants

a. An integer constant must have at least one digit.

b. It must not have a decimal point.

c. It can be either positive or negative.

d. If no sign precedes an integer constant it is assumed to be positive.

e. No commas or blanks are allowed within an integer constant.

f. The allowable range for integer constants is

-32768 to 32767

Example 1: The following decimal integer constants are written incorrectly for the reasons

stated.

12,245 illegal character (,)

36.0 illegal character (.)

10 20 30 illegal character (blank space)

123-45-6789 illegal character (-)

0900 the first digit cannot be a zero

Rules for Real Constants

Real constants are often called Floating Point constants.

The real constants could be written in two forms

Fractional form: In fractional form, the constant contains a decimal point.

Exponential form: In exponential form of real constants, the part appearing before

„e‟ is called mantissa, whereas the part following „e‟ is called exponent.

Rules for real constants in fractional form:

a. A real constant must have at least one digit.

b. It must have a decimal point

c. It could be either positive or negative.

d. Default sign is positive.

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering e. No commas or blanks are allowed within a real constant.

Rules for real constants in exponential form:

a. The mantissa part and exponential part should be separated by a letter e.

b. The mantissa part may have a positive or negative sign. Default sign of mantissa part

is positive.

c. The exponent must have at least one digit, which must be positive or negative integer.

Default sign is positive.

d. Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.

Example 2: The following are not valid floating-point constants for the reasons stated.

1 Either a decimal point or an exponent must be present

1,000.0 illegal character (,)

2E+10.2 The exponent must be an integer quantity (it cannot

contain a decimal point)

3E 10 Illegal character (blank space) in the exponent

Several valid floating-point constants are shown below:

0. 1. 0.2 827.602

50000. 0.000743 12.3 315.006

2E-8 0.006e-3 1.6667E+8 .12121212e12

Rules for Character Constants

a. A character constant is a single alphabet, a single digit or a single special symbol

enclosed within single inverted commas. Both the inverted commas should point to

the left. For example, ’A’ is a valid character constant whereas ‘A’ is not.

b. The maximum length of a character constant can be 1 character.

TYPES OF C VARIABLES

Variable names are names given to locations in memory. These locations can contain

integer, real or character constants.

The types of variable depend on the types of constants that it can handle.

A particular type of variable can hold only the same type of constant.

For example, an integer variable can hold only an integer constant.

Rules for Variable Names

a. A variable name is any combination of 1 to 31 alphabets, digits or underscores. Do not

create unnecessarily long variable names as it adds to your typing effort.

b. The first character in the variable name must be an alphabet or underscore.

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering c. No commas or blanks are allowed within a variable name.

d. No special symbol other than an underscore (as in gross_sal) can be used in a variable

name.

Example 3: The following names are not valid variable names for the reasons stated.

4th The first character must be a letter

“x” illegal character (“)

order-no illegal character (-)

error flag illegal character (blank space)

C KEYWORDS

Keywords are also called Reserved words. Keywords are the words whose meaning has

always been explained to the C compiler.

The keywords cannot be used as variable names.

There are only 32 keywords available in C.

Some compilers may also include some or all of the following keywords.

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering

ESCAPE SEQUENCE:

FORMATTING DATA

Controlling precision of data:

int main()

{

int a,b;

float c,d;

a = 15;

b = a / 2;

printf("%d\n",b);

printf("%3d\n",b);

printf("%03d\n",b);

c = 15.3;

d = c / 3;

printf("%3.2f\n",d);

return 0;

}

In the above example:

In the first printf statement we print a decimal.

In the second printf statement we print the same decimal, but we use a width (%3d) to say

that we want three digits (positions) reserved for the output.

In the third printf statement we say almost the same as the previous one. Print the output

with a width of three digits, but fill the space with 0.

EAST WEST UNIVERSITY

Department of Electronics & Communications Engineering In the fourth printf statement we want to print a float. In this printf statement we want to

print three position before the decimal point (called width) and two positions behind the

decimal point (called precision).

Formatting other types:

int main()

{

printf("The color: %s\n", "blue");

printf("First number: %d\n", 12345);

printf("Second number: %04d\n", 25);

printf("Third number: %i\n", 1234);

printf("Float number: %3.2f\n", 3.14159);

printf("Hexadecimal: %x\n", 255);

printf("Octal: %o\n", 255);

printf("Unsigned value: %u\n", 150);

printf("Just print the percentage sign %%\n", 10);

return 0;

}