Chapter 3: Constants, Variables and Data Types

📌 Learning Objectives
  • 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:

C programs are written using tokens. Tokens are the smallest individual units. C has six types of tokens:

3.2 Keywords and Identifiers

Keywords are reserved words with fixed meanings. They must be written in lowercase. ANSI C has 32 keywords:

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while

Identifiers are user-defined names for variables, functions, and arrays. Rules:

Valid IdentifiersInvalid Identifiers
First_name123 (starts with digit)
_countchar (keyword)
sum1price$ (special symbol)
distance_travelledgroup one (space not allowed)

3.3 Constants

Constants are fixed values that do not change during program execution.

TypeExamplesRemarks
Integer constant123, -321, 0Decimal (base 10)
Octal constant0370, 0435Starts with 0
Hexadecimal constant0x2A, 0X9FStarts with 0x or 0X
Real constant0.0083, -0.75, 435.36Decimal notation
Exponential constant2.1565e2, 3.18E-3Scientific 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;
}
Output:
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 Data Types

TypeKeywordSize (16-bit)Range
Characterchar1 byte-128 to 127
Integerint2 bytes-32,768 to 32,767
Floating pointfloat4 bytes3.4e-38 to 3.4e+38
Double precisiondouble8 bytes1.7e-308 to 1.7e+308
Voidvoid0No value

Integer Type Modifiers

We can use qualifiers to modify the range:

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;
💡 Note: Variables can be initialized at declaration:
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
⚠️ Important: C does not provide warning for integer overflow. It simply gives incorrect results. Always choose the correct data type.

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:

Chapter Exercises

Review Questions

  1. 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.
  2. What are trigraph characters? How are they useful?
  3. Describe the four basic data types. How could we extend the range of values they represent?
  4. What is the difference between declaring a variable and defining a symbolic name?
  5. What are enumeration variables? How are they declared?

Multiple Choice Questions

  1. Which of the following escape sequences moves the cursor to a new line?
    a) \n
    b) \r
    c) \t
    d) \v
  2. Which of the following is not a C language keyword?
    a) volatile
    b) enum
    c) unsigned
    d) go
  3. 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

  1. 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.
  2. 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.
  3. Write a program to count and print the number of negative and positive numbers in a given set. Terminate reading when 0 is encountered.
  4. Write a program to input an integer between 0 and 128 and print its ASCII character.

Interview Questions

  1. Is it possible to declare an identifier that starts with an underscore?
  2. What is the return type of printf function?
  3. What is the difference between declaring and defining a variable?
  4. What is long long int?