Lecture 2
Lecture 2
Lecture 2
Masamba Benson
bmutahwa@gmail.com
Lecture 2: Outline
• Variables, Data Types, and Arithmetic Expressions [K- ch.4]
– 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 assignement
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
Explanation: the computer doesn't keep track of enough decimal places !
The number 2.0e20 is 2 followed by 20 zeros and by adding 1
you are trying to change the 21st digit. To do this correctly, the program
would need to be able to store a 21-digit number. A float number is typically
just six or seven digits scaled to bigger or smaller numbers with an
exponent.
Type Specifiers: long, long long, short,
unsigned, signed
• Type specifiers: extend or limit the range of certain basic types on certain
computer systems
• If the specifier long is placed directly before the int declaration, the
declared integer variable is of extended range on some computer systems.
• Example of a long int declaration: long int factorial;
• On many systems, an int and a long int both have the same range and
either can be used to store integer values up to 32-bits wide (231–1, or
2,147,483,647).
• A constant value of type long int is formed by optionally appending the
letter L (upper- or lowercase) at the end of an integer constant.
• Example: long int numberOfPoints = 131071100L;
• To display the value of a long int using printf, the letter l is used as a
modifier before the integer format characters i, o, and x
Basic Data Types - Summary
Type Meaning Constants Ex. printf
int Integer value; guaranteed to contain at least 16 bits 12, -7, %i,%d, %x,
0xFFE0, 0177 %o
short int Integer value of reduced precision; guaranteed to - %hi, %hx,
contain at least 16 bits %ho
long int Integer value of extended precision; guaranteed to 12L, 23l, %li, %lx,
contain at least 32 bits 0xffffL %lo
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)
unsigned - %hu, %hx,
short int %ho
unsigned 12UL, 100ul, %lu, %lx,
long int 0xffeeUL %lo
long double Extraextended accuracy floating-point value; guaranteed to 12.341, 3.1e-5l %Lf, %Le,
contain at least 10 digits of precision. %Lg
char Single character value; on some systems, sign extension 'a', '\n' %c
might occur when used in an expression.
unsigned Same as char, except ensures that sign extension does not -
char occur as a result of integral promotion.
signed char Same as char, except ensures that sign extension does -
occur as a result of integral promotion.
Knowing actual ranges for types
• Defined in the system include files <limits.h> and <float.h>
• <limits.h> contains system-dependent values that specify the sizes of
various character and integer data types:
– the maximum size of an int is defined by the name INT_MAX
– the maximum size of an unsigned long int is defined by ULONG_MAX
• <float.h> gives information about floating-point data types.
– FLT_MAX specifies the maximum floating-point number,
– FLT_DIG specifies the number of decimal digits of precision for a float type.
Working with arithmetic expressions
• Basic arithmetic operators: +, -, *, /
• Precedence: one operator can have a higher priority, or precedence, over
another operator.
– Example: * has a higher precedence than +
– a+b*c
– if necessary, you can always use parentheses in an expression to force the terms to
be evaluated in any desired order.
• Associativity: Expressions containing operators of the same precedence are
evaluated either from left to right or from right to left, depending on the
operator. This is known as the associative property of an operator
– Example: + has a left to right associativity
• 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