N-Queens Problem in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Placing Queens Without Attacks

The N-Queens problem places N queens on an N x N board so that no two share a row, column, or diagonal. Place one queen per row, trying each column; reject a column if it conflicts with a queen above. When all rows are filled, count a solution. For N = 4 there are 2 solutions; for N = 8 there are 92.

Remember
place(row):
    if row == N: solution found
    for col in 0..N-1:
        if safe(row, col): cols[row] = col; place(row + 1)

Cheat Sheet

Keep these facts handy while you work with N-Queens Problem in C in C.

TopicDetail
One per rowcols[row] = column
ConflictSame column or |dr| == |dc|
N = 42 solutions
N = 892 solutions

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Graph Algorithms.

Example 1 — Count and show the first 4-queens solution

Columns are tried left to right, row by row.

C
#include <stdio.h>
#include <stdlib.h>
#define N 4

static int cols[N], solutions;

static int safe(int row, int col) {
    for (int r = 0; r < row; r++)
        if (cols[r] == col || abs(cols[r] - col) == row - r) return 0;
    return 1;
}

static void place(int row) {
    if (row == N) {
        if (++solutions == 1) {
            printf("first solution:\n");
            for (int r = 0; r < N; r++) {
                for (int c = 0; c < N; c++)
                    printf("%c%s", cols[r] == c ? 'Q' : '.', c + 1 < N ? " " : "");
                printf("\n");
            }
        }
        return;
    }
    for (int col = 0; col < N; col++) {
        if (safe(row, col)) {
            cols[row] = col;
            place(row + 1);
        }
    }
}

int main(void) {
    place(0);
    printf("solutions: %d\n", solutions);
    return 0;
}

How It Works

  1. Placing the first queen in column 0 leads to no complete solution, so the search moves on.
  2. The first complete placement puts queens at columns 1, 3, 0, 2.
  3. The second solution (columns 2, 0, 3, 1) is the mirror image.

Edge Cases & Pitfalls

Check these before relying on N-Queens Problem in C in real programs.

diag

Diagonal test

Two queens are on a diagonal when abs(col1 - col2) == row1 - row2.

grow

Growth

The search space grows quickly; pruning at each row is what keeps it feasible.

Key Takeaways

  • One queen per row.
  • Check column and diagonals.
  • Backtrack on conflicts.

One line: The N-Queens problem places N queens on an N x N board so that no two share a row, column, or diagonal.

Frequently Asked Questions

Placing N queens on an N by N chessboard so that no two queens attack each other.
92 distinct solutions (12 if rotations and reflections are considered the same).
Continue with "C Graph Algorithms" next. Apply graph algorithms in C: traversal, cycle detection, topological sort, Dijkstra, and minimum spanning trees. The C Recursion and Backtracking hub lists every topic in order.

Next: Graph Algorithms

Apply graph algorithms in C: traversal, cycle detection, topological sort, Dijkstra, and minimum spanning trees.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful