9/18/2023
Outline
CSC 1113 • Variables in C
– Variable Naming Conventions
Programming Techniques – Variable Declaration
– Variable Assignments
– Constants
• Data Types
– Data Types Classification
– Data Type Modifiers
– Macros
Mrs.T.D.Gilmini
Senior Lecturer
Department of Computer Science
Learning Outcome of Today’s Lesson Variables
• In general, variables are symbols that can represent values in
• Identify use of constants and variables and formulas.
difference between them • Variables are simply names used to refer to some location in
memory – a location that holds a value with which we are
• Understand appropriate usage of different working
data types • It may help to think of variables as a placeholder for a value
• Write basic C programs with different types of
variables to get the required output
Variables Variables
• C program utilize memory to store a variable : it • C is a "strongly-type" language
must claim the memory needed to store the
values for a variable – When declaring the variable, you must also
• This is done by declaring variables specify the variable type.
• It refers, introduction of a variable for the first
time before it is first used in the program int test = 25;
• In a program, every variable has: float test = 2.3;
– Name (Identifier) char test = 'e';
– Data Type
– Size
– Value
1
9/18/2023
Variable Naming Conventions
Declaring Variables
• Each variable can only be declared once.
• The type of a variable cannot be changed inside
the program.
– It tells the compiler what the variable name is.
– It specifies what type of data the variable will hold.
– After the declaration, complier will allocate memory
for the variable
• Multiple variables of same data type can be
created in one declaration.
Scope of a Variable
• Refers to where variables can be referenced (scope of a
variable is its lifetime in the program.)
• Global Scope
– outside any functions/block
– can be used by later inside blocks of code (read, and
written, from anywhere in your program )
– Hold their values throughout the lifetime of the program
• Local Scope
– Inside Blocks
– place the declaration at the beginning (i.e. before any
nondeclarative statements) of the block to which the
variable is intended to be local
In C, you delimit a block of code by {}
2
9/18/2023
#include <stdio.h>
int main()
{ #include <stdio.h>
int my_num = 7; int main()
{ {
//add 10 my_num int my_num = 7;
my_num = my_num +10; {
//or my_num +=10 - more succinctly int new_num = 10;
}
printf("my_num is %d",my_num); } printf("new_num is %d",new_num); //this is line 8
return 0; return 0;
} }
Constants- const qualifier
• When the const qualifier is used, the declared variable
must be initialized at declaration
• It is then not allowed to be changed
• While the idea of a variable that never changes may
not seem useful, there are good reasons to use const
– For example, if you need the value of π in your
calculations, you can declare a const variable of pi, so a
program or another function written by someone else
cannot change the value of pi
• Naming Convention: Use UPPERCASE words and joined
with underscore.
• Eg: MIN_VALUE
3
9/18/2023
4
9/18/2023
The int type
• The int type stores integers in the form of "whole
numbers“
• An integer is typically the size of one machine word,
which is 32 bits (4 bytes)
• Examples of literals are whole numbers (integers) such
as 1,2,3, 10, 100...
• A 32 bit word (number) has the possibility of
representing any one number out of 4294967296
possibilities (2 to the power of 32)
• When int is 32 bits (4 bytes), it can store any whole
number (integer) between -2147483648 and
2147483647
Data type modifiers
• One can alter the data storage of any data type by
preceding it with certain modifiers
– long
– short
– unsigned
– signed
• long and short are modifiers that make it possible for a data
type to use either more or less memory
• Unsigned -This keyword can be used to make the accepting
values of a data type is positive data type
• Signed - This keyword accepts both negative or positive
value and this is default properties or data type modifiers
for every data type
nt main()
The char type {
char a = 'a';
char c;
• The char type is capable of holding any member of the printf("Value of a: %c\n", a);
execution character set
• A variable of type char is most often used to store a++;
printf("Value of a after increment is: %c\n", a);
character data, hence its name
• Examples of character literals are 'a', 'b', '1', etc., as // c is assigned ASCII values
well as some special characters such as '\0' (the null // which corresponds to the
// character 'c'
character) and '\n' (newline, recall "Hello, World") // a-->97 b-->98 c-->99
– Note that the char value must be enclosed within single // here c will be printed
quotations c = 99;
• Typically has a size of one byte printf("Value of c: %c", c);
– In standard C it never can be less than 8 bits
• Declaring and assigning a char, respectively➔ return 0;
}
5
9/18/2023
The float type
• float is short for floating point
• It stores real numbers also, but is only one
machine word in size (i.e., 4 bytes)
• Therefore, it is used when less precision than a
double provides is required
• float literals must be suffixed with F or f,
otherwise they will be interpreted as doubles
– Examples are: 3.141592f, 4.0f
• float variables can be declared using the float
keyword
Example // C Program to demonstrate use
// of Floating types
#include <stdio.h>
int main()
{
float a = 9.0f;
float b = 2.5f;
// 2x10^-4
float c = 2E-4f;
printf("%f\n", a);
printf("%f\n", b);
printf("%f", c);
return 0;
}
The double type The double type Cont..
• The double and float types are very similar • The distinction between floats and doubles was made
• The float type allows you to store single-precision because of the differing sizes of the two types
floating point numbers, while the double keyword • When C was first used, space was at a minimum and so
allows you to store double-precision floating point the judicious use of a float instead of a double saved
numbers – real numbers, in other words, both integer some memory
and non-integer values • Nowadays, with memory more freely available, you do
• If you use 4 instead of 4.0, the 4 will be interpreted as not really need to conserve memory like this – it may
an int be better to use doubles consistently
• Examples of double literals are 3.1415926535897932, • Indeed, some C implementations use doubles instead
4.0, 6.022e+23 (scientific notation) of floats when you declare a float variable.
• Its size is typically two machine words, or 8 bytes on • If you want to use a double variable, use the double
most machines keyword
6
9/18/2023
Precision of Floating Points
• Precision is specified by the number of digits
after the decimal point for the outputs for
float and double
• If precision is not specified, the default setting
will be output ( 6 digits)
Format Specifiers in C C Input Output (I/O)
• These specifiers define, the type of data to be • The C programming language provides
printed on standard output or taking as input standard library functions to read any given
• Tell the compiler what type of data is in a input and to display data on the console.
variable during taking input using scanf() or – scanf() function to take input from the user
printing using printf(). – printf() function to display output to the user
Reading user response from Keyboard Display output
• printf() -This function is used to print a simple
text sentence or value of any variable which
can be of int, char, float, or any other data
type.
7
9/18/2023
sizeof()
• sizeof is a unary operator