Chapter 3: Constants, Variables and Data Types
- LO 3.1: Know the C character set and keywords
- LO 3.2: Describe constants and variables
- LO 3.3: Identify the various C data types
- LO 3.4: Discuss how variables are used in a program
- LO 3.5: Explain how constants are used in a program
3.1 Character Set and C Tokens
The C character set consists of:
- Letters: A-Z, a-z
- Digits: 0-9
- Special characters: + - * / % = & | ! ~ ^ ? : ; , . ( ) { } [ ] < > #
- White spaces: Space, tab, newline
C programs are written using tokens. Tokens are the smallest individual units. C has six types of tokens:
- Keywords
- Identifiers
- Constants
- Strings
- Operators
- Special symbols
3.2 Keywords and Identifiers
Keywords are reserved words with fixed meanings. They must be written in lowercase. ANSI C has 32 keywords:
Identifiers are user-defined names for variables, functions, and arrays. Rules:
- First character must be a letter or underscore
- Only letters, digits, or underscore are allowed
- Only first 31 characters are significant
- Cannot use a keyword
- Case-sensitive (total, Total, TOTAL are different)
| Valid Identifiers | Invalid Identifiers |
|---|---|
| First_name | 123 (starts with digit) |
| _count | char (keyword) |
| sum1 | price$ (special symbol) |
| distance_travelled | group one (space not allowed) |
3.3 Constants
Constants are fixed values that do not change during program execution.
| Type | Examples | Remarks |
|---|---|---|
| Integer constant | 123, -321, 0 | Decimal (base 10) |
| Octal constant | 0370, 0435 | Starts with 0 |
| Hexadecimal constant | 0x2A, 0X9F | Starts with 0x or 0X |
| Real constant | 0.0083, -0.75, 435.36 | Decimal notation |
| Exponential constant | 2.1565e2, 3.18E-3 | Scientific notation |
| Character constant | 'a', '5', ' ' | Single quote, integer value (ASCII) |
| String constant | "Hello", "1987" | Double quotes |
| Backslash constants | '\n', '\t', '\0' | Escape sequences |
📝 Worked-Out Problem 3.1
Representation of integer constants on a 16-bit computer:
#include <stdio.h>
int main() {
printf("Integer values\n\n");
printf("%d %d %d\n", 32767, 32768, 32769);
printf("%ld %ld %ld\n", 32767L, 32768L, 32769L);
return 0;
}
32767 -32768 -32767
32767 32768 32769
On a 16-bit machine, the maximum integer value is 32767. Larger values overflow and are not stored correctly unless qualified as long.
3.4 Data Types
ANSI C supports three classes of data types:
- Primary (fundamental) data types: int, char, float, double, void
- Derived data types: arrays, functions, pointers
- User-defined data types: structures, unions, enum, typedef
Primary Data Types
| Type | Keyword | Size (16-bit) | Range |
|---|---|---|---|
| Character | char | 1 byte | -128 to 127 |
| Integer | int | 2 bytes | -32,768 to 32,767 |
| Floating point | float | 4 bytes | 3.4e-38 to 3.4e+38 |
| Double precision | double | 8 bytes | 1.7e-308 to 1.7e+308 |
| Void | void | 0 | No value |
Integer Type Modifiers
We can use qualifiers to modify the range:
- short int: smaller range (usually 2 bytes)
- long int: larger range (usually 4 bytes)
- unsigned int: only positive values (range 0 to 65,535 for 16-bit)
- signed int: explicitly signed (default)
3.5 Declaration of Variables
Variables must be declared before use. Syntax:
data_type variable_list;
Examples:
int count; float price, discount; double ratio; char grade;
int count = 0; float price = 99.95; char grade = 'A';
3.6 User-Defined Type Declaration (typedef)
We can create new type names using typedef:
typedef int units; typedef float marks; units batch1, batch2; // batch1, batch2 are int marks maths, science; // maths, science are float
3.7 Declaring a Variable as Constant
Use the const qualifier to make a variable read-only:
const int class_size = 40; // class_size cannot be modified
3.8 Defining Symbolic Constants (#define)
Symbolic constants are defined using the #define preprocessor directive:
#define PI 3.14159 #define STRENGTH 100 #define PASS_MARK 50
Rules:
- No blank space between # and define
- # must be the first character in the line
- No semicolon at the end
- Symbolic names are written in CAPITALS by convention
Chapter Exercises
Review Questions
- State whether the following statements are true or false:
a) All variables must be given a type when they are declared.
b) ANSI C treats the variables 'name' and 'Name' as same.
c) A global variable is also known as external variable. - What are trigraph characters? How are they useful?
- Describe the four basic data types. How could we extend the range of values they represent?
- What is the difference between declaring a variable and defining a symbolic name?
- What are enumeration variables? How are they declared?
Multiple Choice Questions
- Which of the following escape sequences moves the cursor to a new line?
a) \n
b) \r
c) \t
d) \v - Which of the following is not a C language keyword?
a) volatile
b) enum
c) unsigned
d) go - Which of the following is the correct way of specifying long signed integer data type?
a) signed long int
b) long int
c) unsigned long int
d) Both a and b
Debugging Exercises
Find errors, if any, in the following declaration statements:
Int x; float letter, DIGIT; double -p, q; short char c; long int m, count; long float temp;
Programming Exercises
- Write a program to determine and print the sum of the harmonic series: 1 + 1/2 + 1/3 + ... + 1/n. The value of n should be given interactively.
- The price of one kg of rice is Rs. 16.75 and one kg of sugar is Rs. 15.00. Write a program to get these values from the user and display the prices.
- Write a program to count and print the number of negative and positive numbers in a given set. Terminate reading when 0 is encountered.
- Write a program to input an integer between 0 and 128 and print its ASCII character.
Interview Questions
- Is it possible to declare an identifier that starts with an underscore?
- What is the return type of printf function?
- What is the difference between declaring and defining a variable?
- What is long long int?