Presentation is loading. Please wait.

Presentation is loading. Please wait.

C/C++ Programming Environment

Similar presentations


Presentation on theme: "C/C++ Programming Environment"— Presentation transcript:

1 C/C++ Programming Environment
Few Basics Sanjaya Karunasena

2 Outline What is a Computer Program?
How to create a Computer Program using C/C++ Execution of a Computer Program Control flow

3 A Computer Program What is a Program? What is a Computer Program?
A program is a planned sequence and combination of activities designed to achieve specified goals. [ What is a Computer Program? A set of instructions that direct a computer to carry out a given task. [

4 A Computer Program cont.
In a typical computer (Von Neumann Architecture) A program resides in some permanent storage (Disk – File System, ROM, NVRAM, etc) When it is executed, it gets loaded in to the memory (RAM) Then instructions are fetched in to the CPU and executed

5 Program Creation A Programming Language can be used
To write a Program, any Text Editor can be used But an Integrated Development Environment (IDE) makes our life easier Following file extensions typically belongs to C/C++ code files Source Files Header Files *.c *.h *.cc *.chh *.cpp *.hpp *.c++ *.h++ *.cxx *.hxx

6 Program Creation cont. A compiler should be used compile the code written gcc, cc, g++, c++ : Some Unix C/C++ compilers cl.exe : Compiler Linker comes with Microsoft Visual Studio The compiler produces Common Object File Format (COFF – UNIX and Windows systems) or Executable and Linkable Format (ELF – Most UNIX systems) files called Object Files or Object Code *.o in UNIX *.obj in Windows

7 Program Creation cont. *.h Files *.h Files Pre-Processor Compiler
Object File *.cpp File *.cpp File Linker Static Library Object File Executable Note: A Static Library is a collection of object files

8 Program Creation cont. A Linker should be used to link the Object Files with other Object Files and Libraries ld in UNIX link.exe, comes with Microsoft Visual Studio The Linker produces the executable file(s) or libraries *.exe, *.lib, *.dll in Windows a.out default but can be given any name, *.bin, *.a, *.so in UNIX An interpreted program is executed using an Interpreter (No need to compile) VB, JavaScript, PERL, TCL

9 Program Execution Machine Program In Memory Load into Memory Dynamic
Library Executable Note: Kernel API has functions (CreateProcess, fork) which can be used to load and execute a program

10 IDE, Compiler and Linker
IDEs, Compilers and Linkers are also Programs How did the first Compiler / Linker created? This is not a Chicken and Egg problem

11 The Main Function The main function of a C/C++ program could be of couple of forms In C int main (void) In C++ int main ( ) In C/C++ int main (int argc, char ** argv) In Windows GUI Applications int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) When a program is executed the C/C++ run time environment calls this function

12 The Main Function cont. You cannot call main from within a program or take the address of main You cannot overload this function This is the programmer’s entry point to a program What is the system entry point to a program?

13 C Run-Time Startup C Run-Time Startup could be of many forms depend on the type of the application Function name Default for mainCRTStartup (or wmainCRTStartup) An application using /SUBSYSTEM:CONSOLE; calls main (or wmain) WinMainCRTStartup (or wWinMainCRTStartup) An application using /SUBSYSTEM:WINDOWS; calls WinMain (or wWinMain), which must be defined with __stdcall _DllMainCRTStartup A DLL; calls DllMain, which must be defined with __stdcall, if it exists

14 C Run-Time Startup cont.
The C/C++ compiler implements system entry point, mainCRTStartup function for us By default the linker appoint mainCRTStartup function as the system entry point to a program (A different function can be given with the linker option “/ENTRY:function”) Hence the system calls mainCRTStartup The mainCRTStartup calls our main function

15 Actual Execution Flow main mainCRTStartup KERNEL EXECUTABLE Initialize
Get Environment Variables Call to main Clean up Your Program Code main mainCRTStartup KERNEL EXECUTABLE

16 void main() The ISO C++ Standard (ISO/IEC 14882:1998) specifically requires main to return int But the ISO C Standard (ISO/IEC 9899:1999) actually does not If the return type is not compatible with int, the termination status returned to the host environment is unspecified This sounds dangerous However most of the compilers takes cares of this problem for us It’s either return 0 by default or won’t allow you to implement a main function without a return type

17 Summary A program is a set of instructions that directs a computer to carry out a given task A programming language can be used to create a program IDE, compiler, and linker plays an important role In a C/C++ program, Programmer’s entry point is the main function By default system entry point to a C/C++ program is “mainCRTStartup”

18 For next couple of weeks…
We need to quickly come up to the speed with basic C/C++ programming We are going to learn some thing very interesting, Object Orientation! Next week -> Introduction to Pointers Following is good tutorial

19 For next couple of weeks…
Please follow the following sections in order Intro to C++ The basics of C++ If statements If statements, and some boolean information Loops in C++ All you want to know about loops Functions Functions...all about them, making and using the critters Switch case About the switch case structure Pointers Using pointers to access memory locations Structures Structures, all about 'em, and how to use 'em Arrays All about arrays Strings About character arrays (strings) File I/O About file i/o Typecasting Typecasting: making variables look like another data-type Command line arguments How to accept command line arguments (AND checking file existence) Recursion Recursion--a function calling itself Formatted Output in C++ using iomanip Learn how to create nicely formatted output in C++ Understanding the C Preprocessor -- Constants, Macros, and other Tricks Learn how to use the C preprocessor

20 References http://www.delorie.com/djgpp/v2faq/faq22_25.html


Download ppt "C/C++ Programming Environment"

Similar presentations


Ads by Google