7 Lecture

CS410

Midterm & Final Term Short Notes

Calling Conventions, Storage Classes and Variable Scope

Calling Conventions, Storage Classes, and Variable Scope are fundamental concepts in programming. Calling Conventions dictate how functions pass arguments and return values, ensuring smooth inter-function communication. Storage Classes control v


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF

**Question 1:**

What is a "calling convention" in computer programming?


A) A set of rules for naming variables within a function.

B) A set of rules for calling functions from different programming languages.

C) A set of rules that govern how functions pass arguments and return values.

D) A set of rules for defining functions in object-oriented programming.


**Solution:** C


**Question 2:**

Which storage class has a global scope and retains its value across function calls?


A) auto

B) extern

C) static

D) register


**Solution:** B


**Question 3:**

What is the default storage class for local variables in most programming languages?


A) auto

B) static

C) register

D) extern


**Solution:** A


**Question 4:**

Which storage class is used to define local variables with a lifetime that extends throughout the program's execution?


A) static

B) auto

C) extern

D) register


**Solution:** A


**Question 5:**

In C/C++, what keyword is used to declare a function with a variable number of arguments?


A) varargs

B) vararg

C) stdarg

D) ellipsis


**Solution:** D


**Question 6:**

In the context of function calling conventions, what does "caller-saved" refer to?


A) The caller function is responsible for saving and restoring the registers used by the called function.

B) The caller function is responsible for saving and restoring the stack space used by the called function.

C) The caller function is responsible for saving and restoring its own local variables during the call.

D) The caller function is responsible for saving and restoring the called function's local variables during the call.


**Solution:** A


**Question 7:**

What is the purpose of the "register" storage class?


A) It requests the compiler to allocate a register for the variable for faster access.

B) It indicates that the variable's value is stored in a hardware register.

C) It specifies that the variable can only be used within a specific function.

D) It requests the compiler to optimize the variable for space efficiency.


**Solution:** A


**Question 8:**

Which storage class is commonly used to share variables among multiple source files in C/C++?


A) static

B) extern

C) register

D) const


**Solution:** B


**Question 9:**

What happens to a variable declared with the "static" storage class inside a function?


A) The variable becomes a global variable.

B) The variable retains its value across function calls.

C) The variable is only accessible within the function where it is declared.

D) The variable is automatically initialized to zero.


**Solution:** B


**Question 10:**

In C/C++, what is the significance of the "const" storage class for variables?


A) It ensures that the variable's value cannot be modified after initialization.

B) It allows the variable to be accessed from any function in the program.

C) It instructs the compiler to allocate the variable in read-only memory.

D) It specifies that the variable's value cannot be optimized by the compiler.


**Solution:** A



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF

1. Question: What is a calling convention in programming?

   Answer: A calling convention is a set of rules that determine how function arguments are passed and returned between caller and callee functions during program execution.


2. Question: Explain the difference between automatic and static storage classes.

   Answer: Automatic variables have a local scope and are created and destroyed automatically upon entering and leaving the block of their declaration. Static variables retain their value across function calls and have a lifetime throughout the program execution.


3. Question: What is the purpose of the "extern" storage class specifier in C/C++?

   Answer: The "extern" specifier is used to declare a global variable that is defined elsewhere in the program, allowing multiple files to access the same variable.


4. Question: Describe the role of the "register" storage class in C.

   Answer: The "register" specifier suggests to the compiler to store a variable in a CPU register for faster access. However, the compiler can choose to ignore this request.


5. Question: How does the "const" keyword affect variable scope and value modification?

   Answer: The "const" keyword defines a constant variable whose value cannot be modified after initialization. It does not affect the variable's scope; it can still be local or global.


6. Question: What is the difference between function scope and block scope in C/C++?

   Answer: Function scope refers to the visibility of a variable within the entire function, while block scope refers to the visibility of a variable within a specific block or statement enclosed in curly braces.


7. Question: How are function arguments typically passed in the cdecl calling convention?

   Answer: In the cdecl calling convention, function arguments are pushed onto the stack from right to left, and the caller is responsible for cleaning up the stack after the function call.


8. Question: Explain the significance of the "static" keyword in global variable declaration.

   Answer: When "static" is used in global variable declaration, it limits the scope of the variable to the file it is defined in, making it accessible only within that file.


9. Question: What is the lifetime of a variable with the "static" storage class declared inside a function?

   Answer: A variable with the "static" storage class inside a function has a lifetime throughout the program execution and retains its value between successive function calls.


10. Question: How does the "extern" keyword work with functions in C/C++?

    Answer: The "extern" keyword is not used with functions. It is used to declare global variables that are defined in other files, but function declarations are implicitly assumed to be "extern" by default.

Calling Conventions, Storage Classes, and Variable Scope are essential concepts in computer programming that play a vital role in how programs manage memory, pass arguments, and control the visibility and lifetime of variables. 1. Calling Conventions: Calling Conventions define the rules and mechanisms for how function calls are made and how parameters are passed between the caller and the callee functions. Different architectures and operating systems may have their own calling conventions. Common calling conventions include the cdecl, stdcall, and fastcall conventions. The choice of calling convention affects performance, memory management, and interoperability between code modules. 2. Storage Classes: Storage Classes in C and C++ define the scope, lifetime, and linkage of variables and functions. The primary storage classes are auto, register, static, and extern. The 'auto' storage class is the default for local variables, and it signifies automatic storage allocation and deallocation within the scope. The 'register' storage class suggests that a variable should be stored in a CPU register for faster access. The 'static' storage class for local variables allows them to retain their values between function calls. The 'extern' storage class is used to declare variables or functions that are defined in other files, making them accessible across multiple source files. 3. Variable Scope: Variable Scope refers to the region of the program where a variable is accessible and holds a valid value. In C and C++, variables can have block scope, function scope, file scope, or global scope. Variables with block scope are accessible within the block in which they are declared. Variables declared inside a function have function scope, while variables declared outside any function have file scope. Global variables have visibility throughout the entire program, making them accessible from any part of the code. The scope of a variable influences its lifetime and the potential for naming conflicts. Understanding these concepts is crucial for writing efficient and maintainable code. Proper utilization of calling conventions helps improve code performance and interoperability. Storage classes play a vital role in managing memory and controlling variable lifetimes. Additionally, a clear understanding of variable scope ensures proper variable usage, minimizes potential bugs, and allows for effective modular programming. In conclusion, Calling Conventions, Storage Classes, and Variable Scope are fundamental concepts that every programmer must grasp to write robust, efficient, and portable code. With this knowledge, developers can create well-structured programs that optimize memory usage, enhance performance, and facilitate code reuse across various projects.