|
|
1.1 root 1:
2:
3: sizeof C Keyword sizeof
4:
5:
6:
7:
8: Return size of a data element
9:
10:
11: sizeof is a C operator that returns a constant int that gives the
12: size of any given data element. The element examined can be a
13: data object, a portion of a data object, or a type cast. sizeof
14: returns the size of the element in chars; for example
15:
16:
17: long foo;
18: sizeof foo;
19:
20:
21: returns four, because a lloonngg is as long as four cchhaarrs.
22:
23: sizeof can also tell you the size of an array. This is
24: especially helpful for use with external arrays, whose size can
25: be set when they are initialized. For example:
26:
27:
28: char *arrayname[] = {
29: "COHERENT", "Mark Williams C for the Atari ST",
30: "Let's C", "Fast Forward"
31: };
32:
33:
34:
35: main()
36: {
37: printf("\"arrayname\" has %d entries\n",
38: sizeof(arrayname)/sizeof char*);
39: }
40:
41:
42: sizeof is especially useful in malloc routines, and when you need
43: to specify byte counts to I/O routines. Using it to set the size
44: of data types instead of using a predetermined value will in-
45: crease the portability of your code.
46:
47: ***** See Also *****
48:
49: C keywords, data types, operators
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.