summaryrefslogtreecommitdiff
path: root/reference/C/SYNTAX/sizeof.html
blob: 34dc287645bf0d1d6745a3d33dde062c9818b2df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<title>The sizeof operator</title>
<head>
<script language="JavaScript">
</script>
</head>
<body bgcolor="#ffffcc">
<hr>
<center>
<h1>The sizeof operator</h1>
</center>
<hr>
<p>
<b>sizeof</b> will return the number of bytes reserved for a variable or 
<a href="../CONCEPT/data_types.html">data type</a>. 
<p>
The following code shows <b>sizeof</b> returning the length of a data type. 
<p>
<table border=2 width="100%" bgcolor=ivory>
<tr>
<td>
<pre>

        /* How big is an int? expect an answer of 4. */
	
	main()
 	{
	   printf("%d \n", sizeof(int));
        }
</pre>
</td>
</tr>
</table>
<p>
	   
<b>sizeof</b> will also return the number of bytes reserved for a 
<a href="struct.html">structure</a>.
<p>
<table border=2 width="100%" bgcolor=ivory>
<tr>
<td>
<pre>

        /* Will print 8 on most machines. */
	
        main()
	{
	  struct 
	  {
	    int a;
	    int b;
	  } TwoInts;
	
	printf("%d \n", sizeof(TwoInts));
	}
	
</pre>
</td>
</tr>
</table>
<p>
Finally, <b>sizeof</b> will return the length of a variable. 

<p>
<table border=2 width="100%" bgcolor=ivory>
<tr>
<td>
<pre>

	main()
	{
	  char String[20];
	  
	  printf ("%d \n", sizeof String);
 	  printf ("%d \n", sizeof (String));
        }

</pre>
</td>
</tr>
</table>
<p>
In the example above I have printed the size of 'String' twice. This is to 
show that when dealing with variables, the brackets are optional. 

I recommend that you always place the brackets around the sizeof argument.
<p>
<hr>
<h2>Examples:</h2>
<a href="../EXAMPLES/sizeof.c">Example 1</a> Data types.
<p>
<a href="../EXAMPLES/sizeof1.c">Example 2</a> Data objects.

<p>
<hr>
<h2>See also:</h2>
<img src="../../GRAPHICS/whiteball.gif">
The <a href="../FUNCTIONS/strlen.html">strlen </a>function.
<p>
<img src="../../GRAPHICS/whiteball.gif">
Other <a href="../CONCEPT/expressions.html">operators</a>
<p>
<img src="../../GRAPHICS/whiteball.gif">
<a href="../FUNCTIONS/malloc.html">malloc</a> function.


<p>

<hr>
<p>
<center>
<table border=2 width="80%" bgcolor="ivory">
<tr align=center>
<td width="25%">
<a href="../cref.html">                  Top</a>
</td><td width="25%">
<a href="../master_index.html">          Master Index</a>
</td><td width="25%">
<a href="keywords.html">                 Keywords</a>
</td><td width="25%">
<a href="../FUNCTIONS/funcref.htm">      Functions</a>
</td>
</tr>
</table>
</center>
<p>
<hr>
<address>Martin Leslie 
</address><p>
</body>
</html>