Question

In: Computer Science

C++ If you tried to compile a source file that doesn't contain a main() function, would...

C++

If you tried to compile a source file that doesn't contain a main() function, would this error be detected by the preprocessor, the compiler, or the linker? Briefly justify your answer.

Hint: think about how the files that don't have main() get processed in a project with multiple files. Alternatively, try it out and look at the error message you get.

Solutions

Expert Solution

C++ program without main function

  • In C++, programs cannot be executed without the main function, though by using preprocessor directives or source code define with arguments facilitates the program run without main function.
  • There are three methods that assist to process C++ program without the main function  
  1. Macro Arguments
  2. Static initializer
  3. Start function

Let's check with the Code:

1.Macro Arguments

#include<stdio.h> 
#define str main 
int str(void) 
{ 
        printf("Hello! Me without main"); 
        return 0; 
}

Here, A macro which defines the main

# define str main

The output:

Hello! Me without main

2. Static initializer

// Program without using main function
#include <iostream>

int execute()
{
        std::cout << "Int run()";
        exit(EXIT_SUCCESS);
}

static int s = execute();

int main()
{
        std::cout << "Int run() - never executed";
}

The Output:

  Int run()

3. Start function

/* Program without using main function */


#include <stdio.h>

void _start()
{
    printf("Run the program without main !");
    exit(0);
}

Here, start function would facilitate run program without main function.


Related Solutions

/* WordList source file * * *   This file will contain the function definitions you will...
/* WordList source file * * *   This file will contain the function definitions you will implement. *   The function signitures may NOT be changed. You may create your own *   helper functions and include them in this file. * *   In addition to the specific instructions for each function, no function *   should cause any memory leaks or alias m_list in any way that would result *   in undefined behavior. * *   Topics: Multilevel Pointers, Dynamic Allocation, Classes *...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
Write this program in C++ You are given a source file in your work area for...
Write this program in C++ You are given a source file in your work area for this assignment. It consists of a declaration for a Val node. There are declarations for three overloaded operator functions, which you must fill in: operator+, operator*, and operator!. operator+ should implement addition. operator* should implement multiplication. operator! should reverse the digits of its operand (i.e., of "this") and return the reversed integer. So for example !123 should return 321. It should be straightforward to...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
This should be done in JavaScript. The HTML file should only contain an empty main tag....
This should be done in JavaScript. The HTML file should only contain an empty main tag. All other HTML on the page should be created with JavaScript. The JavaScript file should be a separate file.   Make an empty HTML file, put an empty main tag inside the body. In your JavaScript, use querySelector to get a reference to the main tag and save it in a variable named main. Look up three good jokes and store them as separate variables...
Complete the function main in file median.c to implement the computation of the median of a...
Complete the function main in file median.c to implement the computation of the median of a sequence of integers read from stdin. The program should use realloc to allow an arbitrary number of integers to be read into a dynamically allocated array. Every time realloc is called, let the new size of the array be twice the old size plus 1. Look at function readLongLine in the slides for Chapter 6 for an example of how to use realloc. The...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
Inside the skeletal file you'll find a complete main function, as well as the function headers...
Inside the skeletal file you'll find a complete main function, as well as the function headers for the DispMatrix and InitMatrix functions. A 15x5 array of ints is allocated in the main function, without being initialized. Just to highlight the fact that the array will contain undefined values, a call is made to the DispMatrix function, which will display the 2D array's contents (you should just see a bunch of garbage values). The DispMatrix will receive as arguments the base...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT