EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

sizeof() in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » sizeof() in C

sizeof() in C

Definition of C sizeof() Operator

Sizeof() operator in C is machine-dependent functionality which varies from compiler to compiler. It can be said that it is a byte specific functionality. It helps in providing the byte and size of the variables and the number it occupies for the allocation of the variable to the memory. Sizeof() function is exclusively used to find out the exact size of a type of the variable used for programming in C.sizeof operator has a return type which returns total bytes in memory to represent the bytes. It is very useful in the implementation and development of portable applications as it is very flexible and easy to adopt for its versatility.

Syntax with Parameters:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Sizeof() operator in C has various styles for representation:

sizeof(type)

  • type: type is the variable passed in the function to represent the type of data type to be used.

sizeof(variable-name)

  • variable-name: variable-name is the variable passed to the function for determining the bytes occupied by the memory.

sizeof(expression)

  • expression: It is the parameter that is passed to the functionality for determining the bytes in the memory to compute the values for the expression.

How Does sizeof() Operator Work in C?

sizeof() operator is a flexible and versatile operator for computation of the bytes and the memory for ingesting the required values and return those values after computation. It is not at all compiler-specific and thus varies from compiler to compiler. It compiles any unary data type and then used for computing the size of its operand. It returns the size of a variable. If a machine is 32 bits, then the memory allocation and other computation will work in the same way in a manner that the output will vary with a 32-bit machine and if it is 64 bits then it will vary with the 64 bits. Also, if the sizeof operator passes a parameter as expression then it will first parse the entire regular expression and then it will return an output with that size of the expression. Similarly, for the other parameters like type and variable-name, it works the same it will take the parameter as type then it will point to the data type as int, char, or float to be taken into consideration for the function. Even the same works for the variable name the value for the variables is also computable. To compute the number of bytes needed when the variable is assigned as an array or the linked list everything gets calculated using the sizeof () operator seamlessly.

Examples of sizeof() in C

Following are the examples are given below:

Example #1

This program demonstrates the sizeof operator to be used with the primitive data type as an argument to be passed to get a value of the data type.

Code:

#include<stdio.h>
intmain() {
int m = 80;
float n = 5.2;
printf("size of int becomes: %d\n", sizeof(m));
printf("size of float becomes %fu\n", sizeof(n));
printf("size of char becomes: %ld\n", sizeof(char));
return 0;
}

Output:

sizeof() in C-1.1

Example #2

This program is used to demonstrate the sizeof() operator where the sizeof() operator will function with the expression and will have a return type as the regular expression itself after passing from the size of operator as a parameter.

Code:

#include <stdio.h>
intmain()
{
int p = 15;
float f = 18.20;
int q = 32;
double r = 10.34;
printf("Size of Regular expression becomes %lu", sizeof(p + (f - q )*r));
return 0;
}

Output:

sizeof() in C-1.2

Example #3

This program is used to demonstrate the sizeof() operator compatible with the variables assigned and then finding the number of bytes and values that is needed for allocation to the memory as shown in the output.

Code:

#include <stdio.h>
intmain() {
char m_var1 = 26;
int p_var2 = 'i';
double o_var3 = 15.99;
printf("size of the character variable assigned %c\n", sizeof(m_var1));
printf("size of the integer variable assigned %d\n", sizeof(p_var2));
printf("size of the double or float variable assigned%f\n", sizeof(o_var3));
return 0;
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

Output-1.3

Example #4

This program is performed for demonstrating the sizeof operator function by passing the user-defined values as a parameter and then calculating the value of the number of values.

Code:

#include<stdio.h>
structrubik_cube
{
intcube_no ;
char color;
};
intmain() {
structrubik_cube d;
printf("Total number of cubes embedded within the rubik cube with color %c\n", sizeof(d));
}

Output:

Output-1.4

Example #5

This program is used to find the size of the array dynamically using sizeof () operator which is used dynamically allocating the memory to the elements within the array as shown in the output.

Code:

#include <stdio.h>
intmain()
{
intarr[] = { 10,19,24,0,6,42,78,60};
printf("Total elements of array list :%u ", sizeof(arr) / sizeof(arr[5]));
return 0;
}

Output:

Output-1.5

Example #6

This program demonstrates the memory allocation for a float or double value which can be used in the various operating systems as sizeof () operator varies compiler to compiler as shown.

Code:

#include<stdio.h>
#include<stdlib.h>
intmain() {
double *s;
s = (double*)malloc(6 * sizeof(double));
return 0;
}

Output:

No output

Note: Above example can be used as a use case just to get the memory allocation of any number of bits with the function defined for execution.

Advantages of using sizeof() in C

There are several advantages of using the operators in C same is the case with sizeof() operator in C. Some of the advantages of using the sizeof() operators in C are as follows:

  • To find and calculate the number of elements in an array and the type of fashion it is arranged, the sizeof the operator comes as a savior as it is used for the calculation of elements in an array automatically.
  • For dynamic allocation of memory while getting allocated to a block it is a great added advantage as it helps in the memory allocation with enough memory and size of the memory in that particular machine which may be difficult to calculate and keep a hold of.

Conclusion

Sizeof() operator is a flexible and versatile operator in C programming language as It helps in streamlining for allocation of memory and it even helps in the calculation of requisite bytes of memory and the value with desired return types of data types, variables, and Expressions. sizeof operator in C can easily perform dynamic allocation of memory easily.

Recommended Articles

This is a guide to sizeof() in C. Here we also discuss the definition and how does sizeof() operator work in c? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Timer in C#
  2. ASCII Value in C
  3. Void Pointer in C
  4. Stderr in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More