Unit 1 - II Consts Vars and Data Types v1.5
Unit 1 - II Consts Vars and Data Types v1.5
Table of Contents
Structure of a C Program: 3
C Character Set: 5
C Tokens 6
Token Classes in C 6
CONSTANTS 8
1. Integer constants 9
2. Real Constants (Floating-point constants) 9
3. Character constants (Single character constants) 9
4. String constants 9
5. Escape Sequences (Backslash character constants) 10
Variables 10
Rules to declare the variables 10
DATA TYPES 11
Primary Data Types 11
Integer Type(int) 12
Character Type (char): 14
Floating Point (float): 14
Double type(double) 14
Void type(void) 14
Format Specifier for different Data types ( w.r.t GNU C Compiler(GCC) ) 14
Declaration of Variables 15
Structure of a C Program:
Below diagram shows the general structure of a C Program. All C Programs may not have all these sections,
but will have a subset of these sections.
1. Documentation Section
This section consists of comment lines which include the name of the programmer, the author and
other details like time and date of writing the program. The Documentation section helps anyone to get an
overview of the program.
2. Link Section
The link section consists of the header files of the functions that are used in the program. It provides
instructions to the compiler to link functions from the system library.
3. Definition Section
All the symbolic constants are written in the definition section. Macros are known as symbolic
constants.
The declaration part declares all the variables that are used in the executable part. These two parts
must be written in between the opening and closing braces { }. Each statement in the declaration and
executable part must end with a semicolon (;). The execution of program starts at opening braces and ends at
closing braces.
6. Subprogram Section
The subprogram section contains all the user defined functions that are used to perform a specific
task. These user defined functions are called from the main() function directly or indirectly.
Note: All programs we write may not have all the sections described above, as many of these sections are
optional. This is the general structure.
C Character Set:
⇨ It is the set of characters used in or that forms the C language.
⇨ This is analogous to the character set used in a natural language like English.
⇨ These characters are used to form the words, numbers and expressions.
C Tokens
Smallest individual meaningful units in a C program are called Tokens. A Token consists of 1
or more characters from the C Character Set.
Token Classes in C
Following diagram shows token classes (categories) in C with examples for each class.
Keywords in ANSI C:
There are 32 keyword in ANSI C:
auto double int struct
break else long switch
case enum register typedef
char extern return union
Identifiers
Names defined by programmers in a program are called Identifiers. These names can be
variable names, function names etc.
E.g.: x, y, sum, add() etc.
Rules for writing an identifier
1. A valid identifier can have letters (both uppercase and lowercase letters), digits and
underscores.
You can choose any name for an identifier (excluding keywords). However, if you give a
meaningful name to an identifier, it will be easy to understand and work on, for you and your fellow
programmers. ( E.g. instead of x,y,z you can give num1, num2 and sum in case of the addition
problem which is better readable and intuitive.)
CONSTANTS
Constants are fixed values that do not change during execution of the program.
E.g: 5, 100, “hello” , 14.52, ‘A’, ‘b’
1. Integer constants
An integer constant is a numeric constant (associated with number) without any fractional or
exponential part. There are three types of integer constants in C programming:
● decimal constant(base 10)
● octal constant(base 8)
● hexadecimal constant(base 16)
For example: Representing different base constants in C
Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc.
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc
In C programming, octal constant starts with a 0(Zero) and hexadecimal constant starts with a 0x.
Integers are inadequate to represent quantities such as distance, heights, temperature, price and so on. These
quantities are represented by a number containing fractional parts like 12.32, 7.34 etc. Such numbers are called
real constants.
A floating point constant is a numeric constant that has either a fractional form or an exponent
form.
For example:
-2.0
0.0000234 // Fractional form
-0.22E-5 // Exponent form: mantissa E exponent
Note: E-5 is same as 10-5
⇨ A real number may also be expressed in exponential (Scientific) notation.
⇨ The letter e separating mantissa and exponent can be written either in lowercase or in
uppercase letter.
Example : 215.65 may be written as 2.1565e2 in exponential notation. e2 means multiple by 10^2
75000 can be written as 7.5E4 or 7.5E+4
-0.00038 can be written as -3.8e-4
4. String constants
String constants are the constants which are enclosed in a pair of double-quote marks. For example:
Sometimes, it is necessary to use characters which cannot be typed or have special meaning in
C programming. For example: newline (enter), tab etc. In order to use these characters, escape
sequences are used.
For example: \n is used for newlines. The backslash( \ )causes "escape" (or deviation) from the
normal way the characters are interpreted by the compiler. So instead of the character ‘n’, the
meaning of \n is a newline.
Variables
A variable is a data name in the program that may be used to store a data value.
DATA TYPES
Every data used in a C program must have a type associated with it. That is referred to as data type.
E.g: 25 is Integer data type. 2.53 is floating point type etc.
Since data is normally stored in variables, we specify the data type of a variable when we declare the
variable name(i.e creating a variable). Hence language has defined a set of DATA TYPES that can be
used to declare variables.
The 5 Primary Data Types are following (Keyword for each in bracket):
Integral Types:
1. Integer (int)
2. Character (char)
Floating Point types:
3. Floating point(float)
4. Double-precision floating point(double)
Void Type:
5. void type(void)
1. Integer Type(int)
⇨ Integers are whole numbers with a range of values supported by a particular machine.
⇨ There are signed integer and unsigned integer.
⇨ Signed integers use one bit for sign and other bits for magnitude of the number.
⇨ Unsigned integers are always positive (0 or above). They do not reserve any bit for
representing the sign, i.e., they use all the bits for storing the magnitude of the number.
⇨ Different integer types are short int, int, long int. Size of each will vary. Normally short int is
smallest, then int and followed by long int, the largest among three.
⇨ short, long, unsigned etc. are called as modifiers for the basic data type. These are keywords
of the C language.
⇨ Size of data type is the number of bytes(memory space) reserved for any variable of that
type.
⇨ Character type is used to represent a single character in C. E.g. ‘a’ , ‘A’, ‘1’,’$’
⇨ A character constant in C is represented by enclosing a character in single quotes.
⇨ Keyword for character data type is char
⇨ E.g: char a = ‘p’, choice = ‘q’; // Declared 2 character variable a and choice and both
initialized with values
⇨ Characters can be signed or unsigned.
⇨ unsigned char variable can store values from 0 to 255 using it size of 8 bits(1 byte)
⇨ signed char support range from -128 to 127
⇨ Character type belongs to integral data type.
4. Double type(double)
⇨ When the accuracy provided by float is not sufficient, double data type is used. It uses 8
bytes(64 bits) giving a precision of 14 digits.
⇨ E.g. double p = 121.243 ,q; // Declared two double variables. One is initialized with a value
⇨ When you want to extend more precision you can use the long double data type.
⇨ E.g. long double r = 53434.234 ,s; // Declared two double variables. One is initialized with
a value
5. Void type(void)
⇨ void type has no values. Can not declare variables of void type.
⇨ Used to specify type of functions in C.
⇨ Type of a function is said to be void when it does not return any value to the calling function.
⇨ E.g: void display(int a, int b)
⇨ Keyword used is void
Declaration of Variables
Creation of variables using available C data types is referred to as variable declaration.
⇨ General Syntax is:
data-type v1,v2,...vn;
where data-type is any available data type in C, and v1,v2....vn are variable names.
⇨ E.g for Variable declaration(different ways)
int count; // simple declaration of 1 variable name as count. Not initialized
int number, total; // Declaring 2 variables simultaneously
double d =3.25; //Declaring variable and initializing. Variable d now stores values 3.25
⇨ Older versions of C compilers insisted on declaring all the variables in a function at the
beginning of the function. But in recent versions of C Compilers (C99 onwards), variables
can be declared anywhere in the function, just before its first use.
⇨ You cannot use a variable before it is declared.
⇨ Can not declare two variables of the same name in one scope (e.g. in a function).
Food for thought: What might be the value of other variables such as count, number etc. which are
not explicitly initialized. ?
C Language allows programmers (users) to create new data types. This is commonly done by using
existing data types in the language. Two ways of creating user-defined data types
1. Type definition (typedefs)
⇨ This allows users to define an identifier that would represent an existing data type.
⇨ This identifier can be later used to declare variables.
⇨ General Syntax for Type definition is
typedef existing-type identifier;
⇨ typedef is a keyword of the language
⇨ existing-type is any existing type such as int, char etc.
⇨ identifier is the new user-defined data type.
E.g: typedef int mark;
⇨ Now mark is a user-defined data type having similar behavior as int data type.
15 (For non-commercial educational use only)
TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types
2. Enumerations(Enums):
This is another way of creating user-defined data types.
⇨ General Syntax is :
enum identifier {value1, value2,....value-n};
⇨ Here Enumeration constant Mon gets value 5, Tue gets 6,....Sun gets 11.
⇨ Enumeration helps to automatically link meaningful names to integer constants and can
improve readability of the program.
⇨ Think of creating seven variables and assigning values to the represent each day of the week
as in:
int mon=0,tue=1,wed=2,thu=4,fri=5,sat=6,sun=6; // The tedious way :-(
⇨ This is complex when we want to give meaningful names to more integer values, say, months
in a year or 24 different colors. Enumeration helps to avoid this.