{"id":449,"date":"2023-06-28T12:49:51","date_gmt":"2023-06-28T12:49:51","guid":{"rendered":"http:\/\/localhost\/embeddedwala\/?p=449"},"modified":"2023-07-27T04:10:36","modified_gmt":"2023-07-27T04:10:36","slug":"sizeof-operator-related-codes","status":"publish","type":"post","link":"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/","title":{"rendered":"SizeOf Operator Related Codes"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"449\" class=\"elementor elementor-449\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-top-section elementor-element elementor-element-f6c5bb9 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"f6c5bb9\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-e89c44f\" data-id=\"e89c44f\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-inner-section elementor-element elementor-element-27611f0 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"27611f0\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-5416f63\" data-id=\"5416f63\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-243fd24 elementor-widget elementor-widget-text-editor\" data-id=\"243fd24\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>The\u00a0<strong><code>sizeof<\/code><\/strong>\u00a0operator in C is a\u00a0<strong>unary operator<\/strong>\u00a0that returns the size, in bytes, of the operand. The operand can be a\u00a0<strong>variable<\/strong>, a\u00a0<strong>data type<\/strong>, or an\u00a0<strong>expression<\/strong>. In this blog the\u00a0<code>sizeof<\/code>\u00a0operator is discussed in detail, including its syntax, examples, and applications.<\/p><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_51_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor: pointer\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\" role=\"button\"><label for=\"item-69e1bbe150d96\" ><span class=\"\"><span style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #337ab7;color:#337ab7\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #337ab7;color:#337ab7\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input aria-label=\"Toggle\" aria-label=\"item-69e1bbe150d96\"  type=\"checkbox\" id=\"item-69e1bbe150d96\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Syntax\" title=\"Syntax\">Syntax<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Examples\" title=\"Examples\">Examples<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Example-1-Size-of-data-types\" title=\"Example 1: Size of data types\">Example 1: Size of data types<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Example-2-Size-of-arrays\" title=\"Example 2: Size of arrays\">Example 2: Size of arrays<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Example-3-Size-of-structures\" title=\"Example 3: Size of structures\">Example 3: Size of structures<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Example-4-Size-of-pointer-variables\" title=\"Example 4: Size of pointer variables\">Example 4: Size of pointer variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Example-5-Size-of-for-memory-allocation\" title=\"Example 5: Size of for memory allocation\">Example 5: Size of for memory allocation<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/sizeof-operator-related-codes\/#Applications\" title=\"Applications\">Applications<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Syntax\"><\/span>Syntax<span class=\"ez-toc-section-end\"><\/span><\/h2><p>The syntax of the\u00a0<code>sizeof<\/code>\u00a0operator is as follows:<\/p><div><pre>sizeof(operand)\n<\/pre><\/div><p>The operand can be a variable, a data type, or an expression. The\u00a0<code>sizeof<\/code>\u00a0operator returns the size of the operand in bytes.<\/p><h2><span class=\"ez-toc-section\" id=\"Examples\"><\/span>Examples<span class=\"ez-toc-section-end\"><\/span><\/h2><p>Let&#8217;s look at some examples of the\u00a0<code>sizeof<\/code>\u00a0operator in C.<\/p><h3><span class=\"ez-toc-section\" id=\"Example-1-Size-of-data-types\"><\/span>Example 1: Size of data types<span class=\"ez-toc-section-end\"><\/span><\/h3><p>The\u00a0<code>sizeof<\/code>\u00a0operator can be used to determine the size of data types in C. The following example shows the sizes of some commonly used data types:<\/p><div><pre>#include &lt;stdio.h&gt; <br \/>\nint main() \n{\n   printf(\"Size of char: %ld byte(s)\\n\", sizeof(char));\n   printf(\"Size of short: %ld byte(s)\\n\", sizeof(short));\n   printf(\"Size of int: %ld byte(s)\\n\", sizeof(int));\n   printf(\"Size of long: %ld byte(s)\\n\", sizeof(long));\n   printf(\"Size of float: %ld byte(s)\\n\", sizeof(float));\n   printf(\"Size of double: %ld byte(s)\\n\", sizeof(double));\n   printf(\"Size of long double: %ld byte(s)\\n\", sizeof(long double));\n   printf(\"Size of void: %ld byte(s)\\n\", sizeof(void));\n   return 0;\n}\n<\/pre><\/div><p><strong>Output:<\/strong><\/p><div><pre>Size of char: 1 byte(s)\nSize of short: 2 byte(s)\nSize of int: 4 byte(s)\nSize of long: 8 byte(s)\nSize of float: 4 byte(s)\nSize of double: 8 byte(s)\nSize of long double: 16 byte(s)\nSize of void: 1 byte(s)\n<\/pre><\/div><h3><span class=\"ez-toc-section\" id=\"Example-2-Size-of-arrays\"><\/span>Example 2: Size of arrays<span class=\"ez-toc-section-end\"><\/span><\/h3><p>The\u00a0<code>sizeof<\/code>\u00a0operator can also be used to determine the size of arrays in C. The following example shows the size of an array of integers:<\/p><div><pre>#include &lt;stdio.h&gt;\n\nint main() \n{\n   int arr[] = {1, 2, 3, 4, 5};\n   int n = sizeof(arr) \/ sizeof(arr[0]);\n   printf(\"Size of arr[]: %ld byte(s)\\n\", sizeof(arr));\n   printf(\"Number of elements in arr[]: %d\\n\", n);\n   return 0;\n}\n<\/pre><\/div><p><strong>Output:<\/strong><\/p><div><pre>Size of arr[]: 20 byte(s)\nNumber of elements in arr[]: 5\n<\/pre><\/div><h3><span class=\"ez-toc-section\" id=\"Example-3-Size-of-structures\"><\/span>Example 3: Size of structures<span class=\"ez-toc-section-end\"><\/span><\/h3><p>The\u00a0<code>sizeof<\/code>\u00a0operator can also be used to determine the size of structures in C. The following example shows the size of a structure that contains two integers:<\/p><div><pre>#include \n\nstruct Point \n{\n   int x;\n   int y;\n};\n\nint main() \n{\n   struct Point p;\n   printf(\"Size of struct Point: %ld byte(s)\\n\", sizeof(struct Point));\n   printf(\"Size of struct Point variable p: %ld byte(s)\\n\", sizeof(p));\n   return 0;\n}\n<\/pre><\/div><p><strong>Output:<\/strong><\/p><div><pre>Size of struct Point: 8 byte(s)\nSize of struct Point variable p: 8 byte(s)\n<\/pre><\/div><h3><span class=\"ez-toc-section\" id=\"Example-4-Size-of-pointer-variables\"><\/span>Example 4: Size of pointer variables<span class=\"ez-toc-section-end\"><\/span><\/h3><p>The\u00a0<code>sizeof<\/code>\u00a0operator can also be used to determine the size of pointer variables in C. The following\u00a0example shows the size of a pointer variable that points to an integer:<\/p><div><pre>#include \n\nint main() \n{\n   int *p;\n   printf(\"Size of pointer variable p: %ld byte(s)\\n\", sizeof(p));\n   return 0;\n}\n<\/pre><\/div><p><strong>Output:<\/strong><\/p><div><pre>Size of pointer variable p: 8 byte(s)\n<\/pre><\/div><div class=\"bg-black mb-4 rounded-md\"><div class=\"p-4 overflow-y-auto\"><code class=\"!whitespace-pre hljs language-css\"><\/code><\/div><\/div><p><strong>Note:<\/strong>\u00a0The size of a pointer variable is usually the same as the size of a memory address on the system.<\/p><h3><span class=\"ez-toc-section\" id=\"Example-5-Size-of-for-memory-allocation\"><\/span>Example 5: Size of for memory allocation<span class=\"ez-toc-section-end\"><\/span><\/h3><p>The sizeof operator can also be used for memory allocation as shown below:<\/p><div><pre>#include \n#include \n\nint main() \n{\n   int *p;\n   int size = 5;\n   p = (int *)malloc(size * sizeof(int));\n   \n   if (p == NULL) \n   {\n      printf(\"Memory allocation failed!\\n\");\n      return 1;\n   }\n   \n   for (int i = 0; i &lt; size; i++) \n   {\n      p[i] = i + 1;\n   }\n   \n   printf(\"The values of the array are: \");\n   for (int i = 0; i &lt; size; i++) \n   {\n      printf(\"%d \", p[i]);\n   }\n   \n   free(p);\n   return 0;\n}\n<\/pre><\/div><p><strong>Output:<\/strong><\/p><div><pre>The values of the array are: 1 2 3 4 5 \n<\/pre><\/div><p>In this example, the\u00a0<code>malloc()<\/code>\u00a0function is used to allocate memory for an integer array of size 5. The\u00a0<code>sizeof<\/code>\u00a0operator is used to determine the size of each integer element in bytes, which is then multiplied by the number of elements in the array to calculate the total size of the array in bytes. The\u00a0<code>malloc()<\/code>\u00a0function returns a pointer to the first byte of the allocated memory block.<\/p><p>After allocating memory for the array, it checks if the memory allocation was successful<code><\/code>. If the memory allocation failed, it prints an error message and exit the program.<\/p><div class=\"google-auto-placed ap_container\"><p>Next, it initialize the values of the array by iterating over each element and assigning it a value of <code>i + 1 and\u00a0<\/code>prints the values of the array and free the allocated memory using the\u00a0<code>free()<\/code>\u00a0function to avoid memory leaks.<\/p><\/div><h2><span class=\"ez-toc-section\" id=\"Applications\"><\/span>Applications<span class=\"ez-toc-section-end\"><\/span><\/h2><p>As discussed above, The\u00a0<code>sizeof<\/code>\u00a0operator has several applications in C programming, including:<\/p><ul><li><strong>Memory allocation<\/strong>: The\u00a0<code>sizeof<\/code>\u00a0operator is commonly used to allocate memory for variables, arrays, and structures using functions like\u00a0<code>malloc()<\/code>\u00a0and\u00a0<code>calloc()<\/code>.<\/li><li><strong>Array manipulation<\/strong>: The\u00a0<code>sizeof<\/code>\u00a0operator is used to determine the size of an array, which is often used to iterate over the array or perform other operations on the array.<\/li><li><strong>Structure manipulation<\/strong>: The\u00a0<code>sizeof<\/code>\u00a0operator is used to determine the size of a structure, which is often used to allocate memory for a structure or to perform operations on the structure.<\/li><li><strong>Performance optimization<\/strong>: The\u00a0<code>sizeof<\/code>\u00a0operator can be used to optimize performance by reducing memory usage and improving cache locality. For example, when iterating over an array, using the\u00a0<code>sizeof<\/code>\u00a0operator to determine the size of the array can help avoid accessing out-of-bounds memory, which can reduce cache misses and improve performance.<\/li><\/ul><p>In conclusion, the\u00a0<code>sizeof<\/code>\u00a0operator is a powerful tool in C programming that allows the user to determine the size of variables, data types, arrays, structures, and pointers. It has several applications in memory allocation, array and structure manipulation, and performance optimization, making it an essential operator to understand for any C programmer.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>The\u00a0sizeof\u00a0operator in C is a\u00a0unary operator\u00a0that returns the size, in bytes, of the operand. The&#8230;<\/p>\n","protected":false},"author":1,"featured_media":450,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[114,45,44,115],"class_list":["post-449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codes","tag-c-language","tag-code","tag-coding","tag-sizeof"],"acf":[],"views":390,"featured_image_urls":{"full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"thumbnail":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",150,101,false],"medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",300,202,false],"medium_large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"1536x1536":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"2048x2048":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"tp-image-grid":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"covernews-slider-full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"covernews-slider-center":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"covernews-featured":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",520,350,false],"covernews-medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",505,340,false],"covernews-medium-square":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",371,250,false],"_nx_notification_thumb":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/SizeOf-Operator-Related-Codes.png",100,67,false]},"author_info":{"display_name":"embeddedwala","author_link":"https:\/\/embeddedwala.com\/beta\/author\/embeddedwala\/"},"category_info":"<a href=\"https:\/\/embeddedwala.com\/beta\/category\/codes\/\" rel=\"category tag\">Codes<\/a>","tag_info":"Codes","comment_count":"0","_links":{"self":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/comments?post=449"}],"version-history":[{"count":8,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":8854,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/449\/revisions\/8854"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media\/450"}],"wp:attachment":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}