user31772: Different boards have different number of pins.
Is there a way to know this number in code (at least digital and analog pins), or do I have to determine board type (like in this thread) and know the number of pins by myself?

Reasoning for this is that I'm creating prototype using Uno and using 2-3 analog pins for temperature readings, but I want to be able to use the same code for different board in the future with eg. 8 analog pins, so I don't have to remember to change pin numbers in code and maybe solder it convenient way etc. Also being able to use for eg. "last available analog pin" for certain job could help keep code/schematics nice and clean.

user313: You can identify the host processor at run time - each one has its own signature - but not necessarily the board it's running on (consider an Uno and a breadboard-uino for example). So you'd have to make each kind of board you use distinguishable in some way, such as jumpering 3 pins each high or low to make a 3-bit binary code representing one of 8 board types. The next solution is to define that code or some other identifier at compile time with a #define statement, and use the identifier in one or more #ifdef statements to compile board-specific pin definitions.
If you plan on using standard boards the IDE knows about, the IDE pre-defines some useful symbols for you. Here's the compile command the IDE used for an empty sketch for an UNO (I've reformatted it for readability):
"/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++"
-c
-g
-Os
-Wall
-Wextra
-std=gnu++11
-fpermissive
-fno-exceptions
-ffunction-sections
-fdata-sections
-fno-threadsafe-statics
-MMD
-flto
-mmcu=atmega328p
-DF_CPU=16000000L
-DARDUINO=10612
-DARDUINO_AVR_UNO
-DARDUINO_ARCH_AVR

The last four lines make the equivalent of #defines for the clock-frequency, the IDE version number, the Arduino-board, and the overall MCU architecture, respectively.
So in your code, a block like:
#ifdef ARDUINO_AVR_UNO
   // pin definitions for Uno here
#endif

will only be compiled if you selected "Arduino/Genuino Uno" as your board type in the IDE. There are similar symbols for other board types. So for a Nano, ARDUINO_AVR_NANO would be defined instead.

user31794: In the ultimate case, a "board" is not something you can determine programmatically in software; it's not possible in general to tell if, e.g., a particular pin on the microcontroller is connected to a corresponding head pin on the board.
That said, as Edgar Bonet point out, the Arduino board definitions used in the IDE will generally do what you want. Just keep in mind, these are just someone saying "the board should be like this"; a cheap clone board, for example, might be different without a new and proper board definition.
With running code on the microcontroller itself, examining what it can see of the board, you can usually tell which microcontroller you're using. But that just tells you about the chip and its pins, and not about where leads from those pins might go. It sounds to me as if you're asking to find a way to see if your particular microcontroller (e.g., an ATmega328P) is on, say, an Arduino Uno or some other board where certain MCU pins may or may not be hooked up to board pins. The MCU can't tell whether any of its particular pins are connected to headers on the board. As a more specific example, someone could build a board with an MCU with 100 digital IO pins available, yet if only ten of these are brought out to pins on the board, the others are not easily usable.
uino.cc/en/Main/ArduinoBoardUno) or [Arduino Nano]
For your application, you're thinking in a good direction when considering how to make things easier for others using your code. However, I'd suggest that in the long run it's better just to have clear definitions in an obvious place in your sketch (at the top of the file in a single-file sketch, perhaps in a config.ino in a multi-file sketch, or passed in to an initialization function for a library) that says explicitly what pins are used and is easy to change for others. Having the default pins used by the sketch move around if I load the sketch on a different board strikes me as rather confusing.
Keep in mind your audience: the people who are going to be using this on a board different from yours are Arduino hobbyists, and so they're already (or will soon become :-P) familiar with the idea of pins, pin definitions in code, and pin conflicts.

user7508: It is quite easy actually:
Serial.print("number of digital pins: ");
Serial.println(NUM_DIGITAL_PINS);
Serial.print("number of analog inputs: ");
Serial.println(NUM_ANALOG_INPUTS);

Note that these are compile-time constants defined as macros in a file
named pins_arduino.h. There is one such file for every board supported
by the Arduino environment. Just to take Curt J. Sampson's example, if
someone “made a board with an MCU with 100 digital IO pins available,
yet if only ten of these are brought out to pins on the board”, then the
macros would tell you you have 10 digital pins. Assuming of course that
you installed the proper support package for that particular board.

31772: <p>Different boards have different number of pins.</p>

<p>Is there a way to know this number in code (at least digital and analog pins), or do I have to determine board type (like in <a href="https://arduino.stackexchange.com/questions/21137/arduino-how-to-get-the-board-type-in-code">this thread</a>) and know the number of pins by myself?</p>

<hr>

<p><em>Reasoning for this is that I'm creating prototype using Uno and using 2-3 analog pins for temperature readings, but I want to be able to use the same code for different board in the future with eg. 8 analog pins, so I don't have to remember to change pin numbers in code and maybe solder it convenient way etc. Also being able to use for eg. "last available analog pin" for certain job could help keep code/schematics nice and clean.</em></p>
 313: <p>You can identify the host processor at run time - each one has its own signature - but not necessarily the board it's running on (consider an Uno and a breadboard-uino for example). So you'd have to make each kind of board you use distinguishable in some way, such as jumpering 3 pins each high or low to make a 3-bit binary code representing one of 8 board types. The next solution is to define that code or some other identifier at compile time with a #define statement, and use the identifier in one or more #ifdef statements to compile board-specific pin definitions.</p>

<p>If you plan on using standard boards the IDE knows about, the IDE pre-defines some useful symbols for you. Here's the compile command the IDE used for an empty sketch for an UNO (I've reformatted it for readability):</p>

<pre><code>"/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-g++"
-c
-g
-Os
-Wall
-Wextra
-std=gnu++11
-fpermissive
-fno-exceptions
-ffunction-sections
-fdata-sections
-fno-threadsafe-statics
-MMD
-flto
-mmcu=atmega328p
-DF_CPU=16000000L
-DARDUINO=10612
-DARDUINO_AVR_UNO
-DARDUINO_ARCH_AVR
</code></pre>

<p>The last four lines make the equivalent of #defines for the clock-frequency, the IDE version number, the Arduino-board, and the overall MCU architecture, respectively.</p>

<p>So in your code, a block like:</p>

<pre><code>#ifdef ARDUINO_AVR_UNO
   // pin definitions for Uno here
#endif
</code></pre>

<p>will only be compiled if you selected "Arduino/Genuino Uno" as your board type in the IDE. There are similar symbols for other board types. So for a Nano, <code>ARDUINO_AVR_NANO</code> would be defined instead.</p>
 31794: <p>In the ultimate case, a "board" is not something you can determine programmatically in software; it's not possible in general to tell if, e.g., a particular pin on the microcontroller is connected to a corresponding head pin on the board.</p>

<p>That said, as <a href="https://arduino.stackexchange.com/a/36683/31794">Edgar Bonet point out</a>, the Arduino board definitions used in the IDE will generally do what you want. Just keep in mind, these are just someone saying "the board should be like this"; a cheap clone board, for example, might be different without a new and proper board definition.</p>

<p>With running code on the microcontroller itself, examining what it can see of the board, you can usually tell which microcontroller you're using. But that just tells you about the chip and its pins, and not about where leads from those pins might go. It sounds to me as if you're asking to find a way to see if your particular microcontroller (e.g., an ATmega328P) is on, say, an <a href="https://www.arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow noreferrer">Arduino Uno</a> or some other board where certain MCU pins may or may not be hooked up to board pins. The MCU can't tell whether any of its particular pins are connected to headers on the board. As a more specific example, someone could build a board with an MCU with 100 digital IO pins available, yet if only ten of these are brought out to pins on the board, the others are not easily usable.
uino.cc/en/Main/ArduinoBoardUno) or [Arduino Nano]</p>

<p>For your application, you're thinking in a good direction when considering how to make things easier for others using your code. However, I'd suggest that in the long run it's better just to have clear definitions in an obvious place in your sketch (at the top of the file in a single-file sketch, perhaps in a <code>config.ino</code> in a multi-file sketch, or passed in to an initialization function for a library) that says explicitly what pins are used and is easy to change for others. Having the default pins used by the sketch move around if I load the sketch on a different board strikes me as rather confusing.</p>

<p>Keep in mind your audience: the people who are going to be using this on a board different from yours are Arduino hobbyists, and so they're already (or will soon become :-P) familiar with the idea of pins, pin definitions in code, and pin conflicts.</p>
 7508: <p>It is quite easy actually:</p>

<pre class="lang-c++ prettyprint-override"><code>Serial.print("number of digital pins: ");
Serial.println(NUM_DIGITAL_PINS);
Serial.print("number of analog inputs: ");
Serial.println(NUM_ANALOG_INPUTS);
</code></pre>

<p>Note that these are compile-time constants defined as macros in a file
named <code>pins_arduino.h</code>. There is one such file for every board supported
by the Arduino environment. Just to take Curt J. Sampson's example, if
someone “made a board with an MCU with 100 digital IO pins available,
yet if only ten of these are brought out to pins on the board”, then the
macros would tell you you have 10 digital pins. Assuming of course that
you installed the proper support package for that particular board.</p>
