CSC 102 - 044247
CSC 102 - 044247
Variables in C
Topics
Why programming?
Naming Variables
Declaring Variables
Using Variables
The Assignment Statement
Programming – Why?
Computers are used for many different purposes in
many different situations.
But, how can they be so versatile?
Answer: They can be programmed
#define PI 3.14159
#define AGE 52
Case Sensitivity
C is case sensitive
It matters whether an identifier, such as a variable name, is
uppercase or lowercase.
Example:
area
Area
AREA
ArEa
are all seen as different variables by the compiler.
Which Are Legal Identifiers?
AREA area_under_the_curve
3D num45
Last-Chance #values
x_yt3 pi
num$ %done
lucky***
Declaring Variables
Before using a variable, you must give the compiler
some information about the variable; i.e., you must
declare it.
The declaration statement includes the data type of
the variable.
Examples of variable declarations:
int meatballs ;
float area ;
Declaring Variables (con’t)
When we declare a variable
Space is set aside in memory to hold a value of the specified
data type
That space is associated with the variable name
That space is associated with a unique address
Visualization of the declaration
int meatballs ;
More About Variables
C has three basic predefined data types:
length
int length = 7 ; 7
diameter
float diameter = 5.9 ; 5.9
initial
char initial = ‘A’ ;
‘A’
Using Variables: Initialization (con’t)
Do not “hide” the initialization
put initialized variables on a separate line
a comment is always a good idea
Example:
int height ; /* rectangle height */
int width = 6 ; /* rectangle width */
int area ; /* rectangle area */
Examples:
diameter = 5.9 ;
area = length * width ;
Example: Declarations and Assignments
#include <stdio.h>
int main( )
{
int area, length, breadth;
length = 7 ;
breadth = 6 ;
area = length * breadth;
printf (“The Area of a rectangle: \n”) ;
printf (“ %d length \n”, length) ;
printf (“ %d breadth \n”, breadth) ;
printf (“ %d area \n”, area) ;
return 0 ;
}
Enhanced Program
#include <stdio.h>
int main ( )
{
float inches, feet, fathoms ;
int main() {
int x, y;
float salary = 13.48;
char letter = 'K’;
x = 25;
y = 34;
int z = x+y;
printf("%d \n", z);
printf("%f \n", salary);
printf("%c \n", letter);
return 0;}
Constants
Constants are the fixed values that never change during the execution of a
program. Following are the various types of constants:
Integer constants
An integer constant is nothing but a value consisting of digits or numbers.
These values never change during the execution of a program. Integer
constants can be octal, decimal and hexadecimal.
1.Decimal constant contains digits from 0-9 such as,
Example, 111, 1234
2.Octal constant contains digits from 0-7, and these types of constants are
always preceded by 0.
Example, 012, 065
The octal and hexadecimal integer constants are very rarely used in
programming with ‘C’.
Character constants
A character constant contains only a single character enclosed within a
single quote (”). We can also represent character constant by providing
ASCII value of it.
Example, 'A', '9'
String constants
A string constant contains a sequence of characters enclosed within
double quotes (“”).
Example, "Hello", "Programming"
Real Constants
‘C’ also provides real constants that contain a decimal point or a
fraction value. The real constants are also called as floating point
constants. The real constant contains a decimal point and a fractional
value.
Example, 202.15, 300.00
For example, to declare a value that does not change like the classic circle
constant PI, there are two ways to declare this constant:
1.By using the const keyword in a variable declaration which will reserve a
storage memory
#include <stdio.h>
int main() {
const double PI = 3.14;
printf("%f", PI);
//PI++;
// This will generate an error as constants cannot be changed return 0;
}
2.By using the #define pre-processor directive which doesn’t use memory for
storage and without putting a semicolon character at the end of that statement
#include <stdio.h>
#define PI 3.14
int main() {
printf("%f", PI);
return 0;}