C PROGRAMING
Variables
Constants
Symbolic Constants
VARIABLES
Variables are containers for storing data values,
like numbers and characters.
In C, there are different types of variables
(defined with different keywords), for example:
int - stores integers (whole numbers), without
decimals, such as 123 or -123
float - stores floating point numbers, with
decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'.
Characters are surrounded by single quotes
Creating Variables
To create a variable, specify the type and
assign it a value:
Syntax
type variableName = value;
Example:
Create a variable called myNum of
type int and assign the value 15 to it:
int myNum = 15;
FORMAT SPECIFIERS
Format specifiers are used together with
the printf() function to tell the compiler what type
of data the variable is storing. It is basically
a placeholder for the variable value.
A format specifier starts with a percentage sign %,
followed by a character.
For example, to output the value of an int variable,
use the format specifier %d surrounded by double
quotes (""), inside the printf() function:
Example:
int myNum = 15;
printf("%d", myNum); // Outputs 15
Change Variable Values
If you assign a new value to an existing
variable, it will overwrite the previous
value:
Example
int myNum = 15; // myNum is 15
myNum = 10; // Now myNum is 10
DECLARE MULTIPLE VARIABLES
To declare more than one variable of the
same type, use a comma-
separated list:
Example
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
Out put: 61
REAL LIFE EXAMPLES
Often in our examples, we simplify
variable names to match their data type
(myInt or myNum for int types, myChar
for char types, and so on). This is done to
avoid confusion.
However, for a practical example of using
variables, we have created a program that
stores different data about a college
student
EXAMPLE
#include <stdio.h>
int main() {
// Student data
int studentID = 15;
int studentAge = 23;
float studentFee = 75.25;
char studentGrade = 'B';
// Print variables
printf("Student id: %d\n", studentID);
printf("Student age: %d\n", studentAge);
printf("Student fee: %f\n", studentFee);
printf("Student grade: %c", studentGrade);
return 0;
}
OUT PUT:
Student id: 15
Student age: 23
Student fee: 75.250000
Student grade: B
CONSTANTS
If you don't want others (or yourself) to
change existing variable values, you can
use the const keyword.
This will declare the variable as "constant",
which means unchangeable and read-
only:
Example
const int myNum = 15;
myNum = 10; // error: assignment of read-
only variable 'myNum'
Example
You should always declare the variable as
constant when you have values that are
unlikely to change:
Example
const int minutesPerHour = 60;
const float PI = 3.14
SYMBOLIC CONSTANTS
Identifiers are used to represent fixed values
in programs using symbolic constants in the C
programming language. These parameters are
frequently used to increase
the readability, maintainability,
and modifiability of code, which may be
numerical or not. The C
language's "#define" command is used to
declare symbolic constants.
Syntax:
Syntax for defining a symbolic constant in C:
#define MAX_V 100
continu
In this example, the symbolic
constant "MAX_V" is specified to have a
value of 100. It indicates that
the number 100 will be substituted
for "MAX_V" wherever it appears in the
code at compilation time.