user24788: I am writing a chess engine. I need to know how to tell if the king is in check. I was thinking that maybe I can use the approach that Stockfish uses. Where and how in Stockfish's code is check detected?

user24925: Your question has nothing to do with Stockfish. Though it may be interesting to check the original source code.
This is purely algorithmic.
One dumb solution would be implementing a boolean function (is_check) that takes an array as input representing the board. Only white pieces can check the black king, except the white king and vice versa.
If one of the following conditions is verified then there is a check on the board(return True) :

If a knight is placed at (-+)2(-+)1 or (-+)1(-+)2 squares from your king
If a pawn is placed at (-+)1+1 squares from your king (depends on the side you make the verification for as pawns move and attack only in one way)
If your king is in diagonal of a bishop or a queen and that no other pieces interferes
If your king is aligned (column/row) with a rook or a queen and that no other pieces interferes

Otherwise return False.
Note that such a function could be used in another method to check if a planned move is legal (if your king is in check after you played, your move is illegal)

user825: 
Where and how in Stockfish's code is check detected?

This is simple engine programming. You will need to build a bitboard of the attacking squares and two bitboards of your king, one for each color. You will then run AND on the two bitboards. If the result is non-zero, you know the king is in check.
I can show you the code in Stockfish, but does my answer make sense to you? If not, please add a comment so I will try to grab some code.

user24933: If you're curious about the Stockfish code, it can be found here:
 si->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);


We call the attackers_to method to figure out if any pieces are attacking the king's position, including pieces on the king's side.

 Bitboard Position::attackers_to(Square s, Bitboard occupied) const {

    return  (pawn_attacks_bb(BLACK, s)       & pieces(WHITE, PAWN))
         | (pawn_attacks_bb(WHITE, s)       & pieces(BLACK, PAWN))
         | (attacks_bb<KNIGHT>(s)           & pieces(KNIGHT))
         | (attacks_bb<  ROOK>(s, occupied) & pieces(  ROOK, QUEEN))
         | (attacks_bb<BISHOP>(s, occupied) & pieces(BISHOP, QUEEN))
         | (attacks_bb<KING>(s)             & pieces(KING));
 }


We then call the pieces function, which returns all the pieces owned by the opposite side

Finally, we run an AND operator to confirm if any of the opposite side's pieces are threatening the king.


The code is very well written and relatively straightforward to follow, in case you'd like to explore it further.

24788: <p>I am writing a chess engine. I need to know how to tell if the king is in check. I was thinking that maybe I can use the approach that Stockfish uses. Where and how in Stockfish's code is check detected?</p>
 24925: <p>Your question has nothing to do with Stockfish. Though it may be interesting to check the original source code.</p>
<p>This is purely algorithmic.</p>
<p>One dumb solution would be implementing a boolean function (is_check) that takes an array as input representing the board. Only white pieces can check the black king, except the white king and vice versa.</p>
<p>If one of the following conditions is verified then there is a check on the board(return True) :</p>
<ul>
<li>If a knight is placed at (-+)2(-+)1 or (-+)1(-+)2 squares from your king</li>
<li>If a pawn is placed at (-+)1+1 squares from your king (depends on the side you make the verification for as pawns move and attack only in one way)</li>
<li>If your king is in diagonal of a bishop or a queen and that no other pieces interferes</li>
<li>If your king is aligned (column/row) with a rook or a queen and that no other pieces interferes</li>
</ul>
<p>Otherwise return False.</p>
<p>Note that such a function could be used in another method to check if a planned move is legal (if your king is in check after you played, your move is illegal)</p>
 825: <blockquote>
<p>Where and how in Stockfish's code is check detected?</p>
</blockquote>
<p>This is simple engine programming. You will need to build a bitboard of the attacking squares and two bitboards of your king, one for each color. You will then run AND on the two bitboards. If the result is non-zero, you know the king is in check.</p>
<p>I can show you the code in Stockfish, but does my answer make sense to you? If not, please add a comment so I will try to grab some code.</p>
 24933: <p>If you're curious about the Stockfish code, it can be <a href="https://github.com/official-stockfish/Stockfish/blob/0405f3540366cc16245d51531881c55d3726c8b5/src/position.cpp#L338" rel="noreferrer">found here</a>:</p>
<pre><code> si-&gt;checkersBB = attackers_to(square&lt;KING&gt;(sideToMove)) &amp; pieces(~sideToMove);
</code></pre>
<ol>
<li>We call the <code>attackers_to</code> method to figure out if any pieces are attacking the king's position, including pieces on the king's side.</li>
</ol>
<pre><code> Bitboard Position::attackers_to(Square s, Bitboard occupied) const {

    return  (pawn_attacks_bb(BLACK, s)       &amp; pieces(WHITE, PAWN))
         | (pawn_attacks_bb(WHITE, s)       &amp; pieces(BLACK, PAWN))
         | (attacks_bb&lt;KNIGHT&gt;(s)           &amp; pieces(KNIGHT))
         | (attacks_bb&lt;  ROOK&gt;(s, occupied) &amp; pieces(  ROOK, QUEEN))
         | (attacks_bb&lt;BISHOP&gt;(s, occupied) &amp; pieces(BISHOP, QUEEN))
         | (attacks_bb&lt;KING&gt;(s)             &amp; pieces(KING));
 }
</code></pre>
<ol start="2">
<li><p>We then call the <code>pieces</code> function, which returns all the pieces owned by the opposite side</p>
</li>
<li><p>Finally, we run an AND operator to confirm if any of the opposite side's pieces are threatening the king.</p>
</li>
</ol>
<p>The code is very well written and relatively straightforward to follow, in case you'd like to explore it further.</p>
