2

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.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
g_90
  • 23
  • 1
  • 4
  • An example of hard coding above is the 5 and the `"w"`. It is generally considered best practice to do things like `const char *mode = "w"` and `#define UPPER_LIMIT 5` near the top of the file, and then use `mode` and UPPER_LIMIT in the code. (The "w" is a trivial example, and most people would not object to hard coding the mode like this. ) – William Pursell Mar 04 '20 at 17:43
  • hard-coded password is the password in your code like `const char pass[]="mypassword";` – 0___________ Mar 04 '20 at 17:44
  • 2
    If I drink till 4am and go 9 to work - I will hard code for sure :) – 0___________ Mar 04 '20 at 17:45
  • https://en.m.wikipedia.org/wiki/Hard_coding – Jonas Wilms Mar 04 '20 at 17:59

4 Answers4

2

"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

1

The term hard-code something refers to when someone writes data directly to the code without using the proper methods to do the job.

An example of hard-coding would be if I need to return some data from the database but instead I write:

return "(1,2,'up')"

There are several reasons why someone would hardcode something, could be just to test something because is faster to write than the actual function, because it's Friday 14:00 and you want to go home, etc.

Gamopo
  • 1,600
  • 1
  • 14
  • 22
1

Hard Coding is using a value directly instead of creating a variable or constant. So, in your example, "w" is hardcoded, but the 5 is not. So someone reading your code will have some idea of why your loop is iterating 5 times (that's the limit), but not know why it's printing "w".

amoss
  • 1,571
  • 1
  • 15
  • 27
1

I believe that it's a value that is assigned and that cannot be changed.

No, that's called a constant. Hard coding rather means

Hard coding (also hard-coding or hardcoding) is the software development practice of embedding data directly into the source code of a program or other executable object

~ Wikipedia

So a "hard coded value" is defined in the source code, and thus can only be changed by a programmer. In a lot of cases, you actually don't want that. You rather want the value to be changeable by an user or an adminstrator. The only values that should be "hard coded" are things that are never supposed to be changed (e.g. physical constants).

Community
  • 1
  • 1
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151