Click this link to see the Difference Between C and C++
Difference between Nested If-Else and Switch Case Statement
Slno. | Nested if-else Statement | Switch Case Statement |
1. | Its execution is depend on the expression value inside the statement. | Its execution is determined & controlled by the user input. |
2. | Compares both logical expressions and equality. | Compares only equality. |
3. | Evaluates integers, characters, floating points, pointers, boolean datatype etc. | Evaluates mainly integer value and single character, not string. |
4. | Uses multiple different expressions for multiple decisions. | Uses a single expression for multiple decisions. |
5. | Mainly if or else statement is executed. | Execute or checks each case statement one after the other until a break or exit statement encountered. |
6. | Comparatively difficult to write, organise or edit the code. | Easy to write, organise or edit the code. |
7. | Its execution speed is comparatively slow. | Its execution speed is comparatively fast. |
Difference between Local Variable and Global Variable
Slno. | Local Variable | Global Variable |
1. | A variable which is declared inside a function or block of code is known as a local variable. | A variable which is declared outside function or block is known as a global variable. |
2. | It is comparatively a simple variable. | It is comparatively a complex variable. |
3. | The scope of this variable is available within/inside a function in which they are declared. | The scope of this variable is available throughout the program.We can access this variable from any where from the program. |
4. | Local variables are normally declared inside a main/other function body. | Global variables are normally declared at the top of the program usually after header files. |
5. | Variables can be accessed only by those statements inside a main/other function in which they are declared. | Any related statement in the entire program can access this variable. |
6. | The Initial value of local variables are Garbage. |
The initial value of global variables are for int=0,float=0.000000,char-”Blank space(Non-printable char).” |
7. | Life of a this variable is started when it is created/starts executed and lost when the functions terminate. | Life of this variable exists until the program is executing i.e. it is created before the program’s global execution starts and lost when the program terminates. |
8. | Variable’s values are stored in a stack unless specified. | The compiler decides the storage location of variable’s values.It is stored on a fixed location decided by the compiler. |
9. | It is suggested to use this variable in the program mostly. | It is suggested to avoid/if not necessary, the use of global variables in regular practice. |
10. | Local variable’s values are unique for each block/function variable. | Global variable’s values are common for each block/function variable. |
11. | Data sharing is not possible as data of the local variable can be accessed by only one function. | Here, values are shared by all the related variables of the program.Hence, data sharing is possible as multiple functions can access the same global variable. |
12. | Parameters passing is required for local variables to access/share the value in other function. | Parameters passing is not necessary for a global variable as it is visible throughout the program. |
13. | When the value of the local variable is modified in one function, the changes are not visible/reflected in another function. | When the value of the global variable is modified in one function changes are visible/available for rest of the program. |
Difference between Array and String
Slno. | Array | String |
1. | Array can hold any data types data. | It can hold only char data type. |
2. | Array size must be a constant value. hence, array size can’t be changed once declared. | Normally string size also can,t be changed but can be modified using char pointer. |
3. | Array is not ended with null character by default. | string is ended with null (‘\0’) character by default. |
4. | The length of an array is fixed. | The size of string is not fixed. |
5. | Arrays are mutable i.e. the fields can be modified. | Strings are immutable i.e. value can not be changed in memory once created. |
6. | The elements of arrays are stored contiguously in increasing memory locations. | Strings can be stored in any manner in memory locations. |
Difference between Call by Value and Call by Reference
Slno. | Call by Value | Call by Reference |
1. | A copy of actual parameters value is passed into respective formal parameters i.e. A copy of the variable is passed. | The location/Reference (address) of actual arguments is passed into respected formal parameters which is accepted by the pointer i.e. A variable itself is passed. |
2. | Separate memory location is created/allocated for actual and formal parameters. | Same memory location is allocated/created for both actual and formal parameters. |
3. | Changes in formal parameters will not reflect changes in actual parameters. | Changes in formal parameters will reflect changes in actual parameters. |
4. | Primitive datatype are passed using ‘call by value’. | Objects are implicitly passed using ‘call by reference’. |
5. |
Syntax of Calling Function is – FunctionName(variablename1, variable_name2, . . . );
|
Syntax of Calling Funtion is – FunctionName (&variablename1, &variable_name2, . .. .); NB : In case of object, object.functionname( object);
|
6. |
Syntax of Receiving Function is – DataType FunctionName (datatype variablename1, datatype variablename2, . . .)
{ . . . codes } |
Syntax of Receiving Function is – DataType FunctionName ( datatype *variablename1, datatype *variablename2, . . .)
{ . . . codes}
NB : In the case of object, datatype functionname (classtype objectname) { . . . codes } |
7. |
It works locally.
|
It works globally.
|
8. |
The original value is not modified i.e. It does not allow to make any changes in the actual variables.
|
The original value is modified i.e. it allows to make changes in the values of original variables by using function calls.
|
9. |
Supports by most of the programming languages, to pass values as parameters, such as C, C++,C#, php, VB .Net etc.
|
Supports by least no. of the programming languages, to pass values as parameters, such as C++, Java etc.
|
10. |
It is supported by C.
|
Actually, there is no call by reference in C rather we can create the illusion of call by reference in C through pointers. In this case, we are sending the memory location of the argument through a pointer.
|
Difference between Structure and Union
Slno | Similarities |
|
1. | Both are user-defined data types. | |
2. | They allow the user to combine different data types under a single name. | |
3. | They are declared similarly. | |
4. | Both are used to store multiple data items. | |
Slno | Structure | Union |
1. | ‘Struct’ keyword is used to define a structure. | ‘Union’ keyword is used to define a union. |
2. | All members of a structure can be initialized at once. | Only the first member can be initialized at a time. |
3. | Size of memory occupied by the structure is equal to the sum of size of the each member. | Size of memory occupied by the union is equal to the size of the largest member present in it. |
4. | A structure uses separate memory space for all its members i.e. all the members of structure have unique memory location.Members of a structure do not share common memory space. | all the members use same storage one by one i.e.Members of union share common memory space. |
5. | Members of structure can be accessed individually at any time. | At a time, only one member of union can be accessed. |
6. |
In Structure, change in the value of one member can not affect the other in structure. |
In Union, change in the value of one member can affect the value of other member. |
Difference between Macro and Function
Slno | Similarities |
|
1. | Macros and functions are both constructs used in programming to encapsulate code for reuse and improve code organization. | |
2. | They both help reduce errors by encapsulating logic in a single place. | |
3. | They can support conditional execution and recursion depending on the programming language and context. | |
4. | Both macros and functions can accept parameters, allowing them to operate on different data inputs. | |
Slno | Macro | Function |
1. | Macro is a Pre-processed Process. | Function is a compiled process. |
2. | Here, Datatype checking is not done. | Here, Datatype checking is done. |
3. | Code length increases during macro expansion process. | Code length remains same during processing. |
4. | Speed of execution of program is faster. | Speed of execution of program is comparatively slower. |
5. | Macro does not check Compile Errors during processing. | Function checks Compile Errors during compilation of the program. |
6. | Before Compilation, macro name is replaced by macro value i.e. macro expansion. | During function call, mainly transfer of control takes place. |
7. | Generally Macros do not extend beyond one line of code. | Function’s code can be of any number of lines. |
8. | They are useful where small code appears multiple times in the program. | They are useful where large code appears multiple times in the program. |
9. | Use of macro in our program can lead to side effect. | It is believed that there is no side effect of function using in the program. |
10. | We cannot do recursion in macro. | We can do recursion in macro. |
11. | We cannot debug a macro code. | We can debug a macro code. |
12. | We cannot return a parameter value. | We can return a parameter value. |
Difference between Iteration and Recursion
Slno | Similarities |
|
1. | Both involve repetition. | |
2. | Both involve a termination test. | |
3. | Both can occur infinitely. | |
Slno | Iteration | Recursion |
1. | Iteration explicitly uses a repetition structure. | Recursion achieves repetition through repeated function calls. |
2. | Iteration terminates when the loop continuation. | Recursion terminates when a base case is recognized. |
3. | Iteration keeps modifying the counter until the loop continuation condition fails. | Recursion keeps producing simple versions of the original problem until the base case is reached. |
4. | Iteration normally occurs within a loop so the extra memory assigned is omitted. | Recursion causes another copy of the function and hence a considerable memory space’s occupied. |
5. | It reduces the processor’s operating time. | It increases the processor’s operating time. |
Difference between Procedure and Function
Slno | Similarities |
|
1. | Functions and procedures are both fundamental concepts in programming and both encapsulate code for reuse, manage scope, and enhance the readability and maintainability of programs. | |
2. | They can both accept parameters and contribute to error reduction by isolating specific tasks. | |
3. | Both promote modularity and thus help in breaking down complex programs into smaller, manageable logical units, making the code easier to understand, maintain, and debug. | |
Procedure | Function | |
1. | Procedure is a sub program which is included within main program/function. | Functions is also a sub program which is intended for specific task. eg. sqrt(), area(), avg() etc. |
2. | Procedure do not return a value. | Functions may or may not return a value. |
3. | Procedure cannot be called again and again i.e. one time used. | A Function can be called any number of times as per need of the program. |
4. | Global variables cannot be used in procedure. | A function can use both local as well as global variables as per need of the program. |
5. | Procedures can be used only in procedural programming concept such as Dbase, Foxpro etc. | Functions can be used in modular programming concept such as C, C++ etc. |
Difference between Break and Continue
Slno | Similarities |
|
1. | Both statements are control flow statements used in loops to alter the flow of execution of the loop. | |
2. | Both statements can improve the readability and efficiency of loops by avoiding unnecessary iterations or code execution. | |
3. | Both can be used in nested loops. | |
Break | Continue | |
1. | Break is used to terminate the loop code or exit from the block of code where ever it encountered. The control then jumps to next statement after the loop or block. | Continue is used for continuing the next iteration in the loop, leaving certain given condition iteration only. |
2. |
Break statement can be used with looping statement and switch statement. When break statement is used in nested loops, then only the innermost loop is terminated.
|
This statement skips only the given condition in loop and continue the rest of the statements as it is. This continue statement used with looping statement.
|
3. |
Syntax:
{
statement1;
statement2;
statement3; break;
}
|
Syntax:
{
statement1;
continue;
statement2;
statement3; break;
}
|
Difference between Parameters and Arguments
Slno | Similarities |
|
1. | Both are used to pass information to functions or methods in programming. | |
2. | Both influence the behavior and outcome of the function or method. | |
3. | Both are ordered and must match positionally or by name when calling the function. | |
Parameters | Arguments | |
1. | Parameters are the variables listed as part of a function’s or method’s definition. |
Arguments are the actual values or data we pass to the function when we call it. |
2. | They act as placeholders for the values that will be passed to the function when it is called. | These values are assigned to the corresponding parameters in the function definition. |
3. |
For example – In the function definition addition(int a, int b) ‘a’ and ‘b’ are parameters.
|
For example – In the function definition addition(15, 25) ’15’ and ’25’ are arguments.
|
4. |
Parameters define what kind of input the function expects. They are part of the function’s signature.
|
Arguments are the actual input that is provided to the function when it is invoked. They are the real data that the function will operate on during execution.
|
5. |
Parameters are located in the function’s definition.
|
Arguments are located in the function call.
|
6. |
Each parameter receives the value of the corresponding argument.
|
Arguments bind to the parameters and determine the function’s behavior for that particular invocation.
|
7. |
Parameters can have default values, meaning if an argument is not provided, the default value is used. For example – addition(int a, int b=5), here b has default value 5.
|
Arguments can be provided to override the default values of parameters. For example -addition(15) uses the default value of b=5 along with a, while addition(15, 25) overrides it with 25. |
Click this link to see the Difference Between Structure and Class
Difference between Preprocessor Directives and Macro
Pre-Processor Directives | Macro | |
1. |
Preprocessor directives begin with the # symbol. |
They are created using the #define directive. |
2. |
These are instructions that are executed by the C preprocessor before the actual compilation begins. They tell the preprocessor how to modify or process the source code before it is compiled. |
Macros are a specific type of preprocessor directive that define constants, inline functions, or code snippets to be substituted directly into the source code. |
3. |
They are general commands that guide the preprocessing phase of compilation. For examples #include, #define, #ifdef, and #undef. |
They are defined using the #define directive, and they can either be:
|
4. |
They provide a way to manipulate code before the compilation process. Preprocessor directives are used for:
|
|
5. |
Preprocessor Directives are –
#include: For file inclusion.
#define: For defining macros (both object-like and function-like).
#undef: For undefining a macro.
#ifdef, #ifndef, #if, #elif, #else, #endif: For conditional compilation.
#error: For raising compilation errors.
#pragma: For compiler-specific commands.
|
Macros are – (i) #define MAX_LENGTH 100 (ii) #define SQUARE(x) ((x) * (x)) |
6. |
They perform broader tasks such as including header files (#include), controlling conditional compilation (#ifdef), or modifying compiler behavior (#pragma).
|
They are primarily used to replace repetitive code or provide inline code substitution at compile time. |
7. |
They affect the entire source file or multiple files in a project since they instruct the preprocessor on how to handle the code before compilation.
|
They have a limited scope and are used for specific instances where a constant or inline code needs to be substituted in multiple locations within the same file (or across files if included). |
8. |
Preprocessor directives provide more general instructions for how the preprocessor should treat the source code
|
Macros are a type of preprocessor directive specifically used to define constants or inline code snippets |
0 Comments