Chapter 1
Programming in C
LEARNING OBJECTIVES
• Basic concepts • Precedence decreases as we move from top to bottom
• Character set • Type conversion
• Identifier • Documentation section
• Declaring a variable • Preprocessing
• Visualization of declaration • Global declaration
• Constants • Control statements
• Single character constants • Selection/Decision making statement
• String constants • Looping statements
• Using const keyword • Unconditional jump statements
BasiC COnCepts Variable
The name itself represents value, is not constant. Variable is a
Character Set data name whose value varies/changes during program execution.
Variable name is a name given to memory cell (may be one or
A character refers to an alphabet, digit or a special symbol. multiple bytes).
Alphabets: A – Z, a – z
Digits: 0 -9
Special symbols:
Data types
∼ ! # % ∧ and * ( ) - + { } [ ] - < > , . | ? \ | : ; ” ’ White space Represents type of data and set of operations to perform on data .
Data Type
Identifier Primitive/Basic Derived User defined Valueless
Identifier is a user-defined name used for naming a variable or a – Char – Array – Structure
function. – float – pointer – union – void
Rules for naming an identifier
– double Enumeration
• Consists only letters, digits and underscore – integer
• Starts only with an alphabet or underscore
• Keywords cannot be used.
Type Keyword Number of Bytes
• Can be as long as you like, first 31 characters are significant.
Integer int 2
Example: Valid identifiers: RollNo, Roll_No, _Roll_No Floating float 4
rollno, Name2; Double double 8
Invalid: 2name, Roll No. Character char 1
, 3.4 | Unit 3 • Programming and Data Structures
Declaring a Variable Constants
A constant value is one which does not change during the
•• Before using a variable, you must give some information
execution of a program.
to compiler about the variable. i.e., you must declare it.
C supports several types of constants:
•• Declaration statement includes the type and variable
name. 1. Integer constants
2. Real constants
Syntax:
3. Single character constants
Datatype Var_name;
4. Strings constants
Example:
int roll_no;
char ch; Integer constants
float age;
An integer constant is a sequence of digits. It consists of
•• When we declare a variable a set of digits 0 to 9 preceded by an optional + or - sign
•• memory space is allocated to hold a value of specified spaces, commas, and non-digit characters are not permitted
type. between digits.
•• space is associated with variable name Examples for valid decimal integer constants are
•• space is associated with a unique Address. 123
-31
Table 1 Visualization of declaration 0
roll no 562321
int roll no; garbage
+78
Examples for invalid integer constants are
2002
20,000
marks `1000
int marks = 10; 10
3008 Real constants
diameter Real constants consist of a fractional part in their represen-
float diameter = 5.9 5.9 tation. Integer constants are inadequate to represent quanti-
4252 ties that vary continuously.
ch → variable name Examples of real constants are
char ch : ‘A’ A → value
0.0026
-0.97
2820 → address
435.29
Note: The default value is garbage, i.e., an unknown value +487.0
is assigned randomly.
Renaming data types with typedef Typedef is a keyword,
Single character constants
which can form complex types from the basic type, and will A single character constant represents a single character
assign some simpler names for such combinations. This is which is enclosed in a pair of quotation symbols.
more helpful when some declaration is very tough, confus- Examples for character constants are
ing or varies from one implementation to another. ‘5’
For example, the data type unsigned long int is redefined ‘x’
as LONG as follows: ‘;’
typedef unsigned long int LONG;
String constants
Uses of enumerated data types Enumerated data types are
A string constant is a set of characters enclosed in dou-
most useful when one is working over small, discrete set
ble quotation marks. The characters in a string constant
of values, in which each is having a meaning and it is not
sequence may be alphabet, number, special character and
a number.
blank space.
A best example can be given on months jan, feb, mar, …,
dec, which are 12 in number, with assigning consecutive num- Examples of string constants are
bers for it. “VISHAL”
The main advantages are storage efficiency, the c-code “1234”
can become readable “C language”
“!….?”
Programming in C
LEARNING OBJECTIVES
• Basic concepts • Precedence decreases as we move from top to bottom
• Character set • Type conversion
• Identifier • Documentation section
• Declaring a variable • Preprocessing
• Visualization of declaration • Global declaration
• Constants • Control statements
• Single character constants • Selection/Decision making statement
• String constants • Looping statements
• Using const keyword • Unconditional jump statements
BasiC COnCepts Variable
The name itself represents value, is not constant. Variable is a
Character Set data name whose value varies/changes during program execution.
Variable name is a name given to memory cell (may be one or
A character refers to an alphabet, digit or a special symbol. multiple bytes).
Alphabets: A – Z, a – z
Digits: 0 -9
Special symbols:
Data types
∼ ! # % ∧ and * ( ) - + { } [ ] - < > , . | ? \ | : ; ” ’ White space Represents type of data and set of operations to perform on data .
Data Type
Identifier Primitive/Basic Derived User defined Valueless
Identifier is a user-defined name used for naming a variable or a – Char – Array – Structure
function. – float – pointer – union – void
Rules for naming an identifier
– double Enumeration
• Consists only letters, digits and underscore – integer
• Starts only with an alphabet or underscore
• Keywords cannot be used.
Type Keyword Number of Bytes
• Can be as long as you like, first 31 characters are significant.
Integer int 2
Example: Valid identifiers: RollNo, Roll_No, _Roll_No Floating float 4
rollno, Name2; Double double 8
Invalid: 2name, Roll No. Character char 1
, 3.4 | Unit 3 • Programming and Data Structures
Declaring a Variable Constants
A constant value is one which does not change during the
•• Before using a variable, you must give some information
execution of a program.
to compiler about the variable. i.e., you must declare it.
C supports several types of constants:
•• Declaration statement includes the type and variable
name. 1. Integer constants
2. Real constants
Syntax:
3. Single character constants
Datatype Var_name;
4. Strings constants
Example:
int roll_no;
char ch; Integer constants
float age;
An integer constant is a sequence of digits. It consists of
•• When we declare a variable a set of digits 0 to 9 preceded by an optional + or - sign
•• memory space is allocated to hold a value of specified spaces, commas, and non-digit characters are not permitted
type. between digits.
•• space is associated with variable name Examples for valid decimal integer constants are
•• space is associated with a unique Address. 123
-31
Table 1 Visualization of declaration 0
roll no 562321
int roll no; garbage
+78
Examples for invalid integer constants are
2002
20,000
marks `1000
int marks = 10; 10
3008 Real constants
diameter Real constants consist of a fractional part in their represen-
float diameter = 5.9 5.9 tation. Integer constants are inadequate to represent quanti-
4252 ties that vary continuously.
ch → variable name Examples of real constants are
char ch : ‘A’ A → value
0.0026
-0.97
2820 → address
435.29
Note: The default value is garbage, i.e., an unknown value +487.0
is assigned randomly.
Renaming data types with typedef Typedef is a keyword,
Single character constants
which can form complex types from the basic type, and will A single character constant represents a single character
assign some simpler names for such combinations. This is which is enclosed in a pair of quotation symbols.
more helpful when some declaration is very tough, confus- Examples for character constants are
ing or varies from one implementation to another. ‘5’
For example, the data type unsigned long int is redefined ‘x’
as LONG as follows: ‘;’
typedef unsigned long int LONG;
String constants
Uses of enumerated data types Enumerated data types are
A string constant is a set of characters enclosed in dou-
most useful when one is working over small, discrete set
ble quotation marks. The characters in a string constant
of values, in which each is having a meaning and it is not
sequence may be alphabet, number, special character and
a number.
blank space.
A best example can be given on months jan, feb, mar, …,
dec, which are 12 in number, with assigning consecutive num- Examples of string constants are
bers for it. “VISHAL”
The main advantages are storage efficiency, the c-code “1234”
can become readable “C language”
“!….?”