CUITM114 Lecture 3 Identifiers Variables
CUITM114 Lecture 3 Identifiers Variables
and Constants
What is an Identifier
In C an identifier is a name used to identify a variable, function,
or any other user-defined item.
Rules of naming Identifiers
1) An identifier should only consists of alphabetic letters (both
upper and lower case from
2) The first character of an identifier should be alphabet or
underscore not a digit
3) An identifier should not be a keyword such as int, printf etc
What is an Identifier
4) C is a case sensitive, the upper case and lower case are
different, for example Aecs103, AECS103, aecs103 etc. are
different identifiers
Eg
int number1, number2;
double salary,price;
char ch, firstname ;
The rules of naming variables are the same as the rules of
naming an identifier since a variable is an identifier
Variable declaration
A variable should be declared first before its used in a program .A
declaration specifies the variable name and its data type
eg int number1;
double price;
Variable initialisation
Or
int number1;
number1=20;
Example program
Example Program on use of Variables
#include <stdio.h>
int main() {
int number1, number2, sum; //variable declaration
number1 =30; // variable initialisation
number2 = 40;
sum = number1 + number2;
printf ( “The result of adding the two numbers = %d\n”,sum);
return 0;
}
Getting input from the user
• Suppose we want to add 2 numbers number1 and number2 and the values of
the 2 numbers we want them to be entered by the user we use the scanf
function
• Eg
• The above statement uses scanf (the “f” stands for “formatted”) to obtain a
value from the user. The function reads from the standard input, which is
usually the keyboard.
• This scanf has an argument,"%d" . This format control string, indicates the type
of data that should be entered by the user. The %d conversion specifier
indicates that the data should be an integer
• Lets look at an example program
printf( "Enter first number\n" ); // prompt the user to enter first number
scanf(“%d”, &number1) ; //reads input from user and assign value to variable number1
printf( "Enter second number\n" ); // prompts the user to enter second number
scanf(“%d”, &number2) ; //reads input from user and assign value to variable number2
#include<stdio.h>
int main()
{
return 0;
}
Data Types in C
Data Type Range Format specifier
int 2,147,483,648 to %d
2,147,483,647
short int -32,768 to 32,767 %hd
long int -2,147,483,648 to %ld
2,147,483,647
float 1.2E-38 to 3.4E+38 %f
6 decimal places
double 2.3E-308 to 1.7E+308 %lf
15 decimal places
Long double 3.4E-4932 to 1.1E+4932 %Lf
19 decimal places
char %c
Constants
• Constants refer to fixed values that the program may not alter
during its execution. These fixed values are also called literals.
• Constants can be of any of the basic data types like an integer
constant, a floating constant, a character constant, or a string
literal.
• The constants are treated just like regular variables except that
their values cannot be modified after their definition
Defining Constants
• There are two simple ways in C to define constants:
• 1. Using #define preprocessor.
• 2. Using const keyword.
• Eg
#define PI 3.1428
Or const float PI = 3.1428
Use of constants in a program
#include <stdio.h>
int main()
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area);
return 0;
}
Exercise
• Using a constant PI = 3.1428 . Write a program that will find
the area on any circle .