1 Casting often indicate something amiss. Avoid it as able. I do find casting reluctantly needed in some printf() and some assignments between different types, but rarely with specified function argument.

@Edward 2019-01-06 13:10:36

Here are some things that may help you improve your program.

Declare variables only where needed

Old-style C required all variables to be declared at the top of the function in which they were used, but modern C has not required this for many years. For that reason, you can remove the declarations of i and j and incorporate them into the for loops instead, as in the following suggestion. (Note that this requires C99 or later.)

Use size_t instead of int where appropriate

My version of gcc complains because size_t is unsigned and int is unsigned. To address that, we can change the types of i and j:

for (size_t i = 0; i < n; ++i) {
for (size_t j = i; j < n; ++j) {

Think carefully about the algorithm

The diagonal of the matrix doesn't really need to be touched. This can easily be addressed by starting the inner loop from i + 1 instead of i.

Consider using pointers

It might be a bit more clear within the inner loop if pointers were used. Here's one way to do that:

Consider adding testing

Since the transpose of a transpose of any matrix should equal itself, this suggests one method of testing the results. I'd suggest testing a few matrices with small size and manually worked answers and then a larger number of matrices with varying sizes using the double-transpose and checking for equality.

Omit return 0 in main

Since C99, the return 0 at the end of main is implicit and may be omitted.

Note: when I make this suggestion, it's almost invariably followed by one of two kinds of comments: "I didn't know that." or "That's bad advice!" My rationale is that it's safe and useful to rely on compiler behavior explicitly supported by the standard. For C, since C99; see ISO/IEC 9899:1999 section 5.1.2.2.3:

[...] a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.

For C++, since the first standard in 1998; see ISO/IEC 14882:1998 section 3.6.1:

If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

All versions of both standards since then (C99 and C++98) have maintained the same idea. We rely on automatically generated member functions in C++, and few people write explicit return; statements at the end of a void function. Reasons against omitting seem to boil down to "it looks weird". If, like me, you're curious about the rationale for the change to the C standard read this question. Also note that in the early 1990s this was considered "sloppy practice" because it was undefined behavior (although widely supported) at the time.

So I advocate omitting it; others disagree (often vehemently!) In any case, if you encounter code that omits it, you'll know that it's explicitly supported by the standard and you'll know what it means.

@Reinderien 2019-01-06 15:25:38

Omitting return 0 is an option, but one I generally disagree with - it's a quirk of the language that breaks uniformity, and may confuse beginners.

@Edward 2019-01-06 15:28:40

De gustibus non est disputandum. Whether one prefers to use it or not, it's useful for programmers to know of this provision in the standard.

@Reinderien 2019-01-06 15:32:37

I'll agree with that. However, it's also important to note that your answer contains C99 features, but the OP wants ansi (C89). Whereas I think you should keep the C99 recommendations, you need to mention the version difference.

@H.cohen 2019-01-06 15:47:06

@Edward thank you, as it is now- I want the strictness of 89, so the return and value declarations are not avoided. will change to size_t and try to improve the algorithm. I have made more tests, but I omitted them. I thought about making a static switch function, but the code as a hole is short so I wasn't sure if its appropriate. Thank you so much for your time and input!

@Edward 2019-01-06 15:49:16

I've updated my answers to point out which features requires C99 or later. It would be a shame to ignore the last 20 years of language evolution, but if one is restricted to only the 30-year-old language version, it should now be clear which features that affects.

@H.cohen 2019-01-06 15:53:59

@Edward well, if i was not restricted by them I could have just declared the function as: void Transpose(size_t siz, int mat[][siz]). So it's kind of the point in my case :)

@chux 2019-01-06 23:45:06

Yes return 0; may be omitted in C99, C11, C18. Hardy a significant concern to recommend and it is reasonable to include it. As with such minor style concern, follow your group's coding standard.

@Reinderien 2019-01-06 15:37:06

Reconsider ansi

-ansi is equivalent to C89, which is many versions behind the current standard (C99 -> C11 -> C17). C99 is popular and will buy you some great language features. C17 is supported by gcc, though, so you should use that.

Clean up your whitespace

You should add one or two blank lines between each of your functions.

Your tabs are non-uniform - they seem to vary between two and three spaces. Generally 3-4 spaces is standard; choose a standard and apply it with an IDE or advanced text editor.

Use const

printMat does not modify mat, so declare it const.

Don't double-initialize

@Edward correctly indicated that variable declarations should be pulled into the loop. One other thing: you initialize i=0 twice, so the first one has no effect. The j=0 will also have no effect. Avoid doing effect-less assignment.

@H.cohen 2019-01-06 16:01:32

Thank you for your time and input, I will add lines, my white spaces shifted when I moved my code to this platform, I'll be sure to pay more attention to it next time. the const for the print function- is it not enough to state the const int* mat? Would Also want to know about the var initiation - in 89 they must be declared as I did, but I was led to believe it is good practice to init them at declaration - is it not?

@Reinderien 2019-01-06 16:27:34

Yes - const int *mat will do. As for combined declaration and initialization - yes, this is generally a good idea (if you're in C99 and you can postpone declaration). If you're stuck in C89, it still seems like nicer form to only initialize your loop variables once you get to the beginning of the for.