Difference between c and c
-
Upload
karthika-parthasarathy -
Category
Education
-
view
317 -
download
5
description
Transcript of Difference between c and c
Difference between C and C++
1. C is a procedure or function oriented language whereas C++ is a Object oriented language.
2. C has top down approach whereas C++ has a bottom up approach.
3. In C++ new and delete operators are used to allocate and free memory but in C malloc() and free() functions are used for allocation and freeing memory.
4. In C++ prototype of a function have to be defined before using it but in C functions can be used without defining a prototype.
5. Structures of C++ can have functions in them but C structures can only have variables.
6. For I/O C++ has iostream.h and C has stdio.h.
7. Function overloading is allowed in C++ but functions in C does not support overloading.
8. In C we have to include the struct keyword before the name of the structure to declare a instance of a structure. In C++ only the structure name is required. Example - :
struct employee
{
int id;
char *name;
// other variables
};
//Statement to declare structure variable in C
struct employee e1;
//Statement to declare structure variable in C++
employee e1;
main() doesn’t add return 0 statement automatically in C. In C++ it provide this statement.
//In C++ return 0 is added automatically.
int main()
{
printf(“Hello How are you”);
}
//In C return 0 has to be added manually.
int main()
{
printf(“Hello How are you”);
return 0;
}
9. C allows a void* pointer to be assigned to any pointer type without a cast, whereas C++ does not
10. Enumeration constants are always of type int in C, whereas they are distinct types in C++ and may have size different from that of int.
11.C++ identifiers are not allowed to contain two or more consecutive underscores in any position. C identifiers are not allowed to start with two or more consecutive underscores, but may contain them in other positions
12. C allows struct, union, and enum types to be declared in function prototypes, whereas C++ does not.
Common Programming Errors in C This Article lists down the common C programming errors
1. Using Undeclared variables Int main(){ Printf(“%d”,i); /* Error : I is not know to the program */}
2. Using = instead of = =. In C = operator is used for assignment and = = operator for comparison . A compiler does not give a error when = is used for comparison. For example : int j = 10; if (j = 5) Printf(“Condition is True”);else printf(“Condition is False”); and the output is Condition is True In the above example j is not equal to 5 but still it prints the true result Because statement j=5 assigns 5 to j and if statement becomes If (5) Now 5 is a nonzero value so it means true and it enters the if block. 3. Forget to end the comments.
Comment start : /*Comment end: */
4. Forget to put an ampersand (&) in scanf()
Scanf() always requires the address of the variable to store value in it. Always use a & (ampersand) operator with the variable name to pass the address of the variable to scanf().
Scanf(“%d”,&i); /* & with i is required to pass the address of I to scanf() */
5. Array Boundaries
The first element in an array in C is at index 0 (not index 1), and the last index is n-1 (not n), where n is the number of elements in the array.
Students often write loops like
for (i = 1; i <= n; i++) a[i] = ...;
when they should write
for (i = 0; i < n; i++) a[i] = ...; This can lead to unpredictable behavior of the program. 6. “break “ is missing from switch statement. 7. Forget to declare functions.
If functions returns other values than the 'int' type, they have to be declared before they are called. Example: Long i; i = getval();In this case 'i' is assigned an 'int' instead of a 'long'. In such cases the function needs to have a prototype like this Long getval(void);
8. Using memory after freeing it using free() function.
It we continue to use a memory location which is deallocated using free() function it can give us unexpected results.
9. Not Allocating memory to pointers .
The statement Char *str;
Does not associate any memory with str. Now if we use it without allocating any memory Strcpy (str, “Welcome”) the memory references will be some random location Correct Way to do this is : Int main(){ Char str[10]; Strcpy (str, “Welcome”) /* str points to char array */ Return 0;}
10. Array pointer not passed to a called function: If a called function is to store values in an array for later use by the calling function, it should be passed a pointer to an array defined in the calling function.
11. Comparing strings with ==
Never use the == operator to compare the value of strings! Strings are char arrays. The name of a char array acts like a pointer to the string. Consider the following code:
char str1[] = "hello";char str2[] = "hello"; if ( str1 == str2 ) printf("Condition True");else printf("Condition False");
This program prints “Condition False”. Because the == operator is comparing the pointer values of str1 and str2, not the data pointed to by them. The correct way to compare string values is to use the strcmp() library function. For ex:
if ( strcmp(str1,str2) == 0 ) printf("Condition True");else printf("Condition False");
This program prints “Condition True”.
12. Strings are not terminated with NULL characters
C assumes that every string has a null character to mark the end of meaningful data in the string. If this value is missing, many C string functions will keep processing data past the end of the meaningful data and often past the end of the character array itself until it happens to find a zero byte in memory!