1

Where does these functions comes from usually ?

Compiler

Include files

DLL

??

#include <stdio.h>

int f(int a) {
    int b=5;
    char c;

    c = a + b;
    return c;   
}

int main(int argc, char **argv) {

    int a = 3;
    int b;

    b = f(a);   
    printf("Result: %d\n",b);
}

enter image description here

1 Answer 1

1

The Compiler does not add any functions

think how printf() can work ? where can it print ? does your program know or do you allocate a console for printf() to print ? do you know where the main returns to?

no you don't and neither you coded those functions but for proper functioning of your code you need a console window and after your program finishes and wants to return it needs to know where to return to and how to de allocate all the memory , consoles , handles it uses

all these job are done by CRT code (c run-time initialization code) most of those code are in the include file stdio.h (this header includes other needed files )

the compiler then compiles all this code into a single executable

you can also write explicit code and remove all these functions and provide an entry point

here is an example of explicit code that allocates and writes to the allocated Console and Exits using the algo in your query

#include <windows.h>
int f(int a) { return a + 5; }
void main(void) {
    if ( !AllocConsole()) { goto door; }
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
    if (out == INVALID_HANDLE_VALUE) { goto door; }
    char buff[20] = { 0 };  DWORD cwritten = 0;
    wsprintf(buff, "Result: %d\n", f(3));
    WriteConsole(out, buff, sizeof(buff), &cwritten, NULL);
    Sleep(10000);
door:
    ExitProcess(0);
}

compiled and linked in vs 2017 cmdprompt

compiled with cl /Zi /W4 /analyze /GS- /Ox 
linked with /release /nodefaultlib /subsystem:windows /entry:main kernel32.lib user32.lib

here is a screen shot of function calls from ida free 5 enter image description here

you can double click the exe and it will work no need to run this from cmd.exe

enter image description here

1
  • Thanks a lot for another example blabb , i would like to learn more about win 32 programming and how its functions are included in the program .Maybe i should continue looking for some examples of win 32 programming :)
    – KpaK
    Commented May 7, 2018 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.