Finally one site with all the C fundas

P

parag_paul

http://concentratedlemonjuice.blogspot.com/2008/06/about-c-puzzles-on-my-blog.html

I actually am searching for more work done on C now, today there are
many sites that will provide you college level understanding of
pointers, buta time comes when in a software project you will have to
go ahead and decide about using some feature of C++ that you never
used

some funda of C that you never saw ,and it adds to the cleanliness of
code and helps others improve their standards too.
I believe in this policy that when you write good code, you go one
step extra in helping others achieve better coding skill stoo
 
J

Jens Thoms Toerring

I actually am searching for more work done on C now, today there are
many sites that will provide you college level understanding of
pointers, buta time comes when in a software project you will have to
go ahead and decide about using some feature of C++ that you never
used

Sorry, but if you want to publish (rather trivial) C puzzles
you first should observe the distinction between C and C++.
Most of the so-called C-puzzles are actually C++ (I can't
judge if the answers are correct in those cases). C and C++
are different languages. Next you should correct some of
the mistakes. E.g.

2: What is wrong with this macro to find out the
square of a number

#define square(a) a*a

ANSWER:
If you pass an expression to this macro, it fails.
Eg: square(2+3) will be expanded to 2+3*2+3 = 11
whereas the actual result should be 25
Inorder to solve this problem you can write the macro as
#define square(a) ((a)*(a))

The correct answer is, of course, that this macro can't be
made safe for everyday use, even with the extra parentheses,
since e.g. 'square(i++)' will still result in an expression
invoking undefined behaviour..

Or

5. Are the statements 1 & 2 right, if we declare str as follows?

char * const str="Hello";

1. *str='W';
2. str="World";

ANSWER:
1-right
2-wrong

Neither 1 nor 2 are valid assignments. The first one isn't since
you're not allowed to change a string literal (even though you
may get away with it on some systems).

BYW, if in #4 you leave out the 'const' in front of

const char *abcd="Test";

you still wouldn't be allowed to do

*abcd='H';

for exactly the same reason.

Or

6. What is the problem with the following piece of code

typedef struct A{
int i;
};

A* RetStruct()
{
A a;
a.i=10;
return &a;
}

won't even compile since there is no type 'A' defined anywhere.
The compiler probably will complain about the ';' on line 3.

These are 3 out of 6 "puzzles" that could be C, the rest is all
C++ as far as I can see.
Regards, Jens
 
K

Keith Thompson

Regarding the subject "Finally one site with all the C fundas", a bit
of Googling indicates that "funda" is a contraction of "fundamental",
origininating in Indian English. I've never encountered it before. I
would suggest avoiding the term if you want to be understood by
readers outside India.

[...]
2: What is wrong with this macro to find out the
square of a number

#define square(a) a*a

ANSWER:
If you pass an expression to this macro, it fails.
Eg: square(2+3) will be expanded to 2+3*2+3 = 11
whereas the actual result should be 25
Inorder to solve this problem you can write the macro as
#define square(a) ((a)*(a))

The correct answer is, of course, that this macro can't be
made safe for everyday use, even with the extra parentheses,
since e.g. 'square(i++)' will still result in an expression
invoking undefined behaviour..

Also, the statement that "If you pass an expression to this macro, it
fails" is incorrect, or at least incomplete. I can pass the
expression (2+3) to the macro: square((2+3)), and it works just fine.

The answer section explains the inner parentheses around the
arguments, but not the outer parentheses around the whole definition.

The other problem with the macro is its name. By convention, macros
are usually given all-caps names, to warn the reader about the
possibility of, for example, multiple side-effects. This:

#define SQUARE(a) ((a)*(a))

would be unobjectionable. It does have the problem Jens points out,
that any side effects of the argument will occur twice, but using an
all-caps name warns a knowledgeable user to avoid calling it that way.
Or [snip]

Or

6. What is the problem with the following piece of code

typedef struct A{
int i;
};

A* RetStruct()
{
A a;
a.i=10;
return &a;
}

won't even compile since there is no type 'A' defined anywhere.
The compiler probably will complain about the ';' on line 3.

These are 3 out of 6 "puzzles" that could be C, the rest is all
C++ as far as I can see.

Presumably #6 is C++.

Another problem, whichever language it's intended to be, is that it's
not indented. (And the white text on a black background on the web
site is extremely annoying, at least to me.)
 
R

rahul

http://concentratedlemonjuice.blogspot.com/2008/06/about-c-puzzles-on...

I actually am searching for more work done on C now, today there are
many sites that will provide you college level understanding of
pointers, buta time comes when in a software project you will have to
go ahead and decide about using some feature of C++ that you never
used

some funda of C that you never saw ,and it adds to the cleanliness of
code and helps others improve their standards too.
I believe in this policy that when you write good code, you go one
step extra in helping others achieve better coding skill stoo

Probably you should refer the comp.lang.c FAQ first. Its better than
any crap posted
else where in the name of c puzzles. Most of the so called C puzzles
are there with
logical and standard complying answers.
 
P

parag_paul

Probably you should refer the comp.lang.c FAQ first. Its better than
any crap posted
else where in the name of c puzzles. Most of the so called C puzzles
are there with
logical and standard complying answers.

wow,
I should have posted all the question in the limelight individually.,
I am sorry for using the term Funda

Sorry for putting all the uncorrected question here. See this
acatually improves the content in the long run.
Please take this a beta edition. This is not a write and forget
attempt. I will try to improve the questions and answers give time.

Actually, most people dont dive into the intricacies given the amount
of time they want to spend. Thye want answer to get it done ( with
linux 4.2 , gcc is no more what it used to be )
so it is time that we come together and give the most plausible and
accurate answer to all the questions.

I believe this attempt will take my knowledge further and also it will
increase the fundamentals of the readers in the long run.

I will not discard it, please take my intentions to be of the first
rate.
I know that you will brand it as a myth compilation but lets bring
forward the truth in one place atleast.
The FAQ too has some of them common but trust me it is a collection
over the last few years.

Hope you will help me. I will not put all the question all togetehr. I
will go over them individually, and get it checked
 
P

parag_paul

Probably you should refer the comp.lang.c FAQ first. Its better than
any crap posted
else where in the name of c puzzles. Most of the so called C puzzles
are there with
logical and standard complying answers.

I will try to get the correct answers here then, But when you search
online you will come across hundreds of answers , will it be better to
ask them here one by one
 
R

rahul

I will try to get the correct answers here then, But when you search
online you will come across hundreds of answers , will it be better to
ask them here one by one

Most of the questions which you will be posting here ( difference
between char a[10], char *a;
#define square(a) ( (a) * (a) ) etc ) have already been discussed
number of times. People generally
don't like going over the same stuff again. That is exactly what faqs
are for. Get a copy of c.l.c
FAQ first and post here if you don't get the answers there.
 
S

santosh

CBFalconer said:
That was just an archive, and was fine. Then they developed the
(faulty) newsreader and filled Usenet with ignorant newbies with no
knowledge of the proper protocols. DejaNews also had a newsreader,
but I don't recall the google type problems with it.

Google Groups' web interface has been perfectly fine for a long time. If
the majority of their users are "ignorant newbies", that's hardly their
fault. For that matter, at any one time, the majority of the Web's
users would probably be "ignorant newbies" according to some
definitions.

What _is_ a genuine fault with Google Groups is their lackadaisical
attitude towards users who abuse their services to send spam, and to
those who file complaints about the same. This in itself is a
sufficient reason to not use Google Groups to access Usenet. Note that
some traditional news servers also allow this sort of thing. Some are
even notorious for it.
 
H

Hyuga

http://concentratedlemonjuice.blogspot.com/2008/06/about-c-puzzles-on...

I actually am searching for more work done on C now, today there are
many sites that will provide you college level understanding of
pointers, buta time comes when in a software project you will have to
go ahead and decide about using some feature of C++ that you never
used

I thought I understood pointers pretty darn well in high school, but
you sir have proven me wrong! </snark>
 
D

Default User

santosh wrote:

Google Groups' web interface has been perfectly fine for a long time.


Hardly. Two things wrong are that it doesn't auto-trim .sigs, so its
users habitually leave those in, and it doesn't allow proper .sig
separators (it eats the trailing space) so that any user that tries a
..sig will have an incorrect one.

The two qualities they seemed to have required for the news interface
team were to be an incompetent programmer and be completely unfamiliar
with usenet.




Brian
 
S

santosh

Default said:
santosh wrote:




Hardly. Two things wrong are that it doesn't auto-trim .sigs, so its
users habitually leave those in,

How long would it take to trim a signature, particularly when a typical
response involves quite a bit of snipping anyway?
and it doesn't allow proper .sig
separators (it eats the trailing space) so that any user that tries a
.sig will have an incorrect one.

Okay. This is a serious disadvantage, at least to those who use sigs.
 
D

Default User

santosh said:
How long would it take to trim a signature, particularly when a
typical response involves quite a bit of snipping anyway?

Regardless, a decent newsreader does it automatically. It's the case
that many GG users don't trim at all, so getting rid of the .sigs would
be some help.
Okay. This is a serious disadvantage, at least to those who use sigs.

Just part of the overall incompetency of the designers. It took forever
to get automatic quoting going.



Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top