Showing posts with label coding interview questions. Show all posts
Showing posts with label coding interview questions. Show all posts

Saturday, 23 December 2017


 Compiling And Linking    



 C systems generally consist of several parts: a program development environment, the language and the C Standard Library. C programs typically go through six phases to be executed. These are: edit, preprocess, compile, link, load and execute.
 


Creating a Program


Edit a file. This is accomplished with an editor program. Software packages for the C/C++ integrated program development environments such as Eclipse, Codeblock and Microsoft Visual Studio have editors that are integrated into the programming environment. 

You type a C program with the editor, make corrections if necessary, and then store the program on a secondary storage device such as a hard disk. C program file names should end with the .c extension or save it directly.

Preprocessing and Compiling a C Program


Here you give the command to compile the program. The compiler translates the C program into machine language-code (also referred to as object code). In a C system, a preprocessor program executes automatically before the compiler’s translation phase begins.

The C preprocessor obeys special commands called preprocessor directives, which indicate that certain manipulations are to be performed on the program before compilation. These manipulations usually consist of including other files in the file to be compiled and performing various text replacements. The most common preprocessor directives will discussed soon.
  
The compiler translates the C program into machine-language code. A syntax error occurs when the compiler cannot recognize a statement because it violates the rules of the language. The compiler issues an error message to help you locate and fix the incorrect statement. The C Standard does not specify the wording for error messages issued by the compiler, so the error messages you see on your system may differ from those on other systems. Syntax errors are also called compile errors, or compile-time errors.

Source codes in C are saved with .C file extension. Header files or library files have the .H file extension. Every time a program source code is successfully compiled, it creates an .OBJ object file, and an executable .EXE file.




Linking


The next phase is called linking. C programs typically contain references to functions defined elsewhere, such as in the standard libraries or in the private libraries of groups of programmers working on a particular project. The object code produced by the C compiler typically contains “holes” due to these missing parts. A linker links the object code with the code for the missing functions to produce an executable image (with no missing pieces). Compile and link a program If the program compiles and links correctly, a file called .out is produced.

Loading


The next phase is called loading. Before a program can be executed, the program must first be placed in memory. This is done by the loader, which takes the executable image from disk and transfers it to memory. Additional components from shared libraries that support the program are also loaded.

Execution


Finally, the computer, under the control of its CPU, executes the program one instruction at a time. To load and execute the program on a Linux system, type ./a.out at the Linux prompt and press Enter.

Monday, 2 October 2017

Introduction to C++ Language


C++ Programming Language


      C++ is a general purpose programming language and widely used for competitive programming. C++ language is an extension to C language and was developed by Bjarne stroustrup at bell labs. C++ has both high level and low level language features.

C++ is as the name says an extension of the C language. Back in the 1980s it was generally referred to as ‘C with classes’ (just like Objective-C, really), but it’s much more than that.

In addition to having a far more extensive standard library (STL strings are so nice after using C strings for ages), it also comes with a meta-programming language which puts the pre-processor of C/C++ to shame: template-programming.

While C is very powerful, it’s also deliberately very simple, or ‘high-level assembly’ as it’s often referred to. This also means that you’re basically just programming the hardware. C++ allows you to explore far more abstract programming concepts far more easily, as anyone who has ever has wrangled with a template-based project can attest to.

Basically, C is for when you are bootstrapping an OS, writing code for an MCU or trying to optimise the heck out of some drivers. C++ is for when you try to do anything that’d take half a lifetime to accomplish in C, or be impossible in another language.

C++ Language has a lot of features which are not in C :
  • C++ is Object-Oriented.
  • It has the concept of multiple inheritance, polymorphism and encapsulation.
  • All the OOPS features in C++ like Abstraction, Encapsulation, Inheritance etc makes it more worthy and useful for programmers.
  • C++ supports and allows user defined operators Operator Overloading,Virtual Function and function overloading is also supported in it. 
  • Bottom up approach adopted in Program Design. 
  • Stronger Type Checking in C++. 
  • Exception Handling is there in C++.
  • The Concept of Virtual functions and also Constructors and Destructors for Objects.
  • Inline Functions in C++ instead of Macros in C language. Inline functions make complete function body act like Macro, safely.
  • Variables can be declared anywhere in the program in C++, but must be declared before they are used.
  •  C++ is a powerful, efficient and fast language. It finds a wide range of applications – from GUI applications to 3D graphics for games to real-time mathematical simulations.


                           C++ runs on lots of platform like Windows, Linux, Unix, Mac etc. Before we start programming with C++. We will need an environment to be set-up on our local computer to compile and run our C++ programs successfully.If you do not want to setup a local environment you can also use online IDEs for compiling your program.

Using IDE: IDE stands for Integrated Development Environment. IDE is a software application that provides facilities to a computer programmer for developing softwares. There are many online IDEs available which you can use to compile and run your programs easily without setting up a local development environment.

Setup local environment
For setting up your own personal development environment on your local machine you need to install two important softwares:
  1. Text Editor: Text Editors are type of programs used to edit or write texts. We will use text-editors to type our C++ programs. The normal extension of a text file is (.txt) but a text file containing C++ program should be saved with ‘.CPP’ or ‘.C’ extension. Files ending with extension ‘.CPP’ and ‘.C’ are called source code files and they are supposed to contain source code written in C++ programming language. These extension helps the compiler to identify that the file contains a C++ program.
    Before beginning programming with C++, one must have a text-editor installed to write programs.
  2. C++ Compiler: Once you have installed text-editor and typed and save your program in a file with ‘.CPP’ extension, you will need a C++ compiler to compile this file. A compiler is a computer program which converts high-level language into machine understandable low-level language. In other words we can say that it converts the source code written in a programming language into another computer language which computer understands.For compiling a C++ program we will need a C++ compiler which will convert the source code written in C++ into machine codes. Below is the details about setting up compiler on different platforms.
Windows Installation: There are lots of IDE available for windows operating system which you can use to work easily with C++ programming language. One of the popular IDE is Code::Blocks. To download Code::Blocks you may visit this link. Once you have downloaded the setup file of Code::Blocks from the given link open it and follow the instruction to install.

  • After successfully installing Code::Blocks, go to File menu -> Select New and create an Empty file.
  • Now write your C++ program in this empty file and save the file with a ‘.cpp’ extension.
  • After saving the file with ‘.cpp’ extension, go to Build menu and choose the Build and Run option. 
Follow below picture for using Codeblock:

Using Codeblock
Using Codeblock

    Writing first C++ program


    Output:
    Hello World

     
    First C++ program







    Wednesday, 27 September 2017


    C Language Introduction


    C Programming Language

    C is a procedural programming language. It was developed by Dennis Ritchie in 1972. It was mainly developed as a system programming language to write Operating system. 

    The main features of C language include low-level access to memory, simple set of keywords, and clean style, these features make C language suitable for system programming like operating system or compiler development.

    Many later languages have borrowed syntax/features directly or indirectly from C language. Like syntax of Java, PHP, JavaScript and many other languages is mainly based on C language. C++ is nearly a superset of C language (There are few programs that may compile in C, but not in C++).

    C provides the fundamental control-flow constructions required for well-structured programs: statement grouping, decision making (if-else), selecting one of a set of possible values (switch), looping with the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).

    Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variables are typically ``automatic'', or created anew with each invocation. Function definitions may not be nested but variables may be declared in a block-structured fashion. 

    The functions of a C program may exist in separate source files that are compiled separately. Variables may be internal to a function, external but known only within a single source file, or visible to the entire program.

    A Preprocessing step performs macro substitution on program text, inclusion of other source files, and conditional compilation.

    C is a relatively low-level language. This characterisation is not pejorative; it simply means that C deals with the same sort of objects that most computers do, namely characters, numbers, and addresses. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.

    For more on C programming language theory – soon…

    Beginning with C programming:

    Compiler:

    Before we start C programming, we need to have a compiler to compile and run/execute our programs. There are certain compilers like Codeblock ,DevC++,Visual C++ that can be used to start C/C++ with installing a compiler.

    For Windows: There are many compilers available freely for compilation of C programs like Code Blocks  and Dev-CPP.   We strongly recommend Code Blocks.

    For running Codeblock for c/c++ program click here 


    Writing Your first program:


    Output:
    Easy programming

    Line 1:  # is to indicate that this line of code is preprocessed before passing to the compiler and the actual code after preprocessing does not include any line with # i.e the header file “stdio.h” got replace in place of 
    #include <stdio.h> . These header files generally contain declaration of functions. We need stdio.h for the function printf() used in the program.

    Line 2: In C, the execution typically begins with main() function. The void written in brackets indicates that the main doesn’t take any parameter. The int written before main indicates return type of main(). The value returned by main indicates status of program termination.

    Line 3: Brackets [ { and } ] In C language, a pair of curly brackets define a scope and mainly used in functions and control statements like if, else, loops.

    Line 4: printf() is a standard library function to print something on standard output screen/console. The semicolon at the end of printf indicates line termination. In C language, semicolon is always used to indicate end of statement.


    Line 5: The return statement returns the value from main(). The returned value may be used by operating system to know termination status of your program. The value 0 typically means successful termination.

    Geeks4Coding

    Contributors

    Categories

    Popular Posts