Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1

QNO1.DIFFERENCES BETWEEN C AND C++?


Both C and C++ are the most widely implemented computer programming languages, but they are quite
different in nature. C was originally developed by Dennis Ritchie at AT&T Bell Labs between 1969 and 1973,
whereas C++ was developed from C by Bjarne Stroustrup at Bell Labs in 1983. Below are listed the some of the
basic differences between C and C++:
1. Language: C supports procedural programming paradigms for code development. C++ supports both
procedural and object-oriented models and is also called a hybrid language.
2. Relationship: C is a subset of C++, so code written in C++ cannot run in C. C++, however, is a superset
of C, so C++ can run most code written in C.
3. Structure: C is a function-driven language. For example, it uses functions like scanf and printf for input
and output. C++ is an object driven language and uses objects such as cinand cout for input and
output.
4. Object-Oriented Support: C does not support object-oriented programming, so it has no support for
encapsulation, inheritance, and polymorphism. On the other hand, C++ is object-oriented and
supports these three functions.
5. Memory: C provides calloc() and malloc() for dynamic memory allocation, and free() for memory
deallocation. C++ provides the new operator for memory allocation and the deleteoperator for
memory deallocation.
6. Encapsulation: Since C does not support encapsulation, data and functions are separate and act as
free entities. C++ supports encapsulation, so its data and functions are encapsulated together in the
form of an object.
7. Default Header File: The default header file for C is stdio.h and iostream.h for C++.
8. Error Handling: C does not provide direct support for exception handling, whereas C++ does.
9. Approach: C follows a top-down approach, whereas C++ follows a bottom-up approach.

QNo2 Define language? Differentiate between C and C++ as Language?


C is a high-level programming language that was developed in the mid-1970s. ...C++, pronounced "C plus plus,"
is a programming language that was built off the C language. The syntax of C++ is nearly identical to C, but it
has object-oriented features, which allow the programmer to create objects within the code.
The major difference between C and C++ is that C is a procedural programming language and does not
support classes and objects, while C++ is a combination of both procedural and object oriented
programming language; therefore C++ can be called a hybrid language. ... When compared to C++, C is a
subset of C++.

QNO3 VARIABLE TYPES? WHAT ARE THE RULES FOR NAMING A VARIABLE?
A variable is just a named area of storage that can hold a single value (numeric or character). The C language
demands that you declare the name of each variable that you are going to use and its type, or class, before you
actually try to do anything with it.
The Programming language C has two main variable types
 Local Variables
 Global Variables
Local Variables
 Local variables scope is confined within the block or function where it is defined. Local variables must
always be defined at the top of a block.
 When a local variable is defined - it is not initalised by the system, you must initalise it yourself.
 When execution of the block starts the variable is available, and when the block ends the variable
'dies'.
Check following example's output
main()
{
int i=4;
int j=10;

i++;

if (j > 0)
{
/* i defined in 'main' can be seen */
printf("i is %d\n",i);
}
2

if (j > 0)
{
/* 'i' is defined and so local to this block */
int i=100;
printf("i is %d\n",i);

}/* 'i' (value 100) dies here */

printf("i is %d\n",i); /* 'i' (value 5) is now visable.*/


}

This will generate following output


i is 5
i is 100
i is 5
Here ++ is called incremental operator and it increase the value of any integer variable by 1. Thus i++ is
equivalent to i = i + 1;
You will see -- operator also which is called decremental operator and it idecrease the value of any integer
variable by 1. Thus i-- is equivalent to i = i - 1;
Global Variables
Global variable is defined at the top of the program file and it can be visible and modified by any function that
may reference it.
Global variables are initialized automatically by the system when you define them!
If same variable name is being used for global and local variable then local variable takes preference in its
scope. But it is not a good practice to use global variables and local variables with the same name.
int i=4; /* Global definition */

main()
{
i++; /* Global variable */
func();
printf( "Value of i = %d -- main function\n", i );
}

func()
{
int i=10; /* Local definition */
i++; /* Local variable */
printf( "Value of i = %d -- func() function\n", i );
}
This will produce following result
Value of i = 11 -- func() function
Value of i = 5 -- main function
Rules for naming variables:
1 All variable names must begin with a letter of the alphabet, an underscore, or ( _ ), or a dollar sign ($). The
convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged.
2 After the first initial letter, variable names may also contain letters and the digits 0 to 9. No spaces or
special characters are allowed.
3 The name can be of any length, but don't get carried away. Remember that you will have to type this name.
4 Uppercase characters are distinct from lowercase characters. Using ALL uppercase letters are primarily
used to identify constant variables. Remember that variable names are case-sensitive.
5 You cannot use a java keyword (reserved word) for a variable name.

You might also like