{"id":2234,"date":"2024-05-10T07:15:58","date_gmt":"2024-05-10T07:15:58","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2234"},"modified":"2024-05-10T07:15:58","modified_gmt":"2024-05-10T07:15:58","slug":"sizeof-operator-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/sizeof-operator-in-c\/","title":{"rendered":"Sizeof Operator In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#sizeof-operator-in-c\">Sizeof Operator In C<\/a><\/li><li ><a href=\"#usage-of-sizeof-operator\">Usage of sizeof() operator\u00a0<\/a><\/li><li ><a href=\"#type-of-operator\">Type Of Operator<\/a><\/li><li ><a href=\"#need-of-sizeof\">Need of Sizeof\u00a0<\/a><\/li><li ><a href=\"#faq-sizeof-operator-in-c\">FAQ- Sizeof Operator In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sizeof-operator-in-c\">Sizeof Operator In C<\/h2>\n\n\n\n<p><strong>Sizeof<\/strong> is a common operator in C. It&#8217;s used to determine the size, in bytes, of a particular data type or variable. This operator provides information about the memory storage required for its operand. The result of <strong>sizeof<\/strong> is typically an unsigned integral value represented by the type <strong>size_t<\/strong>.<\/p>\n\n\n\n<p>You can use <strong>sizeof<\/strong> with various data types, including basic types like integers and floats, pointer types, as well as more complex types like structures and unions. This operator helps you understand how much memory a specific data type or variable occupies in your computer&#8217;s memory.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sizeof(Expression);\n<\/code><\/pre>\n\n\n\n<p>where \u2018<em>Expression<\/em>\u2018 can be a data type or a variable of any type.<\/p>\n\n\n\n<p><strong>Return:<\/strong>&nbsp;It returns the size size of the given expression.<\/p>\n\n\n\n<p><strong>Time Complexity:&nbsp;<\/strong>O(1)<br><strong>Auxiliary Space:<\/strong>&nbsp;O(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"usage-of-sizeof-operator\">Usage of sizeof() operator&nbsp;<\/h2>\n\n\n\n<p>When the <code>sizeof<\/code> operator is used with a data type as its operand, it returns the size, in bytes, of that data type. <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Data Type Operand:<\/strong> When you use <code>sizeof<\/code> a data type, like <code>sizeof(int)<\/code> or <code>sizeof(char)<\/code>, it tells you how much memory is allocated for that specific data type in bytes. For example, <code>sizeof(int)<\/code> might return 4, indicating that an <code>int<\/code> takes up 4 bytes of memory on your system. This information is essential for memory management and understanding the storage requirements of your variables.<\/li>\n<\/ol>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program To demonstrate\n\/\/ sizeof operator\n#include &lt;stdio.h&gt;\nint main()\n{\n    printf(\"%lu\\n\", sizeof(char));\n    printf(\"%lu\\n\", sizeof(int));\n    printf(\"%lu\\n\", sizeof(float));\n    printf(\"%lu\", sizeof(double));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1\n4\n4\n8<\/code><\/pre>\n\n\n\n<p>2. <strong>Expression Operand:<\/strong> When you use <code>sizeof<\/code> with an expression, like <code>sizeof(variable)<\/code>, it calculates and returns the size of the evaluated expression in bytes. For instance, <code>sizeof(array)<\/code> would give you the size in bytes of the entire array, and <code>sizeof(structure)<\/code> would provide the size of the entire structure in memory. This is helpful for determining the memory footprint of more complex data structures and expressions in your code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program To demonstrate\n\/\/ operand as expression\n#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 0;\n    double d = 10.21;\n    printf(\"%lu\", sizeof(a + d));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>8<\/code><\/pre>\n\n\n\n<p>the <code>sizeof<\/code> operator returns the size of a data type in bytes. Typically, the size of an <code>int<\/code> is 4 bytes, and the size of a <code>double<\/code> is 8 bytes on many systems. So, if you have an <code>int<\/code> variable <code>a<\/code> and a <code>double<\/code> variable <code>d<\/code>, the size of <code>int<\/code> is 4 bytes, the size of <code>double<\/code> is 8 bytes, and the <code>sizeof<\/code> operator returns the size in bytes.<\/p>\n\n\n\n<p>However, it&#8217;s important to note that the <code>sizeof<\/code> operator always returns a result of type <code>size_t<\/code>, which is an unsigned integral type used for sizes. The actual size of <code>size_t<\/code> may vary depending on the system (typically 4 or 8 bytes). So, the result of <code>sizeof<\/code> is not of the <code>double<\/code> type but rather of type <code>size_t<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"type-of-operator\">Type Of Operator<\/h2>\n\n\n\n<p><code>Sizeof()<\/code> is indeed a compile-time operator in C and C++. It is evaluated by the compiler, not at runtime. When the compiler encounters <code>sizeof()<\/code>, it calculates the size of the operand (either a data type or an expression) and replaces <code>sizeof()<\/code> with the computed size during the compilation process.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to illustrate\n\/\/ that the 'sizeof' operator\n\/\/ is a 'compile time operator'\n#include &lt;stdio.h&gt;\n \nint main(void)\n{\n    int y;\n    int x = 11;\n \n    \/\/ value of x doesn't change\n    y = sizeof(x++);\n \n    \/\/ prints 4 and 11\n    printf(\"%i %i\", y, x);\n \n    return (0);\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4 11\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"need-of-sizeof\">Need of Sizeof&nbsp;<\/h2>\n\n\n\n<p>One common use of the <code>sizeof<\/code> operator is to determine the number of elements in an array automatically.<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program\n\/\/ demonstrate the method\n\/\/ to find the number of elements\n\/\/ in an array\n#include &lt;stdio.h&gt;\nint main()\n{\n    int arr&#91;] = { 1, 2, 3, 4, 7, 98, 0, 12, 35, 99, 14 };\n    printf(\"Number of elements:%lu \",\n           sizeof(arr) \/ sizeof(arr&#91;0]));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Number of elements:11 \n<\/code><\/pre>\n\n\n\n<p>The <code>sizeof<\/code> operator is extensively used in dynamic memory allocation, especially when you want to allocate memory for a specific number of elements of a particular data type without hardcoding the size of that data type<\/p>\n\n\n\n<p><strong>Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int* ptr = (int*)malloc(10 * sizeof(int));\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-sizeof-operator-in-c\">FAQ- Sizeof Operator In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695987875069\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is the sizeof () operator in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>sizeof<\/strong> is a widely used operator in C. It&#8217;s a compile-time unary operator used to calculate the size, in bytes, of its operand. The result of <strong>sizeof<\/strong> is typically of the unsigned integral type, often represented by <strong>size_t<\/strong>. This operator is essential for determining memory requirements and is valuable in various aspects of C programming.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695987881917\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. Is size of () a function or operator?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In the C language, <code>sizeof()<\/code> is an operator, not a function, even though it resembles a function call. It is a unary operator used to determine the size, in bytes, of its operand, which can be a data type or an expression. The result of <code>sizeof<\/code> is of the unsigned integral type, often represented by <code>size_t<\/code>. It is essential for compile-time memory allocation and is a fundamental tool in C programming.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695987888736\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the use of size () operator?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. It helps programmers avoid specifying machine-dependent data sizes in their programs, which promotes portability and flexibility. By using <code>sizeof()<\/code>, you can determine the total size of a data type or expression in terms of storage units that are the size of <code>char<\/code>, regardless of the specific machine&#8217;s architecture. This feature is valuable for writing code that can run on different platforms without modification, making it an essential tool for writing portable C programs.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Sizeof Operator In C Sizeof is a common operator in C. It&#8217;s used to determine the size, in bytes, of a particular data type or variable. This operator provides information about the memory storage required for its operand. The result of sizeof is typically an unsigned integral value represented by the type size_t. You can &#8230; <a title=\"Sizeof Operator In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/sizeof-operator-in-c\/\" aria-label=\"More on Sizeof Operator In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5335,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[397],"class_list":["post-2234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-sizeof-operator-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2234"}],"version-history":[{"count":12,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2234\/revisions"}],"predecessor-version":[{"id":10640,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2234\/revisions\/10640"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5335"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}