C का typedef
In this page:
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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Standard Types को Alias करना
उदाहरण के लिए, typedef int Age; कुछ languages के type systems की तरह genuinely एक distinct type नहीं बनाता -- Age पूरी तरह int से interchangeable है, लेकिन इसे इस तरह नाम देना एक नज़र में variable का intended meaning document करता है।
उदाहरण: Aliasing Standard Types
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pointers को Alias करना
typedef int* IntPtr; आपको int* values[10]; की बजाय IntPtr values[10]; लिखने देता है, हालांकि यह particular इस्तेमाल कुछ controversial है क्योंकि यह छिपा सकता है कि एक variable actually एक pointer है, कुछ लोगों के लिए code को पढ़ना मुश्किल बनाते हुए।
उदाहरण: Aliasing Pointers
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक
typedefdeclaration के अंत में semicolon भूल जाना। - पहले से
typedefसे defined एक alias नाम से पहलेstructलिखना। - यह मान लेना कि एक typedef एक नया distinct type बनाता है, जब यह same type का सिर्फ एक और नाम है।
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions