Q.Write the names of the correct header files, which must be included in the following C++ code to compile the code successfully: void main() { char L[]="CS 2018"; int N=strlen(L); cout<<L[N-1]; }
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →To successfully compile the given C++ code, the <iostream> header is needed for cout and the <cstring> header is needed for strlen.
When writing C++ programs, we often use functions and objects that are not defined directly within our code but are part of the C++ Standard Library. For the compiler to understand how to use these pre-defined components, it needs to know their declarations – essentially, their "blueprints." These blueprints are stored in special files called header files. By including the appropriate header files, we make these declarations available to our program, allowing the compiler to link our code with the library's implementations.
Let's break down the given code snippet to identify which standard library components are used and, consequently, which header files are required.
-
Analyze the
strlenfunction:The line
int N=strlen(L);uses thestrlenfunction. This function is a standard C-style string manipulation function that calculates the length of a null-terminated character array (string). In C++, functions related to C-style strings are typically declared in the<cstring>header file.NoteIn C, the equivalent header is
<string.h>. While<string.h>can often be used in C++ for backward compatibility, the C++ standard library provides<cstring>which places these functions within thestdnamespace (thoughstrlenis often also available in the global namespace for convenience). For modern C++ code,<cstring>is the preferred choice. -
Analyze the
coutobject:The line
cout<<L[N-1];usescout. This is the standard output stream object in C++, used for printing data to the console. Thecoutobject, along with other input/output functionalities likecinandcerr, is part of the iostream library. Its declaration is found in the<iostream>header file. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.