Monday 19 December 2016

What is Constant and how to declare a constant in C++ ?


Constant:

Constant is a permanent value that cannot be changed. In C++ constant can be declared by using const keyword.

Types of constant:

1. Numeric
  • int constant
  • float constant
2. Character
  • string constant
  • character constant
const int g =9.8;

If you change the value of constant after initialization an error will be generated by the compiler cannot modify const object.

#define:

#define is also used to define a constant.

General syntax:

#define constant_name value
  Example: #define pai 3.14

Wednesday 23 November 2016

What is String and operations on string ?


String:

String is a collection of characters. A string is written in double-quotation (“ ”). In C++ string is defined as an array of characters.

Declaration:

char variable_name[length];

Example:
char  chars[20];

In the above example a string variable name is declared of length 20. It means that it can only take 20 character.

Initialization:

char chars[20]= ”qwerty” ;
char chars[]= ”qwerty” ;
char chars[20]= {“q” ,”w”, ”e”, ”r”, ”t”, ”y”} ;
gets() function is used to get string input from user.

Example: 
char name[20];
gets(name);

puts() function is used to print string.
puts(name);

gets() and puts() functions are located in "string.h" library file.

What is data type and types in C++ ?

Data Type:

Data type defines a type of a variable or a value. The data is stored in a variable is according to its data type otherwise, it will generate an error.

There are main data types:


  1.     int (integer)
  2.     char (character)
  3.     float
  4.     Boolean

int data type:

int datatype is used to store the integers. It allows a variable to store an integer value. Both negative and positive numbers can be assigned to int datatype but it does not support floating values.

Example:

int roll_no = 5;
int a=-18;

char data type:

char data type is used to store a character. One character will use a 1 bit of memory.
A character is enclosed in single-quotation [‘ ‘].

Example: 

char s = ’a’;

float data type:

float datatype is used to store a numeric value with a fraction or can point value. It allows both negative and positive  numbers as well as floating values.

Example:

float  height = 5.5647;
float radius = 2.8931;

Boolean data type:

Boolean datatype has only two value “true” and “false”.  It is used for conditional statements. A Boolean variable cannot store number, integer and any other values except “true” and “false”.

What is the scope of a variable ?

Scope of variable:

The scope of variable defines the variable access point and life of variable. There are two variable scopes:
  1. Local variable
  2. Global variable

Local variable:

A type of variable that is declared inside a function is called as global variable.
The life of a local variable ends when a function ends and can only be accessed within the function in which it is declared.


Global variable:


A type of variable that is declared outside of any function is called as a global variable. It is generally declared after preprocessor directives and before main () function.
The life of a global variable ends when the execution of programs ends and it can be accessed by any function of the program.

Example:


#include<iostream.h>
#include<conio.h>
int a;
void main()
int b=10;
a=110;
cout<< “value of a: ”<<a ;
cout<<”\n value of b:”<<b;
getch();
}

In the above example "int a" is a globle variable which means that it can be accessed through out the program and "int b" is a local variable which means that it can only be accessed within the main(), not outside the main ().

How to declare and initialize variable in C++ ?

Variable Declaration:

The process of defining the data type and name of the variable is called variable declaration.

Example:

int marks;

Variable Initialization:

The process of assigning a value to a variable at the time of variable declaration is called as variable initialization.

Example:

Int marks =91;
Float height=5.5761;

What is a Variable and what are the rules to write a variable ?

Variable:

Variable is a quantity which means that its value is not fixed. You can understand by the name “vary” mean changing itself. In C++ variable are used to store the input and output data and also used to store the computational result.

Rules to write variable name:


  1. A variable name may have character, number or (_) underscore.
  2. A variable name should be start will character or (_) underscore.
  3. Keywords cannot be used as variable name.
  4. Special characters like $, #, !, @ cannot be used in variable name.

Sunday 6 November 2016

What is keyword in C++ ?

Keyword: 


A keyword is a special type of word which has fixed meaning and used to perform a specific operation.Keywords are written lowercase in C++. You cannot use them or redefine them because keywords have their own definition and use.

Following are some keywords used in C++:

int
struct
long
switch
register
typedef
return
union
short
unsigned
signed
void
sizeof
volatile
static
while