Basic Features of C
Basic Features of C
Variable
A variable is a name given to a memory
location where you can store a data of a particular type. The type is specified when the variable is declared.
consist of any combination of upper or lower case letters, underscores ( _ ) and the digits ( 0 to 9 ).
case sensitive language). This means length and Length, LENGTH are different names.
A variable name cannot begin with a digit. Examples of variable names:
Declaration of Variable
Examples of variable declaration:
int student_no; int age;
short
int long float double
long double
string
There are also type such as unsigned long, unsigned short, unsigned int signed long, signed short, signed int.
Typically float will provide 7 digit precision, double will provide 15 digit precision and long double will provide 19 digit precision
Keywords
significance and when needed are to be used as it is. In the above program include, iostream, using, namespace, std, int, cout, return are keywords.
in any language.
A C++ statement expresses a complete
instruction to the computer. Statements are basic units for specifying what our program is to do.
Most C++ statements end with a
semicolon.
cout<<fail;
//decision making statement
Whitespace
Whitespace refers to spaces, horizontal and vertical tabs and newline characters. Whitespaces are required in a C++ statement for the compiler to be able to distinguish between the elements of the statement.
referenced by a name. There may be more than one function in a C++ program, but there shall be at least one function named main and not more than one function shall have the name main. The brackets ( ) after the function name is essential. A function may have arguments that are placed within these brackets. The function has a type.
available library functions. The name cout is defined in the header file iostream. This is a standard header file that provides the definitions necessary for standard input and output facilities. The standard input device is the keyboard and the standard output device is the monitor.
Namespace
The namespace refers to a group of several names (or entities). The entities in the C++ standard library are all defined within a namespace called std. You may also consider namespace as a surname. You may consider that the full name of cout is std::cout. If we omit the using directive we can write the output statement as
std::cout<< Trial program<<endl;
std::cout<<Average =<<average; //Prints the
Return statement
The return statement ends the function and
returns control to the operating system. It also returns a value (0 in the above example) to the operating system.
EXAMPLE
#include<iostream>
using namespace std; int main() { double num1, num2, average; cout<<"Program to find the average of two numbers" <<endl; cout<<"The first number ="<<endl; cin>>num1; cout<<"The second number ="<<endl; cin>>num2; average = (num1 + num2)/2; cout<< "Trial program"<<endl; cout<<"Average =" <<average <<endl; //Prints the value stored in variable average return 0;