<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Khaled Mustafa</title>
    <description>The latest articles on DEV Community by Khaled Mustafa (@khaledmust).</description>
    <link>https://dev.to/khaledmust</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F680769%2F9ce1f44d-6b9f-4077-a927-0812424a1702.jpg</url>
      <title>DEV Community: Khaled Mustafa</title>
      <link>https://dev.to/khaledmust</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khaledmust"/>
    <language>en</language>
    <item>
      <title>Scope and lifetime of variables</title>
      <dc:creator>Khaled Mustafa</dc:creator>
      <pubDate>Sat, 21 May 2022 07:22:43 +0000</pubDate>
      <link>https://dev.to/khaledmust/scope-and-lifetime-of-variables-djb</link>
      <guid>https://dev.to/khaledmust/scope-and-lifetime-of-variables-djb</guid>
      <description>&lt;h1&gt;
  
  
  Table of Contents
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;
Memory layout of a program in C

&lt;ol&gt;
&lt;li&gt;RAM&lt;/li&gt;
&lt;li&gt;ROM&lt;/li&gt;
&lt;li&gt;What is the difference between a statically defined variable and a dynamically defined variable?&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;
Scope of a variable

&lt;ol&gt;
&lt;li&gt;Block scope&lt;/li&gt;
&lt;li&gt;File scope&lt;/li&gt;
&lt;li&gt;Program scope&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;What is the difference between a variable definition and declaration?&lt;/li&gt;
&lt;li&gt;
Storage classes

&lt;ol&gt;
&lt;li&gt;auto&lt;/li&gt;
&lt;li&gt;static&lt;/li&gt;
&lt;li&gt;extern&lt;/li&gt;
&lt;li&gt;register&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a id="orgbe96f5e"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Memory layout of a program in C
&lt;/h1&gt;

&lt;p&gt;Before jumping into exploring the scope and lifetime of a variable in C we first need to define the memory layout of a common program.&lt;br&gt;
As we know we, have two types of memories &lt;strong&gt;RAM (Random Access Memory)&lt;/strong&gt; which is used for storing the data generated by a program, and &lt;strong&gt;ROM (Read Only Memory)&lt;/strong&gt; which is used for storing the instructions of a program.&lt;/p&gt;

&lt;p&gt;&lt;a id="org0179e5b"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  RAM
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+---------------------+
|    Memory Mapped    |
|    Peripherals      |
+---------------------+
|    Initialized      |
|    Data Segment     |
+---------------------+
|    Uninitialized    |
|    Data Segment     |
+-----+---------------+
|     | Heap          |
|     |               |
|     v               |
|                     |
|                     |
|                     |
|                     |
|     ^               |
|     |               |
|     | Stack         |
+-----+---------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;RAM is divided into mainly four portions:

&lt;ol&gt;
&lt;li&gt;Memory Mapped Peripherals&lt;/li&gt;
&lt;li&gt;Initialized Data Segment:
This segment contains all variables that have been initialized with a value.&lt;/li&gt;
&lt;li&gt;Uninitialized Data Segment (bss)
Contains all uninitialized variables (those that have been defined but not assigned any value)&lt;/li&gt;
&lt;li&gt;Heap&lt;/li&gt;
&lt;li&gt;Stack&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="org4131d18"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  ROM
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+---------------------+
| Interrupt Vector    |
| Table (IVT)         |
+---------------------+
| Text Segment        |
|                     |
+---------------------+
| Constant data       |
| Segment (.ro data)  |
+---------------------+
| Boot loader         |
|                     |
|                     |
+---------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;What we are concerned with here is the text segment which is used for storing the instruction (code) of the program, and the constant data segment which is used for the storage of every statically defined constant.&lt;/p&gt;

&lt;p&gt;&lt;a id="orgabf589e"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between a statically defined variable and a dynamically defined variable?
&lt;/h2&gt;

&lt;p&gt;Variables that have a value before the run-time of the program are called statically defined variables while variables that don't have a value before the runtime of the program and are assigned a value during the runtime and will get its value from elsewhere are known as dynamically defined variables.&lt;/p&gt;

&lt;p&gt;&lt;a id="org47e7edb"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Scope of a variable
&lt;/h1&gt;

&lt;p&gt;The scope of a variable is the region in which we can use a variable.&lt;br&gt;
There are three different variable scopes in C:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Block scope (Function scope)&lt;/li&gt;
&lt;li&gt;File scope&lt;/li&gt;
&lt;li&gt;Program scope&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a id="orgb348afd"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Block scope
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Variables having this type of scope are limited in their usage to the region (block) in which they were defined, we can not use it outside the code block.&lt;/li&gt;
&lt;li&gt;A block (code block) is the whole region starting with &lt;code&gt;{&lt;/code&gt; and ending with &lt;code&gt;}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="org6331053"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  File scope
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Variables that we could refer to in any location in the file in which they have been declared.&lt;/li&gt;
&lt;li&gt;There are two storage classes that we are concerned with regarding the file scope, &lt;code&gt;static&lt;/code&gt; and &lt;code&gt;extern&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;static&lt;/code&gt; storage class has two completely different consequences on a variable (depending on the type of the variable it's used on), if the variable is local then it &lt;em&gt;increases its lifetime to the whole program&lt;/em&gt;, and if the variable is global &lt;em&gt;it limits its scope to the module (file) it was defined in&lt;/em&gt;, in other words, we can't use the keyword &lt;code&gt;extern&lt;/code&gt; on such variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="org21e991b"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Program scope
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;These are all non-static global variables, such that they could be accessed from anywhere in the program, but to use a global variable that has been defined in a file (module) elsewhere from where we intend to use we must first declare it.&lt;/li&gt;
&lt;li&gt;The lifetime of a global variable is the whole lifetime of the program.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="org56edcb5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  What is the difference between a variable definition and declaration?
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Declaration&lt;/em&gt; is informing the compiler of the existence of a variable, so that compiler could ignore it for the time being and later the linker comes and links a value to that variable, we can never use a variable that has not been first declared.&lt;/li&gt;
&lt;li&gt;Declaration involves specifying the variable with a symbol and a data type.
Example for this the keyword &lt;code&gt;extern int k&lt;/code&gt; is just informing the compiler that there is a variable called &lt;code&gt;k&lt;/code&gt; and it's of data type &lt;code&gt;int&lt;/code&gt; that has been defined somewhere else in the program, use it for now and the linker will handle its value.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Only functions and constants could be only declared, rather than defined and declared&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;A &lt;em&gt;definition&lt;/em&gt; provides the compiler with all the information it needs to generate machine code when the entity is used later in the program, (the linker doesn't have a job when the compiler can see the definition of a variable).&lt;/li&gt;
&lt;li&gt;Since constant variables are static variables (i.e. They must have a value before the run-time of the program) a definition must be provided by the programmer before the compilation process.&lt;/li&gt;
&lt;li&gt;A declaration of a built-in type such as &lt;code&gt;int&lt;/code&gt; is automatically a definition because the compiler knows how much space to allocate for it.&lt;/li&gt;
&lt;li&gt;Examples:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Delcare and define int variables i and j. */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* Decleration without definition. */&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* This results in an error because there is no declaration instance for the variable l.
      error: ld returned 1 exit status */&lt;/span&gt;
  &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="org3b7938c"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Storage classes
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;C provides different types of storage classes, the most commonly used ones are:&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;auto&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;static&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;extern&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;register&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's take everything and know what it does.&lt;/p&gt;

&lt;p&gt;&lt;a id="org1fe45b9"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  auto
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This specifier class is rarely used as it's the default storage class for block-scoped variables.&lt;/li&gt;
&lt;li&gt;Each time a block is entered storage for auto variables defined in that block is made available and ceases to exist as soon as we exit the block.&lt;/li&gt;
&lt;li&gt;The allocation and de-allocation of the variable in memory are done automatically, hence the name.&lt;/li&gt;
&lt;li&gt;All uninitialized auto variables have garbage values so we must initialize them before we use them.&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Both these variables are similar to each other. */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a id="orgcc7465a"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  static
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It has two different effects on a variable depending on its scope whether is local or global.&lt;/li&gt;
&lt;li&gt;If the variable is local, the &lt;code&gt;static&lt;/code&gt; specifier extends its lifetime to be the whole program, but it doesn't extend its scope, its scope is limited to the function it was defined in.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;static&lt;/code&gt; in block scope (local variable) with an initializer will only be initialized one time on program startup, not each time the function is called.&lt;/li&gt;
&lt;li&gt;Since its lifetime has been extended its storage location has changed from a normal local variable stored on the stack to a constant variable stored in the program memory.&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The result is %d on the %d iteration.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In essence, the &lt;code&gt;static&lt;/code&gt; specifier transfers the value of the variable from one function call to another.&lt;/li&gt;
&lt;li&gt;When used on a global variable the &lt;code&gt;static&lt;/code&gt; specifier limits the scope of this variable to only the file (module) it has been defined in (i.e. In other words we can't use the keyword &lt;code&gt;extern&lt;/code&gt; on such variables).&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;static&lt;/code&gt; specifier could also be used on function limiting its usage the same way it does to global variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="orge736573"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  extern
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It's used for informing the compiler (declaring) that a variable of a specific name and data type exists somewhere in the program and for now just use it as it's and the linker will handle the assignation of a value to it.&lt;/li&gt;
&lt;li&gt;In essence, it's used to extend the scope of a global variable.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Example, if we have two files &lt;code&gt;file1.c&lt;/code&gt; and &lt;code&gt;file2.c&lt;/code&gt;:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;file1.c&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*Decleration and defintion of a global variable x.*/&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;file2.c&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"file1.c"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of x %d.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Notice that including a C file into another file is a legal, but not advisable thing to do, what we rather would do in a real program is define our variables in a header file and then include it, this example is for illustration only.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="org45a3aac"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  register
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This specifier is used for storing the variable in a register, this is done to decrease the time the program is required to access the memory we know that this variable will be accessed multiple times throughout the lifetime of the program.&lt;/li&gt;
&lt;li&gt;The compiler doesn't guarantee that it will store the specified variable in the register, if no freely available register this specifier would be ignored.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Endianness</title>
      <dc:creator>Khaled Mustafa</dc:creator>
      <pubDate>Sat, 21 May 2022 07:21:15 +0000</pubDate>
      <link>https://dev.to/khaledmust/endianness-hb0</link>
      <guid>https://dev.to/khaledmust/endianness-hb0</guid>
      <description>&lt;h2&gt;
  
  
  What is Endianness?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It refers to how the bytes of our data are ordered inside the memory.&lt;/li&gt;
&lt;li&gt;There are two types of endianness a processor could come with (technically they are three):

&lt;ol&gt;
&lt;li&gt;Big Endian&lt;/li&gt;
&lt;li&gt;Little Endian&lt;/li&gt;
&lt;li&gt;Biendian (From its name these types of processors could be configured as any type.)&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Note that two hex numbers are of size one byte.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Most significant bit and Least significant bit
&lt;/h2&gt;

&lt;p&gt;Consider taking a variable of data type &lt;code&gt;int&lt;/code&gt; (ie. size of 4 bytes) having a value of &lt;code&gt;0x12345678&lt;/code&gt; the &lt;em&gt;Most Significant Bit (MSB)&lt;/em&gt; would be &lt;code&gt;0x1&lt;/code&gt; and the &lt;em&gt;Least Significant Bit (LSB)&lt;/em&gt; would be &lt;code&gt;0x8&lt;/code&gt;, similarly, the most significant byte would be &lt;code&gt;0x1234&lt;/code&gt; and the least significant byte would be &lt;code&gt;0x5678&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the difference between Big Endian and Little Endian?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Big Endian
&lt;/h3&gt;

&lt;p&gt;Stores the most significant bytes first and then stores the least significant byte, so the most significant byte occupies the lower address of the memory while the least significant byte occupies the higher address memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Little Endian
&lt;/h3&gt;

&lt;p&gt;Store the least significant byte first and then stores the most significant byte, so the least significant byte is stored in the lowest memory address while the most significant byte is stored in the higher address.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Consider the hex value of &lt;code&gt;0x03E8&lt;/code&gt;:&lt;br&gt;
Note that &lt;code&gt;0x00&lt;/code&gt; and &lt;code&gt;0x01&lt;/code&gt; are the address of the byte of the memory (as memory is byte-addressable).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     Big Endian       Little Endian
     +--------+        +--------+
     |        |        |        |
 0x00|  0x03  |    0x00|  0xE8  |
     +--------+        +--------+
     |        |        |        |
 0x01|  0xE8  |    0x01|  0x03  |
     +--------+        +--------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why is it important?
&lt;/h2&gt;

&lt;p&gt;Endianness is important when two computers (processors) are communicating with each other, take for an example is processor #A is little-endian while processor #B is big-endian, when processor #A sends out data it's going to send the least significant byte first and since processor #B is of big-endian it's going to interpret it as the most significant byte, thus all the data will be flipped.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to determine the type of endianness your processor is running?
&lt;/h2&gt;

&lt;p&gt;Note that we will have to perform &lt;em&gt;pointer downcast&lt;/em&gt; and the reason for that is the length modifier &lt;code&gt;%x&lt;/code&gt; is of integer type. &lt;a href="https://man7.org/linux/man-pages/man3/printf.3.html#:~:text=and%20S%20conversions.-,Length%20modifier,-Here%2C%20%22integer%20conversion"&gt;Check the man pages&lt;/a&gt;&lt;br&gt;
If we didn't do this conversion the result would be the same integer regardless of the architecture of our processor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The size of data type long long is %d.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The size of data type int is %d.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;sizeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="cm"&gt;/* Assign an 8 byte value to x. */&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mh"&gt;0x0000000000000004&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/*Assign the address of variable x to x_ptr. */&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* Both x_ptr and y_ptr have the same value which is the address of variable x.
   What are doing here is just telling the processor is variable y_ptr is pointing at an integer, rather than the data type long long.
*/&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y_ptr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="n"&gt;x_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* Derefrence the value of y_ptr. */&lt;/span&gt;
&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;y_ptr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/* Just printing out the value of y with 0s leading as padding. */&lt;/span&gt;
&lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The value of y is %#.8x"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result will vary from one architecture to another, the value of y would be &lt;code&gt;0x00000003&lt;/code&gt; if we are running a little-endian processor (which we are, since x86 processors are little-endian), and the value of y would be &lt;code&gt;0x00000000&lt;/code&gt; if we were running a big-endian architecture.&lt;br&gt;
&lt;a href="https://replit.com/@eis3nheim/determineendianness#main.c"&gt;Try out this code&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Fun fact: Origin of the name
&lt;/h2&gt;

&lt;p&gt;The naming Big Endian and Little Endian comes from Jonathan Swift's satire Gulliver's Travels, in his book a big-endian refers to a person who cracks an egg from the large end and a little-endian refers to the one who cracks an egg from the little end.&lt;/p&gt;

</description>
      <category>hardware</category>
      <category>computerarchitecture</category>
      <category>embeddedstystems</category>
    </item>
    <item>
      <title>Stack implementation using C</title>
      <dc:creator>Khaled Mustafa</dc:creator>
      <pubDate>Sat, 14 Aug 2021 16:57:38 +0000</pubDate>
      <link>https://dev.to/khaledmust/stack-implementation-using-c-4akj</link>
      <guid>https://dev.to/khaledmust/stack-implementation-using-c-4akj</guid>
      <description>&lt;h2&gt;
  
  
  Stack
&lt;/h2&gt;

&lt;h2&gt;
  
  
  What is a stack?
&lt;/h2&gt;

&lt;p&gt;A stack is a data structure in which the insertion or deletion of an element is done only to its topmost element called the &lt;strong&gt;top&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;A good analogy for stacks is a stack of cafeteria trays in which trays are stacked on top of each other, and because the last tray is at the top of this stack it's going to be the first one to be removed from the stack.&lt;/p&gt;

&lt;p&gt;This type of data structure is called &lt;strong&gt;Last In First Out (LIFO).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The act of add or inserting an item to a stack is called &lt;strong&gt;Pushing,&lt;/strong&gt; and the act of removing an item is called &lt;strong&gt;Popping.&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L8-pUK78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rh4hnltgqq9gxiyhmpon.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L8-pUK78--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rh4hnltgqq9gxiyhmpon.png" alt="StackIntro"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Specifications of a stack
&lt;/h2&gt;

&lt;p&gt;(These are only the specifications and by no means an algorithm of implementation)&lt;/p&gt;

&lt;p&gt;The first step we must perform in working with any stack is to create it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Initialization
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Precondition: None&lt;/li&gt;
&lt;li&gt;  Post-condition: Created and initialized an empty stack. We initialized our stack to be empty as we don't want to work with a stack filled with garbage values from whatever values existed at that address before we reclaimed it to ourselves.&lt;/li&gt;
&lt;li&gt;  In other words, we are initializing the value of &lt;code&gt;top&lt;/code&gt; to be &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Status
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  We want to always check the status of our stack as we don't want to &lt;em&gt;push&lt;/em&gt; an item to a full stack and similarly we don't want to &lt;em&gt;pop&lt;/em&gt; from an empty stack.&lt;/li&gt;
&lt;li&gt;  StackFull

&lt;ul&gt;
&lt;li&gt;  Precondition: The stack has been created and initialized.&lt;/li&gt;
&lt;li&gt;  Post-condition: Returns &lt;code&gt;True&lt;/code&gt; if the stack is full and &lt;code&gt;False&lt;/code&gt; otherwise.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;  StackEmpty

&lt;ul&gt;
&lt;li&gt;  Precondition: The stack has been created and initialized.&lt;/li&gt;
&lt;li&gt;  Post-condition: Returns &lt;code&gt;True&lt;/code&gt; if the stack is empty, and &lt;code&gt;False&lt;/code&gt; otherwise.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Basic operations on the stack
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  The two basic operations done on any type of a stack are pushing or pulling an item, so let's define those.&lt;/li&gt;
&lt;li&gt;  Push

&lt;ul&gt;
&lt;li&gt;  Precondition: The stack should be created and not Full.&lt;/li&gt;
&lt;li&gt;  Post-condition: The item that we want to add (Argument passed to Push) is added to the top of the stack, thus the top of the stack has increased.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;  Pull

&lt;ul&gt;
&lt;li&gt;  Precondition: The stack should be created and not empty.&lt;/li&gt;
&lt;li&gt;  Post-condition: The top of the stack is removed and returned to us, thus decreasing the top of the stack.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Other operations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  Other operations could be performed on a stack but they are not essential for its operation, nevertheless, they are useful.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Clearing the stack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Precondition: The stack exists and is initialized.&lt;/li&gt;
&lt;li&gt;  Postcondition: The stack is empty.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Determining the stack size
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Precondition: The stack exists and is initialized.&lt;/li&gt;
&lt;li&gt;  Postcondition: Returns the number of entries (items) in the stack.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Determining the top item on the stack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Precondition: The stack exists and is not empty.&lt;/li&gt;
&lt;li&gt;  Postcondition: The top item on the stack is returned, but not removed.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Implementation of the stack using arrays (in C)
&lt;/h2&gt;

&lt;p&gt;There are two types of implementation of an array &lt;strong&gt;Contiguous&lt;/strong&gt; and &lt;strong&gt;Linked&lt;/strong&gt;, we are going to focus for now on the first type.&lt;/p&gt;

&lt;p&gt;If we wanted our stack to be represented as an array, what do we need to accomplish this?&lt;/p&gt;

&lt;p&gt;We will need an array of type "whatever elements" we are going to store them in it, and we are going to need a pointer to keep track of the topmost element in our array (stack).&lt;/p&gt;

&lt;p&gt;So basically a stack is just an array and a pointer to the top element, let's start with those.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I4BySWqs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nra83aafm5ufxlh7lqev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I4BySWqs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nra83aafm5ufxlh7lqev.png" alt="StackStructure"&gt;&lt;/a&gt;&lt;br&gt;
We will create a structure of those two, and call it &lt;code&gt;Stack&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since they are of different types (&lt;code&gt;Top&lt;/code&gt; of type &lt;code&gt;int&lt;/code&gt;, and an array of whatever we are going to store) that we want to combine into one form, we will use a structure.&lt;/p&gt;
&lt;h2&gt;
  
  
  Definition of our stack
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This code contains a lot of constants, that if we wanted to change the type of the item we want to store, or for example wanted more than 10 elements, we will have to change the core definition of the stack, and this contradicts with the principle of &lt;a href="https://en.wikipedia.org/wiki/Information_hiding#:~:text=A%20software%20module%20hides%20information,subset%20of%20the%20total%20program."&gt;information hiding&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// If we wanted to change the type or the number of elements we could do it from here and leave&lt;/span&gt;
&lt;span class="c1"&gt;// the core definition of the stack alone.&lt;/span&gt;
&lt;span class="cp"&gt;#define MAXSTACK 10
&lt;/span&gt;&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;StackEntry&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;typedef&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;StackEntry&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;MAXSTACK&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So that's it for our initial definition of the stack, pretty ain't :P.&lt;br&gt;
Now let's implement the basic operations.&lt;/p&gt;

&lt;p&gt;Notice that, for the basic operations to operate there is a pre-condition that must be fulfilled, and that is checking the status of the stack, so we have to implement those too.&lt;/p&gt;
&lt;h2&gt;
  
  
  Pushing
&lt;/h2&gt;

&lt;p&gt;To push an item to the stack, what do we need?&lt;/p&gt;

&lt;p&gt;We need the item that we are going to push &lt;code&gt;item&lt;/code&gt; and the struct we just defined.&lt;/p&gt;

&lt;p&gt;When specifying any data type, we use &lt;strong&gt;call-by-reference&lt;/strong&gt; when a subprogram (function) may change the parameter.&lt;/p&gt;

&lt;p&gt;That is why we passed the address of structure &lt;code&gt;Stack&lt;/code&gt; to the pointer &lt;code&gt;S&lt;/code&gt;, and to access any element we want in the &lt;code&gt;Stack&lt;/code&gt; we use the &lt;code&gt;-&amp;gt;&lt;/code&gt; arrow operator.&lt;/p&gt;

&lt;p&gt;Bringing into focus that the value of &lt;code&gt;top&lt;/code&gt; indicates how many items there are in our stack meaning that before assigning our &lt;code&gt;item&lt;/code&gt; the value of &lt;code&gt;top&lt;/code&gt; will be pointing to the position to be filled.&lt;/p&gt;

&lt;p&gt;For clarification, since the array starts from &lt;code&gt;0&lt;/code&gt;, the value of &lt;code&gt;Top&lt;/code&gt; will indicate the number of elements that exists until we reach the end of the array, this means that by the end of filling our stack the value of &lt;code&gt;Top&lt;/code&gt; will be the value of &lt;code&gt;MAXSTACK&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;Don't try to print the content of the array at &lt;code&gt;MAXSTACK&lt;/code&gt; as it's an uninitialized memory location and will contain whatever garbage value there is in that location.&lt;/p&gt;

&lt;p&gt;As you can see in the diagram below when there exist two items the &lt;code&gt;Top&lt;/code&gt; is of value &lt;code&gt;2&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HqxYuP9---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4h5t176d2kczpe9i518b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HqxYuP9---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4h5t176d2kczpe9i518b.png" alt="TopPointer"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackEntry&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackFull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;ERROR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Stack is full"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's analyze our code, as stated to push an item we first need to perform our stack status check, so we call our function &lt;code&gt;StackFull()&lt;/code&gt; which we will implement later, if the stack is full, it prevents us from pushing our item, and prints an error message stating that our stack is full, else it allows us to push it.&lt;/p&gt;

&lt;p&gt;So what we codded is "Access the array that is called &lt;code&gt;entry&lt;/code&gt; at index &lt;code&gt;top&lt;/code&gt; and add the &lt;code&gt;item&lt;/code&gt; to the array, then increment the value of &lt;code&gt;top&lt;/code&gt;, to point to the next value to be filled"&lt;/p&gt;

&lt;p&gt;We want to increment the value of &lt;code&gt;top&lt;/code&gt; after we have pushed our element, to point on the next location to fill, that is why we used the postfix increment &lt;code&gt;top++&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Popping
&lt;/h2&gt;

&lt;p&gt;Similarly, we implement the pop function by tweaking some differences, that is instead of writing to the &lt;code&gt;Stack&lt;/code&gt; we are going to read from it and write the value stored there to a variable called &lt;code&gt;item&lt;/code&gt; outside the function (We will pass that value's address) as it is outside the pop function's scope, but before reading remember that &lt;code&gt;Top&lt;/code&gt; is pointing to an empty location and that's if the stack was not full, and it could be pointing to a garbage value and that's if it was full, so first we have to decrement it and then read the value, that why we use the pre-decrement operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackEntry&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;ERROR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Stack is empty"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
        &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the expression &lt;code&gt;s-&amp;gt;top&lt;/code&gt; is evaluated to &lt;code&gt;top&lt;/code&gt; that resides in the &lt;code&gt;Stack&lt;/code&gt;, so &lt;code&gt;--s-&amp;gt;top&lt;/code&gt; is similar to &lt;code&gt;--top&lt;/code&gt; if and only if we are within the scope of the Stack. In other words &lt;code&gt;s-&amp;gt;--top&lt;/code&gt; is a syntax error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other operations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Checking if the stack is empty and if the stack is full
&lt;/h3&gt;

&lt;p&gt;This implementation is rather simple all we have to do is writing a simple &lt;code&gt;if&lt;/code&gt; condition, and return a non-zero value if the stack is empty and full.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack is empty
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;_Bool&lt;/span&gt; &lt;span class="nf"&gt;StackEmpy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The return of this function will always be either 1 if the condition applies and 0 if the condition doesn't apply.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack is full
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;Bool&lt;/span&gt; &lt;span class="nf"&gt;StackFull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;S&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;MAXSTACK&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we used a pointer to the stack (Call by reference) even though we are not changing anything in the stack and only reading, and the reason for this is efficiency as if we passed by the value we would have copied the whole stack into both functions. i.e. It could be &lt;code&gt;Bool StackFull(Stack s)&lt;/code&gt;, meaning we pass the whole stack (Pass by value), but as stated it is a waste of memory space and time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initialization of the stack
&lt;/h3&gt;

&lt;p&gt;Initialization of the stack means moving the top back to the zero position and not clearing the values of the stack.&lt;/p&gt;

&lt;p&gt;This is a critical function as without it the &lt;code&gt;top&lt;/code&gt; will have a garbage value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;CreateStack&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Getting the topmost element
&lt;/h3&gt;

&lt;p&gt;So what we want to do is to print out the topmost item of our stack, in other words, we want to pop it, but at the same time, we don't want to change our stack so we will have to re-pop it again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;StackTop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackEntry&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember that &lt;code&gt;top&lt;/code&gt; is pointing to an empty location if the stack is not full and a garbage value it's full.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finding the stack size
&lt;/h3&gt;

&lt;p&gt;To find the size of the stack we just print out the value of &lt;code&gt;top&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;StackSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Clearing the stack
&lt;/h3&gt;

&lt;p&gt;Clearing the stack is just re-pointing the &lt;code&gt;top&lt;/code&gt; to &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You are going to say that we have already implemented this in a function called &lt;code&gt;CreateStack()&lt;/code&gt;and that's correct but we will reimplement just for the sake of the concept of information hiding (abstraction).&lt;/p&gt;

&lt;p&gt;In the case of our current implementation (using an array) the elements are not destroyed, they still exist in the memory, it's just the value of &lt;code&gt;top&lt;/code&gt; that changed, but in the other implementation (using linked stack) the elements are going to be destroyed (kind of).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ClearStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Traverse Stack
&lt;/h3&gt;

&lt;p&gt;What we want to accomplish is to pass on every element of the stack and display it to the user but we have a little problem here, we want to accomplish this using abstraction concept, the problem lies in that the user has a piece of information that is hidden from us and that is the &lt;strong&gt;type&lt;/strong&gt; of the element stored in the array, similarly the user doesn't know how the stack is implemented, he only uses the function that we have provided so to overcome this problem the user is going to define a function that displays only one element, then we are going to take this function and pass on every element thus displaying all the elements of the stack.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0H3XvGAI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hen3xgrc2yx9rl866p7u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0H3XvGAI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hen3xgrc2yx9rl866p7u.png" alt="TraverseStack"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// User implemented function&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackEntry&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The stack item is: %d&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The data type is known only by the user.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now notice that we are going to pass the address of the &lt;code&gt;Display()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So the traverse function will take first the address of the stack and then the address of the &lt;code&gt;Display()&lt;/code&gt;function. i.e. &lt;code&gt;TraverseStack(&amp;amp;s, &amp;amp;Display)&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;TraverseStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pDisplay&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackEntry&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pDisplay&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To pass a function as a parameter to another function we need to know its address in memory, its return type, and the type of argument it takes. So basically what we have codded is: The parameter &lt;code&gt;pDispalay&lt;/code&gt; will be a pointer to a function that has a &lt;code&gt;void&lt;/code&gt; return type and which takes a single &lt;code&gt;StackEntry&lt;/code&gt; parameter.&lt;/p&gt;

&lt;h2&gt;
  
  
  User level implementation
&lt;/h2&gt;

&lt;p&gt;An example of how a user will use our implementation of the Stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&amp;lt;stdio.h&amp;gt;
#include"StackImplement.h"
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;StackEntry&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"  ----- &lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"|   %d   |&lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s"&gt;n"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Stack&lt;/span&gt; &lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;StackCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;MAXSTACK&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Please enter the value you want to enter in the stack:"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;scanf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;Push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;TraverseStack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;myStack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Display&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>c</category>
      <category>programming</category>
      <category>datastructure</category>
      <category>stack</category>
    </item>
  </channel>
</rss>
