Sie sind auf Seite 1von 19

Advanced C

A Review in C
Data Type
• an extensive system used for declaring
variables or functions of different types.
– NOTE: The type of a variable determines how
much space it occupies in storage and how the bit
pattern stored is interpreted.
Data Type: Types and Description
Types and Description
• Basic Type
• Enumerated Types
• The Type Void
• Derived Types
Basic Types
1. Integer Types
2. Floating-Point Types
3. Character type
Integer Type
• whole numbers
• Positive or negative
• int
– Ex: int id;
Integer Type
Type Storage size Value range
-32,768 to 32,767 or -2,147,483,648 to
int 2 or 4 bytes
2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 0 to 4,294,967,295
short 2 bytes -32,768 to 32,767
unsigned
2 bytes 0 to 65,535
short
long 4 bytes -2,147,483,648 to 2,147,483,647
unsigned
4 bytes 0 to 4,294,967,295
long
Integer Type
sizeof(type) - yields the storage size of the object or
type in bytes.

#include <stdio.h>
#include <limits.h>
int main() {
printf("Storage size for int : %d
\n", sizeof(int));
return 0; }
Floating-Point Type
• Can hold real numbers
• float/double
– Ex.: float accountBalance;
double bookPrice;
• can be represented in exponential form
– Ex.: float normalizationFactor =
22.442e2;
Floating-Point Type
Storage
Type Value range Precision
size
6 decimal
float 4 byte 1.2E-38 to 3.4E+38
places
2.3E-308 to 15 decimal
double 8 byte
1.7E+308 places
long 3.4E-4932 to 19 decimal
10 byte
double 1.1E+4932 places
Floating-Point Type
float.h - defines macros that allow you to use these values and other details
about the binary representation of real numbers in your programs.

#include <stdio.h>
#include <float.h>
int main() {
printf("Storage size for float : %d \n",
sizeof(float));
printf("Minimum float positive value: %E\n",
FLT_MIN );
printf("Maximum float positive value: %E\n",
FLT_MAX );
printf("Precision value: %d\n", FLT_DIG );
return 0; }
Character Type
- char
- Ex.: char test = 'h';
- Size = 1 byte
Void Type
indicates that no value is available.

Types & Description


1.Function returns as void
2.Pointers to void
Function returns as void
• A function with no return value has the return
type as void.
– Ex.: void exit (int status);
Pointers to void
1.- A pointer of type void * represents the
address of an object, but not its type.
1.Ex: a memory allocation function void *malloc(
size_t size ); returns a pointer to void which
can be casted to any data type.
C Qualifiers
• Qualifiers alters the meaning of base data
types to yield a new data type.
– Size qualifiers
– Sign qualifiers
– Constant Qualifiers
– Volatile Qualifiers
Size Qualifiers
• Size qualifiers alters the size of a basic type.
– long
– short
• Ex.: long double i;
Sign Qualifiers
• if a variable needs to hold positive value
only, unsigned data types are used.
– Ex.: unsigned int positiveInteger;
• can hold data from 0 to 232 − 1.
• int and char types only
Constant Qualifier
• const
– const int cost = 20;
• The value cannot be changed.
Volatile qualifiers
• A variable should be declared volatile
whenever its value can be changed by some
external sources outside the program.
• volatile

Das könnte Ihnen auch gefallen