• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

ProgEx01

Started by Frederick J. Harris, November 04, 2009, 02:10:31 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/* C ProgEx01.c

  In this first program (and all the others) use my directions for setting up a project in
  whatever development environment you are using.  For this program I named my project
  ProgEx01 and it is a C program.  The various environments work somewhat differently in
  terms of specifying whether a program is a C or C++ program.  C programs should end their
  source code files with a 'C' or 'c' extension, and C++ usually uses 'cpp'.
 
  However, name this file Main.C.  It is the one and only source file of project ProgEx01.

  Printing to the screen in a console program in C involves the infamous printf function.  It
  is considerably different from Basic's Print statement.  For example, lets say you have a
  variable in a Basic program named iVar and lets further say it is equl to five.  To print
  it out real nice you would do something like this...

  Print "iVar = " iVar

  ...and the output would look something like this...

  iVar = 5

  To do this in C the printf statement would look like this...

  printf("iVar = %u\n", iVar);

  ...and the output would look just as with the basic statement above.  If you look at that
  printf statement you'll see that it is comprised of two parts.  First there is a quantity
  in quotes, followed by a comma, and then the name of a variable to be printed.  The variable
  to be printed - here iVar - is pretty easy to understand, and that part at least seems to
  have an exact correlation with the Basic Print statement.  In terms of the quantity within
  the quotes, the 'iVar = ' part is pretty straight forward, but what's that crazy looking
  %u\n term?

  Within the quotes of a printf statement you'll find three different types of quantities that
  the function uses internally to format the output.  The three types of information are
  literal text, conversion specifiers, and escape sequences.

  Literal text is printed out exactly like in Basic.  In the printf statement above the
  quantity just to the right of the first quote - iVar = - will be output directly.

  Following the 'iVar = ' but still within the quotes is the '%u'  term.  That is a conversion
  specifier.  Conversion specifiers start with a '%' symbol and end with a letter.  Every time
  the printf function encounters a conversion specifier it looks for a variable in the variable
  list to the right of the first comma and outputs that variable's value according to the
  meaning of the letter term following the percent symbol.  Here the %u term specifies that iVar
  is to be output as an unsigned integer.  If it were %d that would inform the function that the
  variable is to be understood as a signed decimal integer.  The variable's value will be output
  at the position the % symbol exists within the format string.  Conversion specifiers must
  exist in a one to one relationship with variables to be output.  If there are three conversion
  specifiers within the format string, there must be three variables to the right of the string.
  In the case above there is only iVar.

  The next thing the function will hit after outputting the value of iVar is the forward
  slash character.  This is, again, a 'special' character to the function.  It is called an
  escape sequence, and it tells the function that the character following the slash is not
  necessarily to be printed, but rather, interpreted in terms of the special meanings asacribed
  to it by the function.  The 'n' character tells the function to insert a new line.  In Basic,
  this is done automatically by the Print statement, unless a semicolon is placed at the end of
  the line.  A \t is used to insert a tab character.  If one actually needs to insert a '\'
  character, it is done like this - \\.  This is necessary with file names.  Here is the first
  program.  Be sure to save it with a *.c extension - not *.CPP!
*/

#include <stdio.h>

int main(void)
{
char szName[]="Frederick J. Harris";         //an array of characters
unsigned int iAge=57,iWeight=210;            //unsigned integers
double dblHeight=5.9583;                     //floating point number

printf("  Name     %s\n",szName);            //%s is the conversion specifier for strings
printf("  Age      %u years\n",iAge);        //%u the conversion specifier for unsigned ints
printf("  Height   %1.4f feet\n",dblHeight); //%f for floating point numbers
printf("  Weight   %u pounds\n\n",iWeight);
printf("  %s\t%u years\t%1.4f feet\t%u pounds\n\n",szName,iAge,dblHeight,iWeight);
printf("  printf(\"Name     \%%s\\n\",szName);\n");
printf("  printf(\"Age      \%%u years\\n\",iAge);\n");
printf("  printf(\"Height   \%%1.4f feet\\n\",dblHeight);\n");
printf("  printf(\"Weight   \%%u pounds\\n\\n\",iWeight);\n");
printf("  printf(\"%%s\\t\%%u years\\t%%1.4f feet\\t%%u pounds\\n\",szName,iAge,dblHeight,iWeight);\n\n");
puts
(
 "  The \%%s, \%%u, and \%%f within the quotes are known as conversion\n\
 specifiers. They tell the function how the variables listed after\n\
 the quotes and seperated by commas (if there are more than one of\n\
 them) are to be interpreted.  In the case above szName[] is an array\n\
 of characters, and the \%%s tells the function to interpret it as a\n\
 NULL terminated string.  The \%%u stands for unsigned integer, and\n\
 of course my age is a good example of that.  The \%%1.4f tells the\n\
 function to display a floating point value with one digit to the left\n\
 of the decimal and four to the right.\n\n"
);
puts
(
 "  In the printf function within the quotes note the forward slash\n\
 characters.  This character is known as an 'escape sequence'.  It\n\
 informs the printf function that the character that follows will\n\
 have special meaning to the function, i.e., it is for the function's\n\
 use.  For example, the slash t's tell the function to print a tab.\n\
 The slash n's tell it to insert a new line."
);
getchar();

return 0;
}

/*
Output:

 Name     Frederick J. Harris
 Age      57 years
 Height   5.9583 feet
 Weight   210 pounds

 Frederick J. Harris   57 years        5.9583 feet     210 pounds

 printf("Name     %s\n",szName);
 printf("Age      %u years\n",iAge);
 printf("Height   %1.4f feet\n",dblHeight);
 printf("Weight   %u pounds\n\n",iWeight);
 printf("%s\t%u years\t%1.4f feet\t%u pounds\n",szName,iAge,dblHeight,iWeight);

 The %s, %u, and %f within the quotes are know as conversion
 specifiers. They tell the function how the variables listed after
 the quotes and seperated by commas (if there are more than one of
 them) are to be interpreted.  In the case above szName[] is an array
 of characters, and the %s tells the function to interpret it as a
 NULL terminated string.  The %u stands for unsigned integer, and
 of course my age is a good example of that.  The percent %1.4f
 tells the function to display a floating point value with one digit
 to the left of the decimal and four to the right.

 In the printf function within the quotes note the forward slash
 characters.  This character is known as an 'escape sequence'.  It
 informs the printf function that the character that follows will
 have special meaning to the function, i.e., it is for the function's
 use.  For example, the slash t's tell the function to print a tab.
 The slash n's tell it to insert a new line.
*/

Jeff Blakeney

You might have mentioned that printf mean "print formatted" and that there are other print statements available in C/C++.  Also, you could have mentioned that printf is similar to PRINT USING$ with PowerBASIC.  Your C example of printf("iVar = %u\n", iVar) could be translated to PB as PRINT USING$("iVar = #", iVar).

Frederick J. Harris

Hi Jeff!

    Yea, there's a good bit more that could be said about printf().  When I get time (or you could too!) I'll add a few details about the various other useful features of printf, such as justification, number padding, etc., kind of like PRINT USING (one of my all time favorite BASIC statements going back to the early DOS days!).  However, in this series of program examples I just wanted to whet some appetites, so to speak.  Show folks who don't do C or C++ that its not that hard to get started, what with really neat free downloadable compilers, all the info on the net, and so on.  So for these first dozen or so programs I didn't want to get into too much detail, especially for those PowerBASIC programmers whose main interest might just be a desire to get a better feel for the variable types and usages of C so they could better understand the Api documentation.