C language is widely used in embedded programming and many programmers write programs in their own style. Sometimes it is very diffcult for programmers to understand
and maintain code written by other programmers. So it is better to have some set of rules and adhere to that while writing programs.
The rules and recommendations presented here are not the only set of rules to be followed, but it can be used as a basis for writing C programs.
I have explained the rules in different categories of C language. The rules are classified under two categories : Required and Advisory.
Required means that rule has to be strictly followed and advisory means it can be followed but not mandatory.
1. File Structure
Rule 1 ( Required )
Include files in C always have the file name extension ".h"
Rule 2 ( Required )
Implementation files in C always have the file name extension ".c".
Rule 3 ( Required )
Divide up the definitions of functions into as many files as possible.
Rule 4 ( Advisory )
Place machine-dependent code in a special file so that it may be easily located when porting code from one machine to another.
Rule 5 ( Required )
Always give a file a name that is unique in as large a context as possible.
Rule 6 ( Required )
File names must be in mixed case starting with lower case
Rule 7 ( Required )
Every include file must contain a mechanism that prevents multiple inclusions of the file.
Rule 8 ( Required )
Use the directive #include "filename.h" for user-prepared include files.
Rule 9 ( Required )
Use the directive #include for include files from libraries.
Rule 10 ( Advisory )
If a file only contains information that is only needed in an implementation file, that file should not be included in another include file.
Rule 11 ( Required )
All function definitions should reside in implementation files
Rule 12 ( Advisory )
Include statements should be sorted and grouped. Sorted by their hierarchical position in the system with low level files included first. Leave an empty line
between groups of include statements.
Rule 13 ( Required )
Always give the project workspace file a name that is unique in as large a context as possible.
Rule 14 ( Required )
Don’t use absolute pathnames for header files. Use the construction for getting them from a standard place, or define them relative to the current directory.
Rule 15 ( Required )
Global ( Extern ) variables and structures are declared ( .h ) and defined ( .c ) in separate files
2. Naming Conventions
Rule 16 ( Required )
The names of variables, constants, and functions are to begin with a lowercase letter.
Rule 17 ( Required )
The declarations for Structures should be in the format given below
Rule 18 ( Required )
The declarations for Enumerations should be in the format given below
Rule 19 ( Required )
In names which consist of more than one word, the words are written together and each word that follows the first is begun with an uppercase letter.
Rule 20 ( Advisory )
Do not use typenames that differ only by the use of uppercase and lowercase letters.
Rule 21 ( Advisory )
Names should not include abbreviations that are not generally accepted.
Rule 22 ( Required )
Choose variable names that suggest the usage.
Rule 23 ( Required )
Named constants (including enumeration values) must be all uppercase using underscore to separate words.
Rule 24 ( Required )
Iterator variables should be called index1, index2, index3 etc..
Rule 25 ( Advisory )
The prefix is should be used for boolean variables and methods.
Rule 26 ( Advisory )
Enumeration constants can be prefixed by a common type name.
3. Control Flow
Rule 27 ( Advisory )
If a conditional expression always has the same result, there is no need for the condition.
Rule 28 ( Advisory )
The control variable in a for loop should be tested against a constant value, not a function or expression.
Rule 29 ( Advisory )
When testing incrementing or decrementing counters try to use the >= or <= instead of == in if-statements. If for some reason the value of the counter gets
higher or lower than expected in the test for equal you would run into problems.
4. Functions
Rule 30 ( Required )
Always write the left parenthesis directly after a function name
Rule 31 ( Required )
Function names must identify the action performed or the information provided by the function.
Rule 32 ( Required )
Do not have overly complex functions.
The number of control flow primitives( if, else, for etc ) in a function should be minimal
Rule 33 ( Advisory )
Do not use ellipsis '...' in function parameters.
Use of the ellipsis notation (...) to indicate an unspecified number of arguments should be avoided. It is better to develop specific methods for all
situations. Use of ellipsis defeats the type checking capability of C++
5. Variables and Constants
Rule 34 ( Required )
The use of magic numbers in the code should be avoided. Numbers other than 0 and 1 should be considered declared as named constants instead
Rule 35 ( Required )
Use of global and static variables should be avoided
Rule 36 ( Required )
Pointers and references should have their reference symbol next to the type rather than to the name.
Rule 37 ( Required )
Declare each variable on a separate line in a separate declaration statement. If the declaration is not self-explanatory, append a comment describing the variable.
Rule 38 ( Required )
Variables should be initialized in separate statements
Rule 39 ( Required )
Static variables should be explicitly initialized to 0
6. Pointers
Rule 40 ( Required )
Do not compare a pointer to NULL or assign NULL to a pointer; use 0 instead.
Rule 41 ( Required )
Pointers to pointers should whenever possible be avoided.
7. Comments
Rule 42 ( Required )
All files must include copyright information.
Rule 43 ( Required )
Every file that contains source code must be documented with an introductory comment that provides information on the file name and its contents.
Rule 44 ( Required )
All comments are to be written in English.
Rule 45 ( Required )
Write some descriptive comments before every function.
Rule 46 ( Required )
Use /* */ for all comments, including multi-line comments.
Rule 47 ( Required )
Tricky code should not be commented but rewritten
Clear code is preferable to well-commented messy code. Every effort should be made to make code clear through good design, simplicity, good naming and layout.
Comments are definitely required where code is not clear.
Rule 48 ( Advisory )
All comments in source code files must be up-to-date at all times during that code's lifetime.
8. Spacing , Indentation , Code Style
Rule 49 ( Required )
There should be only one operation per line.
Rule 50 ( Required )
Use spaces instead of tabs in a source file.The tab size for source files should be set to every three spaces.
Rule 51 ( Required )
Conventional operators should be surrounded by a space character.
Rule 52 ( Required )
Matching braces (‘{‘ ’}’) should line up vertically inline with the parent statement
Rule 53 ( Required )
Place single spaces immediately after commas, round brace in routine parameter lists, Control Flow Structures ( if,while,for ,switch etc )
Rule 54 ( Advisory )
Place single line space between blocks of code for better readability
Rule 55 ( Required )
Place single line space between variable declaration and statements inside the function
Rule 56 (Required)
Write only one function parameter per line in function declaration, definition
Rule 57 (Required)
Parenthesis should be used for return value
Rule 58 ( Required )
Do not use spaces around `.' or `->', nor between unary operators and operands.
Rule 59 ( Required )
Write switch and case keywords on the same level
Rule 60 ( Required )
One line spacing between if and else is not needed.
Rule 61 ( Required )
While using if-else if -else ,Else should be separated from if
9. Compiler Settings
Rule 62 ( Required )
While building the final exe file the mode of the compiler should be in ‘ Release ‘ and not ‘ Debug ‘
Rule 63 ( Required )
If the compiler supports any code checking standards like MISRA C enable that option
Rule 64 ( Advisory )
While compiling it is a good practice to set compiler warning levels to the maximum level. Eliminate warnings by changing your code, not by reducing the
warning level.
10. Portable code
Rule 65 ( Advisory )
Use explicit type conversions for arithmetic using signed and unsigned values.
Rule 66 ( Advisory )
Do not use compiler specific language or pre-processor extensions.
Rule 67 ( Advisory )
Organize source files so that the machine-independent code and the machine-dependent code are inseparate files. Then if the program is to be moved to a new machine,
it is a much easier task to determinewhat needs to be changed.
Rule 68 ( Required )
Always treat include file names as case-sensitive.
Rule 69 ( Advisory )
Do not depend on the order of evaluation of arguments to a function.The order of evaluation of function arguments is strongly compiler dependent.
Never use ++, -- operators on method arguments in function calls.
11. Optimization Techniques
Usage of For Loop
Try using decrementing for loop instead of incrementing for loop.
Passing Parameters to a function
It is better to have structure/ union pointers passed as function parameters instead of structure/ union instances. This is because if a structure/ union as a whole is passed
all the members will be passed as parameters which will increase the code memory. Instead pointers can be passed as parameters.
12. General Recommendations
- File content must be kept within 80 columns.
Naming Conventions
- The terms get/set must be used where an attribute is accessed directly.Eg. employee.getId(); employee.setId(id);
- The term compute can be used in methods where something is computed.Eg. valueSet->computeSum();
- The term find can be used in methods where something is looked up.Eg: vertex.findNearestVertex();
- Complement names must be used for complement operations
Eg : get/set, add/remove, create/destroy, start/stop, insert/delete, increment/decrement, old/new, begin/end, first/last, up/down, min/max, next/previous, old/new,
open/close, show/hide, suspend/resume, etc.
- It is also a good practice to keep track of the software versions and the Bugs found.
- It is always a good practice to have keep regular backups of the software.
Now a days cell phone has become a most important subject in our life, it is very essential in our daily life because its plays each and every role of our needs you can talk through a person one country to another with the help of cell phone you can send messages through cell phone you can access Internet through cell phone and for poor peoples it is very hard to buy a television or a music system it's cost a lot but now they can fulfill there dreams by the help of cell phone, yes now you can watch movies, TV serials and music too. And now this is time to change every body needs a portable thing which they can carry with, for example a worker is working in some company he needs a office stuff and laptop but laptop is not portable it is like a baby with schoolbag and now you can replace it, I mean keep laptop in your home and bring a cell phone because cell phone also have a featured of laptop you can make presentation, worksheet, PDF file, Ms Office and many more through the help of cell phone so my friends I told previously that cell phone is very essential in our daily life.
I was recently browsing through Yahoo answers and few other similar websites. I came across questions like how to clear history, how to delete temp files, etc. I thought of sharing this article.
If you are using Internet Explorer 7.0, follow these simple steps to clear browsing history and temp files:
1. Open IE Window
2. Click Tools > Internet Options
3. In the General tab, you can find "Browsing History."
4. Click Delete.
5. If you wish you can delete Temporary Internet files or History or Cookies separately else you may click "Delete All."
6. Once the deleting process is complete, close the browser.
Browsing History and temp files are successfully deleted now.
If you are using Firefox, follow these steps:
1. Open Firefox browser
2. Click Tools > Options
3. Click the icon named "Privacy."
4. Click "Browsing History Now' or "Clear Now" option depending on the version of Firefox.
5. Click Ok when the browsing history is deleted.
If using Google Chrome, follow these steps to remove browsing history:
1. Open Google Chrome browser.
2. Are you able to see the wrench icon? Click this and choose "Clear browsing data."
3. Check the options depending on what you wish to delete. You can delete only browsing history or can delete cookies, cache, download history, etc.
4. Click "Clear Browsing Data."
Browsing history is now deleted.
Clearing history and temp files is an easy very task regardless of the browser you use.
We have heard of freedom to animals, human being , countries etc. But have any one of you heard of Software Freedom Day. This year September 19th is celebrated across the world as Software Freedom Day or SFD.
This day is meant to spread awareness of independent software and also prompt people to use independent software. This task has been done under the initiative of American based Software Freedom International . Every year the third Saturday in September is celebrated as
SFD.
The SFI was formed under the leadership of renowned software programmer Richard Stallman. They believed that for social development, awareness of computers should be spread to all people.
Ken Thompson, Dennis Ritchie etc of Bell Laboratory the research division of American telephone and Telegraph company (AT & T) developed Unix operating system. In 1973 Unix re launched with ‘C’ programming became very popular in business organizations , Universities etc.
But in 1980s Unix stopped giving source code to its customers. Also copyright rule was followed very strictly. This created uneasiness among software programmers and they decided to fight against the commercialization of software. In 1983 September 27th GNU project started. The meaning of GNU is “GNU is Not Unix”
In 1984 Stallman started the programming of GNU Operating system. In 1985 March GNU Manifesto was published. In 1985 October 4th Free Software Foundation was formed.
In 1992 GNU completed almost the work of its operating system. But could not able to complete its central body or kernel. In 1991 Linus Torwalds of Helsinki University has programmed a kernel called Linux. In 1991 itself he published its source code in internet. He was a post graduate in computer science. He developed this kernel at the age of 21 years.
Many programmers did so many additions and deletions and made Linux a high class kernel.
In 1992 Linux kernel accepted GNU General Public License. This helped GNU to complete its operating system, ie. Linux.
There is a story behind the Free software foundation. When Stallman was working in MIT, that time most softwares are available with their source codes. Everybody in his office were using printer that is network connected. This printer frequently shows problems like the paper getting jammed etc. Stallman made some changes in the driver of the printer. As per this one will get a message in his system if the print job given by him is completed. Also a message will be sent to all systems if paper gets jammed. This was very helpful for all. That time a new printer was installed in their office. Stallman liked to make similar changes in its driver also. But its source code was not available. Stallman approached the people who prepared the source code. But they didn’t give. He was very angry and decided to fight for independent softwares.
The first decision to be taken in ISA design is the type of internal storage in the CPU. The three major choice are: a stack and accumulator architectures. Let us study how a stack organization works.
A stack is a storage device that stores information in a last-in-first-out(LIFO) fashion. Only two type of operations are possible in a stack, namely push and pop operations. Push places data onto the top of the stack, while pop removes the topmost program and in some cases the operating can be used explicitly for execution of a program and in some cases the operating system implements its implicitly, such as in subroutine calls and interrupts, as discussed earlier. Some computers reserve a separate memory for stack operations. However, most computers utilize main memory for representing stacks. For accessing data stored in stack, we need a stack pointer(Sp) register. The SP register is initially loaded with the address of the top of the stack. Each instruction pops its source operands off the stack and pushes its result on the stack. In memory, the stack is actually upside down.
More Articles …
Page 28 of 46