EST102 Programming in C
Module II
Program Basics-Basic structure of C program: Character set, Tokens, Identifiers in C, Variables and
Data Types , Constants, Console IO Operations, printf and scanf, Operators and Expressions: Expressions
and Arithmetic Operators, Relational and Logical Operators, Conditional operator, size of operator,
Assignment operators and Bitwise Operators. Operators Precedence ,Control Flow Statements: If
Statement, Switch Statement, Unconditional Branching using goto statement, While Loop, Do While
Loop, For Loop, Break and Continue statements.(Simple programs covering control flow)
CHARACTER SET
As every language contains a set of characters used to construct words, statements, etc. C
language also has a set of characters which include alphabets, digits, and special symbols. C
language supports a total of 256 characters. C language character set contains the following set
of characters.
Alphabets
Digits
Special Symbols
Alphabets
C language supports all the alphabets from the English language. Lower and upper
case letters together support 52 alphabets.
Lower case letters - a to z
Upper case letters - A to Z
Digits
C language supports 10 digits which are used to construct numerical values in C
language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
- ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > ,< \ | tab newline space NULL bell
backspace vertical tab etc.,
TOKEN
Every C program is a collection of instructions and every instruction is a collection
of some individual units. Every smallest individual unit of a c program is called token.
Every instruction in a c program is a collection of tokens. Tokens are used to construct
IIPE 1
, EST102 Programming in C
c programs and they are said to be the basic building blocks of a c program.
In a c program tokens may contain the following
Keywords
Identifiers
Operators
Special Symbols
Constants
Strings
Data values
Keywords
Keywords are specific reserved words in C each of which has a specific feature
associated with it. Keywords are always in lowercase.
There are total of 32 keywords in C:
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof Static
struct switch typedef union unsigned void
volatile while
Identifier
An identifier is a collection of characters which acts as the name of variable,
function, array, pointer, structure, etc.
Rules for Creating a Valid Identifier
1. An identifier can contain letters (Upper case and lower case), digit and
underscore symbol only.
2. An identifier should not start with a digit value. It can start with a letter or an
underscore.
3. Should not use any special symbols in between the identifier even whitespace.
4. Keywords should not be used as identifiers.
5. There is no limit for the length of an identifier. However, the compiler
considers the first 31 characters only.
6. In C language, the identifiers are case sensitive.
IIPE 2
, EST102 Programming in C
DATA TYPES
Data types in the c programming language are used to specify what kind of
value can be stored in a variable. The memory size and type of the value of a
variable are determined by the variable data type.
In the C programming language, data types are classified as follows
1. Primary data types (Basic data types OR Predefined data types)
2. Derived data types (Secondary data types OR User-defined data types)
3. Enumeration data types
4. Void data type
Primary Data Types
char: The most basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point
value) with single precision.
double: It is used to store decimal numbers (numbers with floating
point value) with double precision.
DATA TYPE MEMORY (BYTES) FORMAT SPECIFIER
int 2 %d
char 1 %c
float 4 %f
double 8 %lf
Note: Size of each data types varies based on machines.
Data Type Qualifier
The basic data types can be augmented by the use of the data type qualifiers
short, long, signed and unsigned. For example, integer quantities can be defined as
short int, long int or unsigned int.
The interpretation of a qualified integer data type will vary from one C compiler
to another, though there are some commonsense relationships. Thus, a short int
may require less memory than an ordinary int or it may require the same amount of
IIPE 3
, EST102 Programming in C
memory as an ordinary int, but it will never exceed an ordinary int in word length.
Similarly, a long int may require the same amount of memory as an ordinary int or it
may require more memory, but it will never be less than an ordinary int.
If short int and int both have the same memory requirements (e.g. 2 bytes), then
long int will generally have double the requirements (e.g. 4 bytes). Or if int and long
int both have the same memory requirements (e.g. 4 bytes) then short int will
generally have half the memory requirements (e.g. 2bytes).
An unsigned int has the same memory requirements as an ordinary int.
However, in the case of an ordinary int, the leftmost bit is reserved for the sign. With
an unsigned int, all of the bits are used to represent the numerical value. Thus, an
unsigned int can be approximately twice as large as an ordinary int. For example, if
an ordinary int can vary from -32,768 to +32,767, then an unsigned int will be
allowed to vary from 0 to 65,535.
void data type
The void data type means nothing or no value. Generally, the void is used to
specify a function which does not return any value. We also use the void data type
to specify empty parameters of a function.
VARIABLES
A variable is an identifier that is used to represent some specified type of
information within a designated portion of the program. Variables in a c
programming language are the named memory locations where the user can store
different values of the same datatype during the program execution.
Variable Declaration
A declaration associates a group of variables with a specific data type. All
variables must be declared before they can appear in executable statements. A
declaration consists of a data type, followed by one or more variable names, ending
with a semicolon.
Example int a,b,c ; float d; char e;
In C language ;(semicolon) is the statement terminator.
Escape Sequence
Certain nonprinting characters, as well as the backslash (\) and the apostrophe
IIPE 4
Module II
Program Basics-Basic structure of C program: Character set, Tokens, Identifiers in C, Variables and
Data Types , Constants, Console IO Operations, printf and scanf, Operators and Expressions: Expressions
and Arithmetic Operators, Relational and Logical Operators, Conditional operator, size of operator,
Assignment operators and Bitwise Operators. Operators Precedence ,Control Flow Statements: If
Statement, Switch Statement, Unconditional Branching using goto statement, While Loop, Do While
Loop, For Loop, Break and Continue statements.(Simple programs covering control flow)
CHARACTER SET
As every language contains a set of characters used to construct words, statements, etc. C
language also has a set of characters which include alphabets, digits, and special symbols. C
language supports a total of 256 characters. C language character set contains the following set
of characters.
Alphabets
Digits
Special Symbols
Alphabets
C language supports all the alphabets from the English language. Lower and upper
case letters together support 52 alphabets.
Lower case letters - a to z
Upper case letters - A to Z
Digits
C language supports 10 digits which are used to construct numerical values in C
language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Special Symbols
- ~ @ # $ % ^ & * ( ) _ - + = { } [ ] ; : ' " / ? . > ,< \ | tab newline space NULL bell
backspace vertical tab etc.,
TOKEN
Every C program is a collection of instructions and every instruction is a collection
of some individual units. Every smallest individual unit of a c program is called token.
Every instruction in a c program is a collection of tokens. Tokens are used to construct
IIPE 1
, EST102 Programming in C
c programs and they are said to be the basic building blocks of a c program.
In a c program tokens may contain the following
Keywords
Identifiers
Operators
Special Symbols
Constants
Strings
Data values
Keywords
Keywords are specific reserved words in C each of which has a specific feature
associated with it. Keywords are always in lowercase.
There are total of 32 keywords in C:
auto break case char const continue
default do double else enum extern
float for goto if int long
register return short signed sizeof Static
struct switch typedef union unsigned void
volatile while
Identifier
An identifier is a collection of characters which acts as the name of variable,
function, array, pointer, structure, etc.
Rules for Creating a Valid Identifier
1. An identifier can contain letters (Upper case and lower case), digit and
underscore symbol only.
2. An identifier should not start with a digit value. It can start with a letter or an
underscore.
3. Should not use any special symbols in between the identifier even whitespace.
4. Keywords should not be used as identifiers.
5. There is no limit for the length of an identifier. However, the compiler
considers the first 31 characters only.
6. In C language, the identifiers are case sensitive.
IIPE 2
, EST102 Programming in C
DATA TYPES
Data types in the c programming language are used to specify what kind of
value can be stored in a variable. The memory size and type of the value of a
variable are determined by the variable data type.
In the C programming language, data types are classified as follows
1. Primary data types (Basic data types OR Predefined data types)
2. Derived data types (Secondary data types OR User-defined data types)
3. Enumeration data types
4. Void data type
Primary Data Types
char: The most basic data type in C. It stores a single character and requires
a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point
value) with single precision.
double: It is used to store decimal numbers (numbers with floating
point value) with double precision.
DATA TYPE MEMORY (BYTES) FORMAT SPECIFIER
int 2 %d
char 1 %c
float 4 %f
double 8 %lf
Note: Size of each data types varies based on machines.
Data Type Qualifier
The basic data types can be augmented by the use of the data type qualifiers
short, long, signed and unsigned. For example, integer quantities can be defined as
short int, long int or unsigned int.
The interpretation of a qualified integer data type will vary from one C compiler
to another, though there are some commonsense relationships. Thus, a short int
may require less memory than an ordinary int or it may require the same amount of
IIPE 3
, EST102 Programming in C
memory as an ordinary int, but it will never exceed an ordinary int in word length.
Similarly, a long int may require the same amount of memory as an ordinary int or it
may require more memory, but it will never be less than an ordinary int.
If short int and int both have the same memory requirements (e.g. 2 bytes), then
long int will generally have double the requirements (e.g. 4 bytes). Or if int and long
int both have the same memory requirements (e.g. 4 bytes) then short int will
generally have half the memory requirements (e.g. 2bytes).
An unsigned int has the same memory requirements as an ordinary int.
However, in the case of an ordinary int, the leftmost bit is reserved for the sign. With
an unsigned int, all of the bits are used to represent the numerical value. Thus, an
unsigned int can be approximately twice as large as an ordinary int. For example, if
an ordinary int can vary from -32,768 to +32,767, then an unsigned int will be
allowed to vary from 0 to 65,535.
void data type
The void data type means nothing or no value. Generally, the void is used to
specify a function which does not return any value. We also use the void data type
to specify empty parameters of a function.
VARIABLES
A variable is an identifier that is used to represent some specified type of
information within a designated portion of the program. Variables in a c
programming language are the named memory locations where the user can store
different values of the same datatype during the program execution.
Variable Declaration
A declaration associates a group of variables with a specific data type. All
variables must be declared before they can appear in executable statements. A
declaration consists of a data type, followed by one or more variable names, ending
with a semicolon.
Example int a,b,c ; float d; char e;
In C language ;(semicolon) is the statement terminator.
Escape Sequence
Certain nonprinting characters, as well as the backslash (\) and the apostrophe
IIPE 4