Blog‎ > ‎

Fun with C

posted 14 Apr 2012, 01:18 by olnf Admin   [ updated 19 Jun 2012, 14:16 ]

Author: Sudeep Jaiswal


Every one of you, at some point has been required to make an account on a website. What interested me about entering passwords was that we couldn’t see what we were typing; the characters were always hidden. I have created a simple program in C to demonstrate how it can be accomplished.

The following program requires you to enter characters. No matter what you type, an asterisk will be shown on the screen. When you are finished, press ‘Enter’ and you will be shown what you typed.

The program has some ‘flaws’ though. You cannot enter a password more than 50 characters long, and even though a typical website won’t allow it, the program will accept spaces and special characters like $, %, #, etc as valid characters. Also, if you happen to type 50 characters without hitting ‘Enter’, the program will show the password immediately.


Line

Program

1

#include<stdio.h>

2

#include<conio.h>

3

int main()

4

{

5

      char c[51];

6

      int i=0;

7

      while(((c[i]=getch())!=13) && i<=50)

8

      {

9

            putchar('*');

10

            i++;

11

      }

12

      c[i]='\0';

13

      printf("\n%s\n",c);

14

      return 0;

15

}



Most human beings are consumers. We are least interested in knowing how things are made or how they work. We don’t have the inclination or patience. So, I’ll try to keep my explanation as simple as possible.

---


Every C program has some pre-requisites; without them the program cannot work. In the first 2 lines, I have included certain ‘h’ files. These are called header files, and they contain the ‘meaning’, shall we say, of the functions we intend to use in the program. Functions are simply little programs themselves which help us perform certain tasks. In most cases, anything followed by a pair of parentheses ‘()’ in C is a function. See line 7, ‘getch()’ is a function; line 9, ‘putchar()’ is one too.

Different functions are defined in different header files. Hence, depending on the functions we use, we need to include the respective header files. The next thing required is ‘main()’. You may have noted it is a function too. Below it, on line 4 is an opening brace and line 15 has a closing brace. The braces indicate the body/extent of ‘main()’.

In lines 5 and 6, I have defined variables used in the program. ‘c[51]’ is an array of 51 characters and ‘i’ is an integer, which I have given the initial value zero. ‘Array’ simply means a collection. Array ‘c’ will be used to store the password.

When talking about functions and parentheses earlier, I said ‘most cases’. See line 7, ‘while()’ is one of the exceptions. It is not a function, but a loop. Inside ‘while()’, there is the condition which will determine how long the loop will run. In simple English, the condition translates to ‘the entered character is not the ‘Enter’ key and current length of the password is less than or equal to 50 characters’. The characters we enter are also getting stored in the array as we will need them later on.

Similar to the ‘main()’ function, ‘while()’ has a pair of braces too, which indicate the extent of the loop. The statements inside the braces are the ones which will be performed if the condition is found to be true; if found false, those statements will be ignored. The loop continues to run as long as the condition is satisfied.

When the condition is true, ‘putchar()’ will put a character on the screen, here it is the asterisk. As I said before, the entered character is also being stored in the array. To avoid overwriting one character with the other, ‘i++’ (‘++’ is essentially ‘+1’) takes the position of the ‘cursor’, we might say, to the next position in the array. So the next entered character will be stored beside the previous one, which is exactly how we write words.

An array of characters in C is called a ‘string’, and rules say that a string must end with a special character ‘\0’ (backslash zero). This is the way computer understands the end of the entered password. In line 13, the ‘printf()’ function is used to print the password on the screen. ‘return 0’ doesn’t do anything substantial and it will not affect the program’s working if we removed it. Same with ‘int’, before ‘main()’ (in line 3). I was taught that it is a good habit to put them.

If you wondered about the semicolons, they are called ‘terminators’. They indicate end of a statement, just as full stops indicate end of a sentence.

---


Typing…


…Voilá!!



That’s it, really. Fellow consumers may download the ‘exe’ and see the program themselves. When you run it, you will simply see a cursor blinking. Just type and press ‘Enter’ when you’re done.

Download: password.zip (7.2 kb)

Comments