user16785: I was learning to program for a void obstacle robot but when I looked at the code I saw two data types long and int.
Int are datatypes that holds  -2,147,483,648 to 2,147,483,647.
Long are also datatypes that holds -2,147,483,648 to 2,147,483,647.
Int and long are like same but I came up with the code where two types of datatype is used as seen below:
int trigPin = 2;
int echoPin = 4;
long duration, cm, inches;

But how can you know when to use which datatype? I searched a lot in the web but I didn't understand so can someone explain this to me please.

user2926: I like to use types from stdint.h.
While they do have minor drawbacks, the benefit they provide is you know exactly the size your are handling, even when compiling on other architectures.
#include <stdint.h>

uint8_t  my_byte = 0xf0;
int16_t  trig_pin = 2;
int16_t  echo_pin = 4;
uint32_t duration, cm , blah;
uint64_t big_int;

// etc.

But obviously you don't want to be moving around a bunch of int32_t when your data only needs int16_t.

user18380: It's the same reasoning as in C: the size of the int type is expected to be the natural word size that your system handles most efficiently. It must also be at least 16 bits wide, no smaller than a short, and no larger than a long.
So an int may be 16-, 32-, or 64-bits, based on the whatever your system handles best, and so is most likely to be 16 bits wide on an 8- or 16-bit CPU, 32 on a 32-bit CPU etc. 
I use int when I want the best performance, whilst taking care to guard when I need more range than offered by 16 bits. These days, you tend to know when you are writing application code for 16-bit systems, although that's not so true for "library" code where portability may be of greater concern.
In your example, assuming the author had chosen their types carefully, the int variables probably require a small range and could afford to be word-size, leading to potentially shorter or faster code (or both). The long ones presumably required more than 16-bit range (they are guaranteed to be at least 32 bits wide). On the processor you'd selected as a compilation target, it looks like int and long were both implemented as 32-bit; this would be different (or should be) if you selected a 16-bit target CPU. 

user368: int in AVR GCC is 16 bits, not 32.

user18361: According to the C language specification, int must be at least 16 bits or longer and long must be at least 32 bits or longer.
It is OK for a compiler to implement int as 32 bits or even 64 bits. It is OK for a compiler to implement long as 64 bits or longer. But it is not allowed for a compiler to implement long as 16 bits.
So when to use which type?
If the values you will be working with can be represented within 16 bits then it's OK to use int. If you need more than 16 bits use long. If you need more than 32 bits use long long.
Do not be caught up with compiler and/or CPU specifics. As mentioned by others, even within the same product range, Arduinos, there are 16 and 32 bit CPUs available. Instead, only trust what the standard guarantees.
The full specification of types in C are:

char must be at least 8 bits
int must be at least 16 bits
long must be at least 32 bits
long long must be at least 64 bits

Note: It is perfectly legal for compilers to implement char, int, long and long long as 64 bits. This is in fact not uncommon among DSPs.

user10794: On the Arduino (AVR models) an int is 16 bits, not 32 bits. Thus it goes from -32768 to +32767.
That is different from long which is 32 bits.

16785: <p>I was learning to program for a <em>void obstacle robot</em> but when I looked at the code I saw two data types long and int.</p>

<p>Int are datatypes that holds  -2,147,483,648 to 2,147,483,647.
Long are also datatypes that holds -2,147,483,648 to 2,147,483,647.</p>

<p>Int and long are like same but I came up with the code where two types of datatype is used as seen below:</p>

<pre><code>int trigPin = 2;
int echoPin = 4;
long duration, cm, inches;
</code></pre>

<p>But how can you know when to use which datatype? I searched a lot in the web but I didn't understand so can someone explain this to me please.</p>
 2926: <p>I like to use types from <code>stdint.h</code>.</p>

<p>While they do have minor drawbacks, the benefit they provide is you know exactly the size your are handling, even when compiling on other architectures.</p>

<pre><code>#include &lt;stdint.h&gt;

uint8_t  my_byte = 0xf0;
int16_t  trig_pin = 2;
int16_t  echo_pin = 4;
uint32_t duration, cm , blah;
uint64_t big_int;

// etc.
</code></pre>

<p>But obviously you don't want to be moving around a bunch of int32_t when your data only needs int16_t.</p>
 18380: <p>It's the same reasoning as in C: the size of the <code>int</code> type is expected to be <em>the natural word size that your system handles most efficiently</em>. It must also be at least 16 bits wide, no smaller than a <code>short</code>, and no larger than a <code>long</code>.</p>

<p>So an <code>int</code> may be 16-, 32-, or 64-bits, based on the whatever your system handles best, and so is most likely to be 16 bits wide on an 8- or 16-bit CPU, 32 on a 32-bit CPU etc. </p>

<p>I use <code>int</code> when I want the best performance, whilst taking care to guard when I need more range than offered by 16 bits. These days, you tend to know when you are writing application code for 16-bit systems, although that's not so true for "library" code where portability may be of greater concern.</p>

<p>In your example, assuming the author had chosen their types carefully, the <code>int</code> variables probably require a small range and could afford to be word-size, leading to potentially shorter or faster code (or both). The <code>long</code> ones presumably required more than 16-bit range (they are guaranteed to be at least 32 bits wide). On the processor you'd selected as a compilation target, it looks like <code>int</code> and <code>long</code> were both implemented as 32-bit; this would be different (or <em>should</em> be) if you selected a 16-bit target CPU. </p>
 368: <p><code>int</code> in AVR GCC is <a href="https://gcc.gnu.org/wiki/avr-gcc#Type_Layout"><strong>16</strong> bits</a>, <em>not</em> 32.</p>
 18361: <p>According to the C language specification, <code>int</code> must be at least 16 bits or longer and <code>long</code> must be at least 32 bits or longer.</p>

<p>It is OK for a compiler to implement <code>int</code> as 32 bits or even 64 bits. It is OK for a compiler to implement <code>long</code> as 64 bits or longer. But it is not allowed for a compiler to implement <code>long</code> as 16 bits.</p>

<p>So when to use which type?</p>

<p>If the values you will be working with can be represented within 16 bits then it's OK to use <code>int</code>. If you need more than 16 bits use <code>long</code>. If you need more than 32 bits use <code>long long</code>.</p>

<p>Do not be caught up with compiler and/or CPU specifics. As mentioned by others, even within the same product range, Arduinos, there are 16 and 32 bit CPUs available. Instead, only trust what the standard guarantees.</p>

<p>The full specification of types in C are:</p>

<ul>
<li><code>char</code> must be at least 8 bits</li>
<li><code>int</code> must be at least 16 bits</li>
<li><code>long</code> must be at least 32 bits</li>
<li><code>long long</code> must be at least 64 bits</li>
</ul>

<p>Note: It is perfectly legal for compilers to implement char, int, long and long long as 64 bits. This is in fact not uncommon among DSPs.</p>
 10794: <p>On the Arduino (AVR models) an <code>int</code> is 16 bits, not 32 bits. Thus it goes from -32768 to +32767.</p>

<p>That is <strong>different</strong> from <code>long</code> which is 32 bits.</p>
