Translate

ads

27 Jun 2019

C programming language commonplace


C programming language commonplace



The idea of this text is to introduce C commonplace.What to try and do once a program produces different|completely different} results in 2 different compilers?
For example, consider the subsequent easy program.


void main()

The above program fails in sayed 360 because the come style of main is void, however it compiles in Turbo C. however will we decide whether or not it's a legitimate c program or not?

Consider the subsequent program as another example. It produces completely different|completely different} results in different compilers.



#include<stdio.h> 
int main() 

int i = 1; 
printf("%d %d %d\n", i++, i++, i); 
return 0; 



2 1 3 - using g++ 4.2.1 on Linux.i686
1 2 3 - using SunStudio C++ 5.9 on Linux.i686
2 1 3 - using g++ 4.2.1 on SunOS.x86pc
1 2 3 - using SunStudio C++ 5.9 on SunOS.x86pc
1 2 3 - using g++ 4.2.1 on SunOS.sun4u
1 2 3 - using SunStudio C++ 5.9 on SunOS.sun4u
  


  Ø What is C standard?
The latest C standard is ISO/IEC 9899:2011, conjointly referred to as C11 because the final draft was revealed in 2011. Before C11, there was C99. The C11 final draft is offered here. See this for complete history of C standards.


  Ø  Can we all know behavior of all programs from C standard?

C standard leaves some behavior of the many C constructs as undefined and a few as some to modify the specification and permit some flexibility in implementation. for instance, in C the use of any automatic variable before it's been initialized yields undefined behavior and order of evaluations of sub expressions is some. This specifically frees the compiler to try and do no matter is best or most effective, ought to such a program be submitted.


  Ø  So what's the conclusion regarding higher than two examples?

Let us contemplate the primary example that is “void main() ”, the quality says following regarding example of main()



The function called at program startup is named main. The implementation
declares no prototype for this function. It shall be defined with a return
type of int and with no parameters:
       int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names
may be used, as they are local to the function in which they are declared):
       int main(int argc, char *argv[]) { /* ... */ }
or equivalent;10) or in some other implementation-defined manner.





So the return type void doesn’t follow the quality and it’s one thing allowed by bound compilers.Let us mention second example. Note the subsequent statement in C standard is listed below unspecified behavior.



The order in which the function designator, arguments, and
subexpressions within the arguments are evaluated in a function
call (6.5.2.2).







  Ø  What to try and do with programs whose behavior is       indefinable or unspecified in standard?

As a programmer, it's ne'er a decent plan to use programming constructs whose behaviour is indefinable or unspecified , such programs should be discouraged. The output of such programs might modification with compiler and/or machine











No comments:

Post a Comment