A solution for n-queens in C This is a code I wrote for solving the famous nqueens problem. It returns only the first solution. I would like your reviews and comments and ways to convert it to a solution which will return a set of all solutions are welcome.
So, here I go:

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int isSafe(int** matrix, long long row, long long col, long long matrixDim) {
    //check col
    for (long long i = 0; i &lt; row; i++) {
        if (matrix[i][col] == 1) {
            return 0;
        }
    }
    //check upper left diagonal
    for (long long i = row, j = col;;) {
        i--; j--;
        if (i &lt; 0 || j &lt; 0) {
            break;
        }
        if (matrix[i][j] == 1) {
            return 0;
        }
    }
    //check upper right diagonal
    for (long long i = row, j = col;;) {
        i--; j++;
        if (i &lt; 0 || j == matrixDim) {
            break;
        }
        if (matrix[i][j] == 1) {
            return 0;
        }
    }
    //check lower left diagonal
    for (long long i = row, j = col;;) {
        i++; j--;
        if (i == matrixDim || j &lt; 0) {
            break;
        }
        if (matrix[i][j] == 1) {
            return 0;
        }
    }
    //check lower right diagonal
    for (long long i = row, j = col;;) {
        i++; j++;
        if (i == matrixDim || j == matrixDim) {
            break;
        }
        if (matrix[i][j] == 1) {
            return 0;
        }
    }
    return 1;
}

void printSqMatrix(int** matrix, long long Dim) {
    for (int i = 0; i &lt; Dim; i++) {
        for (int j = 0; j &lt; Dim; j++) {
            printf("%d ", matrix[i][j]);
        }
        puts("");
    }
}

int solve(int** chessBoard, long long row_index, long long boardSize) {
    if (row_index == boardSize) {
        return 1;
    }
    long long j;
    for (j = 0; j &lt; boardSize; j++) {
        if (isSafe(chessBoard, row_index, j, boardSize)) {
            chessBoard[row_index][j] = 1;
            int retVal = solve(chessBoard, row_index + 1, boardSize);
            if (retVal == 1) {
                return 1;
            }
            else if (retVal == -1) {
                puts("Something seriously wrong with execution");
                exit(1);
            }
            chessBoard[row_index][j] = 0;
        }
    }
    if (j == boardSize) {
        return 0;
    }
    return 1;   //never reaches here (written to solve a warning saying not all codepaths return a value
}

int main(void) {
    int** board = NULL;
    long long sizeOfBoard = 0;
    printf("Enter the size of the board: ");
    scanf("%lld", &amp;sizeOfBoard);
    board = malloc(sizeof(int*) * sizeOfBoard);
    for (long long i = 0; i &lt; sizeOfBoard; i++) {
        board[i] = malloc(sizeof(int) * sizeOfBoard);
        for (long long k = 0; k &lt; sizeOfBoard; k++) {   //if I don't write this loop, the 0s get printed as 3452816845....Why???
            board[i][k] = 0;
        }
    }
    solve(board, 0, sizeOfBoard);
    printSqMatrix(board, sizeOfBoard);
}
//if I don't write this loop, the 0s get printed as 3452816845....Why???



Because malloc doesn't initialize an allocated memory. It contains garbage. You may avoid the explicit loop by using calloc.

As a side note, prefer sizeof(variable) to sizeof(type), e.g.

board[i] = malloc(sizeof(*board[i]) * sizeOfBoard);


This way, if you ever change the type of board squares, this line wouldn't change. No double maintenance.
The code adds queens into the rows sequentially. There is no need to test lower diagonals: there are no queens there yet.
Working with the entire 2-dimensional board is an overkill. A one-dimensional array of queen files suffices. Besides, testing for placement safety becomes much simpler:

bool is_safe(int * queens, long long row, long long col, long long dim) {
    for (long long rank = 0; rank &lt; row; rank++) {
        if (queens[rank] == col) {
            // Another queen is at this column already
            return false;
        if ((row - rank) == abs(queens[rank] - col)) {
            // A square is under diagonal attack
            return false;
        }
    return true;
}

A for having all the solutions, do not break the loop when retVal == 1, and

if (row_index == boardSize) {
    do_something();
    return 1;
}


where do_something() may print the board, or save the board in a global state, or whatever suites your needs.