I have got unused variable error even though i have initialized the variable

Bug description:
When i tried to run the simple pattern cpp program i got an error in output that “unused variable” even though i have initialized the variable.

Expected vs Current Behavior:
I have written code with zero error

Steps to reproduce:

Bug appears at this link:

Screenshot(s)/Screen Recording:

Browser/OS/Device: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Replit Profile: https://replit.com/@sudharshanm1916

Hi @sudharshanm1916 , welcome to rhe forums!
‘Unused’ means that something (the variable) is there but you don’t use or call it.
So yes column is unused as you initialised it but never called it or used it anywhere. You can remove the line int column=5 and the program should work.


Just adding onto what NateDhaliwal said,

This program would normally work fine, but when compiling, you can see that the program uses the -Werror flag when compiling, so it treats all warnings as errors. To fix this, you can:

  1. Edit the Makefile to remove -Werror
  2. Remove the variable column completely (since you’re not using it anyways)
  3. Add the attribute [[maybe_unused]]so the compiler knows that this variable may be unused, and it should compile.

Hi @sudharshanm1916 !
Is your problem solved? If so, please mark the post that helped you most as the Solution. If not, feel free to clarify!

Not my question, but trying to learn more. How does the sytax for the attribute work? Is it a part of the cpp file or is it part of the makefile magic?

1 Like

It’s a standard C(++) feature, unrelated to make

1 Like