Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7042671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:11:48+00:00 2026-05-28T02:11:48+00:00

How do you call scanf from Ada? That is, presumably with an appropriate pragma

  • 0

How do you call scanf from Ada? That is, presumably with an appropriate pragma import declaration, but what would the declaration look like?

(I’m interested in how to call C functions of the more unruly variety from Ada, not how to parse strings per se, so I’m not looking for a pure Ada solution. My setup is Gnat, Ubuntu Linux, x64 if it makes a difference.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:11:48+00:00Added an answer on May 28, 2026 at 2:11 am

    This paper points out that

    Ada has no way of declaring a function that takes different numbers of parameters of different types. One could declare a set of “printf” functions which take a string, a string and an integer, a string and a floating point number, a string and 2 integers, and so on, and then declare each one to be Import (C)2. But this requires lots of declarations, one for each different kind of use in the application program, so it really isn’t practical.

    The same would be true of scanf(), which with Ada 2012 has the added bonus of letting you choose between out and access parameter specs (in earlier revisions, you had to use access because functions weren’t allowed to have out parameters).

    In addition, I don’t believe it’s required that the C compiler has to use the same parameter passing mechanisms for variadic functions as it does for ordinary ones (the reference hints at this, and I recall but can’t now find a recent conversation on these lines).

    That said, here’s an example which appears to work fine on Mac OS X with GCC 4.6.0:

    with Ada.Text_IO; use Ada.Text_IO;
    with Interfaces.C; use Interfaces.C;
    procedure Variadic is
       function Scanf (Fmt : char_array; Result : access int) return int;
       pragma Import (C, Scanf, "scanf");
       Status : int;
       Result : aliased int;
    begin
       Status := Scanf (To_C ("%d\n"), Result'Access);
       Put_Line ("status: " & int'Image (Status));
       if Status = 1 then
          Put_Line ("result: " & int'Image (Result));
       end if;
    end Variadic;
    

    (not sure about the \n in the format parameter!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I call this a flash meeting, but maybe there is another more appropriate name.
I'm puzzled as to how the header, return and call would look. This is
I am trying to call a method from another class with list but it
Call me a 'n00b', but I am new to creating Script Controls. I want
I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
I call the following function with a mouseover event but it's not working. My
I call addNotify() method in class that I posted here. The problem is, that
I call a certain div from another page with jquery to be loaded into
I am trying to trace a high level function call that blocks a certain
Ok, so I have a 2D Array that is initialised with values from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.