1 of 12

Variables and constants, Input/output operations,

MINAKSHI SHARMA

2 of 12

Variables

Definition:�A variable is a storage location in memory with a specific name and type that holds data which can change during program execution.

  • Syntax:

​

data_type variable_name;

​

  • Example:

​

int age;

float salary;

3 of 12

Variables Contd.

​

Rules for Naming Variables:

​

  • Must begin with a letter or underscore ( _ )
  • Can contain letters, digits, and underscores (e.g., var1, name_123).
  • Cannot use reserved keywords (e.g., int, return).
  • Case-sensitive (Age and age are different).

​

Usage:

  • Variables are used to store and manipulate data.�
  • Invalid variable name Example:-
      • 12age
      • for
      • @age

​

4 of 12

Constants

​

  1. Definition:- A constant is a value that cannot be altered by the program once defined.
  2. Constants can be numeric, character, or string values.
  3. Types of Constants in C:
    • Integer constants: Whole numbers (e.g., 10, -42).
    • Floating-point constants: Decimal numbers (e.g., 3.14, -0.01).
    • Character constants: Single character enclosed in single quotes (e.g., 'A').
    • String constants: Sequence of characters enclosed in double quotes (e.g., "Hello").

​

5 of 12

Constants Contd.

Defining Constants:

    • Using #define (Preprocessor Directive)

​

#define PI 3.14159

    • Using const Keyword

​

const int MAX_AGE = 100;

​

​

Rules for Constants:

    • Values assigned to constants cannot be modified.
    • Use descriptive names for readability.

​

Usage:�Constants are used for fixed values in a program to improve readability and maintainability.

​

6 of 12

Difference Between Variable and Constant

7 of 12

Input Operations

Standard Input: scanf()

The scanf() function is used to take input from the user during the execution of the program.

​

​

Syntax:

​

scanf("format_specifier", &variable);

​

Explanation:-

​

  • format_specifier defines the type of input expected (e.g., %d for integers, %f for floats, %s for strings).
  • The & (ampersand) is used to pass the address of the variable to store the input value.

8 of 12

Input Operations Example

​

​

#include <stdio.h>

int main()

{

int age;

printf("Enter your age: ");

scanf("%d", &age); // Takes integer input printf("Your age is: %d\n", age);

}

9 of 12

Output Operations

​

​

Standard Output: printf()

The printf() function is used to display output on the screen.

​

Syntax:

​

printf(“msg”);

printf("format_specifier", value);

10 of 12

Input & Output Operations Example

​

​

#include <stdio.h>

int main()

{

int age;

printf("Enter your age: ");

scanf("%d", &age); // Takes integer input printf("Your age is: %d\n", age);

}

11 of 12

format_specifier

format_specifier determines how the value will be displayed.

  • %d for integers
  • %f for floating-point numbers
  • %s for strings
  • %c for characters

​

12 of 12

Thanks