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

Chapter 2 C Programming

Chapter 2 C Programming

Uploaded by

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

Chapter 2 C Programming

Chapter 2 C Programming

Uploaded by

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

C Programming – Chapter 2

Constants, Variables and Data


Types
C TOKENS
⚫ The smallest individual units are known as
tokens
⚫ C TOKENS are 6 types
⚫ Theyare Keywords, Constants, Strings,
Operators, Identifiers, Special symbols
KEYWORDS AND IDENTIFIERS
⚫ Every C word is classified as either a keyword or
an identifier.
⚫ All keywords have fixed meanings and these
meanings cannot be changed.
Eg: auto, break, char, void etc.,
⚫ Identifiers refer to the names of variables,
functions and arrays. They are user-defined
names and consists of a sequence of letters and
digits, with a letter as a first character.
⚫ Both uppercase and lowercase letters are
permitted. The underscore character is also
permitted in identifiers.
⚫ Rules for Identifiers
⚫ Firstcharacter must be an alphabet
⚫ Must consists of only letters, digits or
underscore
⚫ Only first 31 characters are significant
⚫ Cannot use a keyword
⚫ Must not contain white space

⚫ Ex. ename, roll_no, x1, stu_name


Constants
⚫ Fixed values that do not change during the
execution of a program.
⚫ Types of Constants are
⚫ Numeric Constants
⚫ Integer Constants
⚫ An integer constant refers to a sequence of digits, There are
three types integers, namely, decimal, octal, and hexa decimal.
⚫ Ex. 123 -321 0 56433 +790 – Decimal integers
⚫ Ex. 037 0 0435 0551 - Octal integers
⚫ Ex. 0x9F 0x 0Xbcd - Hexadecimal integers
⚫ Real Constants
⚫ Certain quantities that vary continuously, such as distances, heights
etc., are represented by numbers containing functional parts like
17.548. Such numbers are called real (or floating point) constants.
⚫ Ex. 20.57, 36.08, -0.75
⚫ Also be expressed in exponential or scientific notation
⚫ Syntax is mantissa e expoent
⚫ Ex. 0.65e4, 12e-2
Contd..
⚫ Character Constants
⚫ Single Character Constants
⚫ A single character constants contains a single character
enclosed within a pair of single quote marks.
⚫ Eg: ’5’ ‘X’ ‘;’
⚫ String Constants
⚫ A string constant is a sequence of characters enclosed in
double quotes. The characters may be letters, numbers,
special characters and blank space.
⚫ Eg:”Hello!” “1987” “?….!”
Contd …
⚫ Backslash Character Constants
⚫ C supports special backslash character constants that are
used in output functions. These character combinations are
known as escape sequences.
⚫ Constant Meaning
⚫ ‘\a’ audible alert
⚫ ‘\b’ backspace
⚫ ‘\f’ form feed
⚫ ‘\n’ new line
⚫ ‘\0’ null
⚫ ‘\v’ vertical tab
⚫ ‘\t’ horizontal tab
⚫ ‘\r’ carriage return
Variables
⚫ A variable is a data name that may be used to
store a data value.
⚫ A variable may take different values at different
times of execution and may be chosen by the
programmer in a meaningful way.
⚫ It may consist of letters, digits and underscore
character.
⚫ Eg:
⚫ 1) Average
⚫ 2) Height
Rules for defining variables
⚫ They must begin with a letter. Some systems
permit underscore as the first character.
⚫ ANSI standard recognizes a length of 31
characters. However, the length should not be
normally more than eight characters.
⚫ Uppercase and lowercase are significant.
⚫ The variable name should not be a keyword.
⚫ White space is not allowed.
Data Types
⚫ C is rich in its data types
⚫ 3 classes of data types
⚫ PRIMARY DATA TYPES
⚫ Integral Type
⚫ Integer
⚫ signed type unsigned type
⚫ int unsigned int
⚫ short int unsigned short int
⚫ Long int unsigned long int
⚫ Eg. 32768, 4586235864586
⚫ Character
⚫ signed char
⚫ unsigned char
⚫ Eg. ‘a’, “Hello”, ‘WELCOME”
⚫ Floating Point Type
⚫ float
⚫ double
⚫ long double
⚫ Eg. 13.5, 1290.56, 12e4
⚫ Void – does not having anything
Contd …
⚫ Derived Data types
⚫ Itwas derived from primary data types.
⚫ They are
⚫ Arrays
⚫ Structures
⚫ Union
⚫ Eg. for Derived data type
⚫ Arrays
⚫ Eg. int a[10], b[20];
⚫ Structures
⚫ Eg.struct employee
{ int eno, salary;
char name[15];
float ded;
} emp;
Data types
⚫ User-defined data type
⚫ C allows user to define an identifier that
would represent an existing data type.
⚫ Declaring user-defined data type in 2 types
1) typedef
2) enumerated data type
Contd …
⚫ User-defined data type
1. C allows user to define an identifier that
would represent an existing data type.
⚫ The general form is
⚫ typedef type identifier;
⚫ Eg: 1) typedef int units;
⚫ units batch1, batch2;
⚫ 2) typedef float marks;
⚫ Marks name1[50], name2[50];
Contd …
⚫ User-defined data type
2. enumerated data type which can be used to
declare variables that can have one of the
values enclosed within the braces.
⚫ The syntax is
⚫ enum identifier {value1,value2,……valuen};
⚫ Eg. Enum daya {Monday, Tuesday, … Sunday} week_st, week_end;
⚫ Enum daya {Monday=1, Tuesday=2, … Sunday} week_st, week_end;
⚫ The compiler automatically assigns integer digits beginning with 0 to
all the enumeration constants.
DECLARATION OF VARIABLES
⚫ Tells the compiler what the variable name
is
⚫ It specifies what types of data the
variable will hold
⚫ Primary Data type
⚫ The syntax is
⚫ Data-type v1,v2…..vn;
⚫ Where v1, v2, …..vn name of the variable
⚫ Eg:1.int count;
2.double ratio, total;
Declaration of storage class
⚫ Variables in C can have not only data type
but also storage class that provides
information about their locality and
visibility.
⚫ There are four storage class specifiers,
namely,
⚫ auto,
⚫ static,
⚫ register and
⚫ extern.
Contd…
⚫ auto variable :
⚫ Local variable only to the function in which it is
declared
⚫ Eg. auto int count;

⚫ Static variable:
⚫ Local variable which exists and retains its value even
after the control is transferred to the calling function
⚫ It is automatically initialized to zero.
⚫ Eg. static int x;
Contd…
⚫ Extern (or) Global variable:
⚫ It is known to all the functions in the file.
⚫ It is automatically initialized to zero.
⚫ Eg. extern int tot;

⚫ Register variable:
⚫ Local variable which is stored in the register
⚫ Eg. register char ch;
Storage class example
⚫ /*Example of storage class*/
int m;
main()
{
int i;
float bal;
……
……
function1();
} function1()
{
int i;
float sum;
……
……
}
⚫ Here the variable m is called the global variable. It can be used
in all the functions in the program.
⚫ The variables bal, sum and i are called local variables.
⚫ Local variables are visible and meaningful only inside the
function in which they are declared.
ASSIGNING VALUES TO VARIABLES
⚫ The syntax is
⚫ Variable_name = constant
Eg:1) int a=20;
2) bal=75.84;
3) yes=’x’;

⚫ C permits multiple assignments in one line.


Example:
initial_value=0;final_value=100;
Defining Symbolic Constants
⚫ We often use certain unique constants in a program.
These constants may appear repeatedly in a number of
places in the program. One example of such a constant
is 3.142, representing the value of the mathematical
constant “pi”.
⚫ We face two problems in the subsequent use of such
programs.
⚫ 1. Problem in modification of the programs.
⚫ 2. Problem in understanding the program.
⚫ A constant is defined as follows:
⚫ #define symbolic-name value of constant
⚫ Eg: 1) #define pi 3.1415
⚫ 2) #define pass_mark 50
Rules to #define statement which
define a symbolic constant
⚫ Rules:
⚫ No blank space between the sign ‘#’ and the word define is
permitted
⚫ ‘#’ must be the first character in the line.
⚫ A blank space is required between #define and symbolic name
and between the symbolic name and the constant.
⚫ #define statements must not end with the semicolon.
⚫ After definition, the symbolic name should not be assigned any
other value within the program by using an assignment
statement.
⚫ Symbolic names are NOT declared for data types. Their data
types depend on the type of constant.
⚫ #define statements may appear anywhere in the program but
before it is referenced in the program.
Declaring a variable as constant
⚫ Eg: 1) const int class_size=40;
⚫ This tells the compiler that the value of
the int variable class_size must not be
modified by the program.
Declaring a variable as volatile
⚫ By declaring a variable as volatile, its
value may be changed at any time by
some external source.
Eg:1) volatile int date;
Overflow and underflow of Data
⚫ C does not provide any warning or
indication of overflow. It simply gives
incorrect results.
⚫ An overflow normally results in the lorgest
possible real value whereas an underflow
results in zero.

You might also like