🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.msdos.programmer
18 messages
18 total messages Started by Gregary John Boyles Wed, 03 Jan 1996 11:38
Pascal program works but not C program!
#1157
Author: Gregary John Boyles
Date: Wed, 03 Jan 1996 11:38
589 lines
17105 bytes
EMail : boyles@lux.latrobe.edu.au
        
Why does the Pascal version of this program behave exactly as expected while the
C++ version does not?




******************************* PASCAL VERSION *******************************

program show;


uses crt,dos,crt_,strings;


const maxstring=127;
      escape=chr(27);
      space=' ';
      up='H';
      down='P';
      pageup='I';
      pagedown='Q';
      left='K';
      right='M';
      home='G';
      end_='O';
      null=chr(0);


type nodetypeptr=^nodetype;
     nodetype=record
                   fileline:string;
                   filelinenum:integer;
                   next,previous:nodetypeptr;
              end;


function empty(posptr:nodetypeptr):boolean;

begin
     empty:=posptr=nil;
end;


procedure initialize(var listptr:nodetypeptr);

begin
     listptr:=nil;
end;


procedure add(var listptr:nodetypeptr;fileline:string;filelinenum:integer);

var newptr,tempptr:nodetypeptr;

begin
      new(newptr);
      newptr^.fileline:=fileline;
      newptr^.filelinenum:=filelinenum;
      if (listptr=nil) then
      begin
           (listptr):=newptr;
           (listptr)^.next:=nil;
           (listptr)^.previous:=nil;
      end
      else
      begin
           tempptr:=listptr;
           while (tempptr^.next<>nil) do
           begin
                tempptr:=tempptr^.next;
           end;
           tempptr^.next:=newptr;
           newptr^.next:=nil;
           newptr^.previous:=tempptr;
      end;
end;


procedure remove(var listptr:nodetypeptr);

var tempptr:nodetypeptr;

begin
     tempptr:=listptr;
     listptr:=listptr^.next;
     listptr^.previous:=nil;
     dispose(tempptr);
end;


procedure setpos(var posptr:nodetypeptr;filelinenum:integer);

begin
     while (posptr^.filelinenum<>filelinenum) do
     begin
          if (posptr^.filelinenum>filelinenum) then
          begin
               posptr:=posptr^.previous;
          end
          else if (posptr^.filelinenum<filelinenum) then
          begin
               posptr:=posptr^.next;
          end;
     end;
end;


procedure writescreenfull(width,height:word;posptr:nodetypeptr;stringpos:word);

var y,i:word;
    line1:string;

begin
     window(1,1,width,height);
     clrscr;
     window(1,1,width,height+1);
     y:=1;
     while (y<=height) and (not empty(posptr^.next)) do
     begin
          gotoxy(1,y);
          i:=stringpos;
          while (i<=length(posptr^.fileline)) and ((i-stringpos)<=width) do
          begin
               write(posptr^.fileline[i]+'');
               i:=i+1;
          end;
          writeln;
          posptr:=posptr^.next;
          y:=y+1;
     end;
end;


procedure main;

var infile:text;
    x,width,height,back,fore:word;
    ch:char;
    numfilelines,filelinenum:integer;
    listptr,posptr:nodetypeptr;
    fileline,bottomfileline:string;
    stringpos:word;

begin
     if (paramcount<1) then
     begin
          writeln;
          writeln('Show what . . .');
     end
     else
     begin
          {$I-}
          assign(infile,paramstr(1));
          {$I+}
          if (doserror<>0) then
          begin
               writeln;
               write('infile ');
               write(paramstr(1));
               writeln(' not found . . .');
          end
          else
          begin
               reset(infile);
               initialize(listptr);
               filelinenum:=0;
               while (not eof(infile)) do
               begin
                    readln(infile,fileline);
                    add(listptr,fileline,filelinenum);
                    filelinenum:=filelinenum+1;;
               end;
               numfilelines:=filelinenum-1;
               close(infile);
               posptr:=listptr;
               fore:=textattr and $0f;
               back:=(textattr and $70) div $10;
               height:=hi(windmax);
               width:=lo(windmax);
               bottomfileline:=' '+chr(24)+' '+chr(25)+' pgup  pgdn  s "string" : (search for string)  esc : (quit)';
               for x:=length(bottomfileline)+1 to width do
               begin
                    bottomfileline:=bottomfileline+' ';
               end;
               set_cursor(none);
               clrscr;
               textcolor(back);
               textbackground(fore);
               gotoxy(1,height);
               write(bottomfileline);
               textcolor(fore);
               textbackground(back);
               height:=height-1;
               filelinenum:=1;
               stringpos:=1;
               writescreenfull(width,height,posptr,stringpos);
               ch:=space;
               while (ch<>escape) do
               begin
                    while ((ch<>up) and (ch<>down) and (ch<>pageup) and
                           (ch<>pagedown) and (ch<>escape) and (ch<>left) and
                           (ch<>right) and (ch<>end_) and (ch<>home)) do
                    begin
                         ch:=readkey;
                         if (ch=null) then
                         begin
                              ch:=readkey;
                         end;
                    end;
                    if (ch=up) then
                    begin
                         if ((filelinenum-1)>=1) then
                         begin
                              filelinenum:=filelinenum-1;
                         end
                         else
                         begin
                              filelinenum:=1;
                         end;
                         setpos(posptr,filelinenum);

                    end
                    else if (ch=down) then
                    begin
                         if ((filelinenum+1+height)<=numfilelines) then
                         begin
                              filelinenum:=filelinenum+1
                         end
                         else
                         begin
                              filelinenum:=numfilelines-height+1;
                         end;
                         setpos(posptr,filelinenum);
                    end
                    else if (ch=pageup) then
                    begin
                         if ((filelinenum-height+1)>=1) then
                         begin
                              filelinenum:=filelinenum-height+1;
                         end
                         else
                         begin
                              filelinenum:=1;
                         end;
                         setpos(posptr,filelinenum);
                    end
                    else if (ch=pagedown) then
                    begin
                         if (((filelinenum+height-1)<=numfilelines) and
                             ((numfilelines-(filelinenum+height-1))>=(height-1))) then
                         begin
                              filelinenum:=filelinenum+height-1;
                         end
                         else
                         begin
                              filelinenum:=numfilelines-height+1;
                         end;
                         setpos(posptr,filelinenum);
                    end
                    else if (ch=left) then
                    begin
                         if (stringpos>1) then
                         begin
                              stringpos:=stringpos-1;
                         end;
                    end
                    else if (ch=right) then
                    begin
                         if (stringpos<=(maxstring-width)) then
                         begin
                              stringpos:=stringpos+1;
                         end;
                    end
                    else if (ch=end_) then
                    begin
                         stringpos:=maxstring-width+1;
                    end
                    else if (ch=home) then
                    begin
                         stringpos:=1;
                    end;
                    if (ch<>escape) then
                    begin
                         ch:=space;
                    end;
                    writescreenfull(width,height,posptr,stringpos);
               end;
               height:=height+1;
               window(1,1,width,height);
               clrscr;
               set_cursor(line);
               while (not empty(listptr)) do
               begin
                    remove(listptr);
               end;
          end;
     end;
end;


begin
     main;
end.




******************************** C++ VERSION *********************************

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <alloc.h>
#include <process.h>

const maxstring=127;
const lineend='\n';
const escape=27;
const null='\0';
const space=' ';
const up='H';
const down='P';
const pageup='I';
const pagedown='Q';
const left='K';
const right='M';
const home='G';
const end_='O';


typedef char string[maxstring];
typedef struct nodetypetag{string line;int linenum;struct nodetypetag *next,*previous;} nodetype;
typedef nodetype *nodetypeptr;

string line,line_,line1,bottomline;

char empty(nodetypeptr posptr)
{
     return(posptr==NULL);
}

void initialize(nodetypeptr *listptr)
{
     *listptr=NULL;
}

void add(nodetypeptr *listptr,char *fileline,int Linenum)
{
      nodetypeptr newptr,tempptr;

      newptr=(nodetypeptr)malloc(sizeof(nodetype));
      strcpy(newptr->line,fileline);
      newptr->linenum=Linenum;
      if (*listptr==NULL)
      {
           (*listptr)=newptr;
           (*listptr)->next=NULL;
           (*listptr)->previous=NULL;
      }
      else
      {
           tempptr=*listptr;
           while (tempptr->next!=NULL)
           {
                tempptr=tempptr->next;
           }
           tempptr->next=newptr;
           newptr->next=NULL;
           newptr->previous=tempptr;
      }
}

void remove(nodetypeptr *listptr)
{
     nodetypeptr tempptr;

     tempptr=*listptr;
     *listptr=(*listptr)->next;
     (*listptr)->previous=NULL;
     free(tempptr);
}

void setpos(nodetypeptr *posptr,int linenum)
{
     while ((*posptr)->linenum!=linenum)
     {
          if ((*posptr)->linenum>linenum)
          {
               *posptr=(*posptr)->previous;
          }
          else if ((*posptr)->linenum<linenum)
          {
               *posptr=(*posptr)->next;
          }
     }
}

void writescreenfull(char width,char height,nodetypeptr posptr,char stringpos)
{
     char y;

     window(1,1,width,height);
     clrscr();
     window(1,1,width,height+1);
     y=1;
     while ((y<=height) && (!empty(posptr->next)))
     {
          gotoxy(1,y);
          strcpy(line_,posptr->line);
          line__=&(line_[stringpos]);
          strncpy(line,line__,width-1);
          strcat(line,null);
          cputs(line);
          posptr=posptr->next;
          y++;
     }
}

void removelineends(char *strings,unsigned int maxstrings)
{
     unsigned int i;

     i=0;
     while ((i<maxstrings) && (strings[i]!=lineend))
     {
          i++;
     }
     strings[i]=null;
}

void main(int argc,char *argv[])
{
     FILE *file;
     char x,ch,width,height,back,fore,stringpos;
     int numlines,linenum;
     text_info info;
     nodetypeptr listptr,posptr;


     if (argc<2)
     {
          cputs("\nShow what . . .\n");
     }
     else
     {
          file=fopen(argv[1],"rt");
          if (file==NULL)
          {
               cprintf("\nFile %s not found . . .\n",argv[1]);
          }
          else
          {
               rewind(file);
               initialize(&listptr);
               linenum=0;
               while (fgets(line,maxstring,file)!=NULL)
               {
                    removelineends(line,maxstring);
                    add(&listptr,line,linenum);
                    linenum++;
               }
               numlines=linenum--;
               fclose(file);
               posptr=listptr;
               gettextinfo(&info);
               fore=info.attribute&0x0f;
               back=(info.attribute&0x70)/0x10;
               height=info.screenheight;
               width=info.screenwidth;
               strcpy(bottomline,"  \30  \31  pgup  pgdn  s <string> (search for string)  esc (quit)");
               for(x=strlen(bottomline)+1;x<width;x++)
               {
                    strcat(bottomline," ");
               }
               _setcursortype(_NOCURSOR);
               clrscr();
               textcolor(back);
               textbackground(fore);
               gotoxy(1,height);
               cputs(bottomline);
               textcolor(fore);
               textbackground(back);
               height--;
               linenum=1;
               stringpos=1;
               writescreenfull(width,height,posptr,stringpos);
               ch=space;
               while (ch!=escape)
               {
                    while ((ch!=up) && (ch!=down) && (ch!=pageup) &&
                           (ch!=pagedown) && (ch!=escape) && (ch!=left) &&
                           (ch!=right) && (ch!=end_) && (ch!=home))
                    {
                         ch=getch();
                         if (ch==null)
                         {
                              ch=getch();
                         }
                    }
                    if (ch==up)
                    {
                         if ((linenum-1)>=1)
                         {
                              linenum--;
                         }
                         else
                         {
                              linenum=1;
                         }
                         setpos(&posptr,linenum);

                    }
                    else if (ch==down)
                    {
                         if ((linenum+1+height)<=numlines)
                         {
                              linenum++;
                         }
                         else
                         {
                              linenum=numlines-height+1;
                         }
                         setpos(&posptr,linenum);
                    }
                    else if (ch==pageup)
                    {
                         if ((linenum-height+1)>=1)
                         {
                              linenum=linenum-height+1;
                         }
                         else
                         {
                              linenum=1;
                         }
                         setpos(&posptr,linenum);
                    }
                    else if (ch==pagedown)
                    {
                         if (((linenum+height-1)<=numlines) && ((numlines-(linenum+height-1))>=(height-1)))
                         {
                              linenum=linenum+height-1;
                         }
                         else
                         {
                              linenum=numlines-height+1;
                         }
                         setpos(&posptr,linenum);
                    }
                    else if (ch==left)
                    {
                         if (stringpos>1)
                         {
                              stringpos--;
                         }
                    }
                    else if (ch==right)
                    {
                         if (stringpos<=(maxstring-width))
                         {
                              stringpos++;
                         }
                    }
                    else if (ch==end_)
                    {
                         stringpos=maxstring-width+1;
                    }
                    else if (ch==home)
                    {
                         stringpos=1;
                    }
                    if (ch!=escape)
                    {
                         ch=space;
                    }
                    writescreenfull(width,height,posptr,stringpos);
               }
               height+=1;
               window(1,1,width,height);
               clrscr();
               _setcursortype(_NORMALCURSOR);
               while (!empty(listptr))
               {
                    remove(&listptr);
               }
          }
     }
}
Re: Pascal program works but not C program!
#1158
Author: Kurt Watzka
Date: Wed, 03 Jan 1996 12:17
16 lines
992 bytes
cs102238@lux.latrobe.edu.au (Gregary John Boyles ) writes:

>EMail : boyles@lux.latrobe.edu.au
>        
>Why does the Pascal version of this program behave exactly as expected while the
>C++ version does not?

On my machine, both programs behave exactly identical. The "Pascal" version
does not compile with the PASCAL compiler, while the "C++" version does not
compile with the C++ compiler :-).

Kurt
--
| Kurt Watzka                             Phone : +49-89-2180-6254
| watzka@stat.uni-muenchen.de
| ua302aa@sunmail.lrz-muenchen.de
Re: Pascal program works but not C program!
#1159
Author: Kohn Emil Dan
Date: Thu, 04 Jan 1996 10:18
97 lines
2873 bytes

On 3 Jan 1996, Gregary John Boyles wrote:

> EMail : boyles@lux.latrobe.edu.au
>       
> Why does the Pascal version of this program behave exactly as expected while the
> C++ version does not?
> 
> 
> 
> 
> ******************************* PASCAL VERSION *******************************
> 
/*Pascal  code removed*/ 
> 
> ******************************** C++ VERSION *********************************
> 
> #include <stdio.h>
> #include <conio.h>
> #include <string.h>
> #include <alloc.h>
> #include <process.h>
> 
> const maxstring=127;
> const lineend='\n';
> const escape=27;
> const null='\0';
> const space=' ';
> const up='H';
> const down='P';
> const pageup='I';
> const pagedown='Q';
> const left='K';
> const right='M';
> const home='G';
> const end_='O';
> 
> 
> 
> void writescreenfull(char width,char height,nodetypeptr posptr,char stringpos)
> {
>    char y;
> 
>    window(1,1,width,height);
>    clrscr();
>    window(1,1,width,height+1);
>    y=1;
>    while ((y<=height) && (!empty(posptr->next)))
>    {
>         gotoxy(1,y);
>         strcpy(line_,posptr->line);
>         line__=&(line_[stringpos]);
>         strncpy(line,line__,width-1);
>         strcat(line,null);
                       ^---------------This is a problem!!!!!!!!!
                       
>         cputs(line);
>         posptr=posptr->next;
>         y++;
>    }
> }
> 
> 
> void main(int argc,char *argv[])
   ^-----------------------------This one too, but not too serious
> {
> }
> 
> 


I hadn't read  your code thoroughly, but I think your problem is with 
strcat (I mean surely there is a problem with the usage of strcat, but I 
don't know if it's the only one). In C (and in C++ too), a string of length 
1 is _NOT_ the same thing as a character. So null (as defined by you) is _NOT_ a string.
An empty string is defined as "". If you want to append a '\0' string 
terminator to line_  (I think this was your intention), use the statement:

line_[width-1]='\0'; (or using your constant line_[width-1]=null;) 

Don't bother, if the length of line__ is less than width-1; strncpy padds 
the destination string with '\0'-s.



Another problem, not as serious as the previous one, although this is not
the reason the C++ version might not work; main() should be defined
returning int, not void. 


Hope this helps.


					Best regards, 

								Emil
Re: Pascal program works but not C program!
#1161
Author: Tasmaniac
Date: Sun, 07 Jan 1996 18:29
42 lines
2056 bytes
>> Why does the Pascal version of this program behave exactly as expected while the
>> C++ version does not?
let's have a look...

>>    while ((y<=height) && (!empty(posptr->next)))
>>    {
>>         gotoxy(1,y);
>>         strcpy(line_,posptr->line);
>>         line__=&(line_[stringpos]);
>>         strncpy(line,line__,width-1);
This isn't valid, you can't 'strncpy' single characters to a string.
It SHOULD give you a warning on that.

>>         strcat(line,null);
>                       ^---------------This is a problem!!!!!!!!!
No, it was probably already wrong on the previous line. 

>> void main(int argc,char *argv[])
>   ^-----------------------------This one too, but not too serious
why is this a problem ?

>Another problem, not as serious as the previous one, although this is not
>the reason the C++ version might not work; main() should be defined
>returning int, not void. 
It's 100% legal to declare main as any of the following

void main (void)
int main (void)
void main (int argc, char *argv[])
int main (int argc, char *argv[])
void main (int argc, char **argv)
int main (int argc, char **argv)
void main (int argc, char *argv[], char *envp[])
int main (int argc, char *argv[], char *envp[])
void main (int argc, char *argv[], char **envp)
int main (int argc, char *argv[], char **envp)
void main (int argc, char **argv, char *envp[])
int main (int argc, char **argv, char *envp[])
void main (int argc, char **argv, char **envp)
int main (int argc, char **argv, char **envp)

erhm..  that should be it, I may have missed one...
Re: Pascal program works but not C program!
#1162
Author: Peter Seebach
Date: Mon, 08 Jan 1996 01:09
74 lines
2942 bytes
In article <4cp3vc$8nh@brahms.tfi.be>, Tasmaniac <Tasmaniac@unicall.be> wrote:
>>> void main(int argc,char *argv[])
>>   ^-----------------------------This one too, but not too serious
>why is this a problem ?

Because it conflicts with the ANSI/ISO standard for C.
(ANSI/ISO 9899-1990)

>It's 100% legal to declare main as any of the following

Really?  Wanna bet?  How much?

>void main (void)
Nope.
>int main (void)
Yup.
>void main (int argc, char *argv[])
Nope.
>int main (int argc, char *argv[])
Yup.
>void main (int argc, char **argv)
Yup.
>int main (int argc, char **argv)
Yup.
>void main (int argc, char *argv[], char *envp[])
Nope.
>int main (int argc, char *argv[], char *envp[])
Nope.
>void main (int argc, char *argv[], char **envp)
Nope.
>int main (int argc, char *argv[], char **envp)
Nope.
>void main (int argc, char **argv, char *envp[])
Nope.
>int main (int argc, char **argv, char *envp[])
Nope.
>void main (int argc, char **argv, char **envp)
Nope.
>int main (int argc, char **argv, char **envp)
Nope.

>erhm..  that should be it, I may have missed one...

You missed quite a lot.

Hint: Don't post when you don't have a fucking clue.

I can understand being incorrect.  I cannot understand, or tolerate,
the idiocy of posting such a long "correction" when it is devoid of
accuracy or content.

What, precisely, gives you these ideas?  What authority, great one,
shall we turn to that overrides or supersedes the ANSI and ISO
standards committees?  Do you have a platinum-iridium compiler
we should know about?

ANSI/ISO C *requires* one of two declarations of main in a hosted environment.
(In a freestanding environment, it is implementation defined whether or not
there may be, must be, or is, a function named main.)  They both return int.
One has no arguments, the other has two.

Please read the FAQ, which describes this, and take the time to learn
the material it covers, at least, and also that covered by any decent
C text, *before* posting such garbage as "answers".

If you got this incorrect understanding from a book, get a better book.
There are plenty.

-s
-- 
Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
C/Unix proto-wizard -- C/Unix questions? Send mail for help.  No, really!
Using trn?  Weird new newsgroup problem?  I know the fix!  Email me!
The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
Re: Pascal program works but not C program!
#1165
Author: Gene Wirchenko
Date: Mon, 08 Jan 1996 02:08
27 lines
1313 bytes
Tasmaniac@unicall.be (Tasmaniac) wrote:

[snip]

>>> void main(int argc,char *argv[])
>>   ^-----------------------------This one too, but not too serious
>why is this a problem ?

>>Another problem, not as serious as the previous one, although this is not
>>the reason the C++ version might not work; main() should be defined
>>returning int, not void. 
>It's 100% legal to declare main as any of the following

[snipped garbage list]

     Kindly read the FAQ!  Per the C standard, main() must return int.
That your compiler may not complain makes no difference.  It is not
valid C.

Sincerely,

Gene Wirchenko


C Pronunciation Guide:
     y=x++;     "wye equals ex plus plus semicolon"
     x=x++;     "ex equals ex doublecross semicolon"
Re: Pascal program works but not C program!
#1163
Author: Lawrence Kirby
Date: Mon, 08 Jan 1996 02:48
15 lines
992 bytes
In article <4cpqt6$9r8@solutions.solon.com>
           seebs@solutions.solon.com "Peter Seebach" writes:

...

>>void main (int argc, char **argv)
>Yup.

Oops!

-- 
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Re: Pascal program works but not C program!
#1164
Author: Peter Seebach
Date: Mon, 08 Jan 1996 03:35
28 lines
1662 bytes
In article <821069300snz@genesis.demon.co.uk>,
Lawrence Kirby  <fred@genesis.demon.co.uk> wrote:
>In article <4cpqt6$9r8@solutions.solon.com>
>           seebs@solutions.solon.com "Peter Seebach" writes:
>>>void main (int argc, char **argv)
>>Yup.

>Oops!

Let this be a lesson to all of you - even when you *do* know what you're
doing, take the time to browse the post and make sure you didn't do
anything particularly stupid.

Well, there.  I've made a mistake in news this year, now I can relax,
and maybe '97 will be the year that I post actively in several groups
without making an obvious mistake.

(For those unclear: The declaration above is not legal C, and results
in undefined behavior.  Many books get this wrong.  And to give this
alt.msdos.programmer relevance, where do you think the famed errorlevel
*comes* from?  The return from main().)

-s
-- 
Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
C/Unix proto-wizard -- C/Unix questions? Send mail for help.  No, really!
Using trn?  Weird new newsgroup problem?  I know the fix!  Email me!
The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
Re: Pascal program works but not C program!
#1166
Author: The Amorphous Mass
Date: Mon, 08 Jan 1996 07:25
44 lines
2229 bytes
On Sun, 7 Jan 1996, Tasmaniac wrote:
> >> void main(int argc,char *argv[])
> >   ^-----------------------------This one too, but not too serious
> why is this a problem ?

  Read the FAQ.  Question 11.12.

> It's 100% legal to declare main as any of the following

  If "legal" means "allowed by your compiler right now" then you're 
right.  If "legal" means allowed by the ANSI Standard than you're not.  
This newsgroup is concerned with ANSI Standard C.  The folks over in
comp.os.msdos.programmer.* might be more forgiving. :)

#if 0
> void main (void)
#endif
> int main (void)
#if 0
> void main (int argc, char *argv[])
#endif
> int main (int argc, char *argv[])
#if 0
> void main (int argc, char **argv)
#endif
> int main (int argc, char **argv)
#if 0
> void main (int argc, char *argv[], char *envp[])
> int main (int argc, char *argv[], char *envp[])
> void main (int argc, char *argv[], char **envp)
> int main (int argc, char *argv[], char **envp)
> void main (int argc, char **argv, char *envp[])
> int main (int argc, char **argv, char *envp[])
> void main (int argc, char **argv, char **envp)
> int main (int argc, char **argv, char **envp)
#endif
> 
> erhm..  that should be it, I may have missed one...

  Actually you included 11 too many. :)

/**James Robinson***********************            
  "If a fatal error occurs, the program should not be allowed to continue."
 -- Oracle Pro*C User's Guide         *************james-robinson@uiowa.edu**/
Re: Pascal program works but not C program!
#1171
Author: Richard K. Glover
Date: Mon, 08 Jan 1996 19:58
5 lines
858 bytes
Sorry, but I don't debug other people's entire 100+ line programs for 
free (and I bet not too many other people do, either).  I suggest you try 
tracking down the problem a bit more before asking for help.  Posting the 
entire program and saying "it doesn't work" won't get you very far.
Re: Pascal program works but not C program!
#1173
Author: John R Buchan
Date: Tue, 09 Jan 1996 19:27
25 lines
1148 bytes
In article <4cpqt6$9r8@solutions.solon.com>, seebs@solutions.solon.com says...
>
>In article <4cp3vc$8nh@brahms.tfi.be>, Tasmaniac <Tasmaniac@unicall.be> 
wrote:
<snip>
>>void main (void)
>Nope.
>>int main (void)
>Yup.
>>void main (int argc, char *argv[])
>Nope.
>>int main (int argc, char *argv[])
>Yup.
>>void main (int argc, char **argv)
>Yup.

I think ya mis-yupped this one, partna. :-)

>>int main (int argc, char **argv)
>Yup.
<snip>

-- 
John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
Re: Pascal program works but not C program!
#1176
Author: Gary E. Ansok
Date: Thu, 11 Jan 1996 20:36
29 lines
2019 bytes
In article <4cpqt6$9r8@solutions.solon.com>,
Peter Seebach <seebs@solutions.solon.com> wrote:
>ANSI/ISO C *requires* one of two declarations of main in a hosted environment.
>(In a freestanding environment, it is implementation defined whether or not
>there may be, must be, or is, a function named main.)  They both return int.
>One has no arguments, the other has two.

Implementations are free to accept other declarations of main().  Indeed,
the Rationale (not strictly part of the Standard, of course) says (p. 11)
"While many implementations support more than two arguments to main, such
practice is neither blessed nor forbidden by the Standard; a program that
defines main with three arguments is not _strictly conforming_."

So your program is not erroneous if it expects three arguments to main(),
as long as your compiler accepts it.  It just isn't necessarily portable
to any other system (or compiler).

So if you find a compiler that won't accept "int main(int, char **)",
you have grounds for a complaint.  If your compiler won't accept
"void main(int, char **, char **)", tough.  You're SOL unless there
is some other standard (like POSIX, perhaps) that requires it -- then
your program is limited to systems covered by that other standard.

Yes, I think programmers should remove unnecessary non-portabilities
from their code.  No, I don't think "void main()" should be the instant
flame-trigger it seems to be.

Gary Ansok
ansok@alumni.caltech.edu
Re: Pascal program works but not C program!
#1177
Author: Peter Seebach
Date: Thu, 11 Jan 1996 23:39
52 lines
3273 bytes
In article <4d3scf$4uk@gap.cco.caltech.edu>,
Gary E. Ansok <ansok@alumnae.caltech.edu> wrote:
>Implementations are free to accept other declarations of main().  Indeed,
>the Rationale (not strictly part of the Standard, of course) says (p. 11)
>"While many implementations support more than two arguments to main, such
>practice is neither blessed nor forbidden by the Standard; a program that
>defines main with three arguments is not _strictly conforming_."

>So your program is not erroneous if it expects three arguments to main(),
>as long as your compiler accepts it.  It just isn't necessarily portable
>to any other system (or compiler).

Which is to say, it is no longer strictly conforming, which is to say, it's
not really C anymore.  A merely "conforming" program (one acceptable to
at least *one* conforming C compiler) could be in FORTRAN; as long as the
compiler gives at least one warning, it can translate anything it wants.

The key is that the use of a non-standard declaration of main, just like
the use of strdup(), introduces "undefined behavior".

On at least one implementation I know of, you can use "gets(NULL)" as
a way to check a line of input without using a variable.  This is
also conforming code, and is *EXACTLY* as legal as "void main(void)" or
"int main(argc, argv, envp) char **argv, **envp;".

>So if you find a compiler that won't accept "int main(int, char **)",
>you have grounds for a complaint.  If your compiler won't accept
>"void main(int, char **, char **)", tough.  You're SOL unless there
>is some other standard (like POSIX, perhaps) that requires it -- then
>your program is limited to systems covered by that other standard.

Exactly; although, as I recall, POSIX does not support or encourage void
main, and envp has been deprecated in favor of the extern char *[]
environ.

>Yes, I think programmers should remove unnecessary non-portabilities
>from their code.  No, I don't think "void main()" should be the instant
>flame-trigger it seems to be.

I think it makes sense in a *C* newsgroup.  If you write conforming code,
you are SOL, and you may be making it gratuitously hard for people to help
you with your problems.  In general, problems with code that is not legit
ANSI are hardly ever problems having to do with the C language; they are
often problems with a given compiler or implementation, which means, they
should be addressed in a newsgroup more related to that implementation.

-s
-- 
Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
C/Unix proto-wizard -- C/Unix questions? Send mail for help.  No, really!
Using trn?  Weird new newsgroup problem?  I know the fix!  Email me!
The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
Re: Pascal program works but not C program!
#1181
Author: Lawrence Kirby
Date: Sat, 13 Jan 1996 15:11
61 lines
3446 bytes
In article <4d4742$n50@solutions.solon.com>
           seebs@solutions.solon.com "Peter Seebach" writes:

>In article <4d3scf$4uk@gap.cco.caltech.edu>,
>Gary E. Ansok <ansok@alumnae.caltech.edu> wrote:
>>Implementations are free to accept other declarations of main().  Indeed,
>>the Rationale (not strictly part of the Standard, of course) says (p. 11)
>>"While many implementations support more than two arguments to main, such
>>practice is neither blessed nor forbidden by the Standard; a program that
>>defines main with three arguments is not _strictly conforming_."
>
>>So your program is not erroneous if it expects three arguments to main(),
>>as long as your compiler accepts it.  It just isn't necessarily portable
>>to any other system (or compiler).
>
>Which is to say, it is no longer strictly conforming, which is to say, it's
>not really C anymore.  A merely "conforming" program (one acceptable to
>at least *one* conforming C compiler) could be in FORTRAN; as long as the
>compiler gives at least one warning, it can translate anything it wants.

The difference there is that void main() doesn't require a diagnostic
from the compiler. This doesn't really put it on a higher level though.

>The key is that the use of a non-standard declaration of main, just like
>the use of strdup(), introduces "undefined behavior".
>
>On at least one implementation I know of, you can use "gets(NULL)" as
>a way to check a line of input without using a variable.  This is
>also conforming code, and is *EXACTLY* as legal as "void main(void)" or
>"int main(argc, argv, envp) char **argv, **envp;".

or

 i = i++;

or

 double d;
 scanf("%f", &d);

>>Yes, I think programmers should remove unnecessary non-portabilities
>>from their code.  No, I don't think "void main()" should be the instant
>>flame-trigger it seems to be.
>
>I think it makes sense in a *C* newsgroup.  If you write conforming code,
>you are SOL, and you may be making it gratuitously hard for people to help
>you with your problems.  In general, problems with code that is not legit
>ANSI are hardly ever problems having to do with the C language; they are
>often problems with a given compiler or implementation, which means, they
>should be addressed in a newsgroup more related to that implementation.

Right. Starting a program with void main() is saying "I give free license
to the compiler to do whatever it likes with the code". Posting code in
comp.lang.c with (gratuitously) no defined meaning according to the C
language definitionjust adds noise to a newsgroup which already has plenty.

-- 
-----------------------------------------
Lawrence Kirby | fred@genesis.demon.co.uk
Wilts, England | 70734.126@compuserve.com
-----------------------------------------
Re: Pascal program works but not C program!
#1184
Author: Shadow.King
Date: Fri, 19 Jan 1996 00:51
14 lines
1058 bytes
: > 
: > void main(int argc,char *argv[])
:    ^-----------------------------This one too, but not too serious
: > {
: > }
: > 
: > 

This isn't really a problem, it's usually just the error code that the 
program returns to dos when it exits. As far as I know it doesn't HAVE to 
do this so just void main should be fine, I usually do the saem thing 
with simple programs that don't need to set the error level on exit and 
I've had no problems whatsoever with it. It is if the program is supposed 
to return an error level though.
Re: Pascal program works but not C program!
#1185
Author: Tanmoy Bhattacharya
Date: Fri, 19 Jan 1996 16:37
35 lines
2224 bytes
--text follows this line--
In article <4dmpv0$t77@fangz.com> shad@fangz.com (Shadow.King)
writes: 
<snip>
   : > 
   : > void main(int argc,char *argv[])
   :    ^-----------------------------This one too, but not too serious
<snip>
   This isn't really a problem, it's usually just the error code that the 
   program returns to dos when it exits. As far as I know it doesn't HAVE to 
   do this so just void main should be fine, I usually do the saem thing 

Why don't you make an attempt to know instead of trying to think:
especially when thought leads you to the wrong answer?

Please look at the FAQ available from rtfm.mit.edu if you do not find
any decent C book around. (And yes, I am aware that many books do get
it wrong.)

   with simple programs that don't need to set the error level on exit and 
   I've had no problems whatsoever with it. It is if the program is supposed 
   to return an error level though.

It is not fine to declare main as void: whether the program is simple,
complex or gobbledygook; if you want to write in standard C.

Cheers
Tanmoy
--
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
Re: Pascal program works but not C program!
#1186
Author: The Amorphous Mass
Date: Sat, 20 Jan 1996 01:56
31 lines
2327 bytes
On 18 Jan 1996, Shadow.King wrote:

> : > void main(int argc,char *argv[])
> :    ^-----------------------------This one too, but not too serious

> This isn't really a problem, it's usually just the error code that the 
> program returns to dos when it exits.
                     ^^^
  Most (all?) implementations of MS-DOS ignore the return codes from 
programs, but then MS-DOS puts a great deal of trust in the programmer 
not to blow the machine up.  By contrast DEC C for VMS, which manages memory 
very meticulously, refuses to compile void main() even without strict 
ANSI compliance set -- VMS _always_ wants to know whether the program 
completed without difficulty and that's all there is to it.  VMS tends to 
run on multi-user machines where error recovery (or hard crashes) could be 
rather costly.

> As far as I know it doesn't HAVE to 
> do this so just void main should be fine, I usually do the saem thing 
> with simple programs that don't need to set the error level on exit and 
> I've had no problems whatsoever with it. It is if the program is supposed 
> to return an error level though.

  In addition to the practical problems of not returning error codes, the 
ANSI C code defines main to return int.  Any C program that doesn't exhibits 
undefined behavior from the declaration of main() all the way to the end, 
as far as the C language is concerned.

/**James Robinson***********************            
  "If a fatal error occurs, the program should not be allowed to continue."
 -- Oracle Pro*C User's Guide         *************james-robinson@uiowa.edu**/
Re: Pascal program works but not C program!
#1187
Author: The MadMan
Date: Sun, 21 Jan 1996 13:23
29 lines
1525 bytes
In <4dmpv0$t77@fangz.com> shad@fangz.com (Shadow.King) writes: 
>
>: > 
>: > void main(int argc,char *argv[])
>:    ^-----------------------------This one too, but not too serious
>: > {
>: > }
>: > 
>: > 
>
>This isn't really a problem, it's usually just the error code that the

>program returns to dos when it exits. As far as I know it doesn't HAVE
to 
>do this so just void main should be fine, I usually do the same thing 
>with simple programs that don't need to set the error level on exit
and 
>I've had no problems whatsoever with it. It is if the program is
supposed 
>to return an error level though.

Wait a minute... void main() is NOT correct.  The startup code calls
exit_code = main() and expects a value to be returned to it (it is the
same as one of your routines being called by -   value = function()  
and the function being defined -   void function()   that doesn't
mesh).  Use the C lamguage correcly and it won't bite with little
errors like this.

Later, Jon
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads