From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c4d884378fc6b03c X-Google-Attributes: gid103376,public From: "Norman H. Cohen" Subject: Re: Two questions Date: 1996/11/08 Message-ID: <32835AB2.404B@watson.ibm.com>#1/1 X-Deja-AN: 195304120 references: <32825177.41C6@afit.af.mil> content-type: text/plain; charset=us-ascii organization: IBM Thomas J. Watson Research Center mime-version: 1.0 reply-to: ncohen@watson.ibm.com newsgroups: comp.lang.ada x-mailer: Mozilla 3.0 (Win95; I) Date: 1996-11-08T00:00:00+00:00 List-Id: Ding-yuan Sheu wrote: > (1) Is there an easy way to achieve the same result of the C++ sprintf > function in Ada? If you mean causing formatted output going to go to a string instead of to an output file, the answer is yes, there are versions of Put in the generic packages Ada.Text_IO.Integer_IO, Ada.Text_IO.Modular_IO, Ada.Text_IO.Float_IO, and so forth, that take a string rather than a file as a target. However, details of formatting are determined by optional parameters to Put rather than by a printf-style format string. Also, there is no Ada analog for C functions with a variable number of arguments of mixed type. If x and y are integers and you want to do the equivalent of sprintf(s,"%d %d",x,y), you must make two calls on Put and then concatenate the results (first trimming leading blanks, if this is important to you): Put (X_String, X); Put (Y_String, Y); Move (S, Trim (X_String, Left) & ' ' & Trim (X_String, Right)); -- Move and Trim are from Ada.Strings.Fixed > (2) In a C++ function, prorammers can declare a static local variable > to preserve its value between function calls. Can I do that in Ada? > For example, in C++, I can write a function as follows: > void strcount(char *str) > { > static int total =0; > int count = 0; > > while (*str++) count++; > total = total + count; > } Unlike C functions, Ada subprograms can be nested in an outer package or subprogram. Any variable that is to hold its value from one call on the subprogram to the next should be declared in a construct surrounding the subprogram. For example: package Character_Counts is procedure Count_Characters (Str: in String); function Total_Characters return Natural; end Character_Counts; package body Character_Counts is Total: Natural := 0; -- Hidden from all parts of the program outside this package. -- Retains its value between calls on Count_Characters. procedure Count_Characters (Str: in String) is begin Total := Total + Str'Length; end Count_Characters; function Total_Characters return Natural is begin return Total; end Total_Characters; end Character_Counts; This example is not all that different from declaring a static variable at the file level in C, but Ada also provides more flexibility, e.g. declaring several subpackages in a single file to minimize the region of the program in which the variable is visible, or enclosing a variable in a surrounding SUBPROGRAM (in which case the variable holds its value between calls on the inner subprogram that occur during one call on the outer subprogram, but the variable ceases to exist each time the outer subprogram returns). -- Norman H. Cohen mailto:ncohen@watson.ibm.com http://www.research.ibm.com/people/n/ncohen