I have a program where i want to print out the size of the terminal window in columns and rows.
To do this i want to call this function from int main()
int getConsoleSize(){
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return columns, rows;
}
int main looks like this
int main(){
int columns, rows = getConsoleSize();
printf("columns: %dn", columns);
printf("rows: %dn", rows);
//prevent window from closing
getc(stdin);
return 0;
}
When calling the function getConsoleSize() it gives me the output columns: 5990652n rows: 30
which is clearly not correct. The columns output is also not consistent but is always at least 6-7 numbers long.
But when the code is copy pasted from the function directly into main() it seems to return the correct values with the outputs columns: 120n rows: 30
.
Why is this and how do i fix it so that the function return the correct values
Skalmanen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.