Saturday, January 28, 2017

C Language - Overview

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

C - Environment Setup

Try it Option Online

We have set up the C Programming environment on-line, so that you can compile and execute all the available examples on line. It gives you confidence in what you are reading and enables you to verify the programs with different options. Feel free to modify any example and execute it on-line.

C - Basic Syntax

You have seen the basic structure of a C program, so it will be easy to understand other basic building blocks of the C programming language.

C - Data Types

Data types in c refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.

C - Variables

A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.

C - Constants & Literals

Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.
Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well.
Constants are treated just like regular variables except that their values cannot be modified after their definition.

C - Storage Classes

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program. They precede the type that they modify. We have four different storage classes in a C program −

C - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −

C - Decision Making

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

C - Loops

You may encounter situations, when a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

C - Functions

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.

C - Scope Rules

A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language −

C - Arrays

Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

C - Pointers

Pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer. Let's start learning them in simple and easy steps.

C - Strings

Strings are actually one-dimensional array of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."

C - Structures

Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds.

C - Unions

A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.

C - Bit Fields

Suppose your C program contains a number of TRUE/FALSE variables grouped in a structure called status, as follows −
struct {
   unsigned int widthValidated;
   unsigned int heightValidated;
} status;

C - typedef

The C programming language provides a keyword called typedef, which you can use to give a type, a new name. Following is an example to define a term BYTE for one-byte numbers −
typedef unsigned char BYTE;

C - Input & Output

When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement.

C - File I/O

The last chapter explained the standard input and output devices handled by C programming language. This chapter cover how C programmers can create, open, close text or binary files for their data storage.
A file represents a sequence of bytes, regardless of it being a text file or a binary file.

C - Preprocessors

The C Preprocessor is not a part of the compiler, but is a separate step in the compilation process. In simple terms, a C Preprocessor is just a text substitution tool and it instructs the compiler to do required pre-processing before the actual compilation. We'll refer to the C Preprocessor as CPP.

C - Header Files

A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

C - Type Casting

Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'. You can convert the values from one type to another explicitly using the cast operator as follows −
(type_name) expression

C - Error Handling

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno. It is set as a global variable and indicates an error occurred during any function call. You can find various error codes defined in <error.h> header file.

C - Recursion

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
void recursion() {
   recursion(); /* function calls itself */

C - Variable Arguments

Sometimes, you may come across a situation, when you want to have a function, which can take variable number of arguments, i.e., parameters, instead of predefined number of parameters. The C programming language provides a solution for this situation and you are allowed to define a function which can accept variable number of parameters based on your requirement.

C - Memory Management

This chapter explains dynamic memory management in C. The C programming language provides several functions for memory allocation and management. These functions can be found in the <stdlib.h> header file.

C - Command Line Arguments

It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.

C Programming Questions and Answers

C Programming Questions and Answers has been designed with a special intention of helping students and professionals preparing for various Certification Exams and Job Interviews. This section provides a useful collection of sample Interview Questions and Multiple Choice Questions (MCQs) and their answers with appropriate explanations.

C - Quick Guide

C - Language Overview

C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972.

C - Useful Resources

The following resources contain additional information on C. Please use them to get more in-depth knowledge on this topic.

Discuss C

C is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to develop the UNIX operating system. C is the most widely used computer language. It keeps fluctuating at number one scale of popularity along with Java programming language, which is also equally popular and most widely used among modern software programmers.

C++ Library -

Introduction

This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

C++ Library -

Introduction

iomanip is a library that is used to manipulate the output of C++ program. Using C++, header providing parametric manipulators as shown below −

C++ Library -

Introduction

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming.
Input-Output base classes and types for the IOStream hierarchy of classes as shown below −

C++ Library -

Description

It is used to input-Output forward declarations,this header provides forward declarations for the types of the standard input/output library.

C++ Library -

Description

It is used in standard Input / Output Streams Library.

Declaration

Following is the declaration for iosstream function.

C++ Library -

Description

The Istream used for header providing the standard input and combined input/output stream classes.

Class templates

C++ Library -

Introduction

It is an output stream objects can write sequences of characters and represent other kinds of data. Specific members are provided to perform these output operations.

C++ Library -

Introduction

It is a string stream for header.

Class Templates

Following are the Class Templates for sstram.
S.N. Class Templates Definition
1 basic_istringstream It is an input string stream
2 basic_ostringstream It is an output string stream
3 basic_stringstream It is a basic string steam
4 basic_stringbuf It is a string stream buffer

C++ Library -

Introduction

It is a stream buffer and it is to be used in combination with input/output streams.

Class templates

S.N. Class Definition
1 basic_streambuf It is a basic stream buffer
2 wstreambuf It is used in base buffer class in stream

C++ Library -

Introduction

These are types that encapsulate a value whose access is guaranteed to not cause data races and can be used to synchronize memory accesses among different threads and he atomic library provides components for fine-grained atomic operations allowing for lockless concurrent programming. Each atomic operation is indivisible with regards to any other atomic operation that involves the same object.

C++ Library -

Introduction

It implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them.

C++ Library -

Introduction

It is a standard exception class. All objects thrown by components of the standard library are derived from this class. Therefore, all standard exceptions can be caught by catching this type by reference.

C++ Library -

Introduction

Function objects are objects specifically designed to be used with a syntax similar to that of functions. Instances of std::function can store, copy, and invoke any Callable target -- functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.

C++ Library -

Introduction

It is a Numeric limits type and it provides information about the properties of arithmetic types (either integral or floating-point) in the specific platform for which the library compiles.

C++ Library -

Introduction

It is a localization library and a set of features that are culture-specific, which can be used by programs to be more portable internationally.

C++ Library -

Introduction

It defines general utilities to manage dynamic memory in header.

Allocators

S.N. Allocator & description
1 allocator It is a default allocator.

C++ Library -

Introduction

It is a header describes functions used to manage dynamic storage in C++.The header also defines some specific types and the set_new_handler / get_new_handler pair of functions as shown below −

C++ Library -

Introduction

It is used to generalized numeric operations and this header describes a set of algorithms to perform certain operations on sequences of numeric values. In C++, It includes common mathematical functions and types, as well as optimized numeric arrays and support for random number generation.

C++ Library -

Introduction

It is a standardized way to express patterns to be matched against sequences of characters. some of typical regex parameters are as shown below −

C++ Library -

Introduction

It is an exception classes and this header defines a set of standard exceptions that both the library and programs can use to report common errors.

C++ Library -

Introduction

String is a class and all objects that in string represent sequences of characters.

Declaration

Following is the declaration for std::string.
typedef basic_string<char> string;

C++ Library -

Introduction

Thread is a sequence of instructions that can be executed concurrently with other such sequences in multithreading environments, while sharing a same address spac.

C++ Library -

Introduction

These are objects that pack elements of -possibly- different types together in a single object, just like pair objects do for pairs of elements, but generalized for any number of elements. It is closely related to the pair class (defined in header ): Tuples can be constructed from pairs, and pairs can be treated as tuples for certain purposes.

C++ Library -

Introduction

It defines in header and related to operators typeid and dynamic_cast.

Declaration

Following is the declaration for std::type_info.
class type_info;

C++ Library -

Introduction

It contains utilities in unrelated domains.
  • Pairs − these objects can hold two values of different types: pair, make_pair, piecewise_construct, piecewise_construct_t.

C++ Library -

Introduction

It is a library for arrays of numeric values and this header declares the valarray class and its auxiliary classes and functions.

C++ Library -

Introduction

Arrays are sequence container of fixed size. Container is a objects that holds data of same type. Sequence containers store elements strictly in linear sequence.

C++ Library -

Introduction

Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.

C++ Library -

Introduction

Deque is acronym for Double Ended Queue. It is a sequence container that can change it's size runtime. Container is an object that holds data of same type. Sequence containers store elements strictly in linear sequence.

C++ Library -

Introduction

forward_list is a popularly used sequence container. Container is an object that holds data of same type. forward_list container is implemented as singly linked-list, hence it provides unidirectional sequential access to it's data.

C++ Library -

Introduction

List is a popularly used sequence container. Container is an object that holds data of same type. List container is implemented as doubly linked-list, hence it provides bidirectional sequential access to it's data.
List doesn't provide fast random access, it only supports sequential access in both directions. List allows insertion and deletion operation anywhere within a sequence in constant time.

C++ Library -

Introduction to queue

Queue is a data structure designed to operate in FIFO (First in First out) context. In queue elements are inserted from rear end and get removed from front end.
Queue class is container adapter. Container is an objects that hold data of same type. Queue can be created from different sequence containers.

C++ Library -

Introduction

A set is an Associative container which contains a sorted set of unique objects of type Key. Each element may occur only once, so duplicates are not allowed.
There are four kind of Associative containers: set, multiset, map and multimap.

C++ Library -

Introduction

Stack is a data structure designed to operate in LIFO (Last in First out) context. In stack elements are inserted as well as get removed from only one end.
Stack class is container adapter. Container is an objects that hold data of same type. Stack can be created from different sequence containers.

C++ Library -

Introduction to unordered_map

Unordered map is dictionary like data structure. It is a sequence of (key, value) pair, where only single value is associated with each unique key. It is often referred as associative array. It enables fast retrieval of individual elements based on their keys.

C++ Library -

Introduction

It is an associative container that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.

C++ Library -

Introduction to algorithm

The algorithm library provides several functions that can be used for a variety of purposes, for instance searching, sorting, counting, manipulating and so on. These functions operate on ranges of elements and the range is defined as [first, last).

C++ - Overview

C++ is a statically typed, compiled, general-purpose, case-sensitive, free-form programming language that supports procedural, object-oriented, and generic programming.
C++ is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features.

C++ Environment Setup

Try it Option Online

You really do not need to set up your own environment to start learning C++ programming language. Reason is very simple, we already have set up C++ Programming environment online, so that you can compile and execute all the available examples online at the same time when you are doing your theory work.

C++ Basic Syntax

When we consider a C++ program, it can be defined as a collection of objects that communicate via invoking each other's methods. Let us now briefly look into what do class, object, methods and Instance variables mean.

Comments in C++

Program comments are explanatory statements that you can include in the C++ code that you write and helps anyone reading it's source code. All programming languages allow for some form of comments.
C++ supports single-line and multi-line comments. All characters available inside any comment are ignored by C++ compiler.

C++ Data Types

While doing programming in any programming language, you need to use various variables to store various information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Variable Scope in C++

A scope is a region of the program and broadly speaking there are three places, where variables can be declared −
  • Inside a function or a block which is called local variables,
  • In the definition of function parameters which is called formal parameters.
  • Outside of all functions which is called global variables.

C++ Constants/Literals

Constants refer to fixed values that the program may not alter and they are called literals.
Constants can be of any of the basic data types and can be divided into Integer Numerals, Floating-Point Numerals, Characters, Strings and Boolean Values.

C++ Modifier Types

C++ allows the char, int, and double data types to have modifiers preceding them. A modifier is used to alter the meaning of the base type so that it more precisely fits the needs of various situations.
The data type modifiers are listed here:

Storage Classes in C++

A storage class defines the scope (visibility) and life-time of variables and/or functions within a C++ Program. These specifiers precede the type that they modify. There are following storage classes, which can be used in a C++ Program

Operators in C++

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C++ is rich in built-in operators and provides the following types of operators:

C++ Loop Types

There may be a situation, when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

C++ decision making statements

Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

C++ Functions

A function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.
You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually is so each function performs a specific task.

Numbers in C++

Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C++ Data Types.

C++ Arrays

C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

C++ Strings

C++ provides following two types of string representations:
  • The C-style character string.
  • The string class type introduced with Standard C++.

C++ Pointers

C++ pointers are easy and fun to learn. Some C++ tasks are performed more easily with pointers, and other C++ tasks, such as dynamic memory allocation, cannot be performed without them.
As you know every variable is a memory location and every memory location has its address defined which can be accessed using ampersand (&) operator which denotes an address in memory.

C++ References

A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable.

C++ Date and Time

The C++ standard library does not provide a proper date type. C++ inherits the structs and functions for date and time manipulation from C. To access date and time related functions and structures, you would need to include <ctime> header file in your C++ program.

C++ Basic Input/Output

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming.

C++ Data Structures

C/C++ arrays allow you to define variables that combine several data items of the same kind but structure is another user defined data type which allows you to combine data items of different kinds.
Structures are used to represent a record, suppose you want to keep track of your books in a library. You might want to track the following attributes about each book:

C++ Classes and Objects

The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types.

C++ Inheritance

One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

C++ Overloading (Operator and Function)

C++ allows you to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively.

Polymorphism in C++

The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.
C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.

Data Abstraction in C++

Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.
Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.

Data Encapsulation in C++

All C++ programs are composed of the following two fundamental elements:
  • Program statements (code): This is the part of a program that performs actions and they are called functions.
  • Program data: The data is the information of the program which affected by the program functions.

Interfaces in C++ (Abstract Classes)

An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.
The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.