0% found this document useful (0 votes)
35 views

CUITM114 Lecture 3 Identifiers Variables

Uploaded by

convincenemapare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

CUITM114 Lecture 3 Identifiers Variables

Uploaded by

convincenemapare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

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

5) Spaces are not allowed in an identifier


Variables
• A variable is an identifier which names a storage area created
by a program in the computers memory.
• Each variable in C has a specific data type, which determines
the size and layout of the variable's memory; the range of
values that can be stored within that memory; and the set of
operations that can be applied to the variable.
Variable syntax
datatype variablename;

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

When we assign any initial value to a variable during the declaration,


it is called initialization of variables. When variable is declared but
contain undefined value then it is called garbage value. The variable
is initialized with the assignment operator such as
Syntax
Datatype variablename = constant;

• Eg: int number1=20; //note this is declaration and 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

scanf( "%d", &number1 ); // read number1 entered on the keyboard

• 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

• NB remember // this is just comment and it will not be executed


Example program use of scanf to get
input
#include <stdio.h>

// function main begins program execution


int main( )
{
int number1, number2,sum ; // variable declaration

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

sum = number1 + number2; // assign the total after addition to sum

printf (“ Sum = %d \n”, sum); // prints out sum on the screen

} // end function main


Exercise Try this

• Create a program using C that will accept 3 marks from the


user and it will calculate the total marks and then it will print
out the 3 marks entered and the total on the screen
NB Try out the sample programs and the Practical exercise exercises given so
that you master Programming . You can only understand this module better by
practice
Data types in C
Variables in C have an associated data type. Each data type
requires different amounts of memory and has some specific
operations which can be performed over it. The following are the
different data types used in C
Data types in C
• char: The most basic data type in C. It stores a single character
and requires a single byte of memory in almost all compilers.
• int: As the name suggests, an int variable is used to store an
integer.
• float: It is used to store decimal numbers (numbers with floating
point value) with single precision.
• double: It is used to store decimal numbers (numbers with
floating point value) with double precision.
• void: means no valueThis is usually used to specify the type of
functions which returns nothing
• NB The C language does not provide an inbuilt data type for
strings but it has an access specifier “%s” which can be used to
directly print and read strings. Strings are defined as an array of
characters
Example Use of string in a C program
// C program to read strings

#include<stdio.h>

int main()
{

char str[50]; // declaring string an array of characters

scanf("%s",str); // reading string entered from keyboard

printf(“This is the string entered by user : %s \n",str); // print string

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()
{

const int LENGTH = 10; // use of constants


const int WIDTH = 5;

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 .

You might also like