C programming question
-
- Posts: 1242
- Joined: Thu Jun 10, 2010 2:13 am
- Real Name: Bob Hyatt (Robert M. Hyatt)
- Location: University of Alabama at Birmingham
- Contact:
Re: C programming question
Sorry, "beautiful COBOL" is the biggest oxymoron in the universe. From someone that taught COBOL for MANY years. BTW, for the record, PASCAL sucks just as badly. It is a perfectly good language to learn as a first language. Strongly typed. Mixing types is almost impossible unless you use a hack the pascal guys added after many complained. No pointers? Really?
I was never happier than when the universities moved from Pascal to C, and then to C++. The move to Java is a bit more iffy, as it primarily addresses all the web programmers...
I was never happier than when the universities moved from Pascal to C, and then to C++. The move to Java is a bit more iffy, as it primarily addresses all the web programmers...
Re: C programming question
WOW Lucasart, I was thinking about using C instead of Pascal so you might be able to help me with this bit of code:
Im still a bit confused with your assertion that C is "the bees knees" but I guess this code segment demonstrates that clearly.
Regards
Laurie.
#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);}
Im still a bit confused with your assertion that C is "the bees knees" but I guess this code segment demonstrates that clearly.
Regards
Laurie.
#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);}
Re: C programming question
The fact that you can write such code in C does not mean C is at fault. On the contrary, it points out the power of the C language. The output is as follows:lauriet wrote:#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);}
Code: Select all
On the first day of Christmas my true love gave to me
a partridge in a pear tree.
On the second day of Christmas my true love gave to me
two turtle doves
and a partridge in a pear tree.
On the third day of Christmas my true love gave to me
three french hens, two turtle doves
and a partridge in a pear tree.
On the fourth day of Christmas my true love gave to me
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the fifth day of Christmas my true love gave to me
five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the sixth day of Christmas my true love gave to me
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the seventh day of Christmas my true love gave to me
seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the eigth day of Christmas my true love gave to me
eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the ninth day of Christmas my true love gave to me
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the tenth day of Christmas my true love gave to me
ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the eleventh day of Christmas my true love gave to me
eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
On the twelfth day of Christmas my true love gave to me
twelve drummers drumming, eleven pipers piping, ten lords a-leaping,
nine ladies dancing, eight maids a-milking, seven swans a-swimming,
six geese a-laying, five gold rings;
four calling birds, three french hens, two turtle doves
and a partridge in a pear tree.
It's almost as if this piece of code is defeating information theory...
Trying to figure out how this code actually works, is a path to mental insanity. Best not to.
"Talk is cheap. Show me the code." -- Linus Torvalds.
Re: C programming question
Here's an example of "production quality" C code that shows you it can be very readable, and just as structured as Pascal. Just much more elegant and concise. This function calculates the attack info (attack bitboards by attack type and checkers if in check) in my code:
You can see that I am using a few C tricks here, such as
* chained assignment and assignment/operation relying on C operator precedence and associativity.
* pointer (that will be optimized away) for the sake of readability and not repeating the verbose a->attacks[color] (*total looks more self documenting).
* omiting unnecessary {} whererever possible and reasonable.
It's always a tradeoff. If you are too clever writing C code that relies on tricks, it can be hard to understand and maintain. If you want to wite Pascal style C, well you're not taking advantage of C's beauty and conciceness and that does not make your code easier to read. And there's always a bit of personal taste involved: I may find this code quite nice, and someone else will find it ugly for some other reasons.
Code: Select all
void calc_attack_info(const struct board *b, struct attack_info *ai)
{
memset(ai, 0, sizeof(struct attack_info));
for (int color = 0; color < NB_COLOR; color++) {
bitboard_t *total = &ai->attacks[color];
bitboard_t fss;
// King
*total |= ai->kattacks[color] = kattacks(b->king_pos[color]);
// Knight
fss = get_pieces(b, color, KNIGHT);
while (fss)
*total |= ai->nattacks[color] |= nattacks(pop_lsb(&fss));
// Diagonal
fss = get_BQ(b, color);
while (fss)
*total |= ai->battacks[color] |= battacks(pop_lsb(&fss), b->occ);
// Orthogonal
fss = get_RQ(b, color);
while (fss)
*total |= ai->rattacks[color] |= rattacks(pop_lsb(&fss), b->occ);
// Pawn
fss = get_pieces(b, color, PAWN);
ai->pattacks[color] = shift_bit(fss & ~file_bb(FILE_A), lc_inc(color));
ai->pattacks[color] |= shift_bit(fss & ~file_bb(FILE_H), rc_inc(color));
*total |= ai->pattacks[color];
}
// Checkers
const int us = b->turn, them = opp_color(us);
const int ksq = b->king_pos[us];
if (test_bit(ai->attacks[them], ksq)) {
ai->checkers = nattacks(ksq) & get_pieces(b, them, KNIGHT);
ai->checkers |= pattacks(us, ksq) & get_pieces(b, them, PAWN);
ai->checkers |= battacks(ksq, b->occ) & get_BQ(b, them);
ai->checkers |= rattacks(ksq, b->occ) & get_RQ(b, them);
} else
ai->checkers = 0;
}
* chained assignment and assignment/operation relying on C operator precedence and associativity.
* pointer (that will be optimized away) for the sake of readability and not repeating the verbose a->attacks[color] (*total looks more self documenting).
* omiting unnecessary {} whererever possible and reasonable.
It's always a tradeoff. If you are too clever writing C code that relies on tricks, it can be hard to understand and maintain. If you want to wite Pascal style C, well you're not taking advantage of C's beauty and conciceness and that does not make your code easier to read. And there's always a bit of personal taste involved: I may find this code quite nice, and someone else will find it ugly for some other reasons.
"Talk is cheap. Show me the code." -- Linus Torvalds.
Re: C programming question
Back when I was an undergrad, one of my friends figured out enough of the code to fix this typo.On the eigth day
"A very horrible obfuscator for pascal to annoy my fellow students+teacher for using a silly language"
https://github.com/xSmallDeadGuyx/Pascal-Obfuscator
Re: C programming question
CREATORS ADMIT Unix, C HOAX
In an announcement that has stunned the computer industry, Ken Thompson,
Dennis Ritchie and Brian Kernighan admitted that the Unix operating
system and C programming language created by them is an elaborate April
Fools prank kept alive for over 20 years. Speaking at the recent
UnixWorld Software Development Forum, Thompson revealed the following:
"In 1969, AT&&T had just terminated their work with the
GE/Honeywell/AT&&T Multics project. Brian and I had just started
working with an early release of Pascal from Professor Nichlaus Wirth's ETH
labs in Switzerland and we were impressed with its elegant simplicity and
power. Dennis had just finished reading 'Bored of the Rings', a
hilarious National Lampoon parody of the great Tolkien 'Lord of the
Rings' trilogy. As a lark, we decided to do parodies of the Multics
environment and Pascal. Dennis and I were responsible for the operating
environment. We looked at Multics and designed the new system to be as
complex and cryptic as possible to maximize casual users' frustration
levels, calling it Unix as a parody of Multics, as well as other more
risque allusions. Then Dennis and Brian worked on a truly warped
version of Pascal, called 'A'. When we found others were actually
trying to create real programs with A, we quickly added additional
cryptic features and evolved into B, BCPL and finally C. We stopped
when we got a clean compile on the following syntax:
for(;P("\n"),R--;P("|"))for(e=C;e--;P("_"+(*u++/8)%2))P("| "+(*u/4)%2);
To think that modern programmers would try to use a language that
allowed such a statement was beyond our comprehension! We actually
thought of selling this to the Soviets to set their computer science
progress back 20 or more years. Imagine our surprise when AT&&T and
other US corporations actually began trying to use Unix and C! It has
taken them 20 years to develop enough expertise to generate even
marginally useful applications using this 1960's technological parody,
but we are impressed with the tenacity (if not common sense) of the
general Unix and C programmer. In any event, Brian, Dennis and I have
been working exclusively in Pascal on the Apple Macintosh for the past
few years and feel really guilty about the chaos, confusion and truly
bad programming that have resulted from our silly prank so long ago."
In an announcement that has stunned the computer industry, Ken Thompson,
Dennis Ritchie and Brian Kernighan admitted that the Unix operating
system and C programming language created by them is an elaborate April
Fools prank kept alive for over 20 years. Speaking at the recent
UnixWorld Software Development Forum, Thompson revealed the following:
"In 1969, AT&&T had just terminated their work with the
GE/Honeywell/AT&&T Multics project. Brian and I had just started
working with an early release of Pascal from Professor Nichlaus Wirth's ETH
labs in Switzerland and we were impressed with its elegant simplicity and
power. Dennis had just finished reading 'Bored of the Rings', a
hilarious National Lampoon parody of the great Tolkien 'Lord of the
Rings' trilogy. As a lark, we decided to do parodies of the Multics
environment and Pascal. Dennis and I were responsible for the operating
environment. We looked at Multics and designed the new system to be as
complex and cryptic as possible to maximize casual users' frustration
levels, calling it Unix as a parody of Multics, as well as other more
risque allusions. Then Dennis and Brian worked on a truly warped
version of Pascal, called 'A'. When we found others were actually
trying to create real programs with A, we quickly added additional
cryptic features and evolved into B, BCPL and finally C. We stopped
when we got a clean compile on the following syntax:
for(;P("\n"),R--;P("|"))for(e=C;e--;P("_"+(*u++/8)%2))P("| "+(*u/4)%2);
To think that modern programmers would try to use a language that
allowed such a statement was beyond our comprehension! We actually
thought of selling this to the Soviets to set their computer science
progress back 20 or more years. Imagine our surprise when AT&&T and
other US corporations actually began trying to use Unix and C! It has
taken them 20 years to develop enough expertise to generate even
marginally useful applications using this 1960's technological parody,
but we are impressed with the tenacity (if not common sense) of the
general Unix and C programmer. In any event, Brian, Dennis and I have
been working exclusively in Pascal on the Apple Macintosh for the past
few years and feel really guilty about the chaos, confusion and truly
bad programming that have resulted from our silly prank so long ago."
-
- Posts: 616
- Joined: Thu May 19, 2011 1:35 am
Re: C programming question
Note that growing by constant amount is a VERY BAD idea (I won't say it's lame as I did the same mistake many years ago), always grow by twice the previous capacity (of course with the exception of zero).lucasart wrote:Code: Select all
s->begin = realloc(s->begin, sizeof(int) * (s->end - s->begin + ALLOC_BY));
Please spare us your C++ hatred, it starts to get annoying, in fact if I'd remove the hatred crap from all your posts (that would reduce the real content to maybe less than 50%), they may be actually interesting to read.If only we could have only the good stuff in C++ without all the crap.
You can always use a subset of C++ and write C-like C++, it's up to you.
If you go to a shop, noone forces you to buy all the stuff especially if you don't need it.
-
- Site Admin
- Posts: 1226
- Joined: Wed Jun 09, 2010 7:49 am
- Real Name: Jeremy Bernstein
- Location: Berlin, Germany
- Contact:
Re: C programming question
I was a C++ hater until I discovered C++11 and the amazing additions to the standard library (e.g. Boost). It's incredible, useful stuff, and you don't need to buy into the entire, occassionally absurd world of class inheritance to use it.