0% found this document useful (0 votes)
22 views16 pages

Unit 1 - II Consts Vars and Data Types v1.5

This document provides an overview of constants, variables, and data types in C programming. It discusses the different types of constants like integer, real, character, and string constants. It also covers variable declaration rules and the primary data types like integer, character, floating point, and void. Finally, it discusses declaration of variables and user-defined data types.

Uploaded by

Thomas Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

Unit 1 - II Consts Vars and Data Types v1.5

This document provides an overview of constants, variables, and data types in C programming. It discusses the different types of constants like integer, real, character, and string constants. It also covers variable declaration rules and the primary data types like integer, character, floating point, and void. Finally, it discusses declaration of variables and user-defined data types.

Uploaded by

Thomas Mathew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

TM's

Lecture Notes in C Programming (LNCP)


UNIT 1 Part 2
Constants, Variables, and Data Types
Ver1.5
TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

Table of Contents
​Structure of a C Program: 3

​The Famous “Hello World” Program in C 5

​C Character Set: 5

​Jump Ahead: Addition of user entered numbers 6

​C Tokens 6
​Token Classes in C 6

​Keywords and Identifiers 7


​Keywords 7
​Identifiers 7

​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

​User-defined Data Types 15

2 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

​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.

4. Global Declaration Section


The global variables that can be used anywhere in the program are declared in the global declaration
section. This section also declares the user defined functions.

5. main() Function Section


It is necessary to have one main() function section in every C program. This section contains two parts,
declaration part and executable part.

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

3 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

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.

An example program with all the above sections:

4 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

​The Famous “Hello World” Program in C

​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.

The characters in the C are grouped into the following categories:


⇨ Letters : Uppercase A…Z, Lowercase a…z
⇨ Digits : All decimal digits 0 to 9
⇨ White space Characters: Blank space (' '), new line(\n), horizontal tab(\t), carriage return(\r)
and form feed(\f).
⇨ Special Character : ,(comma) .(period) ; (semicolon) :(colon), & (ampersand), # (number
sign or hash), ( (left parenthesis), [ (left bracket), { (Left brace) etc. See the table for complete
list.

All Special Characters


Symbol Meaning
~ Tilde ) Right parenthesis } Right brace
! Exclamation mark _ Underscore [ Left bracket
# Number sign + Plus sign ] Right bracket
$ Dollar sign | Vertical bar : Colon
% Percent sign \ Backslash ” Quotation mark

5 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

^ Caret ` Apostrophe ; Semicolon


& Ampersand – Minus sign < Opening angle
bracket
* Asterisk = Equal to sign > Closing angle
bracket
( Left parenthesis { Left brace ? Question mark
, Comma
. Period
/ Slash

​Jump Ahead: Addition of user entered numbers

​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.

6 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

​Keywords and Identifiers


Keywords
Keywords are the reserved words in C whose meanings are fixed by the language.
⇨ Keywords are case sensitive. i.e. while and While are not the same.
⇨ In C, All keywords are of lowercase.
⇨ Keywords in C are analogous to words of a natural language like English.
⇨ Keywords cannot be used as variable names. ( E.g. int char = 10; is not allowed in C since
char is keyword)
⇨ All keywords are made of C Character set only.

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

const float short unsigned

continue for signed void


default goto sizeof volatile
do if static while

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.

7 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

2. The first letter of an identifier should be either a letter or an underscore. However, it is


discouraged to start an identifier name with an underscore.
3. There is no rule on the length of an identifier. However, the first 31 characters of identifiers
are discriminated by the compiler.(Depends on the compiler)
4. Cannot use a keyword as an identifier.
5. Identifiers must not contain white spaces. ( student name is is not valid identifier whereas
student_name is valid)

Good Programming Practice

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.)

Exercise: Which are the valid identifiers in C in the following??


9Sum
_xyz
result
Result
total amount
loss$amount

​CONSTANTS
Constants are fixed values that do not change during execution of the program.
E.g: 5, 100, “hello” , 14.52, ‘A’, ‘b’

Constants are classified into different types:

8 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

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.

2. Real Constants (Floating-point constants)

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.

⇨ General form :mantissa e exponent

⇨ The mantissa is either a real number or integer number.

⇨ Exponent is an integer number with + or – sign.

⇨ 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

3. Character constants (Single character constants)


A character constant is a constant which uses single quotation around characters. For example: 'a', 'l',
'm', 'F'

9 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

4. String constants
String constants are the constants which are enclosed in a pair of double-quote marks. For example:

"good" //string constant


"" //null string constant
" " //string constant of six white spaces
"x" //string constant having a single character.
"Earth is round\n" //prints string with newline

5. Escape Sequences (Backslash character constants)

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.

Escape Sequences Character


\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\' Single quotation mark
\" Double quotation mark
\0 Null character

​Variables
A variable is a data name in the program that may be used to store a data value.

⇨ Variable names fall into the category of Identifiers.


⇨ Unlike constants, which remain unchanged during the execution of the program, a variable
may take different values at different times.

10 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

Rules to declare/name variables


⇨ They must begin with a letter or underscore(_) .
⇨ Remaining characters may be letters,digits,underscore..
⇨ Both lowercase and uppercase letters are distinct. Example: Total and total two different
variable names.
⇨ Variable names should not be a keyword in C.
⇨ White space, #, @ etc. are not allowed in variable names.
Note: recent compiler versions support $ also in variable names.

Example: John, delhi, First_tag, int_type ==> valid variable names


Price@store, char, group one, 123, (area) ==> Invalid variable names

​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 data type categories supported by ANSI C are:


⇨ Primary(Fundamental) data types
⇨ Derived Data types
⇨ User-defined data types

Primary Data Types

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)

11 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

Primary data types in C with its variants

Data type Range of Values


char (1 Byte) -128 to 127
int (2 Bytes) -32,768 to 32,767
float (4 Bytes) 3.4e-38 to 3.4e+38
double (8 Bytes) 1.7e-308 to 1.7e+308
Size and Range of Basic Data Types on a typical 16-bit Machine

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.

12 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

Relative sizes of integer variants.


⇨ Size of a data type determines the range of values that can be stored in the variables of
that data type.
⇨ By using equation -2^n to + (2^n)-1 we can find out the range of signed integer(int) data
type. Where n is the number of bits available for storing the data part (magnitude) of the
number(Not the sign).
⇨ For unsigned numbers, the formula is 0 to + (2^n)-1. Here n is the size of the data type in
bits.
⇨ For e.g., On a typical 16-bit computer system, an int has the size 2 bytes( i.e. 16 bits).
⇨ On this system, For signed integers, n = 15 since 1 bit is used for storing the sign. Hence the
range is -2^15 to + (2^15)-1 (i.e -32768 to +32767)
⇨ For unsigned integers, n = 16. Hence the range of values is 0 to + (2^16)-1 (i.e . 0 to
65535)
⇨ Then the range of values supported by different Integer types is as shown in Table.

Type Size(bits) Range Range(Value)


int or signed int 16 -215 to 215 - 1 -32768 to
+32767
unsigned int 16 0 to to 216 - 1 0 to 65535
short int or 8 -27 to 27 - 1 -128 to + 127
signed short int
unsigned short int 8 0 to to 28 - 1 0 to 255
long int or 32 -231 to 231 - 1 -2,147,483,648
signed long int to
2147,483,647
unsigned short int 32 0 to to 232 - 1
2. Character Type (char):
13 (For non-commercial educational use only)
TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

⇨ 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.

3. Floating Point (float):


⇨ Used to represent real numbers.
⇨ Floating point numbers are defined in C by the keyword float. Typical size of float type is 32
bits(4 bytes)
⇨ E.g. float x = 5.2 ,y; // Declared two float variables. One is initialized with a value.
⇨ In fact, A floating point constant is represented as 5.2f ( with a suffix f). Just 5.2 is treated as
a double type by the compiler. In practice, this difference hardly matters.

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

Relative sizes of float type variants.

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

Format Specifier for different Data types ( w.r.t GNU C Compiler(GCC) )


short int ==> %hi or %hd
unsigned short int ==> %hu
char ==> %c
int ==> %d

14 (For non-commercial educational use only)


TM’s C Programming Lecture Notes v1.5 Constants, Variables and Data Types

unsigned int ==> %u


long int ==> %ld
unsigned long int ==> %lu
float ==> %f
double ==> %lf
long double ==>%Lf

​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. ?

​User-defined Data Types (Optional topic)


(Advanced topic at this stage)

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

⇨ Mark can be used to create variables as follows:


mark m1=10, m2=45; // m1 & m2 are variables of mark type.
⇨ Main advantage of typedef is that we can create meaningful data type names for
improving the readability of the program

2. Enumerations(Enums):
This is another way of creating user-defined data types.
⇨ General Syntax is :
enum identifier {value1, value2,....value-n};

⇨ enum is a keyword in C used to create enumerated data types


⇨ Here identifier is the new data type. This identifier can be used to create variables and those
variables can hold any values listed in braces such as value1,value2,...value-n.
⇨ value1,value2,...value-n are called as Enumeration constants.
⇨ They are automatically assigned with some values starting from 0. i.e value1 gets 0, value2
gets 1 and so on.
⇨ Examples
enum day{Mon,Tue,Wed,Thu,Fri,Sat,Sun}; //Enum type day is declared
enum day d1,d2; // Variables of day type
d1 = Mon; // Values assigned to variable d1
d2 = Thu; // Values assigned to variable d2
⇨ Here Enumeration constant Mon gets value 0, Tue gets 1,....Sun gets 6.
⇨ It is possible to assign a different range of values for enum constants other than 0,1,2,....

enum day{Mon =5 ,Tue,Wed,Thu,Fri,Sat,Sun};

⇨ 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.

16 (For non-commercial educational use only)

You might also like