Chapter 1: Variables, Constants, and Keywords:
Variables
A variable is a container that stores a ‘value.’ In the kitchen, we have containers storing rice, dal, sugar, etc. Similar to that variable in c stores the value of a constant. Example:
| a = 3 | a is assigned “3” |
| b = 4.7 | b is assigned “4.7” |
| c = 'A' | c is assigned “A” |
Rules for naming variables in c:
1. The first character must be an alphabet or underscore(_).
2. No commas or blanks are allowed.
3. No special symbol other than underscore is allowed
4. Variable names are case sensitive
Constants
An entity whose value doesn’t change is called a constant.
Types of constant
Primarily there are 3 types of constant:
| 1. Integer Constant | -1,6,7,9 |
| 2. Real Constant | -322.1,2.5,7.0 |
| 3. Character Constant | ‘a’,’$’,’@’(must be enclosed within single inverted commas) |
Keywords
These are reserved words whose meaning is already known to the compiler. There are 32 keywords available in c:
| auto | double | int | struct |
| break | long | else | switch |
| case | return | enum | typedef |
| char | register | extern | union |
| const | short | float | unsigned |
| continue | signed | for | void |
| default | sizeof | goto | volatile |
| do | static | if | while |
Our first C program
& is the “address of” operator, and it means that the supplied value should be copied to the address which is indicated by variable i.
Chapter 1: Practice Set:
Q1. Write a c program to calculate the area of a rectangle:
a) using hardcoded inputs &
b) using inputs supplied by the user
Q2. Calculate the area of a circle and modify the same program to calculate the volume of a cylinder given its radius and height.
Q3. Write a program to convert Celsius (Centigrade degrees temperature to Fahrenheit)
Q4. Write a program to calculate simple interest for a set of values representing principle, no of years, and rate of interest.
