← Back to C Course | Chapter 1: Introduction & Basics | Lesson 13 of 21

C का sizeof Operator

sizeof operator यह return करता है कि एक type, variable, array, struct, या expression कितने bytes occupy करता है, compile time पर evaluated, portable, size-aware C code लिखने के लिए essential।
Syntax
c
sizeof(data_type);
sizeof(variable_name);

printf("%zu", sizeof(variable_name));

sizeof Operator

sizeof operator एक type या variable द्वारा occupied bytes की संख्या return करता है, compile time पर evaluated, और यह portable code लिखने के लिए essential है जो किसी भी type के लिए एक fixed size नहीं मानता।

उदाहरण: The sizeof Operator

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%zu", sizeof(x));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Basic Types का Size

char, int, और double जैसे basic types पर sizeof apply करना दिखाता है कि current system पर हर एक कितनी memory इस्तेमाल करता है, जो अलग-अलग compilers और platforms में अलग हो सकता है।

उदाहरण: Size of Basic Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("char: %zu, int: %zu, double: %zu", sizeof(char), sizeof(int), sizeof(double));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Arrays का Size

एक array पर apply किया गया sizeof हर element द्वारा मिलकर इस्तेमाल की गई bytes की total संख्या return करता है, और इसे एक single element के size से divide करना C में array की length compute करने की standard technique है।

उदाहरण: Size of Arrays

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int numbers[5] = {1, 2, 3, 4, 5};
	int length = sizeof(numbers) / sizeof(numbers[0]);
	// Print formatted text to the screen
	printf("%d", length);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Structures का Size

एक struct पर apply किया गया sizeof structure द्वारा occupy की गई total memory return करता है, जो alignment के लिए compiler द्वारा add की गई padding के कारण इसके individual members के sum से बड़ी हो सकती है।

उदाहरण: Size of Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point {
	char label;
	int x;
};
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%zu", sizeof(struct Point));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Expressions के साथ sizeof

sizeof सिर्फ एक type या variable की बजाय सीधे एक expression पर भी apply किया जा सकता है, runtime पर actually expression की value compute किए बिना यह return करते हुए कि वह expression किस type में evaluate होता।

उदाहरण: sizeof with Expressions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare a and set it to 5
	int a = 5;
	// Print formatted text to the screen
	printf("%zu", sizeof(a + 1.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी function के अंदर एक array parameter पर sizeof इस्तेमाल करना, जहाँ यह array की बजाय एक pointer का size देता है।
  2. sizeof का result %zu की बजाय %d से print करना, जो 64-bit systems पर गलत हो सकता है।
  3. यह मान लेना कि sizeof(struct) इसके members का sum है, जब padding इसे बड़ा बना सकती है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.