You are on page 1of 8

Characteristics of good Computer program.

Every computer requires appropriate instruction set (programs) to perform the required task. The quality
of the processing depends upon the given instructions. If the instructions are improper or incorrect, then
it is obvious that the result will be superfluous.
Therefore, proper and correct instructions should be provided to the computer so that it can provide the
desired output. Hence, a program should be developed in such a way that it ensures proper functionality
of the computer. In addition, a program should be written in such a manner that it is easier to understand
the underlying logic.
A good computer program should have following characteristics:
 Portability: Portability refers to the ability of an application to run on different platforms (operating
systems) with or without minimal changes. Due to rapid development in the hardware and the software,
nowadays platform change is a common phenomenon. Hence, if a program is developed for a particular
platform, then the life span of the program is severely affected.
 Readability: The program should be written in such a way that it makes other programmers or users to
follow the logic of the program without much effort. If a program is written structurally, it helps the
programmers to understand their own program in a better way. Even if some computational efficiency
needs to be sacrificed for better readability, it is advisable to use a more user-friendly approach, unless
the processing of an application is of utmost importance.
 Efficiency: Every program requires certain processing time and memory to process the instructions and
data. As the processing power and memory are the most precious resources of a computer, a program
should be laid out in such a manner that it utilizes the least amount of memory and processing time.
 Structural: To develop a program, the task must be broken down into a number of subtasks. These
subtasks are developed independently, and each subtask is able to perform the assigned job without the
help of any other subtask. If a program is developed structurally, it becomes more readable, and the
testing and documentation process also gets easier.
 Flexibility: A program should be flexible enough to handle most of the changes without having to rewrite
the entire program. Most of the programs are developed for a certain period and they require
modifications from time to time. For example, in case of payroll management, as the time progresses,
some employees may leave the company while some others may join. Hence, the payroll application
should be flexible enough to incorporate all the changes without having to reconstruct the entire
application.
 Generality: Apart from flexibility, the program should also be general. Generality means that if a program
is developed for a particular task, then it should also be used for all similar tasks of the same domain. For
example, if a program is developed for a particular organization, then it should suit all the other similar
organizations.
 Documentation: Documentation is one of the most important components of an application
development. Even if a program is developed following the best programming practices, it will be
rendered useless if the end user is not able to fully utilize the functionality of the application. A well-
documented application is also useful for other programmers because even in the absence of the author,
they can understand it.

1
Enumerated Types
Enumerated Types are a special way of creating your own Type in C. The
type is a "list of key words". Enumerated types are used to make a
program clearer to the reader/maintainer of the program. For example,
say we want to write a program that check for keyboard presses to find if
the down arrow or up arrow has been pressed. We could say: if (
press_value == 32 ). 32 is the computers representation of the down
arrwo. Or, we could create our own enumerated type with the key words:
down_arrow and up_arrow. Then we could say: if ( press_value ==
down_arrow ). This second version is much more readable and
understandable to the programmer.

Enumerated Types
Enumerated Types allow us to create our own symbolic names for a list of related
ideas. The key word for an enumerated type is enum. For example, we could create an
enumerated type for true and false (note: this is done for you by C and is type bool).

enum Boolean
{
false,
true
};

We could also create an enumerated type to represent various security levels so that
our program could run a door card reader:

enum Security_Levels
{
black_ops,
top_secret,
secret,
non_secret
}; // don't forget the semi-colon ;

2
These enumerated types can be used like any other type in a program. The type name
"Boolean" or "Security_Levels" can be used to define variables or the return type of
functions. The actual enumerations can be used directly in the code. For example:

int
main( int number_of_args, char* arg_list[] )
{
Security_Levels my_security_level = top_secret;

if ( my_security_level == black_ops )
{
printf("Welcome Mister Bond\n");
open_safe();
open_door();
}
else if ( my_security_level == top_secret )
{
printf("Welcome Q\n");
open_door();
}
else if ( my_security_level == secret )
{
printf("Please leave Now\n");
}
else if ( my_security_level == non_secret )
{
printf("Warning, The Police have been Called\n");
printf("Surrender yourself to them immediately!\n");
call_police();
}

return 0;
}

In Essence, Enumerated types provide a symbolic name to represent one state out
of a list of states.

3
Enumerated Types are Not Strings
It bears repeating: Even though enumerated type values look like strings, they are new
key words that we define for our program. Once defined the computer can process
them directly.

There is no need to use strcmp with enumerated types.

// CORRECT:

if ( my_security_level == secret ) ...

// INCORRECT:

if ( ! strcmp(my_security_level,secret) ) ...

// ALSO INCORRECT:

if ( ! strcmp(my_security_level,"secret") ) ...

Enums are Not (Really) Integers


It turns out that enumerated types are treated like integers by the compiler.
Underneath they have numbers 0,1,2,... etc. You should never rely on this fact, but it
does come in handy for certain applications.

For example: our security_levels enum has the following values:

black_ops = 0
top_secret = 1
secret = 2
non_secret = 3

4
Printing the "string" representation of the type.
Note: One of the short comings of Enumerated Types is that they don't print nicely. To print the "String"
associated with the defined enumerated value, you must use the following cumbersome code:

if ( my_security_level == secret )
{
printf("secret");
}
else if ( my_security_level == top_secret )
{
printf("top_secret");
}
...

An Enum String Printing Trick

The following "trick" to print enums (in a more compact notation) is made possible
because of the following two facts:

1. C arrays are based on index 0.


2. enums have a "builtin" integer value starting at 0.

The trick is to create an array of strings and use the enums to index the array. Here is
an example of how it is done:

// ONLY use for PRINTING


const char* Security_Levels_Strings[] = {"Black Ops", "Top Secret",
"Secret", "Non Secret"};

int
main()
{
Security_Levels s[4];

s[0] = black_ops;
s[1] = top_secret;
s[2] = secret;

5
s[3] = non_secret;

printf("If enumerated types were integers they would be: \n");

printf("\t%d %d %d %d \n\n", black_ops, top_secret, secret, non_s


ecret );

printf("Thus the array would contain: \n\t");

for (int i=0; i<4; i++)


{
printf( "%d ", s[i] );
}
printf("\n\n");

printf("To access the strings directly we can use:\n");

printf("\tSecurity_Levels_Strings[0] is : '%s'\n", Security_Level


s_Strings[0]);
printf("\tSecurity_Levels_Strings[1] is : '%s'\n", Security_Level
s_Strings[1]);
printf("\tSecurity_Levels_Strings[2] is : '%s'\n", Security_Level
s_Strings[2]);
printf("\tSecurity_Levels_Strings[3] is : '%s'\n", Security_Level
s_Strings[3]);

printf("OR:\n");

printf("\tSecurity_Levels_Strings[ black_ops ] is : '%s'\n", Sec


urity_Levels_Strings[black_ops]);
printf("\tSecurity_Levels_Strings[ top_secret ] is : '%s'\n", Sec
urity_Levels_Strings[top_secret]);
printf("\tSecurity_Levels_Strings[ secret ] is : '%s'\n", Sec
urity_Levels_Strings[secret]);
printf("\tSecurity_Levels_Strings[ non_secret] is : '%s'\n", Sec
urity_Levels_Strings[non_secret]);

printf("\nFinally, to get the strings associated with the stored


values in our array we write:\n");

for (int i=0; i<4; i++)


{
printf( "Security_Levels_Strings[s[%d]], which is '%s'\n", i,
Security_Levels_Strings[s[i]] );
}

return 0;

6
The C programming language provides a keyword called typedef, which you can use to give a type a new name.
Following is an example to define a term BYTE for one-byte numbers:
typedef unsigned char BYTE;
After this type definitions, the identifier BYTE can be used as an abbreviation for the type unsigned char, for
example:.
BYTE b1, b2;

By convention, uppercase letters are used for these definitions to remind the user that the type name is really a
symbolic abbreviation, but you can use lowercase, as follows:

typedef unsigned char byte;


You can use typedef to give a name to user defined data type as well. For example you can use typedef with
structure to define a new data type and then use that data type to define structure variables directly as follows:
#include <stdio.h>
#include <string.h>

typedef struct Books


{
char title[50];
char author[50];
char subject[100];
int book_id;
} Book;

int main( )
{
Book book;

strcpy( book.title, "C Programming");


strcpy( book.author, "Nuha Ali");
strcpy( book.subject, "C Programming Tutorial");
book.book_id = 6495407;

printf( "Book title : %s\n", book.title);


printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);

return 0;
}

When the above code is compiled and executed, it produces the following result:

Book title : C Programming


Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407

typedef vs #define
The #define is a C-directive which is also used to define the aliases for various data types similar totypedef but with
following differences:
 The typedef is limited to giving symbolic names to types only where as #define can be used to define alias for values
as well, like you can define 1 as ONE etc.
 The typedef interpretation is performed by the compiler where as #define statements are processed by the pre-
processor.

Following is a simplest usage of #define:

7
#include <stdio.h>

#define TRUE 1
#define FALSE 0

int main( )
{
printf( "Value of TRUE : %d\n", TRUE);
printf( "Value of FALSE : %d\n", FALSE);

return 0;
}

When the above code is compiled and executed, it produces the following result:

Value of TRUE : 1
Value of FALSE : 0

You might also like