HRISHI COMPUTER EDUCATION PROGRAMMING

©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING

Session 1

Topic No Beginning with C++

Sub-Topic: 1 What is language?

2 Introduction of C and C++

3 Application/ Characteristics/ Advantage of C++

4 Procedure of writing programs

5 Header Files and Pre-processor Directive

6 Declaration of main function

7 Program Statements using keywords

8 cout and cin objects

9 Compiling and Linking process

10 Extensions .cpp, .obj and .exe extension

11 Execution of the program

12 Types of errors in C++

13 Output of the program

14 Comments in C++

©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING

Session 1 Beginning with C++

What is language? ▪ Language is a medium of communication ▪ In day to day life we use language to interact with each other. ▪ E.g. English, Hindi, Marathi etc...

What is Programming Language? ▪ We need to communicate with a computer to get the task done as per our requirement. ▪ But, we can’t communicate it directly as computer understands binary language. ▪ To communicate with computer user friendly languages are developed which are known as programming languages ▪ There are many programming languages which can be used to communicate with computer like C, C++, .Net, Java, Python etc. ▪ Therefore, a programming language is a language that can be used to control the behavior of a computer.

Introduction of C and C++? ▪ Dennis Ritchie and Bjarne Stroustrup has developed C and C++ languages which can be used by users to communicate directly with computers. ▪ C and C++ uses character set which is similar to English. So it becomes easy to learn a language. ▪ The messages written in C++ languages, needs to be translated into binary language or machine language. ▪ To translate these messages special system software is used which is known as compiler. Some languages also use interpreter. ▪ To translate messages written in C or C++ in computer language / binary language Turbo C++ compiler for Dev-C++ compilers are used ▪ Turbo C++ is developed by Borland international. ▪ Dev-C++ 64 bit compiler is developed by Bloodshed Software until 2005, Orwell (Johan Mes) since 2011

Features of the C Programming Language ▪ C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973

©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING

It was mainly developed as a system programming language to write an . ▪ The main features of the C language include low-level access to memory, a simple set of keywords, and clean style, these features make C language suitable for system programming like an operating system or compiler development. ▪ Many later languages has borrowed syntax/ features directly or indirectly from C language. Like syntax of JAVA, PHP, JavaScript and many other languages are mainly based on the C language. C++ is nearly a superset of C language

Features of C++: ▪ C++ is an object-oriented programming language. It was developed by Bjarne Stroustrup at AT & T Bell Laboratories in Murray Hill, New Jersey, USA, in the early 1980’s. ▪ Therefore, C++ is an extension of C with a major addition of the class construct feature. ▪ C++ is incremented version of C ▪ During the early 1990’s the language underwent a number of improvements and changes. In November 1997, the ANSI/ISO standards committee standardized these changes and added several new features to the language specifications. ▪ C++ is superset of C. Therefore, almost all C programs are also C++ programs. ▪ The most important facilities that C++ adds on C are classes, inheritance, function overloading and operator overloading. ▪ Any real life application system such as editor, compiler, databases, communication systems can be easily implemented, maintained & expanded.

Application / characteristics / Advantages of C++. ▪ C++ is a versatile language for handling very large programs. ▪ It is suitable for virtually any programming task including development of editors, compilers, databases, communication systems and any complex real-life application systems. ▪ Since C++ allows us to create a hierarchy-related object, we can build special object- oriented libraries which can be used later by many . ▪ While C++ is able to map the real-world problem properly, the C part of C++ gives the language the ability to get close to the machine-level details. ▪ C++ programs are easily maintainable and expandable. When a new feature needs to be implemented, it is very easy to add to the existing structure of an object.

©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING

Header files, Library Files and preprocessor Directive. Header Files: ▪ Header files contain declaration needed by the programs such as IOSTREAM.H contains declarations that are needed by the cout identifier and << operator. Header files are also called include files. In C header files are optional but in c++ header files are always necessary. If appropriate header files are not included compiler gives an error message any header file can be read with a normal text editor.

Header Files and Library Files: ▪ To use a library function like sqrt(), a library file SLIBCE.LIB must be linked to the program. The appropriate functions from the library file are then connected to the program by the linker. The functions in the source file needs to know the names and types of the functions and other elements in the library file. They are given this information in a header file. Each header file contains information for a particular group of functions. The functions themselves are in the library file, but the information about them is scattered throughout a number of header files. IOSTREAM.H contains information for various I/O functions and objects, MATH.H contains information for mathematics functions MEMORY.H contain memory management function and STRING.H contains string functions.

Pre-processor Directive: ▪ Such statements do not end with a semicolon. Such directives are instructions to the compiler. A part of the compiler called Preprocessor deals with such directives before it begins the real compilation process. #include Directive: o #include directive tells the compiler to insert another file into your source file. The #include directive is replaced by the contents of the file indicated. Filename can be provided to this directive in two ways a) in angle brackets < and > b) in double- quotes. When angle brackets are used the header file is searched in standard INCLUDE directory and when double quotes are used search is done in the current directory i.e. the directory which contains the source file.

Declaration of the main function. Main function Declaration: ▪ The next part of a C program is to declare the main () function. The syntax to declare the main function is ▪ Syntax to Declare the main method: int main() { } ©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING

Program statements using keywords. Character set: ▪ A character set is a set of alphabets, letters and some special characters that are valid in C language.

▪ Alphabets: o Uppercase: A B C ...... X Y Z o owercase: a b c ...... x y z o C accepts both lowercase and uppercase alphabets as variables and functions.

▪ Digits: o 0 1 2 3 4 5 6 7 8 9

Special Characters:White space Characters: ▪ Blank space, newline, horizontal tab, carriage, return and form feed.

, < > . _ ( ) ; $ : % [ ] # ?

‘ & { } “ ^ ! * / | - \ ~ +

C Keywords: ▪ Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example: ▪ int money; ▪ Here, int is a keyword that indicates money is a variable of type int (integer). ▪ As C++ is a case sensitive language, all keywords must be written in lowercase. Here is a list asm double new switch auto else operator template break enum private this case extern protected throw catch float public try char for register typedef class friend return union const goto short unsigned

©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING continue if signed virtual default inline sizeof void delete int static volatile do long struct while

▪ All these keywords, their syntax, and application will be discussed in their respective topics. However, if you want a brief overview of these keywords without going further, visit List of all keywords in C programming. cout and cin objects. cout Statements: ▪ Here ‘cout’ is an object. It is predefined in Turbo C++ to correspond to standard output stream. ▪ A stream is an abstraction that refers to flow of data. ▪ Standard output is usually a screen, although the output can be redirected to other devices. ▪ The operator ‘<<’ is called the insertion or put to operator. ▪ It inserts (or sends) the contents of the variable on its right to the object on its left. For example: cout << string; ▪ It directs the contents of the variable on its right to the object on its left. In C++ this operator is overloaded, which is used in C as a left-shift bit-wise operator. ▪ Unlike in C where while using printf() functions, the data type of variable has to be specified such as %d or %f, in c++ variable of any data type can be passed to the cout object and ‘<<’ operator is overloaded to handle each type of variable. ▪ ‘<<’ can be cascaded multiple times in a single cout statement like cout<<””<< ; instead of giving multiple cout statements. cin Statements: ▪ cin is an object in C++, predefined to correspond to standard input stream. Unless redirected, it represents data coming from keyboard. ‘>>’ is an extraction or get from operator. ▪ ‘>>’ can also be cascaded allowing user to enter multiple values at the same times but this way is not popular as the user cannot be promoted each time. ▪ If extracts (or takes) the value from the keyboard and assign it to the variable on its right. This is similar to scanf() operation. For example: cin>> number1; ▪ This statement causes the to wait for the user to type in a number. The number keyed in is placed in the variable number1. ©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING

▪ cin is pre-defined object in C++ that corresponds to standard input stream. ▪ Here, it represent keyboard.

Compiling and Linking process. 1. Two steps are necessary to transform the source file into machine-readable form: Compiling and linking. 2. Compiling is a transition process that transform text file into machine readable form. The compiler generates an object file with .OBJ extension. 3. Linking process links various library routines necessary for input/ output and other purpose. In a multi file project, the linker also combines the object files generated from your source file. After linking processes executable file is created

The procedure for writing C++ programs. ▪ C++ programs are saved with .CPP extension. ▪ Two steps are necessary to transform the source file into machine-readable form: Compiling and Linking. ▪ Compiling is a translation process that transforms text file into machine-readable form. The compiler generates an object file with .OBJ extension. ▪ Linking process links various library routines necessary for input/output and other purposes. In a multi-file project, the linker also combines the object files generated from your source file. After linking process .exe file is created.

Steps to Execute the Program: ▪ Open any text editor. ▪ Write the code for the program. ▪ Save the file with .CPP as an extension. ▪ Compile the program. ▪ Run the program.

Types of errors in C++. 1. Compilation errors: 2. Linking errors 3. Run time errors 4. Conceptual errors

▪ Compilation and linking errors are shown in the editor. Run time errors are shown on the output screen.

©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy

HRISHI COMPUTER EDUCATION C PROGRAMMING

▪ Compilation errors are detected at the time of parsing of the source code. These include simple typographical errors. ▪ Linker errors occur when the linker can't find a function or other program element referred to in a source file. ▪ Run time Errors which are not detected until the program is running such as division by 0, stack overflow and null pointer assignment etc. ▪ Errors are severe problems that make it impossible to compile a part of code. Warnings indicate potential problems and may not be serious.

Comments in C++. ▪ As compiler ignores comments they do not add to the file size or execution time of the executable program. ▪ // is a one line comment which terminates at the end of the line. Anything after // sign till the end of the line is treated as comment. ▪ /* */, which is the only comment available in C, is a multi-line comment.

©Reserved by HrishiOnlineBuddhi Visit Our Terms & Condition for content usage Policy