500 C sample Programs

  • Thread starter www.hitechskill.com
  • Start date
P

pete

www.hitechskill.com said:
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)
 
G

Guest

pete said:
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() ?
 
P

pemo

pete said:
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
 
J

John F

Harald van D?k said:
pete said:
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
 
P

pete

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
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.
 
G

Guest

John said:
Harald van D?k said:
pete said:
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.
 
R

Richard G. Riley

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
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.).
 
P

pete

=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
John said:
Harald van D?k said:
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.
 
P

pete

Richard said:
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?= said:
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.
 
M

Mark McIntyre

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
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top