Category Archives: C++

Fundamental data types in C Programming Language

Category : C++

In this article,  we are going to talk Fundamental data types in C Programming Language.




While programming, we store the variables in computer’s memory, but the computers have to know that what kind of data we want to store in them.

Data type tells the compiler that what types of data, means integer, character or floating point etc. the variable will store and what will be the range of the values.

The most commonly used data types in C++ are:

  • Character
  • Integer
  • Floating point
  • Boolean
  • Unsigned

Character data type

The char keyword represents the character data type in C++. Any character belonging to the ASCII character set is called a character data type. The maximum size of char data type is 8 bits (1 byte). Signed char and unsigned char are its two distinct types. They occupy the same amount of memory space. Consider the following line of code:

char x;

Here, x is a character type variables that occupies 1 byte of memory. Variables declared as char data type are used to store character constants. Character constants are always represented by single quotes. For example:

char gender;

gender = ‘M’;

Here, gender is a character type variable that occupies 1 byte of memory and store character constant, M, which stands for Male.




Integer data types

The data types which store only integer numbers such as 100, -200 etc. are called integer data types. Its subtypes are:

  • int
  • short int
  • long

int data type

The keyword int stands for the integer data type in C++ and normally has two bytes of memory (16 bits). A 16 bit integer may fall in the range of -215 to 215-1, which means that it can store values in the range of -32768 to 32767

short int data type

short int is used to declare the short integer data type in C++. The maximum size of the short int data type is 2 bytes (16 bits). It may fall in the range -215 to 215-1, which means that it can store the values from -32768 to 32767.

long int data type

long int stands for long integer data type and is used in C++ to store larger integer values. Its size is 4 bytes (32 bits) which means that it can stores values in the range of -2147483648 to 2147483647.




Floating point data type

Integers are only used for storing whole numbers, but sometimes we need to store very large numbers, or numbers with a decimal point. The data types which are used to store such type of data are called floating point data types. Variables of floating point types hold real numbers, such as 4.0, 2.5, 3.33, or 0.1226. there are three different types of floating point data types:

  • float
  • double
  • long double

float data type

In C++, the float data type is used to declare floating point type of variables to store numbers with decimal point. It needs 4 bytes (32 bits) of memory to store values.

double data type

double is a keyword to represent double precision floating point numbers in C++. double data type occupies 8 bytes (64 bits) of memory. The size of double data type is a rational number in the same range as long float and is stored in the form of floating point representation with binary mantissa and exponent.

long double data type

In C++, the long double data type is used to declare  long double type of variables to represent  long double precision point numbers in C++. It needs 10 bytes (80 bits) of memory space in the RAM.




Boolean data type

bool is the keyword used to represent Boolean data type. It is capable of holding one of the two values: true (1) or false (0). It occupies 1 byte of memory.

Unsigned data type

The data types discussed so far are signed data types which means that one bit is reserved for the sign of the value (i.e. +/-). The unsigned numbers are whole numbers and always hold positive values starting from 0 till its maximum size. Here, the sign bit also used for the value which means no bit is reserved for holding a sign (+/-). The unsigned numbers may be classified into four types:

  • unsigned char
  • unsigned integer
  • unsigned short integer
  • unsigned long integer

A summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one, is given below in the table.

S. No Type Typical Bit Width Typical Range
1 char 1byte -127 to 127 or 0 to 255
2 unsigned char 1byte 0 to 255
3 signed char 1byte -127 to 127
4 int 4bytes -2147483648 to 2147483647
5 unsigned int 4bytes 0 to 4294967295
6 signed int 4bytes -2147483648 to 2147483647
7 short int 2bytes -32768 to 32767
8 unsigned short int Range 0 to 65,535
9 signed short int Range -32768 to 32767
10 long int 4bytes -2,147,483,648 to 2,147,483,647
11 signed long int 4bytes same as long int
12 unsigned long int 4bytes 0 to 4,294,967,295
13 float 4bytes +/- 3.4e +/- 38 (~7 digits)
14 double 8bytes +/- 1.7e +/- 308 (~15 digits)
15 long double 8bytes +/- 1.7e +/- 308 (~15 digits)





How to declare and initialize a variable in C++ program

How to declare and initialize a variable in C++ program

Category : C++

How to declare and initialize a variable in C++ program

Welcome to PodinaTutorials.com. In this article we are going to talk about how to declare and initialize a variable in C++ program. In previous article we discussed C Programming Constants and Variables and Naming Rules for variables. Let’s have a look, How to declare a variable in C++.

Declaration of variables

In order to use a variable in C++, we must first declare it. The declaration of a variable specifies that which type of data this variable will store/use. The syntax to declare a new variable is to write the specifier of the desired data type such as int, bool, float etc. followed by a valid identifier. For example:

Data type variable_name;

int a;

When this statement is executed by the CPU, a piece of memory from RAM will be set aside and it will be named as a. More than one variables of the same type can be declared in a single statement by separating them with commas. For example:

int a, b, c;

This line of code declares three variables a , b, ad c, all of type int. This declaration is exactly the same as shown in the following segment of code:

int a;
int b;
int c;

Consider the following example to see the use of variable declaration.

 

#include <iostream.h>
#include <conio.h>
int main()
{
// declaring variables
int a, b;
int result;
// process
a = 55;
b = 22;
result = a – b;
//print out the result
cout << “The Result is = ”<<result;
// Terminate the program
getch();
return 0;
}

Output of the program

The Result is = 33

The declaration of the variables is not necessary at the start of the program. A variable can be declared in the program where it is needed. Consider the following example.

// variable declaration at the point of application
#include <conio.h>
#include <iostream.h>
int main()
{
cout << “Enter a number: ”;
int x; // we need x starting here
cin >> x;
cout << “Enter another number: ”;

int y; // we do not need y until now
cin >> y;
cout << “The sum is: ” << x + y << endl;

getch();
return 0;
}

Output of the program

Enter a number: 6
Enter another number: 11
The sum is: 17

How to declare and initialize a variable in C++ program

Initialization of variables

Assigning values to a variable during declaration time is called initialization. When declaring a regular local variable, its value is by default undetermined. But you may want a variable to store a concrete value at the same moment that it is declared. In order to do that, you can initialize the variable.

Consider the following program to demonstrate the initialization of variables.

#include <conio.h>
#include <iostream.h>
int main()
{
int Vara = 55; // initial value = 55
int Varb =(22); // initial value = 22
int result; // initial value undetermined

result = Vara – Varb;
cout << “The Result is = ”<<result;

getch();
return 0;
}

Output of the program

The Result is = 33

Conclusion

In this article, we discussed how to declare and initialize a variable in C++ program. Declaring a variable is somehow an important concept used in object-oriented programming to save memory and maximize the speed of the program. In next article we will be talking about Some Fundamental data types used in C++ programming.


C Programming Constants and Variables and Naming Rules for variables

C Programming Constants and Variables and Naming Rules for variables

Category : C++

C Programming Constants and Variables and Naming Rules for variables

In this article of PodinaTutorials.com, we are going to talk about C Programming Constants and Variables and Naming Rules for variables. So let us discuss all the detail in the below article.

C++ Constant and Variables

Variables and Constants are the important components of every programing language.

Constants

Constants are the expressions which have fixed value. Constants are generally categorized into Literal, Boolean, and Symbolic constants. They are used to express particular values within the source code of a program.

Consider the following line of code:

VarA = 555;

In this line of code, 555 is a literal constant. The literal constants are further categorized into the following types.

  • String constants
  • Numeric constants
  • Character constants

String constants

A string constant is a sequence of characters enclosed in double quotation marks is called string constant. Its maximum length is 256 characters. Following are some examples of valid string constants.

“Welcome to the first C++ Program”

“The result=”

Numeric constants

Numeric constants consist of positive or negative signed numerals. There are four types of numeric constants. These are:

  • Integer constant
  • Floating point constant
  • Hexadecimal constant
  • Octal constant

Integer constants

Integer constants are those positives or negative signed numbers that do not contain a decimal point e.g. 2010, -321 etc

Floating point constants

The numeric constants having a decimal point are called floating point constants. E.g. 33.55 and -0.22 etc. These constants can also be either positive signed or negative signed. These constants can also be represented in its exponential form by the use of alphabet ‘e’ of ‘E’ to denote the exponents of the numbers. For example.

9010E10
7810.11E-11
-10.990e8
-1.001e-1

Hexadecimal constants

The constants represented in hexadecimal number system are called hexadecimal constants. To represent a hexadecimal constant, begin the constant with 0x of 0X, followed by a sequence of digits in the range 0 through 9 and f (or A through F). the digits a to f (or A to F) represent values in the range 10 through 15. For example.

int I = 0x3fff; // Hexadecimal constant

int j = 0X3FFF; // Hexadecimal constant

Octal constants

The representation of constants in octal number system is called octal constants. To specify an octal constants, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. Consider the following example:

int I = 0377; // Octal constant

int j = 0397; // Error: 9 is not an octal digit

Octal numbers are integer numbers of base 8 and their digits are 0 to 7.

Character constants

A character enclosed within single quotes is called character constant. Consider the following examples:

'A', 'a', '.', '?'

C Programming Constants and Variables and Naming Rules for variables

Variables

A variable in C++ is a name for a piece of memory that can be used to store information. A variable can be thought as a mailbox where information can be put and retrieved from. All computers have memory, called Random Access Memory (RAM) that is available for programs to use. When a variable is declared, a piece of that memory is set aside for that variable.

Consider the following code example:

int  a = 5;

int b = 2;

int result = a – b;

In this segment, a, b and result are the integer variables that reside in RAM and store integer values.

Naming Rules for variables

The symbol used for a variable is called an identifier or variable name. a set of rules are used for naming variables. These rules are:

  • Valid identifier is a sequence of one or more letters, digits or underscores (_) character.
  • Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid.
  • Variable identifiers always have to begin with a letter. They can also begin with an underscore (_) character.
  • In no case, a variable can begin with a digit.
  • A Reserved word of the C++ language cannot be used as an identifier.
  • Names should be meaningful.
  • Names should not be too long.

C++ language is a “case sensitive” language which means that an identifier written in capital letters is not equivalent to the one written in small letters.

 

Keyword: C Programming Constants and Variables and Naming rules for variables


post a comment in c programming language and statement terminator

post a comment in c programming language and statement terminator

Category : C++

post a comment in c programming language and statement terminator

In this article, we are going to discuss how to post a comment in c programming language and statement terminator. Comments are parts of the source code ignored by the compiler. They do nothing but simply increase the readability and understandability of a program. Their purpose is only to allow the programmers to insert notes or descriptions within the source code. On the other hand, statement terminator has used in C and C++ language for the separation of two statements.

C++ programming language supports two types of comments. One is Single line comment and the other one is a Multiline comment.

Single line comment

It is represented by the double slash (//) symbol. It discards everything from where the pair of slashes (//) signs is found up to the end of the same line.

Multiline comment

These types of comments are represented by the symbols (/*……………*/). It ignores everything between the /* characters and the first appearance of the */ characters, with the possibility of including more than one line.

Consider the following program to demonstrate comments.

/* program to demonstrate comments in C++ */
#inlcude <iostream.h>
#inlcude <conio.h>
int main()
{
cout << “Welcome!”; // prints Welcome!
cout << “I am a C++ program demonstrating comments”;
getch();
return 0;
}

Output of the program

Welcome!

I am a C++ program demonstrating comments

In the above example, the first lines are multi-line comment while the comment at line number 6 is a single line comment.

Comments are always ignored by the compiler when it compiles the program. If comments are included within the source code of your programs without using /* or //, the compiler will take them as if they were C++ expressions.

Statement terminator (;)

Each C++ statement is terminated by a symbol named semicolon (;) which is called statement terminator. In C++, the separation between statements is specified with this ending semicolon (;) at the end of each statement. It does not matter to write more than one statement on a single line but it does matter if you do not separate them with semicolons (;). The use of each statement on a separate line is only to add clarity to the program.

post a comment in c programming language and statement terminator

Points to be remembered

C++ is a programming language developed by Bjarne Stroustrup which derives most of its features from C language.

Each C++ program makes use of header files which have definitions for C++ objects.

Pre-processor directives are the directives that are used to include header files to the C++ program.

C++ comments are the explanatory statements that are usually added to the source program statements to make them understandable to the readers.

Author Note

C++ program is one of the computer language to develop state of the art computer programs. C++ programming language is based on the object-oriented concept, through which we can create both simple and complex programs. However, this is not the only language to learn. You can learn more languages in order to make achievements in the field of computer science and software engineering. However, I recommend C++ as a startup language, through which you can develop your own skills for further advancement.

Keyword: post a comment in c programming language and statement terminator


Basic Structure of a C program for Beginner

Basic Structure of a C program for Beginner

Category : C++

Basic Structure of a C program for Beginner

Here in this article of PodinaTutorials.com, we will discuss a basic Structure of a C program for beginner. This article is mainly for new programmers, who want to learn C++ from scratch. So without wasting time, look the below article.

Below we are discussing Basic Structure of a C program for Beginner in detail.

Structure of a C++ program

First have a look to general syntax of a C++ program, which is given below.

Preprocessor directives
int main()
{
Body of the program
}

Each C++ program has three main components. These are: Preprocessor directives, main function, and body of the main function. Now, consider the following simple program example to explain these components.

#include <iostream.h>
#include <conio.h>

int main()
{
cout << “Hello World!”;
getch();
return ;
}

Output of the program

Hello World!

The result of the above program is that it prints “Hello World!” on the screen. It is one of the simplest programs that can be written in C++, but it contains the fundamental components of almost every C++ program.

What is Preprocessor directives (#include, #define)

A preprocessor is a collection of special statements which are executed before the compilation process.

Almost every C++ program contains a preprocessor directive. The #include preprocessor directives is commonly used to insert header files with extension ‘.h’. these header files are already stored in computer in include directory. In the above program, two #include directives have been used, #include<iostream.h> and #include<conio.h>. #include<iostream.h> is used for the C++ object cout and #include<conio.h> for the built-in function getch().

In C++, The other commonly used preprocessor directive is #define which is used to define symbolic constants. Its general format is:

#define identifier value

Forexample

#define PI 3.14159

#define NEWLINE ‘\n’

This defines two new constants: PI and NEWLINE. Once they are defined, they can be used in the rest of the program as if they were any other regular constant, for example:

#include <iostream.h>
#include <conio.h>
#define PI 3.14159
#define NEWLINE ‘\n’

int main()
{
double r=5.0; // radius
double circle;
circle = 2* PI * r;
cout << “Area of the Circle is equal to:”<<circle;
cout << NEWLINE;
getch();
return0;
}

Output of the program

Area of the Circle is equal to: 31.4159

The #define directive is not a C++ statement but a directive for the preprocessor, therefore it assumes the entire line as the directive and does not require a semicolon (;) at its end.

main() Function

the main function is the point by where all C++ programs start their execution. It doesn’t matter whether there are other functions with other names define before or after it, the instructions contained within this function’s definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

The word main is followed in the code by a pair of parentheses ().

Body of the main() Function

After the parentheses of the main function, the body of main function starts which is enclosed in curly braces {}. When this function is executed, the instructions written within it perform their task. Inside the body of main function, C++ statements are written.

C++ Statements

A C++ statement is a simple or compound expression that can actually produce some effect. It is actually an instruction to the computer for taking an action.

For example cout << “Hello World!”; is a C++ statement. The following are some examples of statements.

cout << “Hello World!”;

In fact, this statement performs the only action of displaying the result “Hello World!” on the screen.

cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World! Sequence of characters) into the standard output stream. cout is declared in the <iostream.h> standard header file.

getch();

this is a standard library function defined in the header file <conio.h> and is used to wait for the user to input a character from the keyboard.

return 0;

the return statement causes the main function to return control to the operating system. Return may be followed by a return code. The code 0 return by the return statement tells the operating system that the program terminates normally.

The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. For example, the following program can also be written in a single line.

int main()
{
cout << “Hello World!”;
return 0;
}

Its single line version will be:

int main() { cout << “Hello World!”; return 0; }

Conclusion

This article is all about Basic Structure of a C program for Beginner.Here in this article we discussed How C++ structure look alike and this is the easiest way to write your program in C++ programming language.

Basic Structure of a C program for Beginner

Basic Structure of a C program for Beginner


What is Header files and Reserved words in C++

Category : C++

Header files and Reserved words in C++

Header files and Reserved words in C++ are the two important components of almost every C++ programs. Below we are discussing both terminologies in detail; 

Header files

Header files also known as include files, are standard library files that have an extension of (.h) and which are used to hold declarations for other files.

Consider the following program

 

 

// header file example

#include <iostream.h>

Int main()

{

Cout << “Hello, world!” << endl;

Return 0;

}

Output of the program

Hello, world!

This program prints the string “Hello, world!” to the screen using cout. However, this program never defines cout, so how does the compiler know about the object cout? The answer for this is that cout has been declared in a header file called “iostream”. When the line #include<iostream.h> is used in the program, the compiler locates and read all the declarations from a header file named “iostream”.

What is Header files and Reserved words in C++

What is Header files and Reserved words in C++

Reserved words

Reserved words or keywords are those words which have their special meaning within the C++ language and are reserved for some specific purpose. C++ reserved words cannot be used for any other purpose in a C++ program and even cannot be used as variables. Here is a list of C++ keywords shown in the table below:

asm auto bool break case
const  const_cast  continue  default  delete 
else  enum  explicit  export  extern 
friend  goto  if  inline  int 
new  operator  private  protected public 
short  signed  sizeof static  static_cast 
this  throw  true  try  typedef 
unsigned  using  virtual  void  volatile 
 char  double   float   reinterpret_cast   mutable 
 typename   switch   union   template   return 
 namespace  dynamic_cast  class catch for 
 while   wchar_t   typeid   struct   long 
 register   do   false   –  –

Conclusion

In this article we discussed the basic C++ programming terminologies; Header files and Reserved words. To start with C++ computer programming, one should know these basic terminologies in order to proceed in the language. That’s why we discussed both the terms in detail.