Showing posts with label c tutorial for beginners. Show all posts
Showing posts with label c tutorial for beginners. Show all posts

Saturday 5 October 2013

Taking Input In C ++

Input in C ++:

 Taking input in C ++ programming is very important , as we learnt in previous C tutorial for beginners that C++ program is used to perform calculations and to manipulate data. And data can be manipulated after storing it in memory. In this C++ tutorial, we are going to learn that how to input (cin in c ++) data in computer’s memory.

Storing data in the computer’s memory is a two-step process:

  • Instruct the computer to allocate memory
  • Include statements in the program to put data into the allocated memory


There are two ways to store data:

  • Using cin operator ( >> )
  • Initializing the value in variables

Using cin Operator In C++:

Putting data into variables from the standard input device is accomplished via the use of "cin" and the operator ">>". The syntax of cin together with >> is as follows.

Syntax:

cin >> variable >> variable;
cin is used to take input in c ++ called an input (read) statement. In C++, >> is called the stream extraction operator. 

Suppose we have an int variable ‘number’.

And we take input:


cin >> number;
If user enters 30 then 30 is stored in the variable ‘number’.

For taking input of two variables:


Int feet;
Int inches;
\\  Suppose the input is:
\\  23 , 7
We’ll take input by writing
cin >> feet >> inches;

Declaring and initializing variables:

When we declare a variable at that instant there is a junk value in it. By default, C++ puts junk value in it. But when a variable is declared, memory is allocated according to the size of its data type.  So, when we assign the value in that variable for a first time then it’s called initialization. A variable is said to be initialized the first time a value is placed in the variable. An operator “ = ” is used to initialize the value. “ = ” is called the assignment operator. 

For example:

Int num1 = 20;   //we initialized it with the value the time we declared the variable
Int num2; // declaring variable num2
num2 = 4; //we initialized num2 after declaring it


Allocating memory with constant variables:

If the user want to initialize a variable and doesn’t want to get the value of that variable changed then C++ has given us the facility of using constant variables.

Syntax:

const <datatype> <identifier> = value;
In C++, const is a reserved keyword.
It is like a memory location whose content is not allowed to change during program execution.

Final words:

This c tutorial for beginners prepared by shapes in c will enable our viewers to learn how to take input in C ++.
cin in c++ is a very important keyword which has been been discussed in detail in this C++ tutorial.
A part from that we also discussed the differences between declaration and initialization.Thorough study of this article will definitely help you a lot.

Identifiers in C++

In this post of shapes in C, we are going to discuss sixth C tutorial for beginners in which we are going to discuss about identifiers in C++.

Identifiers are the names of things that appear in programs such as variables, constants and functions. There are some rules for identifiers in C++ that should be followed otherwise; the compiler would give the syntax error.

C++ identifiers:

A C++ identifier consists of letters, digits and the underscore character (_).

Rules:

  • The identifier should begin with a letter or underscore.
  • It should not start with a digit.
  • There should be no spaces between an identifier
  • No other symbols are allowed to write an identifier other than digits, letters and underscore.

Example of illegal identifiers:

  • My Book                             There is a space between My and book
  • 1stbook                               The identifier is starting with a digit
  • One+two                             The symbol ‘+’ can’t be used
  • Party!                                  The symbol ‘!’ can’t be used

Example of legal identifiers:

  • Book1                Here the digit is not used in the start
  • _size                  The identifier is started with underscore
  • Mybook              The identifier is started with an alphabet
  • Your_book     The identifier has two words which are separated by underscore

Types:

There are two types of identifiers:
  1. Predefined
  2. User defined

Example:

cout and cin is a predefined identifier whereas  num and word is a user defined identifier.

Note: In C++, identifiers can be of any length.

Caution: Identifiers are case sensitive. Uppercase and lowercase letters are considered different. So the identifier UPPER is not the same as the identifier upper.

Final Words:

In this c tutorial for beginners of shapes in c, you have learnt the meaning, use and rules of identifiers. In this c tutorial for beginner, we discussed the topic of C++ identifiers types, cautions, important notes and illegal and legal identifiers with examples and its explanation in detail.This c programming tutorial has been designed keeping in mind the beginners in C++.

Saturday 28 September 2013

Operators And Their Precedence In C++

Operators:

Precedence of operators in c++ is the most basic thing used in computer for calculations,these calculations can be done by use of several arithmetic operators. So, we are going to discuss arithmetic operators in this c tutorial for beginners. There are five arithmetic operators in C++. Those c++ operators are listed below:

  1. +  (Addition)
  2. –  (Subtraction)
  3. *  (Multiplication)
  4. /  (Division)
  5. %  (Mod, modulus or remainder)


You can use these operator in both integral and floating number data types except modulus operator. As modulus operator is used in integral data type to find the remainder. 


Let’s assume an expression:

x + 2 * 5 + 6 / y

x and y are unknown variables. This arithmetic expression is formed by using operators and numbers. The numbers in the expression are called operands. 


C++ has two type of operators:

Unary operator: An operator that has only one operands.
Binary operator: An operator that has two operands.

Expression1: 3+4
In this expression, ‘+’ is a binary operator as ‘+’ has two operands.
Expression2: -3, +4
In this expression, ‘-‘ and ‘+’ are unary operators as they have only one operand. The ‘-‘ states that 3 is a negative number and ‘+’ indicates that 4 is a positive number. 


Examples:

Expression Result

  2 + 10 =  12
21 + 89  = 110
30 -  2010
30 -  90  =  -60
2   *  20 =  40
5   /   2  =   2
34 % 5  =   4
4  &   6  =   4


Note:

You should be careful in taking mod of numbers. For example, 22 % 5 = 2 and in -22 % 5 = -2. So, you should be careful when taking mods as there is a difference when signs with the number change.


Associativity And Precedence Of Operators In C:

When more than one operator is used in arithmetic expression, C++ follows the rules of precedence of arithmetic operators in c++ to evaluate the expression. 
Operator precedence in C++:
  1.  * , / , %
  2. + , -
 
Final words:
In this c tutorial for beginners of shapes in c we discussed different operators used in programming , there use and the precedence of operators in c++

Tuesday 17 September 2013

Comments in C++


In this c tutorial for beginners, we are going to discuss about how to write a comment??
Comments is a kind of tool which is used to highlight the specific details and thus, not making it a part of C++ program. Comments are used so that the program you write should be clear not only to you but also to the reader. It is a good programming practice that you include comments to your program to make it clear for the readers.  Comments are for the readers not for the compiler.

Single line commenting:

For the commenting of single line, “\\” is used.
e.g:  //This is a single line comment

Multiple lines commenting:

For the multiple lines commenting, “/* */” is used.
e.g:  /* You can include
                comments that can
       occupy multiple lines  */
Uses of comments:

· Comments used to give reference in the start of the program.

Comments are used to identify the authors of the program, dates on which it is written or the name of organization who wrote it.

Example:
// Written by Tabish Habib
// Written on 1st January 2013
//Written by shapesinc.blogspot.com

·  Comments used to give heading or explanation:

Comments are used to give the heading of the program or brief explanation about the program.

Example:
// Diamond shape
/* This is a generic program to print diamond shape. In this program, we
take a size and with the used of two nested loops, we print a diamond */

·  Step by step commenting:

We use comments to explain each and every step of the program.

Example:
int num=0;    //an int variable num is declared with a zero value assigned
char word=’A’;   //a char variable word is declared with an A assigned
float point=2.2;  //a float variable point is declared with a value 2.2 assigned
bool check=false;  //a bool variable check is declared with a false  assigned

·  Keeping unnecessary code in the program:

If you want to keep the code in the program but don’t want to execute it , then you should comment that code with multiple commenting. This will do the work because commenting is not for the compiler so, when the compiler compiles the program then it doesn’t consider the part that is commented. Compiler ignores everything that appears within comments.


Keyboard short key for commenting:

If you want to comment any part then select that part of code and then first press ctrl+k and then ctrl+c.

Key: Ctrl+K, Crtl+C


Keyboard short key for uncommenting:

If you want to uncomment any part then select that part of code and then first press ctrl+k and then ctrl+u.
Key: Ctrl+K, Crtl+U

Final Words:

In this post of shapes in c, you have learnt the meaning, use and advantages of comments. In this c tutorial for beginners, we discussed the topic of C++ commenting in detail and each and every aspect of how to write a comment.

C++ Data Types

Data types in C++:

In this post of shapes in C, we will discuss fifth C tutorial for beginners in which we will tell you about C++ Data types . The purpose of C++ program is to manipulate different data in solution of a problem. To manipulate data, we need to store our data somewhere. For that purpose, we have data types in C++. Data type is a set of values together with a set of operations.

In C++, data type consists of two things:


  • Range of data type.
  • Operations allowed on it.

C++ data types fall in three categories:


  • Simple
  • Structured
  • Pointer

For now, we are going to focus on simple data types.

Simple data types are the fundamental data types in C++. They further fall in three categories.

  • Integral
  • Floating point
  • Enumeration

c data types



Integral:


Integral data types are further classified in nine categories.

  • Char
  • Short
  • Int
  • Long
  • Bool
  • Unsigned Char
  • Unsigned short
  •  Unsigned int
  •  Unsigned long

Int Data type:


Below are the following rules for int data type.

  1. Positive integers don’t need a + sign in front of them.
  2. No commas are used within an integer.

Size:

     It has a size of 4 bytes.

Bool Data type:


It is a logical data type and it has only two values: true and false. Its purpose is to handle logical expressions.



Char Data type:


It is the smallest integral data type. It is used to represent characters. It can represent every key on your keyboard on the basis of ASCII and EBCDIC codes. ADCII has 128 values and EBCDIC has 256 values.



Floating point data types:


     Float:


It is used in C++ to represent any real number between -3.4E+38 and 3.4E+38. The memory allocated for it is 4 bytes.



    Double:


       It is used in C++ to represent any real number between -1.7E+308 and 1.7E+308. The memory allocated for it is 8 bytes.




Final words:


In this c tutorial for beginners of shapes in c, we have learnt about c data types, its size and range.These c programming tutorials will definitely help you out in your problems.

Saturday 14 September 2013

project file in Visual Studio

In this post of shapes in C, we will discuss our fourth C tutorial for beginners in which we will tell how to make visual studio program.

After installation comes the step of making a project file in your C++ compiler. So, in this C tutorial for beginners, you are going to learn how to make a project file in Microsoft Visual Studio.

Step 1> First open the Microsoft Visual Studio by double clicking on its folder that is located on Desktop. You can also access it from start menu.



open visual studio




Step 2> If this error comes then download the patch mentioned below and install it, your program will become compatible. This error comes due to compatibility issues.

visual studio program


Step 3> After starting the C++ compiler, click on create project.


create project visual studio

Step 4> Click on Win32 console application and then name the project and click OK.

visual studio program


Step 5> Now click on next

visual studio program


Step 6> Now this window will appear

visual studio program


Step 7>Tick empty project and click finish

visual studio program


Step 8> Now this window will appear

visual studio program


Step 9> Now right click on source files and then go to add and then click on new item. Short keyboard key for it is Ctrl + Shift  + A

visual studio program


Step 10> Select C++ file and name it  and click add.

visual studio program

Step 11> Now your first project file is created and you can start your programming.

visual studio program


If you want to open the existing file then right click on existing files then go to add and then click on existing item. Short keyboard key for it is Shift + Alt + A




Now choose the file that you want to open



Note: In this C programming tutorial i want to tell my viewers that  you can not open multiple main files in single project. If you do it then your files won't compile. This is because every file would contain main function and as computer is dumb machine, it does not get that what file we are trying to compile. So always compile on one file at a time and remove other unnecessary files.

Final words:
I hope that this c++ tutorial of shapes in C would have taught you in how to make project file in visual studio.

C++ compiler | Visual Studio 2012

In this post of shapes in C, we are going to show third C tutorial for beginners in which we are going to learn about C++ compiler and how to install visual studio 2012.

What is a compiler?
C/C++ is a human friendly third generation language. It's code is written in English sentences so, that it would be easily understood by most of the people. But computer doesn't understand this language. It only understands machine language (Binary coding). So, compiler is a tool which translate all the code back to the machine language.

Compilers for C++:
  1. Turbo C++
  2. DEV C++
  3. Code Blocks
  4.  Borland
  5. Microsoft Visual Studio
 Now a days, the mostly used compiler is Microsoft Visual Studio so, I would recommend you to use this. Many like code blocks too as that is a programming friendly compiler but Visual studio has many tools and it can be used for other languages too like C sharp.

Note for Turbo C++:
Turbo C++ is a very old compiler of C++ and it is compatible with 32 bit machines. Now a days, people use 64 bit operating systems so, Turbo C++ doesn't work on 64 bit operating systems. To resolve this problem, there is a tool available on internet named DOS BOX. So, you should download it and install in on your computer and then run Turbo C++ in it and Turbo C++ will work on your 64 bit operating system.

Installation of Visual studio C++:

Step 1> This is the setup folder of Microsoft Visual Studio 2012. Now, double click on vs_ultimate to start the setup file.
visual studio 2012

Step 2> Now this window will appear.

visual studio 2012

Step 3> Now click on "I agree to the license term and conditions" and then click next to proceed.
visual studio 2012

  Step 4> If you want to install all features then select all else click install to continue.


visual studio 2012


Step 5> Now your installation will start.
visual studio 2012



Step 6> As your setup has completed, click on the Launch button and enjoy your programming.

visual studio 2012


Congratulations! You've now completed installing the most reputed compiler by Microsoft .

Final Words:
 In this c programming tutorial of shapes in C, we learnt about C++ compilers and installing one of the mostly used compiler, Microsoft Visual Studio 2012.

Video:

Introduction to C programming

Introduction to C programming language:

In this c programming tutorial of shapes in C, we are going to give second C tutorial for beginners in which we gave introduction to C++. C++ is an intermediate language which comprises of both low level and high level features. It is a statically typed, compiled, generic and free form language. C language was developed by Bjarne Stroustrup and was originally named "C with classes" but renamed C++ in 1983.C++ is very close to your actual life. It's working is based in the nature of human being.

Scope:

C++ is a very popular programming language and is applied on a wide variety of hardware, software, websites and operating system platforms. It is also used in development of drivers, graphics programs, video games and high-performance server and client applications. The syntax and its mechanism has greatly influenced many languages like C# and Java.

Features:

C when upgraded to C++ got possession of many more important features. The loops concepts (do while, while, for, switch), arrays(static and dynamic), 2D, 3D and Multi Dimensional arrays, strings, function overloading, the concept of address with a symbol of '&', pointers, structs , unions, classes, operator overloading, virtual functions, inheritance, composition, ,polymorphism, templatization , exception handling, link lists, stacks, queue and many more are the important features of C++.

If-else conditions (controlled structures: selection):

Things like if else allows you to decide between conditions.

Loops (controlled structures: repetition):

Loops like while, for, do while and switch are used to finish the repetition.

Arrays:

Arrays are the storage units in which many variable are made and the values are stored. There are 1D, 2D and multi dimensional arrays.

Function overloading:

Function overloading are piece of codes with parameters in which variables or addresses are passed and thus that function executes. This decreases the length of code and makes easy to understand the code.

Pointers:

Pointers are the variables whose content is an address and it gives many facilities to a programmer like dynamic arrays.

Structs:

Struct is a heterogeneous data structure and it provides the facility of creating self defined variables of your own choice.

Classes:

A class is a collection of a fixed number of components and it has three parts public, private and protected. It is a concept of objet oriented programming. This is where you start privatizing your codes.

Inheritance:

It is a relationship where we inherit the information of base classes.

Polymorphism:

Polymorphism is the phenomenon which is the dynamic calling of functions.


Templatization :

It is the making of the dynamic codes which are applicable on all data types.

Final Words:

Our team of shapes in C classified all the detail of introduction to C++ and divided it into sections and then we gave little little detail in every section of c++ programming tutorials.

Saturday 24 August 2013

History of C++ language

In this post of shapes in c, we are going to discuss our first c tutorial for beginners, we are going to tell you history of c language.This c tutorial starts with the brief introduction of Bjarne Stroustrup, the father of C language and a Danish and British computer scientist, created C programming language in 1979. He started to think about creating a new programming language and the start of it began from his Ph.D thesis.

He soon started his work in "C with classes" and his main goal was to add object oriented programming in C programming language. The features his language included were classes, inlining, basic inheritance, default function arguments and many more. The first compiler for C with classes was called Cfront. It was a self compiler because it was mostly written in C with classes. However, it was abandoned in 1993 as it became difficult to integrate it with new features of C++.

In 1983, C with classes was changed into C++. The ++ operator is an incrementing sign in C++. C++ was added with many new features. Mentioning ones are function overloading, virtual functions and concept of address represented with the sign '&'.

In 1985, The C++ programming language was published which was the Stroustrup's reference to the language.

In 1989, the language was updated with static and protected members.

In 1990, The Annotated C++ Reference Manual and Borland's Turbo C++ compiler were released.

In 1998, the first international standard for  C++ ISO/IEC 14882:1998 was published by C++ standards committee.

In 2005,a technical report was released be C++ standards committee in which they mentioned the features that they planned to add in future.

In mid 2011, the new C++ standard (dubbed  C++11) was finished.

These are all main phases in a chronological order compiled and explained by our team of shapes in c in this c programming tutorial.



It is important to know about C++'s history for all beginners.


Compilers used to code C++ language:
Turbo C++
Visual Studio
Code blocks
Dev C++


Final Words:
In this post of shapes in c, we discussed our first c tutorial for beginners. We told about how gradually history of c language proceeded and what compilers have been used up till now.

Video: