Yes, computer code can be copyrighted. However, it is required that the code is creative. Meaning, it can't be the only way possible to accomplish something. It must be a creative solution.
Software copyright is the extension of copyright law to machine-readable software. While many of the legal principles and policy debates concerning software copyright have close parallels in other domains of copyright law, there are a number of distinctive issues that arise with software. This article will primarily focus on topics particular to software.
Software copyright is used by Software Developers and proprietary software companies to prevent the unauthorized copying of their software. Free and open source licenses also rely on copyright law to enforce their terms. For instance, copyleft licenses impose a duty on licensees to share their modifications to the work with the user or copy owner under some circumstances. No such duty would apply had the software in question been in the public domain.