С data types word and

1999 ISO C Concepts.png

In the C programming language, data types constitute the semantics and characteristics of storage of data elements. They are expressed in the language syntax in form of declarations for memory locations or variables. Data types also determine the types of operations or methods of processing of data elements.

The C language provides basic arithmetic types, such as integer and real number types, and syntax to build array and compound types. Headers for the C standard library, to be used via include directives, contain definitions of support types, that have additional properties, such as providing storage with an exact size, independent of the language implementation on specific hardware platforms.[1][2]

Basic types[edit]

Main types[edit]

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.

Type Explanation Minimum size (bits) Format specifier Range Suffix for decimal constants
char Smallest addressable unit of the machine that can contain basic character set. It is an integer type. Actual type can be either signed or unsigned. It contains CHAR_BIT bits.[3] 8 %c CHAR_MIN / CHAR_MAX n/a
signed char Of the same size as char, but guaranteed to be signed. Capable of containing at least the [−127, +127] range.[3][a] 8 %c (or %hhi for numerical output) SCHAR_MIN / SCHAR_MAX[5] n/a
unsigned char Of the same size as char, but guaranteed to be unsigned. Contains at least the [0, 255] range.[6] 8 %c (or %hhu for numerical output) 0 / UCHAR_MAX n/a
short
short int
signed short
signed short int
Short signed integer type. Capable of containing at least the [−32,767, +32,767] range.[3][a] 16 %hi or %hd SHRT_MIN / SHRT_MAX n/a
unsigned short
unsigned short int
Short unsigned integer type. Contains at least the [0, 65,535] range.[3] 16 %hu 0 / USHRT_MAX n/a
int
signed
signed int
Basic signed integer type. Capable of containing at least the [−32,767, +32,767] range.[3][a] 16 %i or %d INT_MIN / INT_MAX none[7]
unsigned
unsigned int
Basic unsigned integer type. Contains at least the [0, 65,535] range.[3] 16 %u 0 / UINT_MAX u or U[7]
long
long int
signed long
signed long int
Long signed integer type. Capable of containing at least the [−2,147,483,647, +2,147,483,647] range.[3][a] 32 %li or %ld LONG_MIN / LONG_MAX l or L[7]
unsigned long
unsigned long int
Long unsigned integer type. Capable of containing at least the [0, 4,294,967,295] range.[3] 32 %lu 0 / ULONG_MAX both u or U and l or L[7]
long long
long long int
signed long long
signed long long int
Long long signed integer type. Capable of containing at least the [−9,223,372,036,854,775,807, +9,223,372,036,854,775,807] range.[3][a] Specified since the C99 version of the standard. 64 %lli or %lld LLONG_MIN / LLONG_MAX ll or LL[7]
unsigned long long
unsigned long long int
Long long unsigned integer type. Contains at least the [0, 18,446,744,073,709,551,615] range.[3] Specified since the C99 version of the standard. 64 %llu 0 / ULLONG_MAX both u or U and ll or LL[7]
float Real floating-point type, usually referred to as a single-precision floating-point type. Actual properties unspecified (except minimum limits); however, on most systems, this is the IEEE 754 single-precision binary floating-point format (32 bits). This format is required by the optional Annex F «IEC 60559 floating-point arithmetic». Converting from text:[b]

  • %f %F
  • %g %G
  • %e %E
  • %a %A
f or F
double Real floating-point type, usually referred to as a double-precision floating-point type. Actual properties unspecified (except minimum limits); however, on most systems, this is the IEEE 754 double-precision binary floating-point format (64 bits). This format is required by the optional Annex F «IEC 60559 floating-point arithmetic».
  • %lf %lF
  • %lg %lG
  • %le %lE
  • %la %lA[c]
long double Real floating-point type, usually mapped to an extended precision floating-point number format. Actual properties unspecified. It can be either x86 extended-precision floating-point format (80 bits, but typically 96 bits or 128 bits in memory with padding bytes), the non-IEEE «double-double» (128 bits), IEEE 754 quadruple-precision floating-point format (128 bits), or the same as double. See the article on long double for details. %Lf %LF
%Lg %LG
%Le %LE
%La %LA[c]
l or L
  1. ^ a b c d e The minimal ranges −(2n−1−1) to 2n−1−1 (e.g. [−127,127]) come from the various integer representations allowed by the standard (ones’ complement, sign-magnitude, two’s complement).[4] However, most platforms use two’s complement, implying a range of the form −2m−1 to 2m−1−1 with m ≥ n for these implementations, e.g. [−128,127] (SCHAR_MIN = −128 and SCHAR_MAX = 127) for an 8-bit signed char.
  2. ^ These format strings also exist for formatting to text, but operate on a double.
  3. ^ a b Uppercase differs from lowercase in the output. Uppercase specifiers produce values in the uppercase, and lowercase in lower (%A, %E, %F, %G produce such values as INF, NAN and E (exponent) in uppercase)

The actual size of the integer types varies by implementation. The standard requires only size relations between the data types and minimum sizes for each data type:

The relation requirements are that the long long is not smaller than long, which is not smaller than int, which is not smaller than short. As char‘s size is always the minimum supported data type, no other data types (except bit-fields) can be smaller.

The minimum size for char is 8 bits, the minimum size for short and int is 16 bits, for long it is 32 bits and long long must contain at least 64 bits.

The type int should be the integer type that the target processor is most efficiently working with. This allows great flexibility: for example, all types can be 64-bit. However, several different integer width schemes (data models) are popular. Because the data model defines how different programs communicate, a uniform data model is used within a given operating system application interface.[8]

In practice, char is usually 8 bits in size and short is usually 16 bits in size (as are their unsigned counterparts). This holds true for platforms as diverse as 1990s SunOS 4 Unix, Microsoft MS-DOS, modern Linux, and Microchip MCC18 for embedded 8-bit PIC microcontrollers. POSIX requires char to be exactly 8 bits in size.[9][10]

Various rules in the C standard make unsigned char the basic type used for arrays suitable to store arbitrary non-bit-field objects: its lack of padding bits and trap representations, the definition of object representation,[6] and the possibility of aliasing.[11]

The actual size and behavior of floating-point types also vary by implementation. The only requirement is that long double is not smaller than double, which is not smaller than float. Usually, the 32-bit and 64-bit IEEE 754 binary floating-point formats are used for float and double respectively.

The C99 standard includes new real floating-point types float_t and double_t, defined in <math.h>. They correspond to the types used for the intermediate results of floating-point expressions when FLT_EVAL_METHOD is 0, 1, or 2. These types may be wider than long double.

C99 also added complex types: float _Complex, double _Complex, long double _Complex.

Boolean type[edit]

C99 added a boolean (true/false) type _Bool. Additionally, the <stdbool.h> header defines bool as a convenient alias for this type, and also provides macros for true and false. _Bool functions similarly to a normal integer type, with one exception: any assignments to a _Bool that are not 0 (false) are stored as 1 (true). This behavior exists to avoid integer overflows in implicit narrowing conversions. For example, in the following code:

unsigned char b = 256;

if (b) {
	/* do something */
}

Variable b evaluates to false if unsigned char has a size of 8 bits. This is because the value 256 does not fit in the data type, which results in the lower 8 bits of it being used, resulting in a zero value. However, changing the type causes the previous code to behave normally:

_Bool b = 256;

if (b) {
	/* do something */
}

The type _Bool also ensures true values always compare equal to each other:

_Bool a = 1, b = 2;

if (a == b) {
	/* this code will run */
}

Size and pointer difference types[edit]

The C language specification includes the typedefs size_t and ptrdiff_t to represent memory-related quantities. Their size is defined according to the target processor’s arithmetic capabilities, not the memory capabilities, such as available address space. Both of these types are defined in the <stddef.h> header (cstddef in C++).

size_t is an unsigned integer type used to represent the size of any object (including arrays) in the particular implementation. The operator sizeof yields a value of the type size_t. The maximum size of size_t is provided via SIZE_MAX, a macro constant which is defined in the <stdint.h> header (cstdint header in C++). size_t is guaranteed to be at least 16 bits wide. Additionally, POSIX includes ssize_t, which is a signed integer type of the same width as size_t.

ptrdiff_t is a signed integer type used to represent the difference between pointers. It is guaranteed to be valid only against pointers of the same type; subtraction of pointers consisting of different types is implementation-defined.

Interface to the properties of the basic types[edit]

Information about the actual properties, such as size, of the basic arithmetic types, is provided via macro constants in two headers: <limits.h> header (climits header in C++) defines macros for integer types and <float.h> header (cfloat header in C++) defines macros for floating-point types. The actual values depend on the implementation.

Properties of integer types[edit]

  • CHAR_BIT – size of the char type in bits (at least 8 bits)
  • SCHAR_MIN, SHRT_MIN, INT_MIN, LONG_MIN, LLONG_MIN(C99) – minimum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long
  • SCHAR_MAX, SHRT_MAX, INT_MAX, LONG_MAX, LLONG_MAX(C99) – maximum possible value of signed integer types: signed char, signed short, signed int, signed long, signed long long
  • UCHAR_MAX, USHRT_MAX, UINT_MAX, ULONG_MAX, ULLONG_MAX(C99) – maximum possible value of unsigned integer types: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long
  • CHAR_MIN – minimum possible value of char
  • CHAR_MAX – maximum possible value of char
  • MB_LEN_MAX – maximum number of bytes in a multibyte character

Properties of floating-point types[edit]

  • FLT_MIN, DBL_MIN, LDBL_MIN – minimum normalized positive value of float, double, long double respectively
  • FLT_TRUE_MIN, DBL_TRUE_MIN, LDBL_TRUE_MIN (C11) – minimum positive value of float, double, long double respectively
  • FLT_MAX, DBL_MAX, LDBL_MAX – maximum finite value of float, double, long double, respectively
  • FLT_ROUNDS – rounding mode for floating-point operations
  • FLT_EVAL_METHOD (C99) – evaluation method of expressions involving different floating-point types
  • FLT_RADIX – radix of the exponent in the floating-point types
  • FLT_DIG, DBL_DIG, LDBL_DIG – number of decimal digits that can be represented without losing precision by float, double, long double, respectively
  • FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON – difference between 1.0 and the next representable value of float, double, long double, respectively
  • FLT_MANT_DIG, DBL_MANT_DIG, LDBL_MANT_DIG – number of FLT_RADIX-base digits in the floating-point significand for types float, double, long double, respectively
  • FLT_MIN_EXP, DBL_MIN_EXP, LDBL_MIN_EXP – minimum negative integer such that FLT_RADIX raised to a power one less than that number is a normalized float, double, long double, respectively
  • FLT_MIN_10_EXP, DBL_MIN_10_EXP, LDBL_MIN_10_EXP – minimum negative integer such that 10 raised to that power is a normalized float, double, long double, respectively
  • FLT_MAX_EXP, DBL_MAX_EXP, LDBL_MAX_EXP – maximum positive integer such that FLT_RADIX raised to a power one less than that number is a normalized float, double, long double, respectively
  • FLT_MAX_10_EXP, DBL_MAX_10_EXP, LDBL_MAX_10_EXP – maximum positive integer such that 10 raised to that power is a normalized float, double, long double, respectively
  • DECIMAL_DIG (C99) – minimum number of decimal digits such that any number of the widest supported floating-point type can be represented in decimal with a precision of DECIMAL_DIG digits and read back in the original floating-point type without changing its value. DECIMAL_DIG is at least 10.

Fixed-width integer types[edit]

The C99 standard includes definitions of several new integer types to enhance the portability of programs.[2] The already available basic integer types were deemed insufficient, because their actual sizes are implementation defined and may vary across different systems. The new types are especially useful in embedded environments where hardware usually supports only several types and that support varies between different environments. All new types are defined in <inttypes.h> header (cinttypes header in C++) and also are available at <stdint.h> header (cstdint header in C++). The types can be grouped into the following categories:

  • Exact-width integer types that are guaranteed to have the same number n of bits across all implementations. Included only if it is available in the implementation.
  • Least-width integer types that are guaranteed to be the smallest type available in the implementation, that has at least specified number n of bits. Guaranteed to be specified for at least N=8,16,32,64.
  • Fastest integer types that are guaranteed to be the fastest integer type available in the implementation, that has at least specified number n of bits. Guaranteed to be specified for at least N=8,16,32,64.
  • Pointer integer types that are guaranteed to be able to hold a pointer. Included only if it is available in the implementation.
  • Maximum-width integer types that are guaranteed to be the largest integer type in the implementation.

The following table summarizes the types and the interface to acquire the implementation details (n refers to the number of bits):

Type category Signed types Unsigned types
Type Minimum value Maximum value Type Minimum value Maximum value
Exact width intn_t INTn_MIN INTn_MAX uintn_t 0 UINTn_MAX
Least width int_leastn_t INT_LEASTn_MIN INT_LEASTn_MAX uint_leastn_t 0 UINT_LEASTn_MAX
Fastest int_fastn_t INT_FASTn_MIN INT_FASTn_MAX uint_fastn_t 0 UINT_FASTn_MAX
Pointer intptr_t INTPTR_MIN INTPTR_MAX uintptr_t 0 UINTPTR_MAX
Maximum width intmax_t INTMAX_MIN INTMAX_MAX uintmax_t 0 UINTMAX_MAX

Printf and scanf format specifiers[edit]

The <inttypes.h> header (cinttypes in C++) provides features that enhance the functionality of the types defined in the <stdint.h> header. It defines macros for printf format string and scanf format string specifiers corresponding to the types defined in <stdint.h> and several functions for working with the intmax_t and uintmax_t types. This header was added in C99.

Printf format string

The macros are in the format PRI{fmt}{type}. Here {fmt} defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). {type} defines the type of the argument and is one of n, FASTn, LEASTn, PTR, MAX, where n corresponds to the number of bits in the argument.

Scanf format string

The macros are in the format SCN{fmt}{type}. Here {fmt} defines the output formatting and is one of d (decimal), x (hexadecimal), o (octal), u (unsigned) and i (integer). {type} defines the type of the argument and is one of n, FASTn, LEASTn, PTR, MAX, where n corresponds to the number of bits in the argument.

Functions
[icon]

This section needs expansion. You can help by adding to it. (October 2011)

Additional floating-point types[edit]

Similarly to the fixed-width integer types, ISO/IEC TS 18661 specifies floating-point types for IEEE 754 interchange and extended formats in binary and decimal:

  • _FloatN for binary interchange formats;
  • _DecimalN for decimal interchange formats;
  • _FloatNx for binary extended formats;
  • _DecimalNx for decimal extended formats.

Structures[edit]

Structures aggregate the storage of multiple data items, of potentially differing data types, into one memory block referenced by a single variable. The following example declares the data type struct birthday which contains the name and birthday of a person. The structure definition is followed by a declaration of the variable John that allocates the needed storage.

struct birthday {
	char name[20];
	int day;
	int month;
	int year;
};

struct birthday John;

The memory layout of a structure is a language implementation issue for each platform, with a few restrictions. The memory address of the first member must be the same as the address of structure itself. Structures may be initialized or assigned to using compound literals. A function may directly return a structure, although this is often not efficient at run-time. Since C99, a structure may also end with a flexible array member.

A structure containing a pointer to a structure of its own type is commonly used to build linked data structures:

struct node {
	int val;
	struct node *next;
};

Arrays[edit]

For every type T, except void and function types, there exist the types «array of N elements of type T«. An array is a collection of values, all of the same type, stored contiguously in memory. An array of size N is indexed by integers from 0 up to and including N−1. Here is a brief example:

int cat[10];  // array of 10 elements, each of type int

Arrays can be initialized with a compound initializer, but not assigned. Arrays are passed to functions by passing a pointer to the first element. Multidimensional arrays are defined as «array of array …», and all except the outermost dimension must have compile-time constant size:

int a[10][8];  // array of 10 elements, each of type 'array of 8 int elements'

Pointers[edit]

Every data type T has a corresponding type pointer to T. A pointer is a data type that contains the address of a storage location of a variable of a particular type. They are declared with the asterisk (*) type declarator following the basic storage type and preceding the variable name. Whitespace before or after the asterisk is optional.

char *square;
long *circle;
int *oval;

Pointers may also be declared for pointer data types, thus creating multiple indirect pointers, such as char ** and int ***, including pointers to array types. The latter are less common than an array of pointers, and their syntax may be confusing:

char *pc[10];   // array of 10 elements of 'pointer to char'
char (*pa)[10]; // pointer to a 10-element array of char

The element pc requires ten blocks of memory of the size of pointer to char (usually 40 or 80 bytes on common platforms), but element pa is only one pointer (size 4 or 8 bytes), and the data it refers to is an array of ten bytes (sizeof *pa == 10).

Unions[edit]

A union type is a special construct that permits access to the same memory block by using a choice of differing type descriptions. For example, a union of data types may be declared to permit reading the same data either as an integer, a float, or any other user declared type:

union {
	int i;
	float f;
	struct {
		unsigned int u;
		double d;
	} s;
} u;

The total size of u is the size of u.s – which happens to be the sum of the sizes of u.s.u and u.s.d – since s is larger than both i and f. When assigning something to u.i, some parts of u.f may be preserved if u.i is smaller than u.f.

Reading from a union member is not the same as casting since the value of the member is not converted, but merely read.

Function pointers[edit]

Function pointers allow referencing functions with a particular signature. For example, to store the address of the standard function abs in the variable my_int_f:

int (*my_int_f)(int) = &abs;
// the & operator can be omitted, but makes clear that the "address of" abs is used here

Function pointers are invoked by name just like normal function calls. Function pointers are separate from pointers and void pointers.

Type qualifiers[edit]

The aforementioned types can be characterized further by type qualifiers, yielding a qualified type. As of 2014 and C11, there are four type qualifiers in standard C: const (C89), volatile (C89), restrict (C99) and _Atomic (C11) – the latter has a private name to avoid clashing with user names,[12] but the more ordinary name atomic can be used if the <stdatomic.h> header is included. Of these, const is by far the best-known and most used, appearing in the standard library and encountered in any significant use of the C language, which must satisfy const-correctness. The other qualifiers are used for low-level programming, and while widely used there, are rarely used by typical programmers.[citation needed]

See also[edit]

  • C syntax
  • Uninitialized variable
  • Integer (computer science)

References[edit]

  1. ^ Barr, Michael (2 December 2007). «Portable Fixed-Width Integers in C». Retrieved 18 January 2016.
  2. ^ a b ISO/IEC 9899:1999 specification, TC3 (PDF). p. 255, § 7.18 Integer types <stdint.h>.
  3. ^ a b c d e f g h i j ISO/IEC 9899:1999 specification, TC3 (PDF). p. 22, § 5.2.4.2.1 Sizes of integer types <limits.h>.
  4. ^ Rationale for International Standard—Programming Languages—C Revision 5.10 (PDF). p. 25, § 5.2.4.2.1 Sizes of integer types <limits.h>.
  5. ^ «C and C++ Integer Limits».
  6. ^ a b ISO/IEC 9899:1999 specification, TC3 (PDF). p. 37, § 6.2.6.1 Representations of types – General.
  7. ^ a b c d e f ISO/IEC 9899:1999 specification, TC3 (PDF). p. 56, § 6.4.4.1 Integer constants.
  8. ^ «64-Bit Programming Models: Why LP64?». The Open Group. Retrieved 9 November 2011.
  9. ^ «Width of Type (The GNU C Library)». www.gnu.org. Retrieved 30 July 2022.
  10. ^ «<limits.h>». pubs.opengroup.org. Retrieved 30 July 2022.
  11. ^ ISO/IEC 9899:1999 specification, TC3 (PDF). p. 67, § 6.5 Expressions.
  12. ^ C11:The New C Standard, Thomas Plum

Word Size and Data Types

A word is the amount of data that a machine can process at one time. This fits into the document analogy that includes characters (usually eight bits) and pages (many words, often 4 or 8KB worth) as other measurements of data. A word is an integer number of bytes for example, one, two, four, or eight. When someone talks about the «n-bits» of a machine, they are generally talking about the machine’s word size. For example, when people say the Pentium is a 32-bit chip, they are referring to its word size, which is 32 bits, or four bytes.

The size of a processor’s general-purpose registers (GPR’s) is equal to its word size. The widths of the components in a given architecture for example, the memory bus are usually at least as wide as the word size. Typically, at least in the architectures that Linux supports, the memory address space is equal to the word size[2]. Consequently, the size of a pointer is equal to the word size. Additionally, the size of the C type long is equal to the word size, whereas the size of the int type is sometimes less than that of the word size. For example, the Alpha has a 64-bit word size. Consequently, registers, pointers, and the long type are 64 bits in length. The int type, however, is 32 bits long. The Alpha can access and manipulate 64 bits, one word at a time.

Further read : http://www.makelinux.com/books/lkd2/ch19lev1sec2

  1. 07-02-2010


    #1

    karuna is offline


    Registered User


    Question what is the WORD datatype?

    I had a programming class assignment to make a simple guessing game ( we’re just starting out programming in C) and I read this code that changes the color of the console:

    Code:

    HANDLE mainwin = GetStdHandle ( STD_OUTPUT_HANDLE );
        WORD DefaultColor; 
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
        GetConsoleScreenBufferInfo(mainwin, &csbiInfo); 
        DefaultColor = csbiInfo.wAttributes;

    I know what each bit does, but I would like to know what the WORD datatype / structure is? ( sorry if I’m not using the correct terminology, I’m just beginning programming, and it’s really fun )

    Thanks alot for the help


  2. 07-02-2010


    #2

    Elysia is offline


    C++まいる!Cをこわせ!


    WORD in a Windows environment is just that — a word. Now the definition of a word is some type of data that is 16 bits (2 bytes) for x86. Typically an alias for short (ONLY guaranteed under Windows!).

    Quote Originally Posted by Adak
    View Post

    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

    Quote Originally Posted by Salem
    View Post

    You mean it’s included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.


  3. 07-02-2010


    #3

    karuna is offline


    Registered User


    Oh ok, thanks
    So word is sort of like a string that can only hold to characters? (2 bytes?)
    Thanks for the help


  4. 07-03-2010


    #4

    DeadPlanet is offline


    Registered User


    No, a WORD is just a 2-Byte (unsigned) data type, you can use it for whatever you like.

    Windows Data Types (Windows)


  5. 07-03-2010


    #5

    Elysia is offline


    C++まいる!Cをこわせ!


    When we speak of types such as word, dword, qword, etc, they all refer to a storage unit, or place, where we can store at most n bytes (2, 4, 8 in this case respectively). What exactly you want to store in them is up to you, because to the hardware it’s all bits.
    Now, these types are all (usually) represented by integer types (short, int, long long respectively in this case).

    Quote Originally Posted by Adak
    View Post

    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.

    Quote Originally Posted by Salem
    View Post

    You mean it’s included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.


  6. 07-03-2010


    #6

    karuna is offline


    Registered User


    Oh ok, its starting to make more sense now,
    Thanks alot Elysia and DeadPlanet,
    + the link was really helpful as well


  7. 07-05-2010


    #7

    GReaper is offline


    Programming Wraith

    GReaper's Avatar


    Short question: Why are data types bigger that BYTE called *WORD? (WORD, DWORD, QWORD … )

    Devoted my life to programming…


  8. 07-05-2010


    #8

    bernt is offline


    Just a pushpin.

    bernt's Avatar


    Short question: Why are data types bigger that BYTE called *WORD? (WORD, DWORD, QWORD … )

    A word is the data size that a processor naturally handles. So for a 32-bit processor, it’s theoretically 32 bits (or an int), although x86 processors support 16 and 32 bits equally via the *x and e*x registers.

    Since smaller data sizes have to be padded for operations there’s really no speed gain from using e.g. bytes vs. words. So it’s probably more convenient to define data types that are word size rather than byte size — that way you have a much larger int range at no cost of speed.
    Therefore Windows has DWORD (double word) and QWORD (quad word), which correspond to 2 words and 4 words respectively (or a 16-bit long int and long long int).

    Since the modern Windows API really came about in Windows ’95 and that was a 16-bit system, WORD was defined to be a 16-bit data structure (on 16-bit processors, an int, and a short was 8 bits like a char). Hence the 16-bit Windows word. And it stuck on into win32, probably for compatibility reasons.

    EDIT: Nevermind the ’95 part, 16-bit started out with DOS, but the point is still valid.

    Last edited by bernt; 07-06-2010 at 08:23 AM.

    Consider this post signed


  9. 07-05-2010


    #9

    GReaper is offline


    Programming Wraith

    GReaper's Avatar


    Devoted my life to programming…


Introduction

Terminology:

  • Computer program: sequence of statements designed to accomplish some task
  • Programming: planning/creating a program
  • Syntax: rules that specify which statements (instructions) are legal
  • Programming language: a set of rules, symbols, and special words
  • Semantic rule: meaning of the instruction

C++ Programs

C++ Program Basics:

  • Programming instructions must be written according to syntax rules
  • Compiler checks that program follows syntax rules
  • Metalanguage: term used to describe syntax rules
  • C++ program: collection of one or more subprograms—called functions
  • Subprogram or function is collection of statements that, when invoked, performs some task
  • Every C++ program has one and ONLY one function called main()
  • Token: smallest individual program unit
  • Token: divided into special symbols, word symbols, and identifiers

Symbols

Special Symbols:

  • Include mathematical symbols and punctuation marks
  • Blank also special symbol
  • Some tokens made up of two characters (with no blank)—still considered as single symbol

Special Symbol Examples:

+     ? 
-     ,
*     <=
/     !=
.     ==
;     >=  

Word Symbols:

  • Another type of token
  • Reserved words or keywords
  • Reserved words: always lowercase, each considered to be single symbol with special meaning

Word Symbol Examples:

int
float
double
char
void
return  

Identifiers

Using Identifiers:

  • Another type of token
  • Identifiers: used as names for variables, constants, and functions
  • Identifiers: consist of letters, digits, and underscore character (_)
  • Identifiers: must begin with letter or underscore (best not
    to use underscore for portability)
  • C++: case sensitive
  • Some predefined identifiers (cout and cin)
  • Unlike reserved words, predefined identifiers may be redefined—but generally not good idea

Legal Identifier Examples:

first
conversion
payRate

Illegal Identifier Examples:

employee Salary (cannot use space)
Hello! (cannot use special characters, like exclamation mark)
one+two (cannot use special characters, like + character)
2nd (cannot begin with digit)

A Caution: reserved words, «keywords,» and predefined identifiers…

Here is a list of all
the reserved words in Standard C++,
and a few predefined identifiers
for the sake of comparison.

There is a distinction between reserved
words
and predefined identifiers,
which are sometimes collectively referred to
as keywords. Nevertheless, be aware that the terminology is
nonstandard. As an illustration, some authors use keyword in
the same sense that others use reserved word.

C++ Reserved Words

The reserved words of C++ may be conveniently placed into several
groups. In the first group, we put those that were also present in the
C programming language and have been carried over into
C++. There are

32 such reserved words

:

auto   const     double  float  int       short   struct   unsigned
break  continue  else    for    long      signed  switch   void
case   default   enum    goto   register  sizeof  typedef  volatile
char   do        extern  if     return    static  union    while

There are another

30 reserved words that were not in C

, are therefore
new to C++:

asm         dynamic_cast  namespace  reinterpret_cast  try
bool        explicit      new        static_cast       typeid
catch       false         operator   template          typename
class       friend        private    this              using
const_cast  inline        public     throw             virtual
delete      mutable       protected  true              wchar_t

The following 11 C++ reserved words are not essential when the standard
ASCII character set is being used, but they have been added to provide
more readable alternatives for some of the C++ operators, and also to
facilitate programming with character sets that lack characters needed
by C++.

and      bitand   compl   not_eq   or_eq   xor_eq
and_eq   bitor    not     or       xor

Note that your particular compiler may not be completely up-to-date,
which means that some (and possibly many) of the reserved words in
the preceding two groups may not yet be implemented.

Some Predefined Identifiers

Beginning C++ programmers are sometimes
confused by the difference between the
two terms reserved word and
predefined identifier, and
some potential for confusion.

One of the difficulties is that some
keywords that one might «expect»
to be reserved words are not.
The keyword main is a prime
example, and others include things
like the endl manipulator and
other keywords from the vast
collection of C++ libraries.

For example, you could declare a
variable called main inside
your main function, initialize
it, and then print out its value (but
ONLY do that to verify that you can!).
On the other hand, you could not
do this with a variable named else.
The difference is that else is
a reserved word, while
main is «only» a predefined
identifier
.

Here is a short list of some predefined identifiers:

cin   endl     INT_MIN   iomanip    main      npos  std
cout  include  INT_MAX   iostream   MAX_RAND  NULL  string

References:
http://www.cppreference.com/keywords/index.html
http://msdn.microsoft.com/en-us/library/2e6a4at9(VS.80).aspx


Data Types

Using Data Types:

  • Set of values together with set of operations called data type
  • C++ data types classified into three categories:
    1. Simple data type
    2. Structured data type
    3. Pointers

Simple Data Types (three categories):

  1. Integral: integers (numbers without a decimal)
  2. Floating-point: decimal numbers
  3. Enumeration type: user-defined data type

Integral Data Types (further classified):

  1. char
  2. short
  3. int
  4. long
  5. bool
  6. unsigned char
  7. unsigned short
  8. unsigned int
  9. unsigned long
  • Each data type associated with different set of values
  • Size of number represented determines data type
  • Data type determines amount of memory used
  • Should use most efficient data type for program requirements
  • Different compilers may have different range of values for each data type

int Data Type:

  1. Numbers with no decimal point
  2. Positive integers do not require + sign in front of them (but, can include +)
  3. No commas are used within integer
  4. ***Commas used for separating items in a list***

int Data Type Examples:

-6728
0
78

bool Data Type:

  1. Two values: true and false, called logical (or Boolean) values
  2. An expression that evaluates to true or false called logical (Boolean) expression
  3. In C++, bool, true, and false are reserved words
  4. Older compilers do not include bool data type

char Data Type:

  1. Smallest integral data type
  2. Can hold numeric values -128 to 127
  3. Used to represent characters: letters, digits, and special characters
  4. char data type can represent each character on keyboard
  5. char data values represented within single quotation marks (e.g., ‘r’)
  6. Blank is represented as ‘ ‘ (space between single quotes)

char Data Type Examples:

 'A', 'a', '0', '*', '+', '$', '&' 

Floating-Point Data Types:

  1. Represent numbers with decimal points (real numbers)
  2. C++ uses form of scientific notation called floating-point notation
  3. In C++, letter E (or e) represents the exponent

Floating-Point Data Types (further classified):

  1. float
  2. double
  3. long double
  • Each data type associated with different set of values
  • Size of number represented determines data type
  • Data type determines amount of memory used
  • float data type:
  • float data type:

    • Range: -3.4E+38 to 3.4E+38
    • Memory allocated: 4 bytes
    • Maximum number of significant digits (decimal places): 6 or 7
    • Float values called single precision

    double data type:

    • Range: -1.7E+308 to 1.7E+308
    • Memory allocated: 8 bytes
    • Maximum number of significant digits (decimal places): 15
    • Double values called double precision
  • Precision: maximum number of significant digits
  • Should use most efficient data type for program requirements
  • Different compilers may have different range of values for each data type
  • Most newer compilers, double and long double are same

Floating-Point Data Type Examples:

Real Number   C++ Floating-Point Notation
75.924        7.592400E1
0.18          1.800000E-1
0.0000453     4.530000E-5
-1.482        -1.482000E0
7800.0        7.800000E3

***Some compilers, default constant float values to double, and provide a warning when assigning constant float values to float data type variables. You may want to declare float data values as double to eliminate warnings.***

string Type:

  • Programmer-defined data type (not included in earlier versions of C++)
  • To use string data type, string library must be included (include file)
  • Sequence of zero or more characters
  • Enclosed in double quotation marks (e.g., «r»)
  • null string: string with no characters
  • Each character in string has a relative position
  • Position of first character is 0 (zero), 2nd character is 1, etc.
  • Length of string is total number of characters (e.g., «C++» length = 3)

Arithmetic Operators and Operator Precedence

Using Arithmetic Operators and Operator Precedence:

  • Operations inside of parentheses () are evaluated first
  • +, -, *, and / can be used with integral and floating-point data types
  • % modulus operator only used with integral types
  • When dividing integral values, quotient truncated, not rounded
  • Unary operator: used with only one operand
  • Binary Operator: used with two operands

C++ Mathematical Operators (in order of precedence):

1) *   multiplication
1) /   division
1) %   remainder (modulus operator)
2) +   addition
2) -   subtraction

***Operators having same level of precedence are evaluated from left to right.***


Expressions

Using Expressions:

  • Integral expression: all operands are integers
  • Floating-point expression: all operands are floating-point
  • Integral expression yields integral result
  • Floating-point expression yields a floating-point result
  • Mixed expression: operands are integers and floating-point
  • Mixed expression yields a floating-point result (integer changed to floating-point)

Mixed Expression Examples:

2 + 3.5 = 5.5
6  /  4 + 3.9 = 4.9
5.4  *  2 - 13.6 + 18  /  2 = 6.200000000000001

Type Conversion (Casting):

  • Implicit type coercion: value of one type is automatically (implicitly) changed to another type
  • Explicit type conversion: use cast operator (two forms)
    1. static_cast<dataTypeName>(expression): more stable
    2. dataType(expression): C-style typecasting

Type Casting Examples:

static_cast<float>(5) = 5.0
int(5.5) = 5 //C-style typecasting
int('A') = 65 //C-style typecasting

***Remember: char data type yields an integral value. Review (ASCII table).***


Input, Memory, and Data

Using Input:

  • Data must be loaded into main memory before it can be manipulated
  • Storing data in memory is two-step process:
    1. Instruct computer to allocate memory
    2. Include statements to put data into allocated memory

Allocating Memory (Constants and Variables):

  • Name and define data type to store data for each memory location
  • Declaration statement used to allocate memory
  • Variables: memory cells whose contents can be modified during program execution
  • Possible to declare multiple variables in same statement (must be same type)
  • Named Constant: memory location whose data cannot change during program execution
  • Using named constant simplifies code modification — change in declaration statement affects code globally
  • In C++, const is reserved word

Four characteristics of variables:

  1. Name
  2. Type
  3. Size
  4. Value

Syntax for declaring variables to allocate memory:

dataType identifier;

Examples:
int myVar; //declares one int variable
float myFloat, myFloat2, myFloat3; //declares three float variables

Syntax for declaring named constants to allocate memory (uses keyword const and must assign value):

const dataType identifier = value;

Example:
const double PI = 3.14;

Storing Data into Variables (declaring and initializing variables):

  • Variables can be declared anywhere in program
  • Must be declared before can be used
  • In C++, = called assignment operator
  • Data stored in memory (using name of variable or constant), either using assignment statement or input statement
  • C++ does not automatically initialize variables
  • When variable declared, only memory is allocated
  • When variable declared with no value, memory cell may contain value from setting of bits from previous use
  • Expression on right side is evaluated, then its value is assigned to variable (a memory location) on left side
  • Variable initialization: variable given a value at declaration (good programming principle)
  • Remember: assignment (=) is NOT equality (==)

Assignment Statement Examples:

variable = expression;

myVar = 10; //assigns value of 10 to myVar

int i; //allocates memory for variable i
i = i + 2; //evaluates i (right side), adds two to it, and assigns new value to memory location i (left side)

Initialization Statement Examples:

int variable = expression;

int myVar = 10; //allocates memory for variable myVar, and assigns value of 10 to myVar

//declares two double variables (myDouble and myDouble3), initializes myDouble2 with value of 25.5
double myDouble, myDouble2=25.5, myDouble3;

Input (Read) Statement:

  • cin used with >> (stream extraction operator) to gather input
  • When value for variable not known before program is written—value input through cin and >>(stream extraction operator)

Input (Read) Statement Examples:

cin >> variableName >> variableName. . .;

cin >> miles; //computer gets value from standard input device, and places value in memory cell named miles

cin >> feet >> inches; //input multiple values into multiple memory locations with single statement  

Problems Using Mixed Values:

  • Assigning Mixed Values:
    • Assigning floating-point value to int type variable — fractional part dropped (truncated)
    • Assigning integer value to floating-point type variable — adds decimal point (.) and 0 (zero)
  • Reading Mixed Values:
    • Reading (cin >>) floating-point value into int type variable, reading stops at decimal point
    • Remaining value stays in buffer
    • May get error on next read
    • Reading integer value into floating-point type variable — adds decimal point (.) and 0 (zero)
  • ***Problems occur ONLY when placing floating-point values into int type variables, NOT the reverse.***

Increment and Decrement Operators

Using Increment and Decrement Operators:

  • Increment operator (++) increases value of variable by 1
  • Decrement operator (—) decreases value of variable by 1
  • Increment and decrement operators each have two forms: prefix and postfix
  • Prefix syntax:
    ++ variable or --variable
    
    Postfix syntax:
    variable++ or variable--
    

Prefix and Postfix Operator Differences:

  • Prefix and postfix operators used as stand-alone statement—results are same:
  • x = 0;
    
    //either way, x = 1 
    ++x;
    x++;
    
  • Differences in prefix and postfix operators occur in assignment:
  • //First statement assigns 0 to x
    //Second statement increments x to 1 and assigns 1 to y.  Both have the value of 1
    x = 0; 
    y = ++x;
    
    //versus...
    
    //First statement assigns 0 to x
    //Second statement assigns 0 to y and increments x to 1
    //Result: x evaluates to 1 and y evaluates to 0  
    //***Parentheses can override order of operations***
    x = 0; 
    y = x++;
    

Output

Using Output:

  • Output statement written with cout and stream insertion operator (<<)
  • Standard output device normally monitor screen
  • To move cursor to beginning of next line requires either
    predefined word endl, with insertion operator (<<), or escape sequence ‘n’
  • In C++, called escape character and n called newline escape sequence
  • To include cin and cout, appropriate header file must be included:
  • #include <iostream>

Escape Sequence Examples:

  n	//Newline: cursor moves to beginning of next line
  t	//Tab: cursor moves to next tab stop
  b	//Backspace: cursor moves one space left
  r	//Return:	cursor moves to beginning of current line (not next line)
  \	//Backslash: backslash printed
  '	//Single quotation: single quotation mark printed
  "	//Double quotation: double quotation mark printed

Preprocessor Directives:

  • In C++, most operations and functions must be imported from collection of libraries
  • For example, for input/output, header file iostream must be included
  • Preprocessor directives (i.e., names of header files) tell computer where to locate required libraries
  • Preprocessor directives processed by program called preprocessor—before program goes through compiler
  • Preprocessor commands begin with #—must appear at beginning of program
  • General syntax:
  • #include<headerFileName>
    

Using cin/cout and namespace:

  • C++ program contains two parts:
    1. Preprocessor directives (include header files)
    2. The program
  • Preprocessor directives and program statements create C++ source code
  • Source code must be saved in file with file extension .cpp
  • General syntax:
  • #include <iostream>
    using namespace std;
    
  • Compiler generates object code
  • Saved in file with .obj file extension
  • Executable code produced and saved in file with .exe file extension
  • Commonly used header files:
  • Older versions of C++, header files had file extension .h (using no namespace)
  • In (ANSI/ISO Standard) C++, cin/cout declared in header file iostream, within namespace std
  • ANSI C++ removes extension
  • To use cin and cout, use following two statements:
  • #include <iostream>
    using namespace std;
    

Commonly used header files:

<iostream>  //input/output 
<cmath>  //math functions
<string>  //string functions
<iomanip>  //formatting manipulations for input/output

Using string Data Type:

  • Must access definition from header file string
  • Include following preprocessor directive:
    #include<string>

Creating a C++ Program

Putting It Together:

  • Two Parts:
    1. Preprocessor directives (include files)
    2. The program
  • Preprocessor directives and program statements constitute C++ source code
  • Source code saved in file with file extension .cpp
  • Compiler generates object code (saved in file with .obj file extension)
  • Executable code produced (saved in file with .exe file extension)

C++ Program Form and Style:

  • Every C++ program MUST have one and ONLY one function main()
  • Basic parts of function main() (as with any other C++ function):
    1. Function Heading
    2. Function Body
  • Syntax errors found in compilation (i.e., by compiler)
  • Use of Blanks:
    • One or more blanks separate input numbers
    • Blanks also used to separate reserved words, identifiers,and other symbols
  • Semicolons, Brackets, and Commas:
    • All C++ statements end with a semicolon (statement terminator)
    • Brackets {} separate blocks of code
    • Commas separate items in a list
  • Semantics (meaning or interpretation):
    • Program may not run, or run improperly, even with syntax errors removed
    • For example, 2 + 3 * 5 and (2 + 3) * 5
      both are syntactically correct expressions, with different meanings
  • Documentation:
    • Comments help document code:
      Single line comments begin with //, multiple line comments enclosed between /* and */
    • Identifiers should employ meaningful names (e.g., firstName, empID)
    • Identifiers using more than one word: capitalize first letter of each new word, or use underscore between each word
  • User Prompts:
    • When program input through keyboard is expected, must inform user
    • Prompt commands: statements that inform user of expected input
    • Additionally, «echo» print user input for verification
  • More on Assignment Statements:
    • Compound assignment statements: shorthand method of assignment
    • Compound assignment defined: op is binary arithmetic operator, op=
    • Simple assignment statement variable = variable op (another variable or expression);
    • Rewritten as compound assignment: variable op= (another variable or expression);
    • Example:
    • +=, -=, *=, /=, and %=
      
      x *= y; //equivalent to x = x * y;
      

Simple Program:

  • Begin program with comments for documentation
  • Include header files
  • Declare named constants, if any
  • Write definition of function main()

Summary:

  • Program begins with comments (documentation)
  • System resources used for I/O
  • Input statements: get data (e.g., keyboard, file, etc.)
  • Output statements: print/display (or echo) results (e.g., screen, file, etc.)
  • First statement of program—after comments—is preprocessor directive
  • Preprocessor directive includes header file (e.g., iostream)
  • Two types of memory locations for data manipulation:
    1. Named constants (usually put before main when used throughout program)
    2. Variables
  • Program must contain at least one function: main()
  • Variables to manipulate data, can be declared in function main()
  • Body of function main() has following form:
  • int main ()
    {
      variable declarations;
      statements...;
      return 0;
    }
    

Sample Program Code

Compiling and Running a Program



Data types in C refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in the storage and how the bit pattern stored is interpreted.

The following table provides all the data types that you will use during Arduino programming.

void Boolean char Unsigned char byte int Unsigned int word
long Unsigned long short float double array String-char array String-object

void

The void keyword is used only in function declarations. It indicates that the function is expected to return no information to the function from which it was called.

Example

Void Loop ( ) {
   // rest of the code
}

Boolean

A Boolean holds one of two values, true or false. Each Boolean variable occupies one byte of memory.

Example

boolean val = false ; // declaration of variable with type boolean and initialize it with false
boolean state = true ; // declaration of variable with type boolean and initialize it with true

Char

A data type that takes up one byte of memory that stores a character value. Character literals are written in single quotes like this: ‘A’ and for multiple characters, strings use double quotes: «ABC».

However, characters are stored as numbers. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic operations on characters, in which the ASCII value of the character is used. For example, ‘A’ + 1 has the value 66, since the ASCII value of the capital letter A is 65.

Example

Char chr_a = ‘a’ ;//declaration of variable with type char and initialize it with character a
Char chr_c = 97 ;//declaration of variable with type char and initialize it with character 97

ASCII Char Table

unsigned char

Unsigned char is an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255.

Example

Unsigned Char chr_y = 121 ; // declaration of variable with type Unsigned char and initialize it with character y

byte

A byte stores an 8-bit unsigned number, from 0 to 255.

Example

byte m = 25 ;//declaration of variable with type byte and initialize it with 25

int

Integers are the primary data-type for number storage. int stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) — 1).

The int size varies from board to board. On the Arduino Due, for example, an int stores a 32-bit (4-byte) value. This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) — 1).

Example

int counter = 32 ;// declaration of variable with type int and initialize it with 32

Unsigned int

Unsigned ints (unsigned integers) are the same as int in the way that they store a 2 byte value. Instead of storing negative numbers, however, they only store positive values, yielding a useful range of 0 to 65,535 (2^16) — 1). The Due stores a 4 byte (32-bit) value, ranging from 0 to 4,294,967,295 (2^32 — 1).

Example

Unsigned int counter = 60 ; // declaration of variable with 
   type unsigned int and initialize it with 60

Word

On the Uno and other ATMEGA based boards, a word stores a 16-bit unsigned number. On the Due and Zero, it stores a 32-bit unsigned number.

Example

word w = 1000 ;//declaration of variable with type word and initialize it with 1000

Long

Long variables are extended size variables for number storage, and store 32 bits (4 bytes), from -2,147,483,648 to 2,147,483,647.

Example

Long velocity = 102346 ;//declaration of variable with type Long and initialize it with 102346

unsigned long

Unsigned long variables are extended size variables for number storage and store 32 bits (4 bytes). Unlike standard longs, unsigned longs will not store negative numbers, making their range from 0 to 4,294,967,295 (2^32 — 1).

Example

Unsigned Long velocity = 101006 ;// declaration of variable with 
   type Unsigned Long and initialize it with 101006

short

A short is a 16-bit data-type. On all Arduinos (ATMega and ARM based), a short stores a 16-bit (2-byte) value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) — 1).

Example

short val = 13 ;//declaration of variable with type short and initialize it with 13

float

Data type for floating-point number is a number that has a decimal point. Floating-point numbers are often used to approximate the analog and continuous values because they have greater resolution than integers.

Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.

Example

float num = 1.352;//declaration of variable with type float and initialize it with 1.352

double

On the Uno and other ATMEGA based boards, Double precision floating-point number occupies four bytes. That is, the double implementation is exactly the same as the float, with no gain in precision. On the Arduino Due, doubles have 8-byte (64 bit) precision.

Example

double num = 45.352 ;// declaration of variable with type double and initialize it with 45.352

Понравилась статья? Поделить с друзьями:
  • Ряды фурье на excel
  • С какого знака должна начинаться формула в ячейке microsoft excel
  • С какими данными может работать excel
  • С какой знака начинается запись формулы в excel
  • Ряды для вычисления в excel