Computer Science
C Main
C Main is a function in the C programming language that serves as the entry point for a program. It is the first function that is executed when a program is run and is responsible for calling other functions and executing the program's logic. The main function must return an integer value to indicate the program's status.
Written by Perlego with AI-assistance
Related key terms
1 of 5
3 Key excerpts on "C Main"
- eBook - ePub
- George S. Tselikis(Author)
- 2022(Publication Date)
- CRC Press(Publisher)
iostream ) and read them. Don’t rely on them only as a source of some mystic knowledge that hides unseen secrets. They are text files that exist on your computer. Find them and see their content. It is an excellent source of extra knowledge and information.The main() Function
Each C++ program must contain a function named main() . The word main() must be written in lowercase characters. The code of the program, or else the body of the function, must be enclosed in braces ({}) . A statement is a command that will be executed when the program runs. Statements are typically written in separate lines and, almost always, each statement ends with a semicolon. Although the compiler does not care about the layout of the program, proper indentation and spacing make your program easier to read. Braces are used to group declarations and statements into a block or else a compound statement that the compiler treats as one. Besides functions, we’ll use braces in control statements and loops.The main() , as its name implies, is the main function of any program. We’ll talk about functions in Chapter 11 . Simply put, a function is a series of declarations and statements that have been grouped together and given a name (e.g., main() ). A function is called by its name to perform a specific task and may optionally return a value. The main() function is called automatically by the operating system when the program runs. The execution of the program begins with the first statement of main() and ends when the last statement of main() is executed, unless an exit statement (e.g., return ) is called earlier. Of course, if a severe error occurs during the execution of the program, such as a division by 0 , the program will terminate abnormally. The keyword int indicates that main() must return an integer to the operating system when it terminates. This value is returned with the return statement; the value 0 indicates normal termination, while non-zero values indicate some sort of failure. This declaration of main() - eBook - ePub
- Ivor Horton(Author)
- 2011(Publication Date)
- Wrox(Publisher)
Programs that will run as console applications under Visual C++ 2008 are programs that read data from the command line and output the results to the command line. To avoid having to dig into the complexities of creating and managing application windows before you have enough knowledge to understand how they work, all the examples that you'll write to understand how the C++ language works will be console programs, either Win32 console programs or .NET console programs. This will enable you to focus entirely on the C++ language in the first instance; once you have mastered that, you'll be ready to deal with creating and managing application windows. You'll first look at how console programs are structured.A program in C++ consists of one or more functions . In Chapter 1 , you saw an example that was a Win32 console program consisting simply of the function main() , where main is the name of the function. Every ANSI/ISO standard C++ program contains the function main() , and all C++ programs of any size consist of several functions—the main() function where execution of the program starts, plus a number of other functions. A function is simply a self-contained block of code with a unique name that you invoke for execution by using the name of the function. As you saw in Chapter 1 , a Win32 console program that is generated by the Application Wizard has a main function with the name _tmain . This is a programming device to allow the name to be main or wmain depending on whether or not the program is using Unicode characters. The names wmain and _tmain are Microsoft-specific. The name for the main function conforming to the ISO/ANSI standard for C++ is main . I'll use the name main for all our ISO/ANSI C++ examples because this is the most portable option. If you only intend to compile your code with Microsoft Visual C++, then it is advantageous to use the Microsoft-specific names for main .A typical command line program might be structured as shown in Figure 2-1 .Figure 2-1 illustrates that execution of the program shown starts at the beginning of the function main() . From main() , execution transfers to a function input_names() , which returns execution to the position immediately following the point where it was called in main() . The sort_names() function is then called from main() , and, once control returns to main() , the final function output_names() is called. Eventually, once output has been completed, execution returns once again to main() and the program ends.Of course, different programs may have radically different functional structures, but they all start execution at the beginning of main() - eBook - PDF
C++ Programming
Program Design Including Data Structures
- D. Malik(Author)
- 2017(Publication Date)
- Cengage Learning EMEA(Publisher)
You are now ready to create a complete C 11 program. A C 11 program is a collection of functions, one of which is the function main . There-fore, if a C 11 program consists of only one function, then it must be the function main . Moreover, a function is a set of instructions designed to accomplish a specific task. Until Chapter 6, you will deal mainly with the function main . The statements to declare variables, the statements to manipulate data (such as assignments), and the statements to input and output data are placed within the func-tion main . The statements to declare named constants are usually placed outside of the function main . The syntax of the function main used throughout this book has the following form: In the syntax of the function main , each statement ( statement_1 , . . . , statement_n ) is usually either a declarative statement or an executable statement. The statement return 0 ; must be included in the function main and must be the last statement. If int main() { statement_1 . . . statement_n return 0; } Copyright 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. WCN 02-300 2 Creating a C++ Program | 81 the statement return 0 ; is misplaced in the body of the function main , the results generated by the program may not be to your liking. The full meaning of the state-ment return 0 ; will be discussed in Chapter 6. For now, think of this statement as the end-of-program statement. In C 11 , return is a reserved word. A C 11 program might use the resources provided by the IDE, such as the necessary code to input the data, which would require your program to include certain header files. You can, therefore, divide a C 11 program into two parts: preprocessor direc-tives and the program. The preprocessor directives tell the compiler which header files to include in the program. The program contains statements that accomplish meaningful results.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.


