Summary Sheet
Fundamentals of Data Storage
- Variables are named storage locations where data is stored, which may be changed as a program runs. E.g. "nStudents".
- Constants are values that are hard-coded into a program, and which do not chnage value. E.g. "3.14159".
- Ultimately all data stored on a computer, both variables and constants, is stored as a sequence of binary digits, e.g. strings of zeros and ones.
- These binary digits are referred to as "bits".
- Physically these zeros and ones may be implemented using wires with two different voltages, magnetic particles with two different alignments, spots on an optical disk having two different optical properties, or by other means.
- The "type" of a particular variable or constant determines how many bits are used used for that paticular data item, and how the bits are to be interpreted.
- Basic C recognizes four basic categories of data types: Integral, Floating Point, Character, and Character String. Modern C adds a few special types to this list.
- Data may be converted from one type to another, ( possibly with loss of precision ), and new types may be user defined.
Integral Types
- Integral data types include all whole numbers, that is numbers not having any fractional component.
- The bits of integral types are interpreted as simple powers of two:
- The right-most bit, known as the least significant bit, represents the number of 1s. ( 2^0 )
- The next bit represents the number of 2s. ( 2^1 )
- The next bit represents the number of 4s. ( 2^2 )
- The next bit represents the number of 8s. ( 2^3 )
- In general the nth bit from the right represents 2^(n-1)
- For unsigned integral types, the leftmost bit, known as the most significant bit, represents 2^(N-1), where N is the total number of bits in the data item.
- The range of possible values for an unsigned integer of N bits is from 0 to 2^N - 1. ( All 0s to all 1s )
- So for example, a 4-bit unsigned integer could range from 0 to 15, and an 8-bit unsigned integer could range from 0 to 255.
- For signed integral types, the leftmost bit can be thought of as representing a negative 2^(N-1).
- ( The real interpretation in the computer is more complicated, but if you think of it this way you will get the right answers. )
- The most negative value would be the first bit a 1 and all other bits 0s, yielding negative 2^(N-1).
- The most positive value would be the first bit a 0 and all other bits 1s, yielding 2^(N-1) - 1.
- So for example, a 4-bit signed integer could range from -8 to +7, and an 8-bit signed integer could range from -128 to +127.
- A signed integral type having all bits 1 is equal to -1, regardless of how many bits are in the number.
- Signed and unsigned integers with the same number of total bits have the same number of different possible values.
- Unsigned integers use one bit pattern ( all 0s ) to represent zero and all others to represent positive values.
- Signed integers use half of the possible bit patterns to represent negative numbers, one pattern to represent zero, and half minus 1 to represent positive values.
- Specific details of the integer types available on a particular implementation, along with the number of bits allocated to each one and their minimum and maximum allowable values can be found in the file limits.h
int
- The most basic and commonly used integral type is "int".
- The int data type is always the "best" size for the particular computer it is running on, typically 32 bits
- Format specifiers for ints are either %d or %i, for either printf or scanf.
long int
- A long int typically uses twice as many bits as a regular int, allowing it to hold much larger numbers.
- ( The C standard only specifies that a long cannot use a fewer number of bits than a regular int )
- printf and scanf replace %d or %i with %ld or %li to indicate the use of a long int.
- long int may also be specified as just long.
long long int
- C99 introduces the long long int, typically having twice as many bits as a long int and printed/scanned using %lld or %lli format specifiers
short int
- A short int may use fewer bits than a regular int, thereby saving storage space.
- ( The C standard only specifies that a short int cannot use more bits than a regular int. On many machines short ints use the same number of bits as regular ints. )
- printf and scanf replace %d or %i with %hd or%hi to indicate the use of a short int.
- short int may also be specified as just short.
unsigned ints
- Unless otherwise specified, all of the aforementioned int types are signed numbers.
- Any of the above may be preceded by the keyword "unsigned" to indicate that they are unsigned.
- e.g. "unsigned int", "unsigned long int", "unsigned long", etc.
- "unsigned" by itself implies "unsigned int"
- The format specifier for unsigned integers in decimal form is %u. The u may be preceded by l, ll, or h for long, long long, and short unsigned types respectively.
- Unsigned integers can also be printed or scanned in octal or hexidecimal form using the %o, %x, or %X format specifiers in place of %u.
char
- Normally chars are interpreted as characters ( see below )
- Technically the char data type is an integral type, always having exactly 8 bits, ( known as a byte. )
- Signed chars can have values from -128 to +127, and can be printed as integers using %d or %i format specifiers
- chars can also be specified as unsigned, giving them a range from 0 to+255.
- Unsigned chars can be printed using %u, %o, %x, or %X format specifiers.
Integral Constants
- int constants in decimal form must begin with a digit from 1 to 9, and can be followed by additional digits from 0 to 9.
- in octal form they must begin with a 0, and following digits must be in the range from 0 to 7.
- in hexidecimal form they begin with 0x. Followng digits must be in the range 0 to 9, or A to F, or a to f.
- Any of the above may be preceded by a minus sign.
- int constants may be followed by a u, l, or ll to indicate unsigned, long, or long long constants respectively.
- Allowable formats are as follows, where the [ square brackets ] denote optional characters:
Decimal: [±]1-9[0-9...][Ll][Uu]Octal: [±]0[0-7...][Ll][Uu]Hexadecimal: [±]0x[0-9a-fA-F...][Ll][Uu]Integer Overflow
- When integer math yields a result that is too big ( or too negative ) to be represented by the corresponding integer type, then overflow ( or underflow ) is said to have occurred.
- With unsigned numbers the result is defined to "wrap around" to the other end of the integer's range, so if you add 1 to an integer that is at the maximum value for it's type, the result is zero.
- With signed numbers the result of overflow is undefined. In some cases adding 1 to the maximum positive value will wrap around to the most negative value, but in other cases erratic behaviour or even a program crash may occur.
Floating Point Types
- Floating point types include all types in which a number may have a fractional component. Fortunately there are only three that we need to worry about - float, double, and long double.
- Specific details of the floating point types available on a particular implementation, along with the number of bits allocated to each one and their minimum and maximum allowable values can be found in the file float.h
float
- The most basic type of floating point number is the float type.
- According to the IEEE standard, a single precision floating point number is exactly 32 bits long, comprised of:
- one bit for indicating the sign
- 23 bits ( plus one implied ) for recording the digits as a 24-bit integer. This works out to about 6 or 7 decimal digits of precision.
- 8 bits for a binary exponent, to shift the digits left or right. This works out to an absolute range from about 10^-38 to 10^38
- Note that because a float only has 24 bits available for storing the digits of the number, it can actually be less precise than a 32-bit int for certain large integers.
double
- The double precision data type uses twice as many bits as a float, yielding approximately twice the number of digits of precision.
- According to the IEEE standard, a double precision floating point number is 64 bits long, comprised of:
- one sign bit.
- 52 bits ( plus one implied ) for recording digits, which works out to about 15 decimal digits of precision.
- 11 bits for the exponent, which works out to a range of about 10^-308 to 10^308
- The double data type is the preferred floating point type for most scientific and engineering calculations.
long double
- The long double type is guaranteed to have more bits than a double, but the exact number my vary from one hardware platform to another. The most typical implementations are either 80 or 128 bits.
- The IEEE standard for quadruple precision floating point numbers is 128 bits consisting of:
- one sign bit
- 112 bits ( plus one implied ) for digits, working out to about 34 decimal digits of precision
- 15 bits for the exponent, giving a range from about 10^-4932 to 10^4932
Floating point constants
- Floating-point constants are normally indicated by the presence of a decimal point, and are normally doubles.
- Floating point constants may be followed by either an "F" to indicate an ordinary float, or an "L" to indicate a long double.
- Floating point constants can also be expressed in scientific notation
- Allowable formats are as follows:
[±]1-9[0-9...].[0-9...][Ee[±]0-9...][FfLl][±][0].[0-9...][Ee[±]0-9...][FfLl][±]1-9[0-9...]Ee[±]0-9...[FfLl][±]1-9[0-9...]Ff[Ll]
Characters
- Although the char data type is technically a small integer ( see above ), its most common use is to hold numeric codes representing ( ASCII ) characters.
- For example, the ASCII code for the capital letter 'A' is 65.
- Note that the character '9' is not the same as the integer value 9.
- In this case the ASCII code for the character '9' is 57.
- The numerical value 9 in the ASCII code set happens to represent a horizontal tab character.
- The full ASCII code table can be found in the back of most programming textbooks, or online at http://www.asciitable.com/ and many other sites.
Character Constants
- Character constants are enclosed in single quotes, such as 'A'.
- Character constants may also include escape characters such as '\n' or '\t', as shown here:
Escape Sequence Meaning
\a
alarm ( bell ) \b
Backspace \f
Form feed ( clears screen ) \n
New line \r
Carriage return \t
Horizontal tab \v
Vertical tab \\
Backslash \?
Question mark \'
Single quote \"
Double quote \0
Numerical zero ( null byte )
- The \ can also be used to escape a 3-digit octal numerical constant, or a hexidecimal constant beginning with \x
- So '\112' and '\x4A' are both a capitol 'J'. See the ASCII table to confirm.
Character Arithmetic
- Because chars are really small integers, it is possible to do mathematical operations on them. For example:
char letter = 'G', lower, upper;// Presume lower has been given a value somehowletter = letter + 3; // letter has now been changed from 'G' to 'J'if( lower >= 'a' && lower <= 'z' ) // If lower is a lower-case letterupper = lower + ( 'A' - 'a' ) // Convert it to upper-case by adding an offset
Character Strings
- C stores character strings as arrays of type char, terminated by a null byte.
- Constant character strings are enclosed in double quotes, and may include escape characters, such as "\n\n\t Please enter X > "
- These notes will postpone further discussion of character strings until after arrays have been covered.
Special Types
- The enumerated type is an integer with a restricted list of legal values, referred to by names. It will be covered in full details in the section on structs and unions.
- C99 introduces new types _Bool, _Complex, and _Imaginary.
Type Conversions
Implicit
- There are certain cases in which data will get automatically converted from one type to another:
- When data is being stored in a variable, if the data being stored does notmatch the type of the variable.
- The data being stored will be converted to match the type of the storage variable.
- When an operation is being performed on data of two different types.
- The "smaller" data type will be converted to match the "larger" type.
- For example, when an int is added to a double, the computer uses a double version of the int and the result is a double.
- The "smaller" data type will be converted to match the "larger" type.
- The following example converts the value of nTotal to a double precision value before performing the division.
Note that if the 3.0 were changed to a simple 3, then integer division would be performed, losing any fractional values in the reuslt.average = nTotal / 3.0;
- When data is passed to or returned from functions.
Explicit
- When data is being stored in a variable, if the data being stored does notmatch the type of the variable.
- Data may also be expressly converted, using the typecast operator
- The following example converts the value of nTotal to a double precision value before performing the division.
( nStudents will then be implicitly promoted, following the guidelines listed above. )average = ( double ) nTotal / nStudents;
- Note that nTotal itself is unaffected by this conversion.
Constants Exercise
-
For each of the constants in the following table, indicate whether the constant is legal or illegal, what type of constant it is if legal, and why illegal otherwise:
Constant Legal?
Explanation
486
Yes
Decimal int
98.6
Yes
Double
98.6f
Yes
Float - The f indicates float, as opposed to double.
02.479
No
No decimal allowed in an octal constant
0.2479
Yes
Double
"A"
Yes
Character string - Two chars including the null byte.
'A'
Yes
A single char. No null byte.
'ABC'
No
Single quotes are for single chars only
'\n'
Yes
The escape sequence represents a single newline character
000042
Yes
Octal. 4 * 8 + 2 = 34 in decimal
0ffH
No
No 'f' in octal. ( Resembles an old FORTRAN format. )
0xC2F9
Yes
Hexadecimal
+37.1
Yes
Double
.0000
Yes
Double
0xFLU
Yes
Hexidecimal. Value = 15, long, unsigned
0x48.6
No
No decimal point allowed in hexidecimal
0x486e1
Yes
'e' is a valid hexidecimal digit
486e1
Yes
Double. 486 * 10^1 = 4860
0486e1
No
No 'e' allowed in octal
0486
No
No '8' allowed in octal either. :-)
Variables ( Covered Previously in Basic Structure of a C Program )
- A variable is a named storage location, where data may be stored and later changed.
- An identifier is a more general term for a named location, which may contain either data or code.
- Identifiers must begin with a letter or an underscore, preferable letters for user programs.
- The remaining characters must be either alphanumeric or underscores.
- Identifiers may be of any length, but only the first 31 characters are examined in most implementations.
- Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three different identifiers.
- By convention ordinary variables begin with a lower case letter, globals with a Single Capital, and constants in ALL CAPS.
- Multi-word variables may use either underscores or "camel case", such as "new_value" or "newValue".
- Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N, and floating point variables are usually assigned names beginning with other letters.
- Identifiers may not be the same as reserved words. ( See text for a full list. )
- All variables must be declared before they can be used.
- In C, all variables must be declared before the first executable statement of the program.
- C++ allows variables to be declared any time before they are used, but it is still normally good practice to declare all variables at the beginning of the program, unless there is a very good reason to do otherwise.
- ( Exceptions: Loop counter variables are often declared as part of the loop structure. Occasionally it is beneficial to declare variables within a reduced scope, to be discussed later. )
- Variables may be given an initial value at the time they are declared. This is called "initialization", or "initializing the variables".
- Initialization in C should be done using an equals sign.
- Example: double x1 = 0.0, x2 = 0.0;
- UNINITIALIZED VARIABLES ARE DANGEROUS, AND SHOULD BE CONSIDERED TO HOLD RANDOM VALUES.
- Variables may be declared "const", meaning that their values cannot be changed.
- const variables MUST be initialized at the time they are declared.
- By convention, const variables are named using ALL CAPS.
- Examples:
- const double PI = 3.14159;
- const int MAXROWS = 100;
- Note: K&R C did not have the const keyword, and so the #define pre-processor macro was used to define constant values. The const qualifier is a better approach when it is available, because it allows the compiler to perform type checking among other reasons. For CS 107 we will defer the discussion of #define until we get to the chapter on the pre-processor.
Type-Related Functions and Concepts ( Advanced, Optional )
- The sizeof( ) operator returns the number of bytes needed to store a variable or data type, so on most sytems, sizeof( int ) would yield 4, as would sizeof( number ) if number were a variable of type int.
- The keyword typedef allows programmers to define their own type names.
- For example, "typedef float Dollars;" would define a new type named "Dollars" that is really the same as float.
- In this case the programmer can now declare variables to be of type "Dollars" instead of type float.
- One advantage is for portability purposes. If the typedef is changed to "typedef double Dollars", then it will affect all variables of type Dollars in the entire program, with one small change.
- It can also make programs more readable when complicated types are used. For example, "typedef unsigned long long int BigInteger;"
- ( typedef is most commonly used to rename complicated types that we have not yet covered, such as structs and pointers. )
Enumerated Types ( Advanced, Optional )
- Enumerated ( enum ) data types are basically ints, except that they are restricted to a limited set of values, and those values are referred to by name not by number.
- The use of enums where applicable helps make code more readable and also limits the possibilities for bad values, thereby reducing bugs and making the code more maintainable and overall better.
- The enum keyword is used to define a new data type, having a new data type name and list of acceptable named values.
- Once the new enum type has been declared, variables can be declared of the new type, and assigned the named values.
- For example:
enum SizeType { small, medium, large }; // Declares a new data type, "SizeType" SizeType item; // Declares a variable of type "SizeType" // ( Some code left out here. ) if( num < 25 ) item = small; // Use as an int, using the named values instead of numbers cout << "\nThe item is "; switch( item ) { case small: // Named values are valid integers printf( "tiny\n" ); break;
- Named values can be assigned specific numbers. Those not assigned will get successive values. So in the following example, minor2 will have the value 2 and major2 will have the value 101:
enum errorType { none = 0, minor1 = 1, minor2, major1 = 100, major2, fatal1 = 1000 };
- Enumerated type variables can also be initialized.. For example:
errorType errorCode = none;sizeType bookSize = large;
FAQs
C Programming Course Notes - Data Types? ›
The C programming language has five primitive or primary data types. 1. Integer (int): Refers to positive and negative whole numbers (without decimal), such as 10, 12, 65, 3400, etc.
What are data types in C answers? ›Data Type | Example of Data Type |
---|---|
Basic Data Type | Floating-point, integer, double, character. |
Derived Data Type | Union, structure, array, etc. |
Enumerated Data Type | Enums |
Void Data Type | Empty Value |
The C programming language has five primitive or primary data types. 1. Integer (int): Refers to positive and negative whole numbers (without decimal), such as 10, 12, 65, 3400, etc.
How to remember range of data types in C? ›- + 3. ...
- + 2. ...
- int is displayed on 32 bits meaning 2^32 long = 2^64 float is also 32 bits: 1 for sign, 8 for int numbers, 23 for the fraction double is 64 bits: 1 for sign, 11 for int, 52 for the fraction.
Output. Characters will be printed infinitely.
What are basic data types in C explain? ›Main types. The C language provides the four basic arithmetic type specifiers char, int, float and double, and the modifiers signed, unsigned, short, and long. The following table lists the permissible combinations in specifying a large set of storage size-specific declarations.
What are the key words in C? ›auto | else | long |
---|---|---|
case | extern | return |
char | float | short |
const | for | signed |
continue | goto | sizeof |
- Integer.
- Double or Real.
- String.
- Boolean.
- Date/Time.
- Object.
- Variant.
int , long , ptr , and off_t are all 32 bits (4 bytes) in size. int is 32 bits in size. long , ptr , and off_t are all 64 bits (8 bytes) in size. The 32-bit data model for z/OS® XL C/C++ compilers is ILP32 plus long long.
What is the smallest data type in C? ›Generally, the smallest addressable chunk of data in C is a byte.
Does C have arrays? ›
Array in C are of two types; Single dimensional arrays and Multidimensional arrays. Single Dimensional Arrays: Single dimensional array or 1-D array is the simplest form of arrays that can be found in C. This type of array consists of elements of similar types and these elements can be accessed through their indices.
How many digits can a long hold in C? ›Long stores at least 32 bits, giving it a range of -2,147,483,648 to 2,147,483,647.
What is the maximum length of a variable in C is what? ›Constant | Meaning | Value |
---|---|---|
INT_MAX | Maximum value for a variable of type int . | 2147483647 |
UINT_MAX | Maximum value for a variable of type unsigned int . | 4294967295 (0xffffffff) |
LONG_MIN | Minimum value for a variable of type long . | -2147483647 - 1 |
LONG_MAX | Maximum value for a variable of type long . | 2147483647 |
Type Name | Bytes | Range of Values |
---|---|---|
short | 2 | -32,768 to 32,767 |
unsigned short | 2 | 0 to 65,535 |
long | 4 | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 | 0 to 4,294,967,295 |
The double data type has more precision as compared to the three other data types. This data type has more digits towards the right of decimal points as compared to other data types. For instance, the float data type contains six digits of precision whereas double data type comprises of fourteen digits.
What is %d in C? ›%d (Decimal Integer) Format Specifier. %c (Character) Format Specifier. %f (Floating Point) Format Specifier.
Why do we use data types in C? ›Variables in C are associated with data type. Each data type requires an amount of memory and performs specific operations. int − Used to store an integer value. char − Used to store a single character.
What is loop in C? ›What are Loops in C? Loop is used to execute the block of code several times according to the condition given in the loop. It means it executes the same code multiple times so it saves code and also helps to traverse the elements of an array.
What is recursion in C? ›Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve. Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.
What are the 8 simple data types? ›Primitive Data Types. The eight primitives defined in Java are int, byte, short, long, float, double, boolean and char. These aren't considered objects and represent raw values.
What are 10 types of data? ›
- Integer. Integer data types often represent whole numbers in programming. ...
- Character. In coding, alphabet letters denote characters. ...
- Date. This data type stores a calendar date with other programming information. ...
- Floating point (real) ...
- Long. ...
- Short. ...
- String. ...
- Boolean.
Simple data types represent a single value. Simple data types used to create policies: Integer. The integer data type represents a positive whole number or its negative value. Examples of integers are 0 , 1 , 2 , 3 and 4 . Float.
What is algorithm in C? ›An algorithm is a sequence of instructions that are carried out in a predetermined sequence in order to solve a problem or complete a work. A function is a block of code that can be called and executed from other parts of the program.
What is a byte in C? ›A byte is typically 8 bits. C character data type requires one byte of storage. A file is a sequence of bytes. A size of the file is the number of bytes within the file.
How many bits is a int in C? ›- Internal data.
- External data.
- Time-stamped data.
- Structured data.
- Unstructured data.
- Open data.
- Big data.
- Genomic data.
- Numeric Data Type Syntax.
- Integer Types (Exact Value) - INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT.
- Fixed-Point Types (Exact Value) - DECIMAL, NUMERIC.
- Floating-Point Types (Approximate Value) - FLOAT, DOUBLE.
- Bit-Value Type - BIT.
- Numeric Type Attributes.
- Out-of-Range and Overflow Handling.
4 Types of Data: Nominal, Ordinal, Discrete, Continuous.
Is C++ smaller than C? ›C++ is object-oriented, bottom-up, and includes many high-level features. C is low-level, procedural, and top-down. C is still in use because it is slightly faster and smaller than C++.
What is float in C? ›Float is a datatype which is used to represent the floating point numbers. It is a 32-bit IEEE 754 single precision floating point number ( 1-bit for the sign, 8-bit for exponent, 23*-bit for the value.
How many bytes is a char in C? ›
Type | Storage size | Value range |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
Data types simply refers to the type and size of data associated with variables and functions. A data type specifies the type of data that a variable can store such as integer, floating, character etc. These are fundamental data types in C namely integer(int), floating(float), character(char) and void.
What are data types and variables in C? ›In C, there are different types of variables (defined with different keywords), for example: int - stores integers (whole numbers), without decimals, such as 123 or -123. float - stores floating point numbers, with decimals, such as 19.99 or -19.99. char - stores single characters, such as 'a' or 'B' .
What are data types in programming? ›What is a data type? In software programming, data type refers to the type of value a variable has and what type of mathematical, relational or logical operations can be applied without causing an error.
What is data type and data structure in C? ›A Data type is one of the forms of a variable to which the value can be assigned of a given type only. This value can be used throughout the program. A Data structure is a collection of data of different data types. This collection of data can be represented using an object and can be used throughout the program.