www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/02/24/19:30:53

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: More easy questions
Date: Tue, 24 Feb 1998 19:16:44 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 122
Message-ID: <34F362EC.78BF@cs.com>
References: <3 DOT 0 DOT 5 DOT 32 DOT 19980224222117 DOT 0079aa50 AT vip DOT cybercity DOT dk>
NNTP-Posting-Host: ppp207.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Nils Emil P. Larsen wrote:
> 
> - The clib.inf contains info about all the functions like open, close, read
> - but there is nothing about the keywords like for, then, if, char, int (in
> rhide marked with white). Where on the Internet can I get info about the
> keywords? There is lot/enough documentation for all the other functions.
> Please help me!

The purpose for the library documentation is to teach you about the C
library.  'for', 'if', 'char', etc., are not functions but reserved
words that form part of the basic syntax of the language.  This compiler
(and indeed, other compilers) is not designed to teach you how to write
code.  It is designed to compile it.

You need to find a good reference book.  There are plenty of them
available; one of the most recommended is Kernighan and Ritchie's _The C
Programming Language_.  I also enjoy the Waite Group's _New C Primer
Plus_, which is probably easier for the beginner although no less
authoritative.  You can find reference materials on the net but they are
somewhat scattered and haphazard.  One place I look often for general
advice is the comp.lang.c FAQ
(http://www.eskimo.com/~scs/C-faq/top.html), but this is not designed to
teach, merely to advise.  I have also heard of a university that offers
online courses in programming.

I should stress that this is NOT an appropriate topic for this
newsgroup/mailing list.  A better place to go would be
<news:comp.lang.c> or <news:comp.lang.c.moderated>, where you'll find a
great deal more advice.

> - Is it correct that the keyword "if" is used like this: if (expression)
> statement1; else statement2;. If expression is nonzero statement1 is
> executed. If expression zero is statement2 executed. In BASIC I can compare
> to expressions - is this impossible in C? This example dosen't work i my
> way: if (5 = 5) then do_something; else do_something_else;. In BASIC if 5=5
> then do_something else do_something_else would work in my way. Please give
> me an explanation or a possible solution.

The comparison operator in C is '==', not '='.  This confuses many
people and causes many programming bugs, but it's there and that's all
there is to it.  The reasoning is that you can use both assignment ('=')
and comparison ('==') in the same expression; there's nothing mystical
about conditional expressions in C like there is in other languages.

For example, the following code is perfectly valid syntactically:

   int i = 10;
   if ( i = 5 )
       printf( "true\n" );
   else
       printf( "false\n" );

However, it results in erroneous output because the expression "i = 5"
assigns 5 to i, instead of comparing them.  Since the assignment
operator returns the result of the assignment--in this case, 5--the
expression is evaluated as true.  The correct code for the above
situation follows:

   int i = 10;
   if ( i == 5 )
       printf( "true\n" );
   else
       printf( "false\n" );

The '=' vs '==' ambiguity is present because it makes possible code like
the following:

   FILE *fp;
   if ( ( fp = fopen( "hello.txt", "r" ) ) == NULL )
       perror( "hello.txt" );

This code opens the file "hello.txt" for reading, assigns the results to
the file pointer fp, checks to see if the operation succeeded, and
displays an error message if it did not, all in two lines of code (not
counting the variable definition).  That's the efficiency of the C
language.  But don't mix up the operators!

> - How do I convert a ASCII character into a string? I think it may be
> something like this: string1 = ???(13); and it will but the next line
> character into string1. What is ??? ?

There is no such thing as a "string" in C, or at least, there is no
distinct data type that specifically represents one.  Strings are
represented as arrays of char, which are themselves merely an elaborate
kind of pointer-to-char.  To place a character into a string, you assign
it to an array element of the string, or use library functions like
strcpy(), strcat(), etc.  Note that space must be allocated to store the
character(s) in memory; the following will NOT work:

   char *s;
   s[0] = 'a';  /* alternative:  *s = 'a'; */

Since s is not initialized to anything--it has no memory associated with
it--such an assignment will probably cause your program to crash. 
Correct:

   char s[10];
   s[0] = 'a';
   s[1] = '\0';  /* required to terminate string */

Alternative:

   char s[10];
   sprintf( s, "%c", 'a' );  /* see library docs for sprintf() */

It is possible to use the address-of operator ('&') on a char variable,
and treat the result as a string, but this can have severely unintended
consequences because of the way the C language delineates strings.

> Thank you very much again - these things would take me hours if you don't
> help me!

They will take hours anyway.  Learning ain't cheap.  It doesn't have to
be difficult, though - get a good textbook!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "If 'everybody knows' such-and-such, |
|       aka Fighteer I       | then it ain't so, by at least ten    |
|   mailto:fighteer AT cs DOT com   | thousand to one."                    |
| http://www.cs.com/fighteer |                 - Lazarus Long       |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019