C Programing
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
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
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