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

Lecture 04 C Variables and Constants

The document discusses variables and constants in C programming. It explains that C programs typically include variable declarations and constants. Variables store values in memory locations and must be declared with a name and data type. The document outlines the rules for naming variables in C and explains how to declare and define variables. It also discusses using format specifiers to work with different data types and how to take user input and print output using variables.

Uploaded by

Must Boy
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)
44 views

Lecture 04 C Variables and Constants

The document discusses variables and constants in C programming. It explains that C programs typically include variable declarations and constants. Variables store values in memory locations and must be declared with a name and data type. The document outlines the rules for naming variables in C and explains how to declare and define variables. It also discusses using format specifiers to work with different data types and how to take user input and print output using variables.

Uploaded by

Must Boy
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/ 15

Variables and

Constants

© G.Y. Namani Variables & Constants Slide 1


C program structure
• Virtually all structured programs, including C
programming share a similar overall
structure:
• Statements to establish the start of the program
e.g. #include directives.
• Variable declarations and constants
• Program control statements (blocks of code) e.g.
Conditional statements (if, if… else, etc.), loops
(for loop, while loop, etc.)

© G.Y. Namani Variables & Constants Slide 2


Statements to establish the
start of the C program
• The #include directives or
preprocessor headers are used to start
C.
• The next to the directives is the main
function, named main().
• From here is where open curly bracket
({) shows the beginning of our
program.

© G.Y. Namani Variables & Constants Slide 3


Variables
• A variable is a location in memory where a
value can be stored for use by a program.
• Example:
• int integer1;
• int integer2;
• int sum;
• These definitions specify that the variables
integer1, integer2 and sum are of type int.

© G.Y. Namani Variables & Constants Slide 4


Variable names
• All variable definitions must include
two things variable name and its data
type.
• To indicate the memory location, each
variable should be given a unique
name called identifier.
• To declare any variable in C language
you need to follow rules of C language.

© G.Y. Namani Variables & Constants Slide 5


Rules for C variable names
1. Only alphabet, digits and underscore can
be used to name a variable.
2. The first character in a variable name
must be an alphabet or underscore (_)
3. No comma or blanks are allowed within a
variable name.
4. No special symbol other than underscore
is allowed in naming a variable.
© G.Y. Namani Variables & Constants Slide 6
Rules for C variable names cont…
5. C keywords like int, float, struct, if,
while, etc. cannot be used as variable
names.
6. Every variable name always should exist
on the left hand side of assignment
operator.

© G.Y. Namani Variables & Constants Slide 7


Variable declaration
• Before we use a variable in C we must
declare it.
• We must identify what kind of
information will be stored in it. This is
called defining a variable.
• Variables must be declared at the start
of any block of code, but most are
found at the start of each function.
© G.Y. Namani Variables & Constants Slide 8
Format specifiers
• C has lots of format specifiers to work
with various data types.
• Format specifier defines the type of
data to be printed on standard output.
• Whether to print formatted output or
to take formatted input we need
format specifiers.

© G.Y. Namani Variables & Constants Slide 9


Format specifiers cont…
Supported data
Format specifier Description
types
char
%c Character
unsigned char
short
unsigned short
%d Signed integer
int
long
%f Floating point float
%lf Floating point double
%s String char *

© G.Y. Namani Variables & Constants Slide 10


Reading input of variable
• Using variables in C for input or output
can be a bit of a hassle at first, but bear
with it and it will make sense.
• We'll be using the scanf function to read
in a value and then printf to read it back
out.
• Let's look at the program on next slide
and then pick apart exactly what's going
on.
© G.Y. Namani Variables & Constants Slide 11
Reading input variable cont…
/*the program obtains, calculates and
prints the sum of two numbers*/
#include <stdio.h>
int main() {
int _number1, number2, sum;
//prompt the user for first number
printf("Enter the first number\n");
//store the value of first number
scanf("%d",&_number1);

© G.Y. Namani Variables & Constants Slide 12


Reading input variable cont…
//prompt the user for second number
printf("Enter the second number\
n"); //store the value of second number
scanf("%d",&number2);
//calculate the sum of the two numbers
sum=_number1+number2;
printf("The sum of two numbers is %d\
n",sum);
return 0;
}
© G.Y. Namani Variables & Constants Slide 13
Reading input variable cont…
• "%d" – represents the format specifier
for integer value.
• It tells the scanf function to read in an
integer
• & – is an address operator for storing
value that user inputs.

© G.Y. Namani Variables & Constants Slide 14


Thank you for listening!

© G.Y. Namani Variables & Constants Slide 15

You might also like