← Back to C Course | Chapter 12: Advanced Topics | Lesson 1 of 20

C का typedef

typedef एक type को एक नया, छोटा या ज़्यादा clear नाम देता है, एक लंबे title के लिए एक nickname की तरह। यह code को पढ़ना आसान बनाता है।
Syntax
c
typedef existing_type new_name;

new_name variable_name = value;

typedef क्या है?

typedef unsigned long ulong; आपको अपने code में हर जगह ज़्यादा verbose unsigned long की बजाय ulong लिखने देता है, जो pointers या function signatures वाले लंबे, unwieldy type names के लिए खासकर valuable बन जाता है।

उदाहरण: What is typedef?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef unsigned long ulong;
// Program execution starts in main()
int main() {
	ulong x = 100;
	// Print formatted text to the screen
	printf("%lu", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard Types को Alias करना

उदाहरण के लिए, typedef int Age; कुछ languages के type systems की तरह genuinely एक distinct type नहीं बनाता -- Age पूरी तरह int से interchangeable है, लेकिन इसे इस तरह नाम देना एक नज़र में variable का intended meaning document करता है।

उदाहरण: Aliasing Standard Types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef int Age;
// Program execution starts in main()
int main() {
	Age myAge = 25;
	// Print formatted text to the screen
	printf("%d", myAge);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Structures को Alias करना

typedef struct {int x; int y;} Point; आपको बाद में ज़्यादा verbose struct Point p; की बजाय सिर्फ Point p; से variables declare करने देता है -- यह shorthand real-world C codebases और libraries में बहुत common है।

उदाहरण: Aliasing Structures

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef struct {int x; int y;} Point;
// Program execution starts in main()
int main() {
	Point p = {3, 4};
	// Print formatted text to the screen
	printf("%d %d", p.x, p.y);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pointers को Alias करना

typedef int* IntPtr; आपको int* values[10]; की बजाय IntPtr values[10]; लिखने देता है, हालांकि यह particular इस्तेमाल कुछ controversial है क्योंकि यह छिपा सकता है कि एक variable actually एक pointer है, कुछ लोगों के लिए code को पढ़ना मुश्किल बनाते हुए।

उदाहरण: Aliasing Pointers

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef int* IntPtr;
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	IntPtr ptr = &x;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}

typedef के लिए Best Practices

इस convention को follow करना (जैसे typedef struct {...} Point;) code में तुरंत visually clear बना देता है कि Point एक typedef'd type है, न कि built-in एक, readers को आपके custom types को int या char जैसे standard से एक नज़र में अलग करने में मदद करते हुए।

उदाहरण: Best Practices for typedef

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Create a type alias with typedef
typedef struct {int x; int y;} Point;
// Program execution starts in main()
int main() {
	Point p = {1, 2};
	// Print formatted text to the screen
	printf("%d %d", p.x, p.y);
	// 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. एक typedef declaration के अंत में semicolon भूल जाना।
  2. पहले से typedef से defined एक alias नाम से पहले struct लिखना।
  3. यह मान लेना कि एक typedef एक नया distinct type बनाता है, जब यह same type का सिर्फ एक और नाम है।

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.