Major differences between ANSI C and K&R C

Major differences between ANSI C and K&R C


Answer:

Following are the major differences between ANSI C and K&R C (Kernighan and Ritchie):

 Support function prototyping.

 Support const and volatile data type qualifiers.

 Support wide characters and internationalization.

 Permit function pointers to be used without dereferencing.

1. Function Prototyping:

 Function prototype includes function names, arguments, data types, and return value data types.

In ANSI C when we create any function with an argument if we want to call that function then we have to give the same arguments and same data type same applies to return value too.

Example:

//creating a function in ANSI C
unsigned long abc(char *a, double b)
{
/** body/content**/
}
//calling function in ANSI C
abc(char *a, double b);

"It fixes the major issue of K&R C because in K&R C we can create or call a function without an argument which leads to program crash."

2. Support const and volatile data type qualifiers.

*If we declare some data using constant keyword than that data become fixed we can't change its value later.*
Example:
int printf(const char* abc,...........);

Declares a abc argument that is of a const char * data type, meaning that the function printf cannot modify data in any character array that is passed as an actual argument value to abc.

Volatile keyword specifies that the values of some variables may change asynchronously, giving a hint to the compiler’s optimization algorithm not to remove any “redundant” statements that involve “volatile” objects.
 eg: 
char get_io() 
 { 
volatile char* io_port = 0x7777;
char ch = *io_port; /*read first byte of data*/ 
ch = *io_port; /*read second byte of data*/ 
 } 
If io_port variable is not declared to be volatile when the program is compiled, the compiler may eliminate second ch = *io_port statement, as it is considered redundant with respect to the previous statement.

"K&RC didn't support const and volatile data type qualifiers"

3. Wide characters and internationalization.

ANSI C allows a wide character (more than one byte of storage per character).

setlocale function is defined in ANSI C, which allows users to specify the format of the date, monetary and real number representations.

Function prototype of setlocale function:

#include<locale.h> 
char setlocale (int category, const char* locale);

"K&RC didn't support wide character and internationalization"

4. Function pointers without dereferencing

ANSI C specifies that a function pointer may be used like a function name. No referencing is needed when calling a function whose address is contained in the pointer.

For Example, the following statement given below defines a function pointer funptr, which contains the address of the function foo. 
extern void foo(double xyz,const int *ptr);
void (*funptr)(double,const int *)=foo;
The function foo may be invoked by either directly calling foo or via the funptr. 
foo(12.78,”Hello world”); 
funptr(12.78,”Hello world”);
 K&R C requires funptr be dereferenced to call foo. (* funptr) (13.48,”Hello usp”);

 ANSI C also defines a set of C processor(cpp) symbols, which may be used in user programs. These symbols are assigned actual values at compilation time.

ANSI C is an Upgraded version of K&R C

upgrades are in below table:

 Support/permit  ANSI C  K & R C
 function prototyping.  Yes  No
 const and volatile data type qualifiers.  Yes  No
 wide characters and internationalization.  Yes  No
 unction pointers to be used without dereferencing.  Yes  No

No comments:

For Query and doubts!

Powered by Blogger.