470,811 Members | 1,144 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

500 C sample Programs

HiTechSkill.Com offers free information, tests, and sample interview
questions that will help to improve your information technology skills.
http://www.hitechskill.com

Apr 9 '06 #1
10 5506
www.hitechskill.com wrote:

HiTechSkill.Com offers free information, tests, and sample interview
questions that will help
to improve your information technology skills.
http://www.hitechskill.com


500 C sample Programs! Let the corrections begin!

/* program no : 1 */
/* purpose : display the message */
/* date of written : 14/12/2004 */

#include <stdio.h>

void main()
{
printf("%s"," welcome to c programming writting");
}
/*************************/

1. void main is a nonportable type for main.
Code like this needs to say what implementation it's for.
2. main, defined with empty parentheses
is an obsolecent feature of the language.
3. A text stream that doesn't end in a newline character
is nonportable, and printf with a %s,
is a slightly overconvoluted way of outputting
a that particular string litteral.
4. There's no return statement, which is allowable in C99,
but not C89.

/* program no : 2 */
double radius=0.0,area;
printf("%lf", area);

1. All the same problems as program number 1 and more.
2 %lf is allowed in C99 as the format specifier for double,
but the right printf format specifier for double in both
C89 and C99, is %f.

/* program no : 3 */
1. All the same problems as program number 2.

Skipping past all the nearly identical programs to ...

/* program no : 22 */
/* purpose : read and print upper to lower */

printf("%c", any+32);

1. All the same problems as program number 1 plus
2. A program that has the stated purpose of this one,
really really should show how to use the tolower function.

"Basic (50 programs)" is a bad choice of names,
for a link to a list of 25 programs.
"While Loops(100 programs)" and "For Loops(100 programs)"
have all the same problems repeated over and over.

At this point, I'm assuming that all the programs
have at least all the same problems as program number 1.

/* program no : 231*/
getch();

getch isn't part of standard C and there's an apparent typo
which causes no files to be #included.

These four links are all the same:
Functions (50 programs)
Pointers (100 programs)
Files(100 programs)
Unix Commands(25programs)

--
pete
Apr 9 '06 #2
pete wrote:
1. void main is a nonportable type for main. [...] 4. There's no return statement, which is allowable in C99,
but not C89.


Even on implementations which document 'void' as a valid return type
for main() ?

Apr 9 '06 #3
pete wrote:
www.hitechskill.com wrote:

HiTechSkill.Com offers free information, tests, and sample interview
questions that will help
to improve your information technology skills.
http://www.hitechskill.com


500 C sample Programs! Let the corrections begin!

/* program no : 1 */
/* purpose : display the message */
/* date of written : 14/12/2004 */

#include <stdio.h>

void main()
{
printf("%s"," welcome to c programming writting");
}
/*************************/

1. void main is a nonportable type for main.
Code like this needs to say what implementation it's for.
2. main, defined with empty parentheses
is an obsolecent feature of the language.
3. A text stream that doesn't end in a newline character
is nonportable, and printf with a %s,
is a slightly overconvoluted way of outputting
a that particular string litteral.
4. There's no return statement, which is allowable in C99,
but not C89.

/* program no : 2 */
double radius=0.0,area;
printf("%lf", area);

1. All the same problems as program number 1 and more.
2 %lf is allowed in C99 as the format specifier for double,
but the right printf format specifier for double in both
C89 and C99, is %f.

/* program no : 3 */
1. All the same problems as program number 2.

Skipping past all the nearly identical programs to ...

/* program no : 22 */
/* purpose : read and print upper to lower */

printf("%c", any+32);

1. All the same problems as program number 1 plus
2. A program that has the stated purpose of this one,
really really should show how to use the tolower function.

"Basic (50 programs)" is a bad choice of names,
for a link to a list of 25 programs.
"While Loops(100 programs)" and "For Loops(100 programs)"
have all the same problems repeated over and over.

At this point, I'm assuming that all the programs
have at least all the same problems as program number 1.

/* program no : 231*/
getch();

getch isn't part of standard C and there's an apparent typo
which causes no files to be #included.

These four links are all the same:
Functions (50 programs)
Pointers (100 programs)
Files(100 programs)
Unix Commands(25programs)


I think he was hungover on 1/1/2005
--
==============
Not a pedant
==============
Apr 9 '06 #4
"Harald van D?k" wrote:
pete wrote:
1. void main is a nonportable type for main.

[...]
4. There's no return statement, which is allowable in C99,
but not C89.


Even on implementations which document 'void' as a valid return type
for main() ?


That's why pete wrote "nonportable" and not "illegal".

regards
John
Apr 9 '06 #5
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

pete wrote:
1. void main is a nonportable type for main.

[...]
4. There's no return statement, which is allowable in C99,
but not C89.


Even on implementations which document 'void' as a valid return type
for main() ?


Code which intended to compile on some platforms but not others,
is nonportable code. That's what "nonportable" means.
If there were no allowances for implementations which
document other types for main(),
then the code would just simply be undefined.
By contrast:

/* BEGIN program no : 1B */
/* purpose : display the message */

#include <stdio.h>

int main(void)
{
puts("welcome to c programming writting");
return 0;
}

/* END program no : 1B */

.... program 1B is highly portable, in C89 and C99.

K&R C is pretty much a defunct language these days.

--
pete
Apr 9 '06 #6
John F wrote:
"Harald van D?k" wrote:
pete wrote:
1. void main is a nonportable type for main.

[...]
4. There's no return statement, which is allowable in C99,
but not C89.


Even on implementations which document 'void' as a valid return type
for main() ?


That's why pete wrote "nonportable" and not "illegal".


pete wrote 'not' 'allowable', not nonportable, for the missing return
statement I was asking about.

Apr 9 '06 #7
On 2006-04-09, pete <pf*****@mindspring.com> wrote:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

pete wrote:
> 1. void main is a nonportable type for main.

[...]
> 4. There's no return statement, which is allowable in C99,
> but not C89.


Even on implementations which document 'void' as a valid return type
for main() ?


Code which intended to compile on some platforms but not others,
is nonportable code. That's what "nonportable" means.
If there were no allowances for implementations which
document other types for main(),
then the code would just simply be undefined.
By contrast:

/* BEGIN program no : 1B */
/* purpose : display the message */

#include <stdio.h>

int main(void)
{
puts("welcome to c programming writting");
return 0;
}

/* END program no : 1B */

... program 1B is highly portable, in C89 and C99.

K&R C is pretty much a defunct language these days.


Could you expand on that? Do you mean their style, the first edition,
the second edition, the non standard examples? Which is defunct?
(Considering there are billions of lines of code out there on working,
maintained platforms which are based on their programmers K&R based
styles.).
Apr 9 '06 #8
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

John F wrote:
"Harald van D?k" wrote:
pete wrote:
> 1. void main is a nonportable type for main.
[...]
> 4. There's no return statement, which is allowable in C99,
> but not C89.

Even on implementations which document
'void' as a valid return type
for main() ?


That's why pete wrote "nonportable" and not "illegal".


pete wrote 'not' 'allowable', not nonportable, for the missing return
statement I was asking about.


You are correct that a return 0 statement
would be wrong for such a case.

--
pete
Apr 9 '06 #9
Richard G. Riley wrote:

On 2006-04-09, pete <pf*****@mindspring.com> wrote:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= wrote:

pete wrote:
> 1. void main is a nonportable type for main.
[...]
> 4. There's no return statement, which is allowable in C99,
> but not C89.

Even on implementations which document 'void' as a valid return type
for main() ?


Code which intended to compile on some platforms but not others,
is nonportable code. That's what "nonportable" means.
If there were no allowances for implementations which
document other types for main(),
then the code would just simply be undefined.
By contrast:

/* BEGIN program no : 1B */
/* purpose : display the message */

#include <stdio.h>

int main(void)
{
puts("welcome to c programming writting");
return 0;
}

/* END program no : 1B */

... program 1B is highly portable, in C89 and C99.

K&R C is pretty much a defunct language these days.


Could you expand on that? Do you mean their style, the first edition,
the second edition, the non standard examples? Which is defunct?
(Considering there are billions of lines of code out there on working,
maintained platforms which are based on their programmers K&R based
styles.).


"K&R C" refers to the language
described in the first edition from 1978.

That language is much more different from C89,
than C89 is from C99.

There's no void type, no long double type.
There's no prototypes.
stdio is the whole standard library.

I'm sure there's a few more differences,
but I don't write in that language,
so I'm not completely familiar with all of the differences.

--
pete
Apr 9 '06 #10
On Sun, 9 Apr 2006 15:13:41 +0200, in comp.lang.c , "Richard G. Riley"
<rg****@gmail.com> wrote:
On 2006-04-09, pete <pf*****@mindspring.com> wrote:
K&R C is pretty much a defunct language these days.


Could you expand on that? [...] Which is defunct?


He means K&R C. If you're unsure what that means, it refers to
pre-standard C. Not the style, but the language.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Apr 9 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Rhino | last post: by
2 posts views Thread by Martin Wingelaar | last post: by
reply views Thread by Ken Gallagher | last post: by
reply views Thread by pmp | last post: by
reply views Thread by msmithuk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.