-2
#include    <stdio.h>
int main(void){
    int row, col, mat[row][col], i, j, high = 0, sec_high = 0;
    scanf("%d %d", &row, &col);
    for(i = 0; i < row; i++)
        for(j = 0; j < col; j++){
        printf("\nEnter %d row %d col.\n", i, j);
    scanf("%d", &mat[row][col]);
    printf("%d \n", mat[row][col]);
        }
    for(i = 0; i < row; i++){
        for(j = 0; j < col; j++){
            if (high < mat[i][j]){
                sec_high = high;
                high = mat[i][j];                               
            }
            else if(sec_high < mat[i][j]){
                    sec_high = mat[i][j];
        }
        printf("i%d j%d h%d s%d mat%d\n", i, j, high, sec_high, mat[i][j]);
        }
    }
    printf("The  second highest number is: %d.", sec_high); 
    
}

What I am trying to do over here is simply to take out the second highest number from a matrix. Every thing is fine until The 3rd printf come where I am getting random numbers huge numbers. Which is totally unexpected since I have already printed those numbers once while inputting and every thing was fine. So what is the logical error over here?

Anagh Basak
  • 147
  • 9
  • 5
    You use `row` and `col` before you initialize or read anything into them. `int row, col, mat[row][col]` – Retired Ninja Nov 16 '20 at 06:54
  • 3
    Read the documentation of your compiler (e.g. [GCC](http://gcc.gnu.org/)...) and debugger (e.g. [GDB](https://www.gnu.org/software/gdb/)...). Read also [*Modern C*](https://modernc.gforge.inria.fr/). Read the [documentation](https://en.cppreference.com/w/c) of every function you are using (e.g. `scanf`, `printf`, etc...). Compile with all warnings and debug info : `gcc -Wall -Wextra -g`. Use `gdb` to understand the behavior of your program – Basile Starynkevitch Nov 16 '20 at 06:55
  • 1
    [This answer](https://stackoverflow.com/a/41410503/841108) is relevant to your question – Basile Starynkevitch Nov 16 '20 at 06:58
  • 2
    C programs are executed from the top to bottom. Not all lines at once. – Lundin Nov 16 '20 at 07:09
  • What is the difference between assigning to a variable and reading into one? – Anagh Basak Nov 25 '20 at 02:28

1 Answers1

1
int row, col, mat[row][col], i, j, high = 0, sec_high = 0;

is wrong, since row and col are not initialized. They could contain arbitrary values. What would happen if row contained 0x1fff0123 and col contained 0x34567890 ?

scanf("%d %d", &row, &col);

is also wrong, since scanf can fail and you don't test that.

At last, allocating large matrixes as automatic variables on the call stack is poor taste. Since the call stack is very limited (e.g. to a megabyte, and that means about 250 000 int-s, which is not much, since sizeof(int) is usually four bytes)

I recommend allocating your matrixes in the heap, using C dynamic memory allocation, as shown in this answer. Be sure to test success of malloc or of calloc and don't forget to call free later. Consider using valgrind to hunt memory leaks.

If you use a recent GCC compiler with all warnings enabled, e.g. as gcc -Wall -Wextra -g, you would get useful warnings. Then you can use the GDB debugger. Once your code is correct and you want to benchmark it, enable optimizations by adding the -O2 compiler flag.

Be sure to read more about C (e.g. the Modern C book and this reference) and the documentation of your C compiler and your debugger. You might be later interested by the C11 standard n1570 and by static analyzers for C code such as Frama-C (or by this draft report).

Take inspiration from existing open source C code

e.g. on github. Or GNU software (e.g. bash or make or emacs or libc or binutils or bison or gdb etc...) or interpreters or runtime components in Python or Ocaml or SBCL, or simple C compilers such as nwcc or tinycc (both of these are coded in C mostly) or libraries like libonion or GTK and many others.

Consider using the Clang static analyzer on your code.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547