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

Lecture8 C Variables (1)

The document provides an overview of variable declaration and usage in C programming, detailing the rules for naming, declaring, and defining variables. It explains the basic data types available, including int, long, float, double, char, and void, along with their storage requirements and value ranges. Additionally, it emphasizes the importance of initializing variables and adhering to naming conventions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture8 C Variables (1)

The document provides an overview of variable declaration and usage in C programming, detailing the rules for naming, declaring, and defining variables. It explains the basic data types available, including int, long, float, double, char, and void, along with their storage requirements and value ranges. Additionally, it emphasizes the importance of initializing variables and adhering to naming conventions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

C Programming:

variables
Variables
 The name of a variable can be composed of letters, digits, and the underscore
character.
 Before variables can be used, they must be declared.
 Before the program is linked, variables must have been defined in one of the
source files.
 Simple variables are declared by first specifying the type and then the name of
the variable.
 Upper and lowercase letters are distinct because C is case-sensitive.
 A variable definition tells the compiler where and how much storage to create for
the variable. A variable definition specifies a data type and contains a list of one
or more variables of that type as follows
 type variable_list;
 A variable declaration is useful when using multiple files and can define a variable
in one of the files which will be available at the time of linking of the program. Use
the keyword extern to declare a variable at any place.

© Prof Suvendi Rimer, UJ


2
Variables
 There are five basic data types associated with variables:
 int - integer: a whole number.
 long - integer: a large whole number.
 float - floating point value: a number with a fractional part.
 double - a double-precision floating point value.
 char - a single character.
 void - valueless special purpose type .

© Prof Suvendi Rimer, UJ


3
Variables

 An int variable has no fractional part. Integer variables tend to be used for
counting.
 To declare an int you use the instruction: int variable name;
 For example: int a; declares that you want to create an int variable called a.
 To assign a value to our integer variable we would use the following C
statement:
int a=10;
 Unsigned int: takes two or four bytes to store, range -32,768 to 32,767 or
-2,147,483,648 to 2,147,483,647
 Signed int: takes two or four bytes to store, range 0 to 65,535 or 0 to
4,294,967,295

© Prof Suvendi Rimer, UJ


4
Variables

 C uses one of two keywords to declare a variable that is to be associated with


a decimal number: float and double. They each offer a different level of
precision.
 float: approx. seven digits of precision and takes four bytes to store.
 double: approx. thirteen digits of precision and takes eight bytes to store.
 Float example:
float total;
double sum;
 To assign a numerical value to the floating point and double precision
variables, we would use the following C statement:
total=0.0;
sum=12.50;

© Prof Suvendi Rimer, UJ


5
Variables

 Character Variables
 C only has a concept of numbers and characters.
 C has no understanding of strings but a string is only an array of characters
and C does have a concept of arrays.
 To declare a variable of type character we use the keyword char. - a single
character stored in one byte.
 Unsigned char: 1 byte, range 0 to 255
 Signed char: 1 byte, range -128 to 127

© Prof Suvendi Rimer, UJ


6
Variables

 For example:
char c;
 To assign or store a character value in a char data type is easy - a character
variable is just a symbol enclosed by single quotes. For example, if c is a char
variable you can store the letter A in it using the following C statement:
c='A‘
 Note that you can only store a single character in a char variable. Remember
that a char variable is 'A' and not "A".

© Prof Suvendi Rimer, UJ


7
Variables
 Before you can use a variable you have to declare it. Attempts to use a variable that
has not been defined will cause a compiler error. Using a variable means storing
something in it. You can store a value in a variable using:
name = value;
 To do this you state its type and then give its name.
 Single variable: The following example declares a single integer variable.
int i;
 Multiple variables: You can declare any number of variables of the same type
with a single statement. The following example declares three integers: a, b
and c.
int a, b, c;
 You have to declare all the variables that you want to use at the start of the
program.

© Prof Suvendi Rimer, UJ


8
Variables
 You can assign an initial value to a variable when you declare it. The following
example sets the int variable to one as soon as it's created.
int i=1;
 This is just the same as:
int i; or i=l;
 Just be aware that the compiler may be able to speed up the operation if you
initialise the variable as part of its declaration.
 Don't assume that an uninitialized variable has a sensible value stored in it. Some
C compilers store 0 in newly created numeric variables but nothing in the C
language compels them to do so.
 Variable names:
 should be lowercase for local variables
 should be UPPERCASE for constants

© Prof Suvendi Rimer, UJ


9

You might also like