hard-coding

What exactly is hard coding?


I believe that it's a value that is assigned and that cannot be changed. An example would be:

int limit =5;
    for(int i =0 ; i<limit; i++){
        printf("w");

Also what would be the reason why me shouldn't use hard coding? Sorry i am new to programming.


Solution

  • "Hard Coding" means something that you want to embed in your program or any project - that can not be changed directly (for example, using a value directly instead of using a variable or constant). If you are not hard coding, then you do something like prompting the user for the data or allowing the user to put the data on the command line, for example.

    So, in the example you provided, you can say that printing "w" is "hard-coded". Also, as @Ingo Leonhardt pointed out, limit is also "hard-coded" to be 5.

    Here is another example:

    Let's say we want to hard code the location of a file we are working on as being on the C: drive, you would just put the path name of the file all together in your source code:

    int main()
    {
        const char *filename = "C:\\myfile.txt";
    
        printf("Filename is: %s\n", filename);
    }
    

    The file name is "hard-coded" as: C:\myfile.txt