Introduction to c



Download 0.91 Mb.
Page2/13
Date28.05.2018
Size0.91 Mb.
#51782
1   2   3   4   5   6   7   8   9   ...   13

The ‘C’ Character Set


A valid ‘C’ program consists of characters from the following character set:

Description

Characters

Alphabetic characters

a, b, c,…………, z

A, B, C,………..., Z



Digits

0, 1, 2,…….., 8, 9

Special Characters

Space ‘ , ; _ + - * / “ { } [ ]

( ) ? > < = ! ~ & % # : \ ^ .



Program statements in ‘C’ can use 92 out of 95 printable characters (all printable ASCII characters except ASCII 36 ($), ASCII 64 (@) and ASCII (`). Data and stings within a program can also include any other characters.

Keywords

Keywords are words that are part of the ‘C’ language and they are recognised by ‘C’ as having specific meaning in specific contexts. Such words are cannot be used as variable/function/constant name in ‘C’. Keywords are in lowercase only. The ANSI standard ‘C’ has 32 keywords are in lowercase only. The ANSI standard C has 32 Keywords 27 of the keywords are defined by Dennis Ritchie and 5 by ANSI. Additional keywords are defined in different versions.

auto break case char continue default

do double else extern float for

for goto if int long register

return short sizeof static struct switch

typedef union unsigned while const enum

signed void volatile



Identifiers

Identifiers are names, which the user assigns within a ‘C’ program to variables, arrays constants or functions. The identifier name can include any alphabet (a-z, A-Z) digits and underscore. The first letter must be an alphabet and a maximum of 32 characters.



Data Types

A Data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable.

‘C’ supports four fundamental data types i.e. Integer (int), Character (char) Floating point (float) and Double precision floating point (double) and two additional void and enum.

‘C’ also allows using the qualifiers to extend the accuracy of integers and floating points. But qualifiers will consume more memory. The qualifiers are short, long, signed and unsigned. Then it is called derived data type.



Char Type: Used to store a single character of information. Char constant must be enclosed between two single quotation marks. i.e. “a” is not allowed but ‘a’ is allowed. C allows the qualifiers signed and unsigned with Char type.

Int Type: Integer type variables can only contain whole number. i.e. They can’t store decimal point or fractional part. int type can be qualified using long, short, signed and unsigned. e.g.:- 10, -23

Float type: Floating point numbers are numbers that can have a decimal point and fractional part. They also called real numbers. Also floating point variable can hold more digits than integer type. This type gives a precision of 6 digits. But floating-point calculations are slower in execution and occupy more memory than integer type. e.g.:- 10.199999, 5.54567, -23.465371 Floating point variables can also store numbers in scientific notation. e.g.: 123.45e6 for 123.45 x 106.

Double Type: If the accuracy provided by float type is not sufficient, the double precision floating point type. i.e., Double type can be used. Double type gives an accuracy of 16 digits. They can also store numbers in scientific notation. To get an extended double precision use long qualifier with double.

Size and Range of Data Types

Data type

Size

Range

Char/ Signed Char

1

-128 to +127

Unsigned Char

1

0 to 255

Int/ Signed Int

2

-32768 to +32767

Unsigned Int

2

0 to 65535

Signed Short Int

1

-128 to +127

Unsigned Short Int

1

0 to 255

Long Int/ Signed long int

4

-214,83,74,648 to +214,83,74,647

Unsigned long int

4

0 to 429,67,49,295

Float

4

-3.4e38 to +3.4e38

Double

8

-1.7e308 to +1.7e308

Long double

10

-3.4e4932 to +1.1e4932


Variable Declaration

‘C’ requires that any identifier including variables be declared before it appears in an executable statement. The variable declaration statements must be the first statements in a code-block. Declaration tells the compiler, the name of the variable and the data type of that variable. In ‘C’ preceding the variable name with the data type followed by a space does a variable declaration.



syntax: datatype variable_name;
e.g.:- int age; float result; etc.

You can declare all the variables of same type in one declaration statement.


e.g.:- int x, y, z, age;

Declaration that creates an initial value is termed as definition.


e.g.:- int x = 0, y = 0; char = y’; etc.,

ARRAYS

An array is a group of related data items allocated consecutively in memory that share a common name. The individual data items in an array are called elements and are addressed by the array name and the element’s index number or subscript in square brackets ([ and ]). Arrays can be of any data type and have up to 7 dimensions. The dimensions may vary depending on the compiler. Arrays must be declared before they are used. The number of elements in each dimension must be declared in square brackets.



syntax : datatype variable_name[ size ]
where size is the number of elements in the array.

e.g.:- int age[10]; float results[12]; int marks[10][5]; etc.

In ‘C’ array subscript starts from 0 to arraysize-1. Each array element can be used as like the normal variable in any operation. Array elements can be assigned values at anywhere in the program using the equal to (=) symbol.

e.g.:- marks[5][2]=10; age[0]=21; etc.

Arrays can be initialised at the time of declaration with a list of values enclosed in braces. For example,

int age[3] = {1,2,3}; int table[2][3]={ 1,2,3,4,5,6};

int table[2][3]={ {1,2,3},{4,5,6}};

int table[2][3]= {

{1,2,3},

{4,5,6}


}; etc.


Download 0.91 Mb.

Share with your friends:
1   2   3   4   5   6   7   8   9   ...   13




The database is protected by copyright ©ininet.org 2024
send message

    Main page