What is the Role and Placement of Typedef in Struct in C Programming?

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Line
Click For Summary
SUMMARY

The discussion focuses on the role and placement of the `typedef` keyword in C programming, specifically in relation to structures. The example provided illustrates how to define a structure named `album` and create instances of it using both direct struct declarations and typedefs. The `typedef` keyword allows for the creation of new identifiers for existing data types, enhancing code readability and maintainability. It is emphasized that typedefs should be declared before any procedure definitions to ensure proper usage.

PREREQUISITES
  • Understanding of C programming syntax and structure definitions
  • Familiarity with the `typedef` keyword and its purpose in type aliasing
  • Knowledge of variable scope and the implications of global versus local variables
  • Basic experience with data types in C, such as `int`, `short`, and `long`
NEXT STEPS
  • Explore advanced usage of `typedef` in C, including nested structures
  • Learn about the implications of variable scope in C programming
  • Investigate the differences between typedefs and macros in C
  • Study best practices for structuring C code for readability and maintainability
USEFUL FOR

C programmers, software developers, and computer science students looking to deepen their understanding of data structures and type definitions in C programming.

transgalactic
Messages
1,386
Reaction score
0
first i create this structure :

Code:
struct album { 
  char *name; 
  char *artist; 
  int numTracks; 
}

if i want my variables not to be global
i can write this inside main:

struct album CD1, CD2, CD3;

or
"
typedef struct album Album;

... then inside main:

Album CD1, CD2, CD3; "

i can't understand this last part regarding where to write the typedef line

and what is the role of type of typedef?
 
Technology news on Phys.org
typedef allows you to use different identifiers to reference data types.
ie.

Code:
typedef unsigned short USHORT
typedef unsigned long  ULONG
typedef unsigned int    UINT

int main()
{  
    USHORT myShort = 5;
    ULONG   myLong = 10;
    UINT     sum = myShort+myLong;
    return 0;
}

typedefs should be used before the procedure definitions.
 

Similar threads

  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 66 ·
3
Replies
66
Views
6K
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
8K
  • · Replies 10 ·
Replies
10
Views
7K
Replies
10
Views
2K
Replies
12
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
7K
  • · Replies 8 ·
Replies
8
Views
5K
Replies
89
Views
7K