0% found this document useful (0 votes)
19 views

5.introduction To C Language

It is something very useful to students

Uploaded by

tanniruvarshitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

5.introduction To C Language

It is something very useful to students

Uploaded by

tanniruvarshitha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Character Set: A character denotes any alphabet, digit, white space or any

special symbol that is used to represent information. A character set is

collection of characters.

Token: A token is the smallest individual unit of a program.

Instruction: An instruction is a statement that is given to computer to

perform a specific operation.

Function: A function is a collection of instructions that performs a

particular task.

Program: A program is a well-organized collection of instructions that is

used to communicate with the computer system to accomplish desired

objective.
Character set:
In C programming, a character set is a set of characters that is used to
represent the letters, digits, and other symbols in a computer. The
character set refers to a set of all the valid characters that we can use in the
source program .The C programming language supports four types of
characters in its character set: Alphabets, Digits, Special characters, and
White spaces.

Tokens

A token is the smallest individual unit (or element) of a program. The

tokens used in a program are:


 Keywords.

 Identifiers.

 Literals (constants).

 Variables.

 Operators.

 Special symbols.

Keywords:
Keywords are the pre-defined or built-in words. Each keyword has its own
definition that is defined by the language developers. A C compiler can
recognize keyword and replaces its definition whenever it is needed.
Keywords also called as reserved words. Each keyword has its own purpose
and it should be used only for that purpose. It is important to note that all
the keywords should be in lowercase.There are 3 types of keywords:
Identifiers:
As pre-defined names (i.e., keywords) are needed to develop a C program,

User-defined names also needed. These user-defined names are called as

identifiers. Identifiers are the names given to various program elements

such as variables, constants, arrays, functions, pointer,etc.

While naming identifier need to follow set of rules(naming conventions)

Rule #1: An identifier should not be a keyword.

Ex: salary, name, GOTO (valid)

int, goto (invalid)

Rule #2: The first character of identifier must be a letter or an underscore .

The subsequent characters may be alphabets, digits or underscore. Special

symbols are not allowed.

Ex: pradeep K123 _abc (valid)

1Raj (invalid)
Rule #3: No special symbol is used except underscore (_). No spaces are

allowed in an identifier.

Ex: gross_sal Rect_area (valid)

Gross salary s.i. profit&loss (invalid)

Rule #4: Upper and lower case letters in an identifier are distinct (or

different since C is case sensitive language).

Ex: The names amount, Amount, aMOUnt and AMOUNT are not the same

identifiers.

Rule #5: An identifier can be arbitrarily long. Some implementations of C

recognize only the first eight characters, though most compilers recognize

more (typically, 31 characters).

Data type
Data type is a types of data that states the possible values that can be taken,
how they are stored and what operations are allowed on them.Before we
learn variables we need to know regarding data types since variables have
to be associated with data types in prior before used since C is statically
typed language.
Basic data types: The fundamental data types in C, such as integers, float,
characters, double and void are used to represent simple values and are
known as the "basic data types.".They are also referred as primitive data
types or built-in data types or primary data types.
Data type Size (in bytes)

int 2 bytes or 4 bytes (varies from one compiler to another).

float 4 bytes

double 8 bytes

char 1 byte.

void 0 bytes.
Type modifiers: Except the data type void, the primitive data types may

have various type modifiers preceding them. Type modifiers are the

keywords that are used to modify the behaviour of existing primitive data

types. There are two types of modifiers:

 Size modifiers: These type modifiers modify the number of bytes a

primitive data type occupies. Based on size, the maximum and

minimum values, a primitive data type specifies, will be changed.

The size modifiers include: long and short.

 Sign modifiers: These type modifiers modify the sign of a primitive

data type. The sign modifiers include: signed and unsigned.

a) Size modifiers: A compiler can decide appropriate sizes depending

on operating system and hardware for which it is being written.

compiler short int long

16-bit 2 2 4

32-bit 2 4 4
b)Sign modifiers: if unsigned type modifier is preceding a primitive data

type, then the variables of the specified type accept only positive values. If

signed type modifier is preceding a primitive data type, then the variables

of specified type accept both positive and negative values.

Below we can observe various data types including type modifiers:

The format specifier in C is used to tell the compiler about the type of data

to be printed or scanned in input and output operations. They always

start with a % symbol and are used in the formatted string in functions

like printf(), scanf, sprintf(), etc.


-3.4e38 to +3.4e38 with 6 digits of precision.

-1.7e308 to +1.7e308 with 10 digits of precision.

-1.7e4932 to +1.7e4932 with 10 digits of precision.


10

For signed data types use formula -2(n-1) to 2(n-1)-1


Use those formulas to to find range

For unsigned data types use formula 0 to 2n-1

In both the cases n is no of bits

Derived and User-defined data types: These are the data types derived from

primitive data types and few are defined by the user according to his needs.

These data types will be defined by using primitive data types. The derived

and user-defined data types include: array, functions , pointers, struct,

union, enum.
Array : An array in C is a fixed-size collection of similar data items stored
in contiguous memory locations.

Function: A function is a group of statements that together perform a task.


Every C program has at least one function, which is main(), and all the
most trivial programs can define additional functions.

Pointer : A pointer is a variable whose value is the address of another


variable, i.e., direct address of the memory location.

Structure: A structure type is a user-defined composite type. It is composed


of fields or members that can have different types

Union : A union is a special data type available in C that allows to store


different data types in the same memory location.

Enumeration : is a user-defined data type that consists of a set of named


constant values.
Variables:

A variable is a named location in memory that holds a value and that value

may be varied during execution of a program.


Declaring a variable: All variables should be declared before we use them in

the program. The variable declaration tells the compiler two things:

1. What the name(s) of variables are?

2. Where the values are being stored?

Usually, the declarative instruction is written as a first statement before all

the executable statements.

The declarative instruction has the following syntax:

[Storage class] <data type> <variable name(s)>;

In this syntax, The content in square brackets is optional. The content in

angle brackets is mandatory. There should be spaces in between. The

declarative instruction should always be ended with a semi-colon.

 The storage class specifies the default value a variable(s) holds,

storage location of variable(s), scope and life time of variable(s).

These include: auto, extern, register, static.


 The data type is a keyword that specifies the type of data that is

being hold by the variable(s).

 The variable name is any legal identifier. In other words, it should be

built based on the rules of identifier. If there are more than one

variable of the same type, then separate them with commas.

Ex: int a,b,c; //a,b and c are integer variables

long double m; // m is a long double variable.

char sex; // sex is a character variable

char name[20]; //name is a character array that hold 19 characters.

Initializing a variable(defining a variable): Initializing a variable is the

process of assigning a value to the variable. The initialization can be done

as follows:
[Storage class] <data type> <variable name>=<value>; ….(1)
(or)
[Storage class] <data type> <variable name>;
<variable name>=<value>; ….(2)
(or)
[Storage class] <data type> <variable name>;
Using scanf() read value that has to be initialized …..(3)
In these first two syntaxes, we observe an operator, i.e., assignment operator

(=), which is used to assign a value of Right operand to Left operand.

In the second syntax, the variable name should be declared earlier.

Ex: int a=20; is equivalent to int a; a=20;

int a=20; //declarartion and definition in single statement.


Static
int a; initialization

a=20; // declarartion done prior and later provided definition

Static Initialization: Here, the variable is assigned a value in advance(before

compilation) .This variable then acts as a constant.

In syntax 3 we used scanf() to read the value to be initialized at the time of

executing the program.

int a;
Dynamic
initialization
scanf(“%d”,&a);
Dynamic Initialization: Here, the variable is assigned a value at the run time.

The value of this variable can be altered every time the program is being

run.

Type qualifiers

Type qualifiers are the keywords that add new meanings to existing data

types. There are two type qualifiers: const, volatile.

 Making a variable as read-only variable: In order to make the value of

variable as unchanged during the execution of a program, initialize

the variable with the type qualifier const as follows:

const [Storage class] <data type> <variable name>=<value>;

Ex: const double PI=3.1412;

This initialization tells the compiler that the value of PI must not be

modified by the program. However, it can be used on the right hand

side of assignment statement like other variable.


Ex: double x;

x=PI;

 Making a variable as modifiable externally: In order to make

variable’s value modifiable at any time by some external sources

(from outside program), we use type qualifier volatile. For example,

volatile int x;

The value of x may be altered by some external factors even if

it does not appear on the left-hand side of an 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 whether any

external alteration has changed the value.

You might also like