lynx   »   [go: up one dir, main page]

Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Recursion C Math Functions

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Nested Structures C Structs & Pointers C Unions C typedef

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators

C Projects

C Projects

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Certificate

C typedef


C typedef

The typedef keyword lets you create a new name (an alias) for an existing type. This can make complex declarations easier to read, and your code easier to maintain.

For example, instead of always writing float, we can create a new type called Temperature to make the code clearer:

Example

#include <stdio.h>

typedef float Temperature;

int main() {
  Temperature today = 25.5;
  Temperature tomorrow = 18.6;

  printf("Today: %.1f C\n", today);
  printf("Tomorrow: %.1f C\n", tomorrow);

  return 0;
}
Try it Yourself »

Here, Temperature is just another name for float. However, the code is more expressive: it tells us that these numbers are temperatures, not just any floating-point numbers.


Why Use typedef?

  • Simplifies code: Shorter, easier-to-read type names.
  • Improves clarity: Expresses intent better (for example, AGE instead of just int). Also helps avoid confusion when many variables share the same base type (like float or double).

Good To Know: In modern C, typedef is often used together with struct, enum, and function pointers to keep code clean and easier to read.


typedef with struct

typedef can be useful with struct, because it lets you avoid writing struct every time:

Example

#include <stdio.h>

// Without typedef:
struct Car {
  char brand[30];
  int year;
};

// With typedef:
typedef struct {
  char brand[30];
  int year;
} Car;

int main() {
  struct Car car1 = {"BMW", 1999}; // needs "struct"
  Car car2 = {"Ford", 1969}; // shorter with typedef

  printf("%s %d\n", car1.brand, car1.year);
  printf("%s %d\n", car2.brand, car2.year);
  return 0;
}
Try it Yourself »

This example shows how typedef makes it easier to work with multiple struct variables, like different car models:

Example

#include <stdio.h>

// With typedef
typedef struct {
  char brand[30];
  char model[30];
  int year;
} Car;

int main() {
  Car car1 = {"BMW", "X5", 1999};
  Car car2 = {"Ford", "Mustang", 1969};
  Car car3 = {"Toyota", "Corolla", 2011};

  printf("%s %s %d\n", car1.brand, car1.model, car1.year);
  printf("%s %s %d\n", car2.brand, car2.model, car2.year);
  printf("%s %s %d\n", car3.brand, car3.model, car3.year);

  return 0;
}
Try it Yourself »

Here you can see how typedef keeps the code clean when structs are nested inside other structs:

Example

#include <stdio.h>

// Define three structs with typedef
typedef struct {
  char firstName[20];
  char lastName[20];
} Owner;

typedef struct {
  char brand[20];
  int year;
  Owner owner;
} Car;

typedef struct {
  char name[30];
  Car featuredCar;
} Dealership;

int main() {
  Owner person = {"John", "Doe"};
  Car car1 = {"Toyota", 2010, person};
  Dealership d = {"City Motors", car1};

  printf("Dealership: %s\n", d.name);
  printf("Featured Car: %s (%d), owned by %s %s\n",
    d.featuredCar.brand,
    d.featuredCar.year,
    d.featuredCar.owner.firstName,
    d.featuredCar.owner.lastName);

  return 0;
}
Try it Yourself »

It is up to you whether you want to use typedef or not. Your code will work the same without it. However, in modern C it is often used to make code shorter, clearer, and easier to maintain.



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

Лучший частный хостинг