Subscribe by Email


Showing posts with label Theory. Show all posts
Showing posts with label Theory. Show all posts

Thursday, September 25, 2014

What is a programming paradigm?

The fundamental style in which you design and write computer programs is called a programming paradigm. It’s the way you build the elements and structures of your programs. Presently we have the following 6 kinds of programming paradigms:
- Imperative
- Declarative
- Functional
- Object – oriented
- Logic
- Symbolic

Just as the different methodologies define the software engineering, different paradigms define the programming languages. Different languages are designed for supporting different paradigms. For example,
- Object oriented paradigm is adopted by smalltalk
- Functional programming is adopted by Haskell

Though there are certain programming languages that support only one kind of paradigm, there are many others that can work with multiple paradigms. Examples are:
- Object pascal
- C++
- Java
- C#
- Scala
- Visual basic
- Common lisp
- Scheme
- Perl
- Python
- Ruby
- Oz
- F#

The pascal and C++ programs can either be purely object oriented or purely procedural or may contain features of both. It is up to the programmers, how they want to use the paradigms. In object oriented programming, the program is considered as a collection of objects that can interact with each other. Whereas, in functional programming the program is considered as a sequence of the evaluations of stateless functions.
The process – oriented programming helps in designing the programs as a set of processes running concurrently in systems with multiple processors. The shared data structures are used by these processes. Some techniques are allowed by programming paradigms while others are forbidden. For example, the use of side effects is forbidden while using pure functional programming. The use of the dangerous goto statement is not allowed in structured programming. Because of this the modern programming paradigms are considered to be overly strict when compared to their older counterparts.
Proving the theorems can be easy if you avoid the use of certain techniques or if you understand the programs’ behavior. Sometimes programming models are compared with the paradigms. The abstractions of the systems are called systems. An example is the von Neumann model which is used in the sequential computers. There are a number of models for computers using parallel processing.
 Most of the models are based upon message passing, shared memory, hybrid etc. machine code and the assembly language instructions are the programming paradigms of the lowest level. The assembly language makes use of the mnemonics for operations. They are often referred to as the first generation languages. The assembly language is still in use for programming the embedded systems for obtaining direct control over the machine. The next generation of the languages is represented by the procedural languages. Examples are cobol, fortran, algol, PL/I, basic, c etc. procedural paradigm is adopted by all these languages.
The experience and ability of the programmer affects the efficiency and the efficacy of the problem’s solution. Later, the object oriented languages came in to the scenario such as the smalltalk, simula, java, Eiffel, C++ etc. These languages use objects (data and functions or methods for handling it) for modeling the real world problems. The object’s methods are the only way through which the user can access the data. Object oriented paradigm has led to the possibilities of creation of the object – oriented assembler language. For example, the HLA (high level assembly) is such a language that provides support for the advanced data types. In declarative programming paradigms, the problem is told to the computer but not the method. The program thus consists of properties that can be used for finding the result that is expected. The program is not a procedure. 


Saturday, September 3, 2011

What is a C PREPROCESSOR ? Why is it important?

C preprocessor or cpp is a program that is executed prior to the execution of the compiler. It’s very important as it removes all the comments from the source and performs textual substitution based on the code and passes it to the compiler for actual compiling and it also handles directives for the inclusion of source files. Consider the following statement:
#define
This is the way all macros and defines are declared. Before the start of compilation, the preprocessor resolves all of the defines. Macro is also a kind of define, but it is capable of appearing to perform some logical operations, math’s functions and it has a unique name. While defining a macro, it is imperative that there should be no space between the name of the macro and opening parentheses. If a space is present, it makes it difficult for the compiler to determine whether its macro or not. Instead it’ll handle it like a simple substitution define statement.
Often there are a lot of extra parentheses in a macro definition but they are extremely important. Most experienced C programmers always put parentheses around each and every variable in a macro and entire expression. This avoids error and allows any macro to work properly. A macro cannot be expanded within a quoted string, although, the arguments’ text can be quoted and that will be treated as a string literal. This can be done using “#” directive. There are macros called variadic macros which can take a varying number of arguments. They are useful when writing wrappers to variables of number functions. X macro is a header file which contains a list of similar macro calls. Macros are of two types namely object-like and function-like.
Object like macros do not accept parameters whereas function like macros do. Function like macro should not have a whitespace between the variable and the opening parentheses. Presence of a whitespace will make the compiler treat it as an object like macro. Object like macros are conventionally used to create symbolic names for constants and are generally considered as part of good programming. You can extend a macro across many lines by using a back slash escape sequence at the end of every line. The ternary conditional operator “?:” is also a function like macro. Token pasting or concatenation is another very easy to abuse feature of C preprocessor. Using this we can join together two arguments using ## preprocessor. This is basically used to create more elaborated macros. Multiple statements macros must be avoided as far as possible because they can show unexpected behavior. We can make a macro safe by replacing the semicolon with comma since it has lowest precedence.
Conditional compilation is a very useful construct and is used to include a feature if we are using a certain processor, a certain operating system and certain hardware. The following are the directives that can be used for conditional compilation: #if, #ifdef, #ifndef, #else, #elif and #endif. More trivial programs are too large and cannot be place within a single file and it becomes very cumbersome to work with. Therefore, it becomes very necessary for these files to work and communicate together as one large program.
What do we mean by pragma? It’s a kind of instruction to the compiler to perform a particular action at the time of compilation. The pragmas vary from compiler to compiler since they are not standardized. If the compiler provides a source listing file, then it does have pragmas to format output. #ptragma is used suppress specific error messages, to stack debugging and to manage heaps.


Sunday, August 28, 2011

Assignment and logical comparison in the C language

It is necessary to understand how C expresses logical relations. C treats logic as being arithmetical expression. The value 0 (zero) represents false, and all other values represent true. Code written by people uncomfortable with the C language can often be identified by the usage of #define to make a "TRUE" value. Because logic is arithmetic in C, arithmetic operators and logical operators are one and the same. there are a number of operators that are typically associated with logic:

Relational and Equivalence Expressions:
a < b 1 if a is less than b, 0 otherwise. a > b
1 if a is greater than b, 0 otherwise.
a <= b 1 if a is less than or equal to b, 0 otherwise. a >= b
1 if a is greater than or equal to b, 0 otherwise.
a == b
1 if a is equal to b, 0 otherwise.
a != b
1 if a is not equal to b, 0 otherwise

C does not have a dedicated Boolean type as many other languages do. 0 means false and anything else true. Often #define TRUE 1 and #define FALSE 0 are used to work around the lack of a Boolean type. It is a better idea to indicate what you are actually expecting as a result from a function call, as there are many different ways of indicating error conditions, depending on the situation. Another thing to note is that the relational expressions do not evaluate as they would in mathematical texts.

Logical Expressions
a || b
when EITHER a or b is true (or both), the result is 1, otherwise the result is 0.
a && b
when BOTH a and b are true, the result is 1, otherwise the result is 0.
!a
when a is true, the result is 0, when a is 0, the result is 1.

C uses short circuit evaluation of logical expressions. That is to say, once it is able to determine the truth of a logical expression, it does no further evaluation. we need not worry here about trying to access an out-of-bounds array element if it is already known that i is greater than or equal to zero.

Bitwise Boolean Expressions:
The bitwise operators work bit by bit on the operands. The operands must be of integral type. The six bitwise operators are & (AND), | (OR), ^ (exclusive OR, commonly called XOR), ~ (NOT), << (shift left), and >> (shift right). The negation operator is a unary operator which precedes the operand. The others are binary operators which lie between the two operands. The precedence of these operators is lower than that of the relational and equivalence operators; it is often required to parenthesize expressions involving these operators.

Assignment of values:
Programmers should take special care of the fact that the "equal to" operator is ==, not =. This is the cause of numerous coding mistakes and is often a difficult-to-find bug, as the expression (a = b) assigns a a value equal to b and subsequently evaluates to b; but the expression (a == b), called equality operator, checks if a is equal to b. It needs to be noted that, if you confuse = with ==, your mistake will often not be brought to your attention by the compiler. A statement such as if (c = 20) {} is considered perfectly valid by the language, but will always assign 20 to c and evaluate as true. A simple technique to avoid this kind of bug is to put the constant first.


Facebook activity