So throughout my learning of C and C++, I've always been discouraged from using static. So recently, I studied the Linux ls command's source code and found that most of the variables were static! Why is static so discouraged? Is it just discouraged in C++ or also in C?
The static declaration on member variables declares global data. Sometimes global data is required, but it is global data which is discouraged, whether it is declared as static members or data simply declared in global scope.
When it's required, there is no substitute. A typical example is an application object representing the instance of an application in typical GUI frameworks. There are values, handles and objects which are representative of the application, which for any given process does represent both global information and the kind of resources for which only one per process can exist.
For example, for any given processes, most operating systems provide a process id. That is, by its very nature, a global value, at least conceptually. The instance of a given application is related to that, and especially for applications which enforce a rule where only one instance of that application is allowed to execute on a machine, the instance (or the application object in a GUI framework which provides the instance) is global.
Static functions, on the other hand, are not at issue.
The reason all other uses of global or static member declarations is discouraged is that it violates encapsulation. Most abuses of static values mean there can't be multiple instantiations of an object, if the values declared as static members are otherwise not conceptually global in nature.
Novices might declare global or static values simply because they want access to that value from other classes, when such values are not conceptually global in nature. What they really intend, but may not recognize, is that when classes work together as a family, they may need to store some kind of pointer or connection between instantiations for them to communicate.