Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

C Programming/Language Reference

From Wikibooks, open books for an open world
<C Programming
Previous: Code libraryC ProgrammingNext: Compilers

Table of keywords

[edit |edit source]

ANSI (American National Standards Institute) C (C89)/ISO C (C90)

[edit |edit source]

Very old compilers may not recognize some or all of the C89 keywordsconst,enum,signed,void,volatile, as well as any later standards' keywords.

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

ISO C (C99)

[edit |edit source]

These are supported in most new compilers.

  • _Bool
  • _Complex
  • _Imaginary
  • inline

ISO C (C11)

[edit |edit source]

These are supported only in some newer compilers

  • alignof
  • _Alignas
  • _Atomic
  • _Generic
  • _Noreturn
  • _Static_assert
  • _Thread_local

Although not technically a keyword, C99-capable preprocessors/compilers additionally recognize the special preprocessor operator_Pragma, which acts as an alternate form of the#pragma directive that can be used from within macro expansions. For example, the following code will cause some compilers (incl. GCC, Clang) to emit a diagnostic message:

#define EMIT_MESSAGE(str)    EMIT_PRAGMA(message(str))#define EMIT_PRAGMA(content) _Pragma(#content)EMIT_MESSAGE("Hello, world!")

Some compilers use a slight variant syntax; in particular, MSVC supports__pragma instead of_Pragma.

Specific compilers may also—in a non-standards-compliant mode, or with additional syntactic markers like__extension__—treat some other words as keywords, includingasm,cdecl,far,fortran,huge,interrupt,near,pascal, ortypeof. However, they typically allow these keywords to be overridden by declarations when operating in standards-compliant modes (e.g., by defining a variable namedtypeof), in order to avoid introducing incompatibilities with existing programs. In order to ensure the compiler can maintain access to extension features, these compilers usually have an additional set of proper keywords beginning with two underscores (__). For example, GCC treatsasm,__asm, and__asm__ somewhat identically, but the latter two are always guaranteed to have the expected meaning since they can't be overridden.

Many of the newly introduced keywords—namely, those beginning with an underscore and capital letter like_Noreturn or_Imaginary—are intended to be used only indirectly in most situations. Instead, the programmer should prefer the use of standard headers such as<stdbool.h> or<stdalign.h>, which typically use the preprocessor to establish an all-lower-case variant of the keyword (e.g.,complex ornoreturn). These headers serve the purpose of enabling C and C++ code, as well as code targeting different compilers or language versions, to interoperate more cleanly. For example, by including<stdbool.h>, the tokensbool,true, andfalse can be used identically in either C99 or C++ without having to explicitly use_Bool in C99 orbool in C++.

See also the list of reserved identifiers[1].

Table of operators

[edit |edit source]

Operators in the same row of this table have the sameprecedence and the order of evaluation is decided by theassociativity (left-to-right orright-to-left). Operators closer to the top of this table havehigher precedence than those in a subsequent group.

OperatorsDescriptionExample UsageAssociativity
Postfix operatorsLeft to right
()function call operatorswap (x, y)
[]array index operatorarr [i]
.member access operator
for an object of struct/union type
or a reference to it
obj.member
->member access operator
for a pointer to an object of
struct/union type
ptr->member

Unary OperatorsRight to left
!logical not operator!eof_reached
~bitwise not operator~mask
+ -[2]unary plus/minus operators-num
++ --post-increment/decrement operatorsnum++
++ --pre-increment/decrement operators++num
&address-of operator&data
*indirection operator*ptr
sizeofsizeof operatorfor expressionssizeof 123
sizeof()sizeof operatorfor typessizeof (int)
(type)cast operator(float)i

Multiplicative OperatorsLeft to right
* / %multiplication, division and
modulus operators
celsius_diff * 9.0 / 5.0

Additive OperatorsLeft to right
+ -addition and subtraction operatorsend - start + 1

Bitwise Shift OperatorsLeft to right
<<left shift operatorbits << shift_len
>>right shift operatorbits >> shift_len

Relational Inequality OperatorsLeft to right
< > <= >=less-than, greater-than, less-than or
equal-to, greater-than or equal-to
operators
i < num_elements

Relational Equality OperatorsLeft to right
== !=equal-to, not-equal-tochoice != 'n'

Bitwise And OperatorLeft to right
&bits & clear_mask_complement

Bitwise Xor OperatorLeft to right
^bits ^ invert_mask

Bitwise Or OperatorLeft to right
|bits | set_mask

Logical And OperatorLeft to right
&&arr != 0 && arr->len != 0

Logical Or OperatorLeft to right
||arr == 0 || arr->len == 0
Conditional OperatorRight to left
?:size != 0 ? size : 0

Assignment OperatorsRight to left
=assignment operatori = 0
+= -= *= /=
%= &= |= ^=
<<= >>=
shorthand assignment operators
(foo op=bar represents
foo =foo opbar)
num /= 10

Comma OperatorLeft to right
,i = 0, j = i + 1, k = 0

Table of data types

[edit |edit source]
TypeSize in BitsCommentsAlternative Names
Primitive Types in ANSI C (C89)/ISO C (C90)
char≥ 8
  • sizeof gives the size in units ofchars. These "C bytes" need not be 8-bit bytes (though commonly they are); the number of bits is given by theCHAR_BIT macro in thelimits.h header.
  • Signedness is implementation-defined.
  • Any encoding of 8 bits or less (e.g. ASCII) can be used to store characters.
  • Integer operations can be performed portably only for the range 0 ~ 127.
  • All bits contribute to the value of thechar, i.e. there are no "holes" or "padding" bits.
signedcharsame aschar
  • Characters stored like for typechar.
  • Can store integers in the range -127 ~ 127 portably[3].
unsignedcharsame aschar
  • Characters stored like for typechar.
  • Can store integers in the range 0 ~ 255 portably.
short≥ 16, ≥ size ofchar
  • Can store integers in the range -32767 ~ 32767 portably[4].
  • Used to reduce memory usage (although the resulting executable may be larger and probably slower as compared to usingint.
shortint,signedshort,signedshortint
unsignedshortsame asshort
  • Can store integers in the range 0 ~ 65535 portably.
  • Used to reduce memory usage (although the resulting executable may be larger and probably slower as compared to usingint.
unsignedshortint
int≥ 16, ≥ size ofshort
  • Represents the "normal" size of data the processor deals with (the word-size); this is the integral data-type used normally.
  • Can store integers in the range -32767 ~ 32767 portably[4].
signed,signedint
unsignedintsame asint
  • Can store integers in the range 0 ~ 65535 portably.
unsigned
long≥ 32, ≥ size ofint
  • Can store integers in the range -2147483647 ~ 2147483647 portably[5].
longint,signedlong,signedlongint
unsignedlongsame aslong
  • Can store integers in the range 0 ~ 4294967295 portably.
unsignedlongint
float≥ size ofchar
  • Used to reduce memory usage when the values used do not vary widely.
  • The floating-point format used is implementation defined and need not be the IEEE single-precision format.
  • unsigned cannot be specified.
double≥ size offloat
  • Represents the "normal" size of data the processor deals with; this is the floating-point data-type used normally.
  • The floating-point format used is implementation defined and need not be the IEEE double-precision format.
  • unsigned cannot be specified.
longdouble≥ size ofdouble
  • unsigned cannot be specified.

Primitive Types added to ISO C (C99)
longlong≥ 64, ≥ size oflong
  • Can store integers in the range -9223372036854775807 ~ 9223372036854775807 portably[6].
longlongint,signedlonglong,signedlonglongint
unsignedlonglongsame aslonglong
  • Can store integers in the range 0 ~ 18446744073709551615 portably.
unsignedlonglongint
intmax_tthe maximum width supported by the platform
uintmax_tsame asintmax_t
  • Can store integers in the range 0 ~ (1 << n)-1, with 'n' the width of uintmax_t.

User Defined Types
struct≥ sum of size of each member
  • Said to be anaggregate type.
union≥ size of the largest member
  • Said to be anaggregate type.
enum≥ size ofchar
  • Enumerations are a separate type fromints, though they are mutually convertible.
typedefsame as the type being given a name
  • typedef has syntax similar to a storage class likestatic,register orextern.

Derived Types[7]
type*

(pointer)
≥ size ofchar
  • 0 always represents the null pointer (an address where no data can be placed), irrespective of what bit sequence represents the value of a null pointer.
  • Pointers to different types may have different representations, which means they could also be of different sizes. So they are not convertible to one another.
  • Even in an implementation which guarantess all data pointers to be of the same size, function pointers and data pointers are in general incompatible with each other.
  • For functions taking variable number of arguments, the arguments passed must be of appropriate type, so even0 must be cast to the appropriate type in such function-calls.
type [integer[8]]

(array)
integer × size oftype
  • The brackets ([])follow the identifier name in a declaration.
  • In a declaration which also initializes the array (including a function parameter declaration), the size of the array (theinteger) can be omitted.
  • type [] is not the same astype*. Only under some circumstances one can be converted to the other.
type (comma-delimited list of types/declarations)

(function)
  • Functions declared without any storage class areextern.
  • The parentheses (())follow the identifier name in a declaration, e.g. a 2-arg function pointer:int (* fptr) (int arg1,int arg2).

Character sets

[edit |edit source]

Programs written in C can read and write any character set, provided the libraries that support them are included/used.

The source code for C programs, however, is usually limited to the ASCII character set.

In a file containing source code, the end of a line is sometimes, depending on the operating system it was created on not a newline character but compilers treat the end of each line as if it were a single newline character.

Virtually all compilers allow the$,@, and` characters in string constants. Many compilers also allow literal multibyte Unicode characters, but they are not portable.

Certain characters must be escaped with a backslash to represent themselves in a string or character constant. These are:

  • \\ Literal backslash
  • \" Literal double quote
  • \' Literal single quote
  • \n Newline
  • \t Horizontal tab
  • \f Form feed
  • \v Vertical tab

Additionally, some compilers allow these characters:

  • \r Carriage return
  • \a Alert (audible bell)
  • \b Backspace

\xhh, where the 'h' characters are hexadecimal digits, is used to represent arbitrary bytes (including\x00, the zero byte).

\uhhhh or\Uhhhhhhhh, where the 'h' characters are hexadecimal digits, is used to portably represent Unicode characters.

References

[edit |edit source]
  1. http://publib.boulder.ibm.com/infocenter/comphelp/v7v91/topic/com.ibm.vacpp7a.doc/language/ref/clrc02reserved_identifiers.htm list of reserved identifiers
  2. Very old compilers may not recognize the unary+ operator.
  3. -128 can be stored in two's-complement machines (i.e. most machines in existence). Very old compilers may not recognize thesigned keyword
  4. ab-32768 can be stored in two's-complement machines (i.e. most machines in existence). Very old compilers may not recognize thesigned keyword
  5. -2147483648 can be stored in two's-complement machines (i.e. most machines in existence). Very old compilers may not recognize thesigned keyword
  6. -9223372036854775808 can be stored in two's-complement machines (i.e. most machines in existence)
  7. The precedences in a declaration are:
    [],() (left associative) — Highest
    * (right associative) — Lowest
  8. The standards do NOT place any restriction on the size/type of the integer, it's implementation dependent. The only mention in the standards is a reference that an implementation may have limits to the maximum size of memory block which can be allocated, and as such the limit on integer will be size_of_max_block/sizeof(type)
Previous: Code libraryC ProgrammingNext: Compilers
Retrieved from "https://en.wikibooks.org/w/index.php?title=C_Programming/Language_Reference&oldid=3670420"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp