Introduction
Keywords, identifiers, variables, and constants are basic building blocks of C programming. Keywords have predefined meanings in the C language, identifiers are names given to program elements, variables store values that can change, and constants represent values that should not be changed during program execution. In this chapter, you will practice these concepts through simple programs covering naming rules, declarations, initialization, changing values, and using constants. C Keywords Identifiers Variables and Constants Practice Questions with Solutions to help you understand the concepts.
Q1. Declare and Initialize Variable in C
Problem Statement
Create variables to store a student’s age, marks, and grade, then display their values.
C Program
#include <stdio.h>
int main()
{
int age = 15;
float marks = 87.5;
char grade = 'A';
printf("Age = %d\n", age);
printf("Marks = %.1f\n", marks);
printf("Grade = %c", grade);
return 0;
}
Sample Output
Age = 15
Marks = 87.5
Grade = A
Concepts Covered
- Variable declaration
- Variable initialization
intfloatchar- Identifiers
Q2. Change the Value of a Variable in C
Problem Statement
Create a variable containing a number, change its value, and display both values.
C Program
#include <stdio.h>
int main()
{
int number = 10;
printf("Before change = %d\n", number);
number = 25;
printf("After change = %d", number);
return 0;
}
Sample Output
Before change = 10
After change = 25
Concepts Covered
- Variables
- Assignment
- Changing variable values
- Identifiers
Q3. Use Meaningful Identifier in C
Problem Statement
Create meaningful identifiers for the length, width, and area of a rectangle.
C Program
#include <stdio.h>
int main()
{
int length = 12;
int width = 5;
int area;
area = length * width;
printf("Length = %d\n", length);
printf("Width = %d\n", width);
printf("Area = %d", area);
return 0;
}
Sample Output
Length = 12
Width = 5
Area = 60
Concepts Covered
- Identifiers
- Meaningful variable names
- Variable declaration
- Assignment
- Arithmetic expression
Q4. Use a Constant With const
Problem Statement
Create a constant for the value of PI and use it to calculate the area of a circle.
C Program
#include <stdio.h>
int main()
{
const float PI = 3.14;
float radius = 5;
float area;
area = PI * radius * radius;
printf("Area of circle = %.2f", area);
return 0;
}
Sample Output
Area of circle = 78.50
Concepts Covered
- Constants
const- Variables
- Floating-point values
- Mathematical formulas
Q5. Try to Change a Constant
Problem Statement
Create a constant representing the maximum marks and observe what happens when you try to change its value.
C Program
#include <stdio.h>
int main()
{
const int MAX_MARKS = 100;
printf("Maximum Marks = %d\n", MAX_MARKS);
/*
MAX_MARKS = 150;
*/
return 0;
}
Sample Output
Maximum Marks = 100
Concepts Covered
const- Constant values
- Read-only variables
- Comments
The assignment
MAX_MARKS = 150;has been placed inside a comment so that the program can compile successfully. Aconstobject should not be modified after initialization.
Q6. Identify Valid and Invalid Identifiers
Problem Statement
Understand which names can be used as identifiers in C.
Valid Identifiers
studentName
student_age
marks1
totalMarks
_number
Invalid Identifiers
2marks
student-name
total marks
float
C Program
#include <stdio.h>
int main()
{
int studentAge = 15;
int student_marks = 85;
int marks1 = 90;
printf("Age = %d\n", studentAge);
printf("Student Marks = %d\n", student_marks);
printf("Marks 1 = %d", marks1);
return 0;
}
Sample Output
Age = 15
Student Marks = 85
Marks 1 = 90
Concepts Covered
- Valid identifiers
- Invalid identifiers
- Underscore
- Digits in identifiers
- Naming rules
Q7. Use a Keyword Correctly
Problem Statement
Use the C keyword int to declare an integer variable.
C Program
#include <stdio.h>
int main()
{
int number = 50;
printf("Number = %d", number);
return 0;
}
Sample Output
Number = 50
Concepts Covered
- Keywords
int- Variable declaration
- Identifier
- Integer value
Q8. Use Multiple Variables With Different Values
Problem Statement
Create variables to store the details of a product: product ID, price, and availability.
C Program
#include <stdio.h>
int main()
{
int productId = 101;
float price = 499.99;
char availability = 'Y';
printf("Product ID = %d\n", productId);
printf("Price = %.2f\n", price);
printf("Available = %c", availability);
return 0;
}
Sample Output
Product ID = 101
Price = 499.99
Available = Y
Concepts Covered
- Variables
- Identifiers
intfloatchar- Format specifiers
Q9. Use a Constant for a Fixed Tax Rate
Problem Statement
Create a constant tax rate and calculate the tax amount for a product priced at ₹1,000.
C Program
#include <stdio.h>
int main()
{
const float TAX_RATE = 18.0;
float price = 1000;
float tax;
tax = (price * TAX_RATE) / 100;
printf("Price = %.2f\n", price);
printf("Tax = %.2f", tax);
return 0;
}
Sample Output
Price = 1000.00
Tax = 180.00
Concepts Covered
- Constants
const- Variables
- Arithmetic expressions
- Percentage calculation
Q10. Combine Variables and Constants in a Practical Program
Problem Statement
Create a program that stores a student’s name, marks in three subjects, and a constant representing the maximum marks for each subject. Calculate the total marks and percentage.
C Program
#include <stdio.h>
int main()
{
char studentName[] = "Rahul";
int mathMarks = 85;
int scienceMarks = 78;
int computerMarks = 92;
const int MAX_MARKS = 100;
const int SUBJECTS = 3;
int totalMarks;
float percentage;
totalMarks = mathMarks + scienceMarks + computerMarks;
percentage = (totalMarks * 100.0) / (MAX_MARKS * SUBJECTS);
printf("Student Name: %s\n", studentName);
printf("Math Marks: %d\n", mathMarks);
printf("Science Marks: %d\n", scienceMarks);
printf("Computer Marks: %d\n", computerMarks);
printf("Total Marks: %d\n", totalMarks);
printf("Percentage: %.2f%%", percentage);
return 0;
}
Sample Output
Student Name: Rahul
Math Marks: 85
Science Marks: 78
Computer Marks: 92
Total Marks: 255
Percentage: 85.00%
Concepts Covered
- Identifiers
- Variables
- Constants
const- Character arrays
- Arithmetic calculations
- Percentage calculation
- Multiple data values
Key Takeaways
- Keywords in C are reserved words with predefined meanings in C.
- Identifiers in C are names used for variables, functions, arrays, and other program elements.
- Identifiers cannot normally start with a digit.
- Spaces and special characters such as
-cannot be used in ordinary identifiers. - Keywords in C cannot be used as an identifier.
- C identifiers are case-sensitive.
- Variable in C store values that can be changed during program execution.
- The
constkeyword can be used to declare an object whose value should not be modified through that object. - Meaningful identifiers make programs easier to understand.
- Constants are useful for fixed values such as maximum marks, tax rates, and mathematical values.
FAQs
1. What is keywords in C?
Keywords are reserved word that has a predefined meaning in the C language. Examples include int, float, if, return, and while.
2. What is an identifier in C?
An identifier is a name given to a program element such as a variable, function, array, or structure.
For example:
int studentAge;
Here, studentAge is an identifier.
3. What are the rules for naming identifiers in C?
An identifier can contain letters, digits, and underscores. It cannot start with a digit, cannot contain spaces or characters such as -, and cannot be a C keyword.
4. Are C identifiers case-sensitive?
Yes. C treats uppercase and lowercase letters as different.
For example:
age
Age
AGE
are three different identifiers.
5. What is a variable in C?
A variable is a named storage location used to hold a value that can change during program execution.
Example:
int age = 15;
6. What is a constant in C?
A constant is a value that is intended to remain unchanged during a program’s execution. The const keyword can be used when declaring an object whose value should not be modified.
Example:
const int MAX = 100;
7. Can a keyword be used as a variable name in C?
No. Reserved keywords such as int, return, if, and while cannot be used as ordinary identifiers.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
