Important Differences in C Programming
Important Differences in C Programming
Interpreter Compiler
Scans the entire program and translates it as a
Translates program one statement at a time.
whole into machine code.
It takes less amount of time to analyze the It takes large amount of time to analyze the
source code but the overall execution time is source code but the overall execution time is
slower. comparatively faster.
Generates intermediate object code which
No intermediate object code is generated,
further requires linking, hence requires more
hence are memory efficient.
memory.
Continues translating the program until the first It generates the error message only after
error is met, in which case it stops. Hence scanning the whole program. Hence debugging
debugging is easy. is comparatively hard.
Ex-Programming language like Python, Ruby Ex-Programming language like C, C++ use
use interpreters. compilers.
Difference between High level language & Low level language
Points High level language Low level language
Learning High-level languages are easy to Low-level languages are difficult to
learn. learn.
Understanding High-level languages are near to Low-level languages are far from
human languages. human languages.
Execution Programs in high-level languages Programs in low-level languages are
are slow in execution. fast in execution.
Uses These languages are normally used These languages are normally used
to write application programs.
to write hardware programs.
It uses symbols for processes and I/O. No symbols are used, completely in text.
1. In else if ladder, the control goes through the every else if statement until it finds true value of the statement
or it comes to the end of the else if ladder. In case of switch case, as per the value of the switch, the control
jumps to the corresponding case.
2. The switch case is more compact than lot of nested else if. So, switch is considered to be more readable.
3. The use of break statement in switch is essential but there is no need of use of break in else if ladder.
4. Another difference between switch case and else if ladder is that the switch statement is considered to be less
flexible than the else if ladder, because it allows only testing of a single expression against a list of discrete
values.
5. Since the compiler is capable of optimizing the switch statement, they are generally considered to be more
efficient. Each case in switch statement is independent of the previous one. In case of else if ladder, the code
needs to be processed in the order determined by the programmer.
Switch case statement works on the basis of equality operator whereas else if ladder works on the basis of true false(
zero/non-zero) basis.
1 Test the loop condition each time through the Loop while we check the condition at the
loop & it keeps executing while the test bottom.
expression is a true value.
2.When the conditional expression is false then When the do- while is used ,the body of loop
the statements in body of Loop are skipped. is executed at least once, since the condition is
at the bottom of the loop.
Recursive function – is a function that is Iterative Instructions –are loop based repetitions
partially defined by itself of a process
Infinite recursion occurs if the recursion step An infinite loop occurs with iteration if the loop-
does not reduce the problem in a manner that condition test never becomes false
converges on some condition.(base case)
Recursion terminates when a base case is Iteration terminates when the loop-condition fails
recognized
Recursion is usually slower then iteration due Iteration does not use stack so it's faster than
to overhead of maintaining stack recursion
Recursion uses more memory than iteration Iteration consume less memory
Infinite recursion can crash the system infinite looping uses CPU
cycles repeatedly
Example: Find Factorial of a Number using Example: Find Factorial of a Number using
Recursion Iteration
int factorial(int n) int factorial (int n){
{
/* Base condition if n equals to 1 then return int i,fac = 1;
1 */
if(n==1) for ( i = 1; i <= n; i++){
return 1;
fac = fac * i;
else
/* Recursive call */ }
return (n * factorial(n-1) ); return fac;
} }
main() main()
{ {
printf("%d",factorial(5)); printf("%d",factorial(5));
} }
Output:120 Output:120
It not specified what happens if this array, i.e., string, is modified. So the value of a
string is the sequence of the values of the contained characters, in order.
Difference between array and pointer
Pointer Array
1. A pointer is a place in 1. An array is a single, pre allocated chunk
memory that keeps address of of contiguous elements (all of the same
another place inside type), fixed in size and location.
2. Pointer can’t be initialized at 2. Array can be initialized at definition.
definition. Example: int num[] = { 2, 4, 5}
3. Pointer is dynamic in nature. 3. They are static in nature. Once memory
The memory allocation can be is allocated , it cannot be resized or freed
resized or freed later. dynamically.
4. The assembly code of Pointer 4. The assembly code of Array is different
is different than Array than Pointer.
Arrays Structures
1. An array is a collection of related 1. Structure can have elements of
data elements of same type. different types
2. An array is a derived data type 2. A structure is a programmer-defined
data type
3. Any array behaves like a built-in 3. But in the case of structure, first we
data types. All we have to do is to have to design and declare a data
declare an array variable and use it. structure before the variable of that type
are declared and used.
Insert the complete example program in above difference
Memory is allocated before the execution of Memory is allocated during the execution of the
the program begins. program.
(During Compilation)
No memory allocation or deallocation actions Memory Bindings are established and destroyed
are performed during Execution. during the Execution.
Variables remain permanently allocated. Allocated only when program unit is active.
Calloc Malloc
Function allocates a region of memory large enough to allocates "size" bytes of memory.
hold "n elements" of "size" bytes each. Also
initializes contents of memory to zeroes.
Number of 2 1
arguments
What are the differences between syntax error, run time error and logic errors.?
1) Syntax Errors are errors in the syntax of your code. This kind of error is the easiest to figure
out. They are revealed when you attempt to compile your program. An example would be as
mentioned - forgetting the semi colon at the end of a statement in a language like C.
2) Run time errors are unhandled errors that are raised during runtime. For example, your code
fails to get access to a remote machine. There is no way to be sure at compile time (when you
compile your program) whether this error is SURELY going to happen or not. You can suspect
that it happens though.
3) Logic Errors are the sneakiest. Your syntax is correct. No runtime error is being raised,
however your program logic is not correct (for example an incorrect formula).
1. The key difference between linker and loader is that the linker
generates the executable file of a program whereas, the loader loads
the executable file obtained from the linker into main memory for
execution.
2. The linker intakes the object module of a program generated by the
assembler. However, the loader intakes the executable
module generated by the linker.
3. The linker combines all object module of a program to
generate executable modules it also links the library function in the
object module to built-in libraries of the high-level programming
language. On the other hands, loader allocates space to an
executable module in main memory.
4. The linker can be classified as linkage editor, and dynamic
linker whereas loader can be classified as absolute loader,
relocatable loader and dynamic run-time loader.