Solved

c language

Posted on 1998-04-12
16
498 Views
Last Modified: 2010-04-15
the primary disadvantage of the programming language c
0
Comment
Question by:chen8
  • 7
  • 4
  • 4
  • +1
16 Comments
 

Accepted Solution

by:
bhattu earned 50 total points
Comment Utility
pointers! the facility that allows the user to handle memory locations directly are the primary disadvantage. and that is why
Pascal is still considered to be the most structured language in
schools. Even Java doesn't support it and hence is a better
substitute.

Similarly, u can ask about the disadvatages of C++. and IMO, the
main disadvantage is its ability of compatibility with C, thus
resulting in things like explicit type-casting.
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
OO apart, "explicit type-casting" is one of those few things actually making C++ *fairly* different from C. So what are you talking about?

And, in general, i agree pointers are something difficult for a newbie programmer - indeed they say C and C++ are programming languages for programmers -, but where's the fault other than one's programming skills?

IMO, C - as any other programming language - should be used for its intended tasks; and C, in a couple of words, is a general-purpose low-level programming language. Any *disadvantages* of C come from improper use.

So, what's "the primary disadvantage of the programming language c"? The fact that you must already be a programmer, IMO.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
The problem (if any) with C is the power that it puts at the programmer's disposal.

The syntax itself is fairly simple, and has few cases where one can make an error and accidentally create different working code. (There is a famous example in FORTRAN, where a missing ',' in a DO loop statement made it appear like a valid assignment rather than an error, so the program compiled OK but just did not work - hence causing a mrjor problem for the spacecraft it was guiding !!).

The syntax can be very succinct, but it can allow a programmer to be too clever for his/her own good and write code that is difficult to read (you can do wonderful things with #defines, parenthesis in complicated typedefs and using assignments as expressions).  Succinct languages are notoriously hard to read, have a look at an APL program one day!!

Conversely, C is not overly wordy.  Consider the simplest COBOL program, with all its special declarative divisions and sections, and the wordiness of the most simplest operations (mind you, the COBOL language today is much better than it used to be)

C is also powerful, and that power can be easily misused allowing one to easily write programms that can go wild at runtime, allocating memory, leaving dangling pointers etc etc.  But languages that limit the power of the programmer never seem to make it as general purpose languages, because the restrictions eventually get in the way of what you are trying to do.

None of the above are really bad feature of any given language .. but rather shortcomings of programmers.  Give a programmer the power, and he'll make powerful mistakes.  Give him the chance to write succint code, and he'll write his whole program in an unintelligible single line.

Of course, it _is_ possible to have a bad programming language - or at least bad within a certain frame of reference.  A language that lets the programmer express his data and algorithms in a clear and simple manner and whose syntax does not get in the way of that exporession is a good language.  A language that does not is a bad one.  On the whole, C falls into the 'good' category.

0
 

Expert Comment

by:bhattu
Comment Utility
Hi Ronslow,

THat was a wonderful explanation. But what we were discussing is
the disadvantages C language has, i guess, we were not labelling
it to be 'good' or 'bad'. If i understand properly, what chen
actually wanted to ask was 'what are the pitfalls of programming in C, or where exactly does the programmer commit mistakes (due to
which feature of C?). And, pointers are, IMO, the MAIN reason for
bad programs. (I repeat, its the programs that may be bad, not the language, but the features of a language help in people writing such programs. If we simply disallow the programmers to
allocate and handle memory then the number of bugs, errors, and
faulty programs would reduce by a substantial amount.

Any comments invited.

- bhattu.

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
"If we simply disallow the programmers to allocate and handle memory" then most of the useful programs written in C would no longer work.

Pointers may be difficult to learn, but their presence in C is NOT a disadvantage .. without them the language becomes much less powerful.  You cannot really get away without the concept of pointers without crippling the language.  Even Java has them .. but disguises them syntactically os they look don't look like pointers (more like C++ references) .. but that means the the runtime environment for Java needs to manage memory for you.

I cannot think of a single (major) disadvantage about the C language that is not also an advantage ... its all swings and roundabouts.  If you remove a particular 'disadvantage' then the language becomes less powerful, or requires extra run-time support etc etc.

There are things that can be improved in C .. C9X addresses many of these, as does C++.

I guess one disadvantage is that ANSI C has remained compatible with the older K&R C and has to support 'bad' syntax and semantics (like implicit int functions).

C++ adds some improvements (like in-line functions, block structure with declarations intermixed with code etc, casting operators, templates, constructors/destructors, overloading) - although some people may argue that some of these are not improvements at all.

0
 
LVL 7

Expert Comment

by:busuka
Comment Utility
Absolute. Pointers are great feature of C. Only disadvantage I see
in C (;-) is that program without commentaries with heavy usage of
pointers is almost unreadable. Also tricks like
x = (a>b) ? c:d; can confuse inexperienced reader. But: "it was
hard to write, it must be hard to read" :))) Also great thing
about C that many ppl seems forget: functions with variable number
of parameters. I never saw smth. like that in other languages.
Don't like it ? Switch to Basic ;)))

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Readability is a problem with most (if not all) programming languages .. I don't know of any that don't have some clever little constructs in them.  But then, one cannot expect to be able to read a program written in any given lagnauge if you don't know the language.  Some languages, however, take longer to learn than others.  C is a fairly compact language - just enough control statements, a few primitive types and operators, and few tricky bits of semantics.

0
 
LVL 7

Expert Comment

by:busuka
Comment Utility
BTW, Ronslow. I've mailed to Linda and two previous questions
were deleted, points are refunded. BTW, Linda said that in the
morning she found only 3 question from 4-pack.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Expert Comment

by:bhattu
Comment Utility
Talking of readability:

Just guess what this program would do? The following is a perfectly valid program in C, and the result is amazing:

----------------------------------------------------------------

#include <stdio.h>
main(t,_,a)
char *a;
{

return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-86,0,a+1)+a)):
1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}

-----------------------------------------------------------------
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
What's wrong with that ... all the code I write looks like that .. well, after a few drinks it does :-)

0
 
LVL 7

Expert Comment

by:busuka
Comment Utility
OLD trick bhattu :))) It prints longish text (rhymes or smth.)
right ?
0
 
LVL 7

Expert Comment

by:busuka
Comment Utility
BTW, I compiled it under UNIX and gcc said couple warnings about 'unknown escape
sequence' in lines 9, 10, 11, 12, 13. Several words came out garbled, but find WHERE
errors are almost impossible ;)
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Have a look at http://reality.sgi.com/csp/ioccc The International Obfuscated C Code Contest

here you'll see such gems as

float s=1944,x[5],y[5],z[5],r[5],j,h,a,b,d,e;int i=33,c,l,f=1;int g(){return f=
(f*6478+1)%65346;}m(){x[i]=g()-l;y[i]=(g()-l)/4;r[i]=g()>>4;}main(){char t[1948
]=" `MYmtw%FFlj%Jqig~%`jqig~Etsqnsj3stb",*p=t+3,*k="3tjlq9TX";l=s*20;while(i<s)
p[i++]='\n'+5;for(i=0;i<5;i++)z[i]=(i?z[i-1]:0)+l/3+!m();while(1){for(c=33;c<s;
c++){c+=!((c+1)%81);j=c/s-.5;h=c%81/40.0-1;p[c]=37;for(i=4;i+1;i--)if((b=(a=h*x
[i]+j*y[i]+z[i])*a-(d=1+j*j+h*h)*(-r[i]*r[i]+x[i]*x[i]+y[i]*y[i]+z[i]*z[i]))>0)
{for(e=b;e*e>b*1.01||e*e<b*.99;e-=.5*(e*e-b)/e);p[c]=k[(int)(8*e/d/r[i])];}}for
(i=4;i+1;z[i]-=s/2,i--)z[i]=z[i]<0?l*2+!m():z[i];while(i<s)putchar(t[i++]-5);}}

or this one

#define C " "
#define O( _ ) # _
#define R( n , d ) e ( n , d )
#define e(p,o)o##p
#define D O ( % ) O ( l ) O ( d )
#define U R ( e ( g , n ) , e ( o , l ) )
#define M H ( R ( e ( c , i ) , t ) , R ( e ( a , t ) , s ) )
#define P H ( R ( e ( f , t ) , n ) , R ( e ( i , r ) , p ) ) (
#define H(O,r)R(O,r)
#include<stdio.h>
      H ( R (       e ( f ,       e ) , e       ( d , e
)      ) , R      (      e ( p      ,      y ) ,      t      ) ) H
( R      ( e      ( t      , c      ) ,      u )      , R      ( e
( r ,      t      ) , s      )      ) { U      n      , t ;      }
F ; H (       R ( e (       f , e )       , e ( d
, e )      )      , R (      e      ( p ,      y      ) , t      )
) H      ( R      ( e      ( d      , e      ) ,      e (      n ,
g      ) ) ,      R      ( e (      i      , s )      ,      e ( n
      , u ) )       ) U B ;       M F I [       4 * 5 ]
,      w [ 4      *      5 ] ,      p      [ 4 ]      =      { { 9
, 1      } ,      { 1      , 1      } ,      { 1      , 1      } ,
{ 6 *      7      , 1 }      }      ; M R      (      e ( t      ,
n ) , i       ) J ; M       R ( e (       d , i )
, e (      o      , v )      )      o ( F      f      ) { e      (
f ,      i )      ( f      . t      - 1      ) {      J =      P D
O      ( / )      D      , f .      n      , f .      t      ) ; }
      R ( e (       e , s )       , e ( l       , e ) )
{      J = P      D      , f .      n      ) ; }      }      M R (
e (      d ,      i )      , e      ( o      , v      ) )      n (
F * f      )      { U a      =      ( * f      )      . n ,      b
= ( * f       ) . t ;       e ( f ,       i ) ( b
) { H      (      R ( e      (      e , l      )      , i )      ,
e (      h ,      w )      ) (      a )      { U      i =      b %
a      ; b =      a      ; a =      i      ; } b      =      b < 0
      ? - b :       b ; ( *       f ) . n       e ( = ,
/      ) b ;      (      * f )      .      t e (      =      , / )
b ;      } }      R (      e (      t ,      n )      , i      ) R
( e (      n      , i )      ,      e ( a      ,      m ) )      (
R ( e (       t , n )       , i ) a       , R ( e
( r ,      a      ) , e      (      h , c      )      ) * v      [
] )      { B      m ,      W ;      R (      e (      t ,      n )
,      i ) i      ,      c ; R      (      e ( r      ,      o ) ,
      f ) ( i       = 1 ; i       < 5 ; i       = i + 1
)      { e (      f      , i )      (      a > i      )      { J =
H (      R (      e (      f ,      n )      , a      ) ,      R (
e ( c      ,      s ) ,      s      ) ) (      v      [ i ]      ,
D O ( /       ) D , &       p [ i -       1 ] . n
, & p      [      i - 1      ]      . t )      ;      } n (      p
+ i      - 1      ) ;      } I      [ 0      ] =      p [      1 ]
;      R ( e      (      r , o      )      , f )      (      i = 0
      ; i < p       [ 0 ] .       n ; i =       i + 1 )
{      I [ i      +      1 ] .      n      = I [      i      ] . n
* p      [ 2      ] .      t +      p [      2 ]      . n      * I
[ i ]      .      t ; I      [      i + 1      ]      . t =      I
[ i ] .       t * p [       2 ] . t       ; n ( I
+ i +      1      ) ; }      R      ( e (      r      , o )      ,
f )      ( W      = ~      ( ~      0 e      ( <      , <      ) p
[      0 ] .      n      * 2 )      ;      ~ ( B      )      0 - W
      ; W = W       - 1 ) {       w [ c =       0 ] = I
[      0 ] ;      R      ( e (      r      , o )      ,      f ) (
m =      W ,      i =      1 ;      ! (      i >      p [      0 ]
. n )      ;      i = i      +      1 , m      =      m / 4      )
{ e ( f       , i ) (       ( m & 3       ) < 2 )
{ e (      f      , i )      (      m & 1      )      { w [      c
] .      n e      ( =      , *      ) I      [ i      ] .      t ;
e      ( f ,      i      ) ( (      w      [ c ]      .      t e (
      = , * )       I [ i ]       . n ) <       0 ) { w
[      c ] .      n      e ( =      ,      * ) -      1      ; w [
c ]      . t      e (      = ,      * )      - 1      ; }      } R
( e (      e      , s )      ,      e ( l      ,      e ) )      {
w [ c ]       . n e (       = , * )       I [ i ]
. n ;      w      [ c ]      .      t e (      =      , * )      I
[ i      ] .      t ;      } }      R (      e (      e ,      s )
,      e ( l      ,      e ) )      {      w [ c      =      c + 1
      ] = I [       i ] ; }       } R ( e       ( r , o
)      , f )      (      m = W      ,      i = c      =      0 ; i
< p      [ 0      ] .      n ;      i =      i +      1 ,      m =
m / 4      )      { e (      f      , i )      (      ( m &      3
) > 1 )       { w [ 0       ] . n =       w [ 0 ]
. n *      w      [ c +      1      ] . t      +      ( ( m      &
1 )      ? -      1 :      + 1      ) *      w [      c +      1 ]
.      n * w      [      0 ] .      t      ; w [      0      ] . t
      = w [ 0       ] . t *       w [ c =       c + 1 ]
.      t ; }      }      n ( w      )      ; e (      f      , i )
( !      p [      3 ]      . t      e (      | ,      | )      ( !
( p [      3      ] . n      -      w [ 0      ]      . n )      e
( & , &       ) ! ( p       [ 3 ] .       t - w [
0 ] .      t      ) ) )      {      R ( e      (      r , o      )
, f      ) (      m =      W ,      i =      0 ;      i <      p [
0      ] . n      ;      i = i      +      1 , m      =      m / 4
      ) { o (       I [ i ]       ) ; J =       P C O (
%      ) O (      c      ) C ,      O      ( * )      O      ( / )
O (      + )      O (      - )      [ m      & 3      ] )      ; }
o ( I      [      i ] )      ;      J = P      C      O ( =      )
C ) ; o       ( w [ 0       ] ) ; J       = P O (
% ) O      (      c ) ,      e      ( 0 ,      1      ) ) ;      }
} H      ( R      ( e      ( n      , r      ) ,      u )      , R
(      e ( t      ,      e ) ,      r      ) ) J      -      1 ; }

and here is another beautiful program...

#define _POSIX_SOURCE
#include                                <time.h>
#include                               <stdio.h>
#define                             extern/* ioccc*/
#define                           condition 22+d+e> 31
#define                         declarator main ( void )
#define                       keyword ae(t,d+e-9); ae(m,4)
#define                     false ae (t, d+ e+ 22); ae (m,3)
#define                   syntax_error(n); } else if (J < n) {
#define                 relational_expression(i, o, ccc) i o ccc
#define               errno translation_unit/*Apollo 13*/int errno
#define             iterative_statement  for  (  expressions ) block
#define             translation_unit  declarator  compound_statement
#define             ae(unary_expression,ae)     unary_expression= ae
#define             declaration char T[16];     struct tm ae(s,{ 0})
#define             if_part {macrolist list     ae(b,J%4); type_name
#define             tokens if (t==25&&d==28     &&a > 10) ae (t, 18)
#define             selection_statement(a,b,c) if(a){b; } else{ c; }
#define             storage_class ae(e,(2*b+4*c+6*d+N)%7); statement
#define             statement_list iterative_statement return'^'^'^'
#define            macro(x,y,cast) ae(M,x); ae(N,y)syntax_error(cast)
#define           block { if(relational_expression(J,<,1700))if_part;}
#define          declaration_list int J,a,b,c,d, e,t,m,M,N; declaration
#define         true keyword; selection_statement(t==26,ae(t,19),tokens)
#define        compound_statement { declaration_list ; statement_list ; }
#define       expressions ae(J,1582); relational_expression(J,<,2200); ++J
#define      list macro (24, 5, 2200) ae(M, 24); ae(N, 6); } ae(a, J % 19);
#define     type_name ae (c, J % 7);ae (d, (19 * a + M) % 30); storage_class
#define    statement  selection_statement ( condition,true,false)pptoken cast
#define   macrolist macro(22,2,1800)macro     (23, 3,1900) macro (23, 4, 2100)
#define  pptoken ae(s.tm_year,J-1900);ae       (s.tm_mon,m-1); ae(s.tm_mday,t);
#define cast (void)strftime(T,sizeof T,         "%a %b %d %Y",&s),(void)puts(T);
#include                               <errno.h>

where <errno.h> is
extern int errno;

is there no end to the beauty of this language?

0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
PS: You realy need fixed rather than proportional fonts to see the last two examples nicely -- copy and paste them into Notepad.

Actually, the second example seems to have lost something in the formatting and doesn't look very pretty at all .. go to the site I mentioned an you'll find it there along with many other examples.


0
 

Expert Comment

by:bhattu
Comment Utility
Hi,

Busuka u r right, the code prints a rhyme, the warnings must have cropped in due to errors in cut/pasting the code, just as RONSLOW's second example lost smthng in the formatting.

The point however still remains: do we call such programs beautiful? The programmer has definitely written a beauty, and has done his/her best to make the code undecipherable, but the point is that, allowing such a thing also makes room for errors, which might be completely untraceable. But then whoever is writing such a code definitely knows what he/she is doing.

I wonder why chen is keeping quite on this!

cheers,
- bhattu.
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
The beauty is in the way the text of the program forms a picture.

As a program, they are both incredibly clever and incredibly stupid.

Such obfuscated code is a fun mind game to fiddle with, but NOONE (i hope) writes real code anything like this.

Of course, you can write code just as bad (if not worse) in other langauges .. but C is so popular and powerful that it is a good candidate for this - hence the competitions.

Mind you .. if a programmer gets too clever for himself with macros and goto's etc then sometimes you end up with something almost as bad (from a maintenance point of view).

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now