Home » Interview Questions » Interviews » Story Details
Printable Version

Interview Question: Difference between typpedef and #define in C/C++ language

by Kamal Rawat on Jan 13, 2012

What are the differences between typedef and #define in C/C++ language
Comments: 0    Views: 769

MCN Professionals | Interview Question of the day

MCN Professionals is starting Industrial Training for MCA-2012 Batch. Have a look at our Industrial Training Program and Course Details.
-----------------------------------------------------------------------------------------------------------------------------------------

Today's Question:

What are the differences between typedef and #define in C or C++ languages ?
Solution:

There are many differences between typedef & #define. The major differences are as follows:

1.
#define is handled by the preprocessor, which will just copy-paste the #define values from the point of definition to the point of use. Where as typedef is handled by the compiler and is the actual definition of a new type. By the time control will reach the compiler, all the #define will be replaced.

Because of this difference, following can be the implications:

A) typedef should be terminated with semicolon, #define should not be terminated with semicolon.

B) There can be side effects of replacement in #define, For example:
typedef char* string_t;
#define string_d char*

string_t s1, s2; // Both s1 and s2 are of type char*
string_d s3, s4; // s3 is char* but s4 is of type char (and not char*)

The problem in second declaration is because preprocessor will replace it as

char* s3, s4;

Which means that s3 will be of type char* but s4 will be of type char (* has to be specified with all the variables if we want all of them to be pointers like char *s3, *s4; ).
C) typedef follows the scope rule. i.e if a new type is defined in a scope (inside a function), then the new type name will only be visible till the scope is there. But when preprocessor encounters a #define, then it will replace all the occurences, after that (No scope rule is followed). For example:
int main(){

    { // New scope STARTS
        typedef int myInt_t;
        #define myInt_d int
       
        myInt_t a;   // OK. a is of type int
        myInt_d b;   // OK. b is of type int
    }// New scope ENDS

    myInt_t  c;  // ERROR.. type myInt_t not found
    myInt_d  d;  // OK. d is of type int
}
2.
#define can be used to define macros also, but typedef can only be used to provide a new name for already existing type (it cannot create a new type). similarly #define can used to define variables like

#define N 10

This will not actually define N but will replace N with 10 in the entire file. so can be used for named-constants.

typedef can not be used for any other purpose but giving new name for already defined types.
3.
There are certain type definitions which you can only define using typedef and not #define. Consider the below cases:

A) If I want to give a new name for integer array of size 10, I can do so like:
typedef int arr[10];
B) If you have more that one word in the type (like unsigned int).
typedef unsigned int UINT;

We cannot define this type using #define
C) For giving new name to struct types, like
typedef struct{
    int a;
    char b;
} myType;

If you know about more differences feel free to comments :)

---------------------------------------------------------------------------

Interview Questions Archive:

         To see all the questions in the category click here...


Post a Comment
*
DevExpress PowerBuilder Web Development Windows Development Languages Software Engineering Databases
iPhone Architecture Secutiry UML & Modeling Operating Systems Networking Testing
Graphics Design Project Management Hardware Open Source Games Development Business Intelligence Visual Studio LightSwitch 2011
MonoDevelop Visual Studio 2010 ASP.NET HTML, DHTML XML PHP JavaScript
Silverlight Web Services WCF Windows Forms WPF Windows Services Dynamic Link Libraries
ActiveX COM, DCOM, ATL C# VB.NET C++ F# Java
Pascal SQL Server Oracle DB2 MS-Access Windows Servers Windows
Linux Unix SAP LINQ .NET Framework ADO.NET Reporting
Crystal Reports SQL Server Reporting Services Igenda Reports Active Reports Adobe Fireworks Arrays & Collections Hosting
Future Trends Android Windows Phone Smart Devices Business M&A Investment & Funding
Web Browsers Internet Explorer Firefox Safari Common Entrepreneurs Students
Consulting Wiki Gadgets MobileMe iCloud iOS Social Media
Facebook Twitter LinkedIn Google+ Microsoft Kinect XBox
Wii Playstation DirectX i OS OS X CIO, CTO, CEO Windows 8
Web Design Expression Blend 4 Photoshop CS5 Creative Suite 5.5 Expression Web 4 Expression Studio 4 Creative Suite® 5.5 Design
Creative Suite 5.5 Web Creative Suite 5.5 Production Startups Funding M&A Laptops Smart Phones
Desktops Cameras & Camcorders Netbooks Tablets Virtualization Microsoft Surface WordPress
Software Products Cloud Computing Current Affairs Technology TV TV
Earnings XAML E-Commerce MonoTouch Mono for Android Deals Electronics
Mobile Phone Laptop Tablet Book Computer Press Releases Reviews
Products Books Companies Windows Azure SQL Azure Interviews Mac
Web Browsers Symbian Windows Forms WPF Windows Services HTML 5 Office 365
SharePoint 2010 Exchange Server Adobe Visual Studio 2012 iPad Flex / Flash Games
Windows 9
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName