2. Fundamental C Data Types
• Bits, Bytes and Words
• The smallest unit of memory is called a bit.
–It can hold one of two values: 0 or 1.
–The bit is the basic block of computer memory.
• A byte is 8 bits.
• A word is a natural unit of memory for a given
computer design.
–Some computers have 32-bit words and more recent
computers have 64-bit words.
–The larger a word is the more information it can store.
• The C language provides a number of fundamental
data types. 2
4. Fundamental C Data Types
• The concept of types is important in that it determines
the space necessary to store the values of a
particular type, and which valid operations are defined
on certain types.
• Whenever an identifier, (variable name, function
name, formal parameter name, user-defined type
name, etc.) is declared to be of a certain type, the
type name always comes first, followed by the
identifier.
<type> <identifier> …
4
5. Void and Scalar data types
• The void data type indicates ‘nothing’.
• The void keyword may be used
– to indicate that a function does not return a value,
e.g. void do_it(int a)
– To indicate that no parameters are required in the
function definition, for e.g., int do_it(void)
Scalar Types
• Scalar types are largest group and are called scalar
because the types have only one dimension (one
value).
• There are three sub-groups of scalar types, namely:
arithmetic, pointer and enumerated types.
5
6. Arithmetic data types
• These types are numeric types on which arithmetic operations,
amongst others, can be performed.
• Integer Types
– An integer type is a whole number.
– The sizes of the integer data types are very implementation
dependant, and are therefore the types most closely associated
with the host computer architecture.
– The sizes of these types range from 8 bits to 64 bits on some
computers.
– Bit operations are allowed
– In the following table, the square brackets […] indicate an optional
word, since without it the compiler will accept it anyway as the
default.
6
7. Arithmetic data types
7
Type Bits Description
_Bool ? Boolean type, 0 = false, 1 = true
char 8 character type (-128→127 OR 0 →255)
signed char 8 Signed character type (-128→127)
unsigned char 8 Unsigned character type (0→255)
[signed] short [int] 16 Signed short integer type (-32767→32768
unsigned short [int] 16 Unsigned short integer type (0→65535)
[signed] int 32 Signed normal integer type (−2147483647 →
+2147483647)
Unsigned int 32 Unsigned normal integer type (0 → +4294967295)
long long [int] 64 Signed long integer type (−9223372036854775808
→ +9223372036854775808)
unsigned long long 64 Unsigned long integer type (0 →
+18446744073709551616)
8. Arithmetic data types
8
Floating Point Types
• A floating-point number more or less corresponds to a real
number.
• Integers are whole number, whereas floating point numbers
can represent both whole and fractional numbers.
• No bit operations are allowed on floating point types.
• The three floating point types are:
– float (1×10−37 → 1×1037)
– double (1×10−308 → 1×10308)
– long double (1×10−4932 → 1×104932)
• Floating point numbers can represent a much larger range of
values than integers, but tend to be slower than integer
operations.
9. Arithmetic data types
• Floating point types
– Single precision floating point number (float)
• Occupies 32 bits typically
– Double precision floating point number (double)
• Occupies 64 bits typically
– float: approx seven digits of precision and takes four bytes to store.
– double: approx. 13 digits of precision and takes eight bytes to store. 9
10. Enumerated Types
• In C, enumerated types indicate a user-defined type which
simply assigns names to a range of int types.
• The general format for the creation of enumerated types is:
• enum <identifier> {<enumeration list>} ;
• Example
– enum WeekDays {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
• In the above example a type with the name enum WeekDays
was created.
– The value of Sun is 0, Mon is 1, ... Sat is 6.
– enum WeekDays today = Wed;
10
11. Variables
• Simple variables are declared by first specifying the
type and then the name of the variable.
• Variables are declared when we want to use them at
a certain point, but the definition of the variable only
occurs later in the program (on the external level), or
it is defined in another file (on the external level).
• Attempts to make use of a variable that has not
been defined will result in an error. This includes
reading from the variable or assigning a value to the
variable.
• The general syntax and some derivations for
defining variables are shown in the following table:
11
12. Variables
12
<type> <name>; int count; Simple declaration
<type> *<name>; int *count; Pointer variable
declaration
<type> <name>,
<name>;
int count1, count2; Multiple variable
declaration
<type> <name>[<nr of
elements>];
int data[10]; Array variable
declaration
• Once a variable is declared it is possible to initialize that variable
with a value in the same statement using the assignment operator
– Eg. int test = 10;
• The sizeof function will return the number of bytes a given
variable occupies
13. Type Conversions
• Conversions between different types are commonplace in most
programming languages.
• Sometimes these conversions takes place without the
programmer being aware of it.
• This type of conversion is termed an implicit conversion.
• A forced conversion, where the programmer forces the
compiler to perform a type conversion, is called casting in C
terminology.
13
14. Type Conversions
Implicit Conversions
• Implicit conversion most often occur in arithmetic expressions,
where different integer types, and possibly floating point types
may be mixed.
• The simple rule is that when an arithmetic operation is
performed on two operands of different types, the one with the
smaller internal representation, will be converted to the other.
• If a double operand is multiplied by an int operand (for
example), the value of the int type operand are first converted
to a double value before the multiplication is performed.
14
15. Type Casting
• Often the programmer needs to force the conversion of one
type to a similar type.
• This can be accomplished by means of the prefixed cast
operator, which consists of the name of an type enclosed by
parenthesis:
• (type) <value or variable>
• Example
– int x = -100;
– double y = (double) x;
15
16. Constants
• Constants refer to fixed values that may not be altered by the
program.
• The C pre-processor allows for a means to define constants
which can then be used later in a program.
• The constant data types must be defined before the main
function.
• The constant name is normally written in capitals and does
not have a semi-colon at the end. The use of constants is
mainly for making your programs easier to be understood and
modified by others and yourself in the future.
• The format of a constant is shown below:
#define CONSTANTNAME value
• for example:
#define VAT 0.14
#define PI 3.14
• First comes #define, than the name followed by the value.
16
17. Printf() Function
• The printf function do differ from the sort of functions that you create
for yourself in that they can take a variable number of parameters.
• In the case of printf the first parameter is always a string (e.g. "Hello
World") but after that you can include as many parameters of any type
as required by your specifiers.
• The format for using the printf () function is:
printf(string,variable,variable,variable...)
• where the ... means you can carry on writing a list of variables
separated by commas as long as required.
• The string is critical because it specifies the type of each variable in
the list and how you want it printed.
• The string is usually called the control string or the format string.
• The way that this works is that printf scans the string from left to right
and prints on the screen, or any suitable output device, any
characters it encounters - except when it reaches a % character.
17
18. Printf() Function
• The % character is a signal that what follows it is a specification for
how the next variable in the list of variables should be printed.
• printf uses this information to convert and format the value that was
passed to the function by the variable and then moves on to process
the rest of the control string and anymore variables it might specify.
• For example:
printf("Hello World");
• only has a control string and, as this contains no % characters it
results in Hello World being displayed and doesn't need to display
any variable values.
• The specifier %d means convert the next value to a signed decimal
integer and so: printf("Total = %d",total);
• will print Total = and then the value passed by total as a decimal
integer.
• They can be variables, constants or even expressions that are
evaluated first before the value is printed.
18
19. ANSI C Conversion Specifications for
scanf() and printf()
19
Converstion specification Output
%c Single character
%d, i Signed decimal integer
%e Floating point number e-notation
%E Floating point number E-notation
%f, g Floating point number, shorter
decimal notation
%s Character string
%x Hexidecimal number
20. Character data type
• A char variable is used to store a single character.
• A character constant is formed by enclosing the character
within a pair of single quotation marks.
• C represents letters as a number which is related to the
symbol or character being stored via the ASCII table
• Example:
– char x = ‘b’;
– In this case the ASCII value of the character ‘b’ is stored in x
20
21. Escape Characters
• Escape characters are used when printing out characters using
printf that have a functional significance in C such as the or %
characters.
• They are produced by first typing a followed by the escape
character
21
Escape Sequence Produced Character
n New line
” ”
’ ’
t Horizontal tab
a Audible Alert
22. Operands and Expressions
• Every expression in C results in a value whose type depends
on the operands in the expression.
• Operands
– Num = a + b – c
assignment Operators
Expression
• Operand values in C may be converted implicitly to match the
types of other operands according to fixed rules.
• Every operator has a precedence level and associativity.
• Operators cannot perform their functions on all data types, but
have a limited number of operand types with which they can
function.
• Conditional operators return type int with either the value 0 or
1, which corresponds to FALSE and TRUE respectively. 22
25. Arithmetic Operators
• The two arithmetic operators that are used frequently are ++ and
--.
• You can place these in front or behind variables.
• ++ specifies increment,
• -- specifies decrement
• If the operator is placed in front, it is prefix if it is placed behind, it
is postfix.
• Prefix means, increment before any operations are performed,
postfix is increment afterwards. These are important
considerations when using these operators.
• C allows compound assignment operators (*=, +=, /=, -=).
– Eg: int i = 5; i *= 5; The int i would have the value 25 after the operation.
– This is identical to writing: int i = 5; i = i* 5;
25
26. Integer and Floating-point conversion
• When an integer is assigned to a floating point number its value
doesn’t change but it will be represented as a floating point
number in memory.
• When a floating-point number is assigned to an integer the values
that come after the decimal place will be truncated and lost.
• When an operator is applied to two operands it is important to
take note of the data type of the operands.
• If both operands are integers the result of the operation will be an
integer.
• If one of the operands is a floating-point number the result will be
a floating-point number.
• This is primarily important when performing division as when two
integers are divided an integer division will take place and the
result will be an integer with the portion of the result that falls after
the decimal being truncated.
26
27. Logical Operators
• Logical operators simulate boolean algebra in C.
• Examples of Logical/Boolean Operators:
– && is AND
– || is OR
– ! is NOT
– &, |, and ^.
• For example, && is used to compare two objects with AND: x != 0
&& y != 0
• Expressions involving logical operators undergo Short-Circuit
Evaluation. Take the above example into consideration. If x != 0
evaluates to false, the whole statement is false regardless of the
outcome of y != 0.
27
28. Relational Operators
• The relational operators from mathematics < and the > are used
for comparing two objects.
• There are six possibilities in C: <, <=, >, >=, !=, and ==. The first
four a self-explanatory, the != stands for "not equals to" and == is
"equivalent to".
• NOTE: a = b is different from a == b. Most C compilers will allow
both statements to be used in conditionals like if, but they have
two completely different meanings.
28
29. Bit Operators
• Bit Shifts
– << is the left shift operator
– Eg. char x = 8; (x is 000010002) x<<2; results in x having a value of 32
(001000002)
– >> is the right shift operator
• Logical Operators
– & is the AND operator
– | is the OR operator
– ~ is the complement operator
– ^ is the XOR operator
29
01001000 |
10111000 =
--------------
11111000
01001000 &
10111000 =
---------------
00001000
01110010 ^
10101010 =
--------------
11011000
~01110010
--------------
~10001101
char x = 72;
char y = 184;
y = x&y;
y will be equal to 8