0

Warning: This query also contains specifics about MSVC compiler.(If you find it important to change the tags feel free to do so)
I used vscode(dev cmd-invoked) integrated terminal to compile.
cl {filename} to compile and
{filename} to run

Question
As once I was wondering how printf works. So, I sat down and tracked down the code for printf and shortened it as much as possible.

But now I hit a dead-end. there are so many functions and macros I had no idea existed. But that's not the problem I'm stating here. The problem is: I came across some function that I never found its source code in any of the header files. (I've came across files like these before but always ignored, but now its time that I learn about it..)

int main()
{
// printf("Printf base code search\n");
char* _ArgList;
char* _Format = "Hello Printf!!";
_ArgList = (char*)&(_Format) + ((sizeof(_Format) + sizeof(int) - 1) & ~(sizeof(int) - 1));
_vfprintf_l(__acrt_iob_func(1), _Format, (void*) 0, _ArgList); 
}

I have extracted the macros from multiple files(~ >5) and put it in the required place to minimize code lines. Now I can print any string with this code. There are two functions at the last line:

  1. _vfprintf_l()
    I learnt that it is responsible for the actual printing of text.
  2. __acrt_iob_func(1)
    I later found out from stack overflow itself that it's not a function but 'stdout'(I don't have details)

But I couldn't find any details about it on any header files or internet.
So, I'll appreciate if I get some information about these
Q1. _vfprintf_l(). How is it declared and where? Why can't I find it's source code. Is it because it's inside a dll file? If yes, then which one? I learnt as much as I can from here (microsoft docs about it)
Q2. __acrt_iob_func(1). How is it defined and where?..
Q3. Where are all the built-in types like int, char are defined in MSVC. I read the documentation from here (MS docs)
and here, here (stackoverflow links) Where are these types defined. Is it defined using C or is it defined using Assembly language?..
Q4. Since I haven't included ANY header files but how does the C built-in data types and the built-in functions like(sizeof() and _vf_printf_l() are getting linked. (other compiler users can also share how it happens on there too).

This question is most recent among these.
Q5. in stackoverflow this answer says that most cpu's support these data types directly. Does that mean built-in data types are programmed in machine language all the time or we can get by using a high-level language(doesn't seem likely)? If I were to build a new data type(so basic that I can't use the fundamental data types defined in c to build it) Will I need to use assembly instructions to build that data type. If I were successful in creating the above data type then, How would I link it with the program as an external data type. How can I link it as a built-in data type(so that I don't have to include or link any file for that

Let me tell you I have done my homework on it for 2 days with 0 or low clarity. Even after that if you think I need to read a info source, I will still appreciate, but please keep that in mind
I'm in no hurry. Please answer as much as you can.

Update:
All these questions can be summarized/answered into/from one question.
-> How and where the built-in data types are defined. so that it is able to successfully link to a code without linking another file(like int) and how can a programmer add his own BUILT-IN data-type.
<--The End-->
(I know It's not necessary, I'm asking if any way I can start learning about these, If it is a separate topic altogether then knowing the name of it would be even helpful.)

Francis
  • 54
  • 4
  • _"_vfprintf_l(). How is it declared and where?_" - [`_vfprintf_l` @ learn.microsoft.com](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/vfprintf-vfprintf-l-vfwprintf-vfwprintf-l?view=msvc-170) – Ted Lyngmo Feb 19 '22 at 03:08
  • 1
    You are asking questions that can't be answered in the space of a Stack Overflow answer box. There are _entire university courses_ about this stuff. You _can_ teach yourself, but online sources won't get you very far, you need to visit a physical library and get your hands on some textbooks. I'd suggest you start with [this one](https://csapp.cs.cmu.edu/) and [this one](https://www.cs.princeton.edu/~appel/modern/c/) and [this one](https://craftinginterpreters.com/). – zwol Feb 19 '22 at 03:10
  • Typically the big Visual Studio comes with the full source of the CRT. If you create a debug build, and from the VS debugger steps into any CRT function you should come to its source. I don't know if it works for VSCode. – Some programmer dude Feb 19 '22 at 03:10
  • 1
    It looks like you've tried to inline `printf`. That doesn't work because the `...` parameter to `printf` cannot be inlined. The macros are pulling sneaky tricks with reading the parameters off the call stack, but inlining the function means that there is no activation frame from which to pull the parameters. – Raymond Chen Feb 19 '22 at 03:15
  • SO is a question and answer site. Note that *question* is singular, not plural. Numbered lists of questions are not acceptable. In addition, you've asked about several different topics in the same post, which also is not appropriate. You may want to spend some time reading the [help] pages to better understand how the site works. – Ken White Feb 19 '22 at 03:47
  • If you want to learn how `printf` works, based on smaller, simpler examples, I can recommend [question 15.4](http://www.c-faq.com/varargs/varargs1.html) in the [C FAQ list](http://www.c-faq.com/), and [section 25.2](https://www.eskimo.com/~scs/cclass/int/sx11b.html) in [these C course notes](https://www.eskimo.com/~scs/cclass/int/top.html). – Steve Summit Feb 19 '22 at 05:08

1 Answers1

1

Q1. _vfprintf_l(). How is it declared and where? Why can't I find it's source code. Is it because it's inside a dll file? If yes, then which one? I learnt as much as I can from here (microsoft docs about it)

Q2. __acrt_iob_func(1). How is it defined and where?..

I cannot speak to these two identifiers specifically, but ultimately the implementations of printf and I/O buffer functions are going to be in compiled code that Microsoft does not publish source code for.

Q3. Where are all the built-in types like int, char are defined in MSVC. I read the documentation from here (MS docs) and here, here (stackoverflow links) Where are these types defined. Is it defined using C or is it defined using Assembly language?..

There is no single definition of int or char. The behaviors of these types arise from combinations of declarations in header files, behaviors built into the compiler, and the processor. For example, a target processor may have a variety of add instructions that add 8-bit, 16-bit, or 32-bit integers or 32-bit or 64-bit floating-point values, and it may have compare instructions that compare, for example, 32-bit integers and store information about the results in a set of flags or a register, and it may have conditional branch instructions that use those flags in different ways to effect signed comparisons or unsigned comparisons. In other words, implementing the C source code a < b may be done not with a single instruction but with a compare followed by a conditional branch, and the choice of which conditional branch to use depends on whether a and b are signed or not. These choices are built into the compiler.

Q4. Since I haven't included ANY header files but how does the C built-in data types and the built-in functions like(sizeof() and _vf_printf_l() are getting linked. (other compiler users can also share how it happens on there too).

Each C implementation includes object or library files that contain compiled code, and commands to build executable files often include these library files by default. If you are using an IDE, it may include the library files by default when linking.

Q5. in stackoverflow this answer says that most cpu's support these data types directly. Does that mean built-in data types are programmed in machine language all the time or we can get by using a high-level language(doesn't seem likely)? If I were to build a new data type(so basic that I can't use the fundamental data types defined in c to build it) Will I need to use assembly instructions to build that data type. If I were successful in creating the above data type then, How would I link it with the program as an external data type. How can I link it as a built-in data type(so that I don't have to include or link any file for that

Types are abstract concepts that are implemented through a variety of means. For example, there are not single assembly instructions that implement 16-bit signed integers. If short is a 16-bit signed integer in a C implementation, then it is effected in how the compiler interprets the source code, how it generates assembly language, how the processor executes instructions and more. Essentially, if the goal is to make a 16-bit signed integer, it is a programming exercise of the form “Make the software behave like this,” and then programmers write software to do that. To make a new type, you would have to rewrite parts of the compiler, at least.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312