Q1) What are the header files? Explain their significance.
Header Files : Header files are files that contain definitions and declarations of functions, classes, and variables
that you want to use in your program. It is like information or reference sheet for the compiler, telling it what functions and definitions exist before they are used.
In languages like C and C++, header files usually have a `.h` extension and are included in programs using the #include directive.
Example in C :
#include <stdio.h>
This tells the compiler to include the declarations needed for input/output functions like printf(), scanf().
Example in C++ :
#include <iostream.h>
This tells the compiler to include the declarations needed for (or lets you use) input/output functions like cout<<,
cin>>.
What do header files usually contain?
- Function declarations
- Class definitions
- Constants
- Macros
Types of header files:
- Built-in / Standard header files :
- Already provided by C++ (like <iostream>, <cmath>, <string>).
- User-defined header files :
- Made by programmers to organize their own code (like “myfile.h”).
Why do we use them?
- To re-use code
- To keep code organized
- To avoid rewriting the same thing
- To make programs cleaner and
- easier to understand
The significance of header files are –
- They help you reuse code – Instead of writing the same functions again and again, you can just include a header file. This saves time and effort.
Example in cpp :
#include <math.h>
Now you can use math functions like sqrt() without writing them yourself.
- They keep your program organized – Large programs can be messy. Header files separate the structure (definitions) from the implementation (code). This makes the program easier to understand, fix, and update.
- They allow team collaboration – In group projects – One person writes class/function declarations in `.h` and Others implement them in .cpp. Everyone can work without confusion, because the header clearly shows what functions exist.
- They reduce errors – When functions are declared in one header file and used everywhere, there is one source of code without error.
- If something changes:
- You update the header file once
- All other files understand the change
- This prevents mistakes.
- They speed up compilation – Header files tell the compiler what functions exist and their types. This helps the compiler process your code efficiently.
Summary in one sentence:
- Header files provide a clean and organized way to reuse code, declare functions/classes, and help the compiler understand your program.
- Header files are important because they tell your program what tools it can use.
They:
* Save time — you don’t need to rewrite functions.
* Keep code organized — separate declarations from actual code.
* Avoid errors — one shared place to declare functions and classes.
* Help teamwork — everyone knows what functions and classes exist.
In simple words, Header files are like a list of instructions that help your program understand and use different parts of the code easily.
Q) Write the difference between Global and local variable.
- Global Variable: A variable declared outside all functions. It can be used anywhere in the program.
- Local Variable: A variable declared inside a function or block. It can be used only inside that function or block.
Scope (Where it can be used) :
-
- Global Variable: Can be used anywhere in the program
- Local Variable: Can be used only inside the function/block where it is declared
Lifetime Running :
-
- Global Variable: Exists for the whole execution of the program
- Local Variable: Exists only while the function/block is running
Declaration Place :
-
- Global Variable: Declared outside all functions
- Local Variable:Declared inside a function/block
Default Value :
-
- Global Variable: Automatically initialized to 0 (for basic types)
- Local Variable: Not automatically initialized — contains garbage value if not assigned
Memory Usage :
-
- Global Variable: Stored in global/static memory
- Local Variable: Stored in stack memory
Accessibility :
-
- Global Variable: Accessible by all functions
- Local Variable: Accessible only by that specific function/block
Example:
#include <iostream>
using namespace std;
int x = 10; // Global variable
void test()
{
int y = 5; // Local variable
cout << x << " " << y;
}
int main()
{
test();
cout << x; // Works
cout << y; // Error: y is local to test()
}
