VARIABLES, DATA TYPES,
ASSIGNMENT OPERATOR
Variables
The definition of a variable specifies three things :
·
Its visibility, which indicates exactly
where the variable can be used, e.g. for whole file or only in function
·
Its lifetime, which determine whether
the variables exists temporarily or permanently, eg local and global
·
Its type and where allowed, its initial
value
To define a variable, you
must understand the following characters :
·
Each variables has a name
·
Each variables has a type
·
Each variables holds a value
that you assign to the variables
Naming variables
Variable names can be short as a single letter or as long as 31
characters. The name must begin with a
letter of the alphabet but after the first letter they can contain a letters,
numbers and underscore (_)
character.
The following variables are all
valid:
sales Aug91_sales i indexAge
Amt jejari
luas
The following variables are all
invalid:
81-sales Aug91+sales my
age printf scanf main include define
? Uppercase letter in variable names are different from lowercase letter.
Give your names that help
describe the value they are holding.
Define a variable
There are two places where you
can define a variable:
Þ after the brace of a block of code (usually at the top of a
function)
Þ before a function name ( such as before main() in the program)
Example:
main()
{
int i,j; /* These
three lines define 4 variables */
char c;
float k;
/* Rest of
program follows */
.
}
Explanations:
int i,j; /*
Define the variables i and j as integers. */
char c; /*
Define the variable c as a character. */
float k; /*
Define the variable k as a floating point. */
Basic Data type
Variables can hold different types of data. C supports seven built-in data types and it
identifiers them by keywords.
Data type
|
C keyword
|
Bits
|
Range
|
Integer
Long integer
Short integer
unsigned
integer
|
int
long
short
unsigned
|
16
32
8
16
|
-32768 to
32767
-4294967296 to
4294967295
-128 to 127
0 to 65535
|
Character
|
char
|
8
|
0 to 255
|
Floating point
double
floating point
|
float
double
|
32
64
|
approximately
6 digits of precision
approximately
12 digits of precision
|
Table : Basic
Data Type
Assignment
statements and Assignment Operators
The assignment operator (=)
behave differently from what you might
be used to in other languages. The operator used for simple assignment of
values to variables, which is consistent with its use in most other programming languages.
FORMAT
variable
= expression ;
The expression is any
variable , constant, expression or combination that produces a resulting
data type that is the same as the variable’s data type.
Examples:
age=32;
salary=25000.00;
dependents=2;
However, the assignment
operator also can be used on other ways,
such as for multiple assignment statements and compound assignments .
Multiple Assignments
If two or more equal signs appear in an expression , each of them performs an assignments.
Examples:
a=b=c=d=100;
value
= 5 + (r = 9 - c);
Assignment Operators
C++ provides several assignment operators for abbreviating
assignment expressions. For example the
statement
c = c+3;
can abbreviated with the addition assignment operators += as
c += 3;
Any statement of the form
variable
= variable operator expression;
where operator is one of
the binary operators +, -, *, /, or % can be written in the form
variable operator = expression;
Operator Example Equivalent
+= bonus+=500; bonus=bonus+500;
-= budget
- = 50; budget=budget
- 50;
*= salary
*=1.2; salary=salary
*1.2;
/= factor/=.5; factor=factor/.5;
%= daynum %=7 daynum=daynum%7;
Table : Display the arithmetic assignment operators in C
Console Input
Console input (scanf)
You already know about console output (printf function). In this section we’re going to introduce an
important input function, scanf or console input. scanf is an object of the
istream class and is said to be “tied to” or connected to the standard input
device, normally the keyboard. The
stream-extraction operator as used in the following statement causes a value
for integer variable grade (assuming grade has been declared as int) to be
input from scanf to memory.
scanf
("%d", &grade);
Note that the stream-extraction operation is smart enough to know
what type of the data is. Assuming the grade has been properly declared, no
additional data type needs to be specified for use with the stream-extraction operator.
/* C program that demonstrate the use of scanf to
calculate the sum of two integer input from
the keyboard */
#include <stdio.h>
main()
{
int x;
int y ;
int sum;
printf(“\nEnter
an integer :“) ;
scanf("%d",
&x) ;
printf(“\nEnter an
integer :“) ;
scanf("%d", &y) ;
sum =
x + y;
printf( “\nSum of two
integers is %d“,sum );
return 0;
}
/* C program that demonstrate the use of scanf to
calculate the sum of two integer input from
the keyboard */
#include <stdio.h>
main()
{
int x, y ;
printf(“\nEnter
two integers :“) ;
scanf("%d
%d”, &x, &y) ;
printf( “\nSum
of two integers is %d“, (x + y) );
return 0;
}
Format identifier or Specifiers
The format specifier tells printf() where to put a value string and
what format to use in printing the value, to match the format specifier to the
type of variable that is desired :
%c
|
Single
character
|
%s
|
String
|
%d
|
Signed decimal
|
%f
|
Floating point
(decimal notation)
|
%e
|
Floating point
(exponential notation)
|
%u
|
Unsigned
decimal integer
|
%x
|
Unsigned
hexadecimal integer (uses"abcdef")
|
%o
|
Unsigned octal
integer
|
L or L ( %ld,
%lu, % lx, %lo )
|
Long integer
or long double floating point
|
Exercise
Write a c
program to:
1. Calculate
the area of a circle
2. Calculate
the circumference of a circle
HINT: PI=3.14 ( ini adalah
constant bukan variable )
area=PI
x r x r
circumference
= 2 x PI x r
Where
r is a radius
The program will ask user to enter a value
of the radius
No comments:
Post a Comment