{"id":14714,"date":"2024-03-15T09:47:01","date_gmt":"2024-03-15T09:47:01","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/"},"modified":"2025-08-06T12:27:42","modified_gmt":"2025-08-06T12:27:42","slug":"how-is-the-sizeof-used-in-c","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/","title":{"rendered":"C++ sizeof Operator Explained: Usage &#038; Examples"},"content":{"rendered":"<p>In C++, sizeof is an operator used to fetch the size of a type or variable in bytes. The syntax for it is as follows:<\/p>\n<p>size of the type<br \/>\nsize of the expression<\/p>\n<p>type is a data type, while expression can be either an expression or a variable.<\/p>\n<p>The sizeof operator returns the type size_t, which is an unsigned integer type.<\/p>\n<p>The sizeof operator can be used in two ways:<\/p>\n<ol>\n<li>The sizeof(type) operator is used to get the size of a type, for example sizeof(int) will return the size of the int type, and sizeof(double) will return the size of the double type.<\/li>\n<li>sizeof(expression) is used to obtain the size of an expression or a variable. For example, sizeof(arr) will return the size of the array arr, while sizeof(x) will return the size of the variable x.<\/li>\n<\/ol>\n<p>It is important to note that the sizeof operator does not actually calculate the value of the expression, but instead determines the size of the expression or variable at compile time. Therefore, the arr in sizeof(arr) is not modified or called.<\/p>\n<p>For array types, sizeof returns the size of the entire array, not just the size of a pointer to the first element.<\/p>\n<p>For example, the following examples demonstrate the usage of the sizeof operator:<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-meta\">#<span class=\"hljs-keyword\">include<\/span> <span class=\"hljs-string\">&lt;iostream&gt;<\/span><\/span>\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\r\n    <span class=\"hljs-type\">int<\/span> x = <span class=\"hljs-number\">5<\/span>;\r\n    <span class=\"hljs-type\">int<\/span> arr[<span class=\"hljs-number\">10<\/span>];\r\n\r\n    std::cout &lt;&lt; <span class=\"hljs-string\">\"Size of int: \"<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(<span class=\"hljs-type\">int<\/span>) &lt;&lt; <span class=\"hljs-string\">\" bytes\"<\/span> &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; <span class=\"hljs-string\">\"Size of double: \"<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(<span class=\"hljs-type\">double<\/span>) &lt;&lt; <span class=\"hljs-string\">\" bytes\"<\/span> &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; <span class=\"hljs-string\">\"Size of x: \"<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(x) &lt;&lt; <span class=\"hljs-string\">\" bytes\"<\/span> &lt;&lt; std::endl;\r\n    std::cout &lt;&lt; <span class=\"hljs-string\">\"Size of arr: \"<\/span> &lt;&lt; <span class=\"hljs-built_in\">sizeof<\/span>(arr) &lt;&lt; <span class=\"hljs-string\">\" bytes\"<\/span> &lt;&lt; std::endl;\r\n\r\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\r\n}\r\n<\/code><\/pre>\n<p>Output result:<\/p>\n<pre class=\"post-pre\"><code>Size of int: 4 bytes\r\nSize of double: 8 bytes\r\nSize of x: 4 bytes\r\nSize of arr: 40 bytes\r\n<\/code><\/pre>\n<p>In this example, sizeof(int) returns 4 because the int data type typically occupies 4 bytes. Additionally, sizeof(x) returns 4 because the variable x is of type int. Finally, sizeof(arr) returns 40 because the array arr contains 10 elements of type int, with each element occupying 4 bytes, totaling 40 bytes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In C++, sizeof is an operator used to fetch the size of a type or variable in bytes. The syntax for it is as follows: size of the type size of the expression type is a data type, while expression can be either an expression or a variable. The sizeof operator returns the type size_t, [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[695,381,19879,415,19880],"class_list":["post-14714","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-c-operators","tag-c-programming","tag-c-sizeof","tag-data-types","tag-size-calculation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ sizeof Operator Explained: Usage &amp; Examples - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Master the C++ sizeof operator: Learn size calculation for types\/variables with practical syntax examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ sizeof Operator Explained: Usage &amp; Examples\" \/>\n<meta property=\"og:description\" content=\"Master the C++ sizeof operator: Learn size calculation for types\/variables with practical syntax examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-15T09:47:01+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-06T12:27:42+00:00\" \/>\n<meta name=\"author\" content=\"Olivia Parker\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Olivia Parker\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/\"},\"author\":{\"name\":\"Olivia Parker\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\"},\"headline\":\"C++ sizeof Operator Explained: Usage &#038; Examples\",\"datePublished\":\"2024-03-15T09:47:01+00:00\",\"dateModified\":\"2025-08-06T12:27:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/\"},\"wordCount\":274,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"C++ operators\",\"C++ Programming\",\"C++ sizeof\",\"data types\",\"size calculation\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/\",\"name\":\"C++ sizeof Operator Explained: Usage & Examples - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-15T09:47:01+00:00\",\"dateModified\":\"2025-08-06T12:27:42+00:00\",\"description\":\"Master the C++ sizeof operator: Learn size calculation for types\/variables with practical syntax examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ sizeof Operator Explained: Usage &#038; Examples\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9\",\"name\":\"Olivia Parker\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g\",\"caption\":\"Olivia Parker\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C++ sizeof Operator Explained: Usage & Examples - Blog - Silicon Cloud","description":"Master the C++ sizeof operator: Learn size calculation for types\/variables with practical syntax examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/","og_locale":"en_US","og_type":"article","og_title":"C++ sizeof Operator Explained: Usage & Examples","og_description":"Master the C++ sizeof operator: Learn size calculation for types\/variables with practical syntax examples.","og_url":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-15T09:47:01+00:00","article_modified_time":"2025-08-06T12:27:42+00:00","author":"Olivia Parker","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Olivia Parker","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/"},"author":{"name":"Olivia Parker","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9"},"headline":"C++ sizeof Operator Explained: Usage &#038; Examples","datePublished":"2024-03-15T09:47:01+00:00","dateModified":"2025-08-06T12:27:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/"},"wordCount":274,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["C++ operators","C++ Programming","C++ sizeof","data types","size calculation"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/","url":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/","name":"C++ sizeof Operator Explained: Usage & Examples - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-15T09:47:01+00:00","dateModified":"2025-08-06T12:27:42+00:00","description":"Master the C++ sizeof operator: Learn size calculation for types\/variables with practical syntax examples.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-is-the-sizeof-used-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"C++ sizeof Operator Explained: Usage &#038; Examples"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3ff7b3da0e45ac5dbbef2502f3cea8d9","name":"Olivia Parker","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56c66f189ba32a6f9eb50f31a38fe774e2a725c213d4070835ccc51b8fbbc54b?s=96&d=mm&r=g","caption":"Olivia Parker"},"url":"https:\/\/www.silicloud.com\/blog\/author\/oliviaparker\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=14714"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14714\/revisions"}],"predecessor-version":[{"id":158754,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/14714\/revisions\/158754"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=14714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=14714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=14714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}