Lecture2 - Variables Data-Types Arithmetic-Expressions
Lecture2 - Variables Data-Types Arithmetic-Expressions
Arithmetic Expressions
Lecture 2
Programming
Lecture 2: Outline
• Variables, Data Types, and Arithmetic Expressions
– Working with Variables
– Understanding Data Types and Constants
• The Basic Integer Type (int)
• The Floating Number Type (float)
• The Extended Precision Type (double)
• The Single Character Type (char)
• The Boolean Data Type (_Bool)
• Storage sizes and ranges
• Type Specifiers: long, long long, short, unsigned, and signed
– Working with Arithmetic Expressions
• Integer Arithmetic and the Unary Minus Operator
• The Modulus Operator
• Integer and Floating-Point Conversions
– Combining Operations with Assignment: The Assignment Operators
– Types _Complex and _Imaginary
Variables
• Programs can use symbolic names for storing
computation data
• Variable: a symbolic name for a memory location
– programmer doesn’t have to worry about specifying (or even
knowing) the value of the location’s address
• In C, variables have to be declared before they are used
– Variable declaration: [symbolic name(identifier), type]
• Declarations that reserve storage are called definitions
– The definition reserves memory space for the variable, but
doesn’t put any value there
• Values get into the memory location of the variable by
initialization or assignment
Variables - Examples
int a; // declaring a variable of type int
L-value R-value
int x =16;
printf("%i %#X %#o\n", x,x,x);
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 storage (binary)
letter = 'A'; /* OK */
letter = A; /* NO! Compiler thinks A is a variable */
letter = "A"; /* NO! Compiler thinks “A" is a string */
letter = 65; /* ok because characters are really
stored as numeric values (ASCII code),
but poor style */
Data display vs data storage
/* displays ASCII code for a character */
#include <stdio.h>
int main(void)
{
char ch;
ch='A';
printf("The code for %c is %i.\n", ch, ch);
return 0;
}
A 65 display
The Boolean Data Type _Bool
• A _Bool variable is defined in the language to be large enough to
store just the values 0 and 1.The precise amount of memory that is
used is unspecified.
• _Bool variables are used in programs that need to indicate a
Boolean condition. For example, a variable of this type might be
used to indicate whether all data has been read from a file.
• By convention, 0 is used to indicate a false value, and 1 indicates a
true value. When assigning a value to a _Bool variable, a value of 0
is stored as 0 inside the variable, whereas any nonzero value is
stored as 1.
• To make it easier to work with _Bool variables in your program, the
standard header file <stdbool.h> defines the values bool, true,
and false:
bool endOfData = false;
• The _Bool type has beed added by C99.
• Some compilers (Borland C, Turbo C, Visual C) don’t support it
Storage sizes and ranges
• Every type has a range of values associated with it.
• This range is determined by the amount of storage that is allocated
to store a value belonging to that type of data.
• In general, that amount of storage is not defined in the language. It
typically depends on the computer you’re running, and is, therefore,
called implementation- or machine-dependent.
– For example, an integer might take up 32 bits on your computer, or it
might be stored in 64.You should never write programs that make any
assumptions about the size of your data types !
• The language standards only guarantees that a minimum amount of
storage will be set aside for each basic data type.
– For example, it’s guaranteed that an integer value will be stored in a
minimum of 32 bits of storage, which is the size of a “word” on many
computers.
Integer overflow
• What happens if an integer tries to get a value too big for its type
(out of range)?
#include <stdio.h>
int main(void) {
int i = 2147483647;
printf("%i %i %i\n", i, i+1, i+2);
return 0;
}
Program output:
2147483647 -2147483648 -2147483647
Explanation:
On this computer, int is stored on 32 bits: the first bit represents
the sign, the rest of 31 bits represent the value.
Biggest positive int value here: 231-1 = 2147483647
Floating point round-off error
#include <stdio.h>
int main(void)
{
float a,b;
b = 2.0e20 + 1.0;
a = b - 2.0e20;
printf("%f \n", a);
return 0;
}
Program output:
4008175468544.000000
long long Integer value of extraextended precision; guaranteed 12LL, 23ll, %lli,
int to contain at least 64 bits 0xffffLL %llx, %llo
unsigned Positive integer value; can store positive values up 12u, 0XFFu %u, %x, %o
int to twice as large as an int; guaranteed to contain at
least 16 bits (all bits represent the value, no sign bit)
• There are three imaginary types; An imaginary number has just an imaginary part:
• float _Imaginary represents the imaginary part with a type float value.
• double _Imaginary represents the imaginary part with a type double value.
• long double _Imaginary represents the imaginary part with a type long double
value.
• Complex numbers can be initialized using real numbers and the value I, defined in
<complex.h> and representing i, the square root of –1
_Complex example