Constants, Variables and Keywords- part 5.

Constants, Variables and Keywords- part 5.

play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

Constants, Variables, and Keywords – Part 5

 1. Constants:

Constants are fixed values that do not change during the execution of a program. They can be of various types, like integer constants, floating-point constants, character constants, and string constants.



Examples:

  • Integer Constant: 5, -10, 100
  • Floating-point Constant: 3.14, -0.99, 2.5e3
  • Character Constant: 'A', 'b', '$'
  • String Constant: "Hello", "12345"

 2. Variables:

Variables are memory locations that hold data and whose value can change during program execution.

  • They should have meaningful names.
  • The value stored in a variable can be updated.

Declaration and Initialization:

# Python example
age = 25 # Variable 'age' holding an integer value
name = "Alice" # Variable 'name' holding a string value
c
// C example
int age = 25; // Variable 'age' holding an integer value
char name[] = "Alice"; // Variable 'name' holding a string value

 3. Keywords:

Keywords are reserved words that have special meanings in programming languages. They cannot be used as variable names.

Examples of Keywords:

  • In Python: if, else, while, for, def, return, break
  • In C/C++: int, float, char, if, else, while, return, void
  • In Java: class, static, public, void, new, try, catch

 Differences Summary:

Feature Constants Variables Keywords
Definition Fixed values Changeable data Reserved words
Changeable No Yes No
Example 3.14, "Hello" age = 25, name = "Bob" int, for, while

 Quick Exercise:

  1. Identify constants, variables, and keywords in the following line:
    count = 10
    while count > 0:
    print("Hello")
    count -= 1
  2. Write any 3 keywords of C language.

अगर आपको किसी और टॉपिक पर जानकारी चाहिए या और प्रैक्टिस करनी है, तो बताएं!

Constants, Variables and Keywords- part 5.

5. Variables and Constants

Introduction to C

Here is Part 5 of the “Getting Started with C Programming” series, focusing on:

✅ Constants, Variables, and Keywords in C


🔹 1. What are Constants in C?

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

🧱 Types of Constants:

Type Example Description
Integer Constant 100, -25 Whole numbers
Floating Constant 3.14, -0.5 Decimal numbers
Character Constant 'A', '9' Single characters in single quotes
String Constant "Hello" Group of characters in double quotes

✅ Example:

c
const int age = 18;
printf("Age = %d", age); // Age = 18

📝 const keyword makes a variable read-only (cannot be modified later).


🔹 2. What are Variables in C?

A variable is a named location in memory used to store data temporarily.

💡 Syntax:

c
data_type variable_name;

🧪 Example:

c
int marks = 95;
float pi = 3.14;
char grade = 'A';

📌 Rules for Naming Variables:

  • Must begin with a letter (A–Z or a–z) or underscore (_).

  • No spaces or special characters.

  • Case-sensitive (Totaltotal).

  • Avoid using keywords as variable names.


🔹 3. What are Keywords in C?

Keywords are reserved words with special meaning. They cannot be used as variable names.

📘 Examples of Keywords in C:

cpp
int, float, return, if, else, while, for, break, continue, const, void, char, double

🚫 Example:

c
int return = 5; // ❌ Error! "return" is a keyword

🧠 Quick Recap:

Concept Purpose Example
Constant Fixed value const int a = 10;
Variable Storage for changing data float height = 5.7;
Keyword Reserved words in C language if, int, while

📌 Practice Questions:

  1. Which of the following is a valid variable name?
    a) 1value
    b) value1
    c) float
    d) value#

✅ Correct Answer: b) value1

  1. What will happen if you try to change the value of a const variable?

✅ It will give a compile-time error.


Would you like a quiz, worksheet, or Part 6: Data Types in C next?

unit-2 – constant,variable & data types

Constants, Variables and Keywords- part 5.

Variables and Constants – Pearson Higher Education



Leave a Reply

Your email address will not be published. Required fields are marked *

error: