{"id":3352,"date":"2024-03-06T11:31:32","date_gmt":"2024-03-06T11:31:32","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=3352"},"modified":"2024-03-06T11:31:32","modified_gmt":"2024-03-06T11:31:32","slug":"typedef-versus-define-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/typedef-versus-define-in-c\/","title":{"rendered":"Typedef Versus #define In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#typedef-versus-define-in-c\">Typedef Versus #define In C<\/a><\/li><li ><a href=\"#difference-between-typedef-and-define\">Difference between typedef and #define:<\/a><\/li><li ><a href=\"#faq-typedef-versus-define-in-c\">FAQ- Typedef Versus #define In C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"typedef-versus-define-in-c\">Typedef Versus #define In C<\/h2>\n\n\n\n<p><strong>Typedef<\/strong>: The typedef Function to provide data type a new name. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to demonstrate typedef \n#include &lt;stdio.h&gt; \n  \n\/\/ After this line BYTE can be used \n\/\/ in place of unsigned char \ntypedef unsigned char BYTE; \n  \nint main() \n{ \n    BYTE b1, b2; \n    b1 = 'c'; \n    printf(\"%c \", b1); \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>c<\/code><\/pre>\n\n\n\n<p><strong>#define&nbsp;in C is referred as a directive that is used to #define alias.<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to demonstrate #define \n#include &lt;stdio.h&gt; \n  \n\/\/ After this line HYD is replaced by \n\/\/ \"Hyderabad\" \n#define HYD \"Hyderabad\" \n  \nint main() \n{ \n    printf(\"%s \", HYD); \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Hyderabad<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"difference-between-typedef-and-define\"><strong>Difference between typedef and #define:<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>typedef<\/code> gives names to types, like calling an <code>int<\/code> a &#8220;Score.&#8221; <code>#define<\/code> gives names to values, like saying 3.14 is &#8220;PI.&#8221;<\/li>\n\n\n\n<li><code>typedef<\/code> is handled by the compiler, while <code>#define<\/code> is managed by a preprocessor before compiling.<\/li>\n\n\n\n<li><code>#define<\/code> doesn&#8217;t need a semicolon, but <code>typedef<\/code> does.<\/li>\n\n\n\n<li><code>#define<\/code> just swaps text in your code, while <code>typedef<\/code> creates new types.<\/li>\n\n\n\n<li>With <code>typedef<\/code>, if you create a new type inside a function, it only works within that function. <code>#define<\/code> works everywhere in your code.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n\/\/ C program to demonstrate importance \n\/\/ of typedef over #define for data types \n#include &lt;stdio.h&gt; \ntypedef char* ptr; \n#define PTR char* \nint main() \n{ \n    ptr a, b, c; \n    PTR x, y, z; \n    printf(\"sizeof a:%zu\\n\" ,sizeof(a) ); \n    printf(\"sizeof b:%zu\\n\" ,sizeof(b) ); \n    printf(\"sizeof c:%zu\\n\" ,sizeof(c) ); \n    printf(\"sizeof x:%zu\\n\" ,sizeof(x) ); \n    printf(\"sizeof y:%zu\\n\" ,sizeof(y) ); \n    printf(\"sizeof z:%zu\\n\" ,sizeof(z) ); \n    return 0; \n} <\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nsizeof a:8\nsizeof b:8\nsizeof c:8\nsizeof x:8\nsizeof y:1\nsizeof z:1<\/code><\/pre>\n\n\n\n<p>The program given above specifies that the size of a is 8 which has the size of the pointer.That is, pointers are stored as 8 bytes. Compiler comes to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef char* ptr;\nptr a, b, c;<\/code><\/pre>\n\n\n\n<p>The statement will change into<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char *a, *b, *c;<\/code><\/pre>\n\n\n\n<p>Further, it will declare a, b, c as char*.<\/p>\n\n\n\n<p>#define will work as the following<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#define PTR char*\nPTR x, y, z;<\/code><\/pre>\n\n\n\n<p>The statement will turn into<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char *x, y, z;\n\n\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In this situation, you have three things: <code>x<\/code>, <code>y<\/code>, and <code>z<\/code>.<\/li>\n\n\n\n<li><code>x<\/code> is like a signpost that points to a group of characters (char*), so it takes up as much space as a signpost (the size of a pointer).<\/li>\n\n\n\n<li><code>y<\/code> and <code>z<\/code> are like individual characters (char), so they take up much less space, just 1 byte each.<\/li>\n<\/ul>\n\n\n\n<p>So, <code>x<\/code> is bigger because it&#8217;s a signpost, while <code>y<\/code> and <code>z<\/code> are smaller because they are like single characters. This happens because when you use macros with pointers, the first one gets the special treatment of being a pointer, while the others become regular characters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-typedef-versus-define-in-c\">FAQ- Typedef Versus #define In C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1699514616252\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is #define used for in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, <code>#define<\/code> is like a tool that helps us create shortcuts, called macros. These macros are like nicknames we give to values. Before the computer actually puts our code together, it replaces these nicknames with the actual values. So, we can use <code>#define<\/code> to make our code shorter and clearer, especially when we want to name constants or create simple functions<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699514637981\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is the difference between typedef and struct in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.<br \/>In C, a <code>struct<\/code> is like a container that lets us group different types of information under one name. It&#8217;s like having a box where we can put variables of all shapes and sizes together.<br \/>Now, <code>typedef<\/code> is like giving a nickname to an existing type. So, we can use <code>typedef<\/code> to create a shortcut name for our struct or any other data type. It&#8217;s like saying, &#8220;Hey, instead of calling it &#8216;struct,&#8217; let&#8217;s just call it something simpler, like &#8216;Person&#8217;.&#8221;<br \/>In summary, <code>struct<\/code> helps us make a mixed bag of variables, and <code>typedef<\/code> helps us give a simpler name to our mixed bag.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1699514677596\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is typedef example?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <code>typedef<\/code> is especially handy for giving a simpler name to structures. In your example, it says, &#8220;Let&#8217;s call this structure &#8216;person&#8217; instead of saying &#8216;struct {int age; char *name}.'&#8221; And you&#8217;re right, when you use <code>person<\/code>, it becomes a type specifier, not a variable name.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Typedef Versus #define In C Typedef: The typedef Function to provide data type a new name. For example, Output #define&nbsp;in C is referred as a directive that is used to #define alias. Output Difference between typedef and #define: Output The program given above specifies that the size of a is 8 which has the size &#8230; <a title=\"Typedef Versus #define In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/typedef-versus-define-in-c\/\" aria-label=\"More on Typedef Versus #define In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":3354,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[563],"class_list":["post-3352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-typedef-versus-define-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=3352"}],"version-history":[{"count":5,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3352\/revisions"}],"predecessor-version":[{"id":7941,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/3352\/revisions\/7941"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/3354"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=3352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=3352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=3352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}