0

I am trying to learn C language using Visual Studio 2019 and tried the below code

#include <stdio.h>
void f1();
# pragma startup f1
int main()
{
    printf("Hey");
    return 0;
}
void f1()
{
    printf("Hello1\n");

}

I was expecting the output to be
Hello1
Hey

but I am getting
Hey

Looking forward to inputs on what I am doing wrong here.

Thanks

Alok
  • 368
  • 3
  • 13
  • Did you compile it with MSVC or gcc? Don't compile it with gcc, obviously. – Lundin Nov 04 '21 at 10:26
  • https://stackoverflow.com/questions/15314039/is-pragma-directive-compiler-dependent – Mat Nov 04 '21 at 10:27
  • As mentioned in my question, I am using Visual Studio 2019, so I believe it would be Microsoft compiler in the background. Not sure which compiler it chooses as I just click the run button to check the output. – Alok Nov 04 '21 at 10:27
  • 4
    If you're learning C, forget `#pragma`, you don't need pragma directives for the moment. Pragmas are an advanced topic. – Jabberwocky Nov 04 '21 at 10:28
  • https://learn.microsoft.com/en-us/cpp/c-language/c-pragmas?view=msvc-160 – Mat Nov 04 '21 at 10:29
  • 4
    Microsoft C compiler does not know about the startut pragma and silently ignores it. Pragmas are only intended to request special processing to *specific compilers*. Do not use them if you are learning C. – Serge Ballesta Nov 04 '21 at 10:33
  • 1
    @SergeBallesta The other way around. `# pragma startup` is known by MSVC and Embarcadero and probably a few others, but not by gcc. – Lundin Nov 04 '21 at 10:34
  • @Lundin: AFAIK, Visual Studio has no support for gcc, but it can use Clang as a back end. The libraries are still Microsoft specific, but at least the language part is closer to the standard... – Serge Ballesta Nov 04 '21 at 10:35
  • 1
    @Lundin: `#pragma startup` may have been supported by older versions but it is not referenced in current Microsoft documentation. And I can confirm that MSVC2019(v142) does ignore it (as do the CLang backend). – Serge Ballesta Nov 04 '21 at 10:41
  • @SergeBallesta So which compiler _is_ it supposed to be used for? Embarcadero only? – Lundin Nov 04 '21 at 11:01
  • 2
    @Alok Which friendly manual told you to use `# pragma startup`, more precisely? – Lundin Nov 04 '21 at 11:02
  • A book named "Let us C" talks about pragma startup and I was trying it out to learn more about it. – Alok Nov 04 '21 at 11:07
  • @SergeBallesta I just tested the pragma in older MSVC through WINE/Godbolt and none of them seem to recognize the pragma indeed. So it is neither for MSVC nor gcc, nor clang, apparently. – Lundin Nov 04 '21 at 11:08
  • 1
    @Alok Ah, from what I remember, that book has a big red anti-recommendation from various C gurus using this site, including Antii Happila who took some time to read the thing, see revision 43 of this book list: https://stackoverflow.com/posts/562377/revisions. Antii is a well-known contributor to this site and knows what he is talking about. While Yashwant Kanetkar is just some random dude who apparently shouldn't be writing books. – Lundin Nov 04 '21 at 11:15
  • 1
    @Alok: then my advice is that you should look for a better book... Beginners should never try to use pragmas. – Serge Ballesta Nov 04 '21 at 11:16
  • Thank you all for your suggestions and inputs. – Alok Nov 04 '21 at 11:18
  • @Alok throw away that book and get a book from that list: https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list/562377#562377 – Jabberwocky Nov 04 '21 at 11:19

1 Answers1

2

The example you are using will only work in Borland/Embarcadero compilers, something the author of that book should have clarified for you. This pragma will not work with MCVS, gcc and clang compilers.

For gcc-like compilers, you can use this compiler extension instead:

void f1 (void); 

void __attribute__((constructor)) f1 (void) 
{ 
  puts("Hello world"); 
}

Regarding Let us C, it turns out you are reading a harmful book written by an incompetent author. From SO's C book recommendation list:

Also do not use the book Let Us C (16th Edition, 2017) by Yashwant Kanetkar. Many people view it as an outdated book that teaches Turbo C and has lots of obsolete, misleading and incorrect material. For example, page 137 discusses the expected output from printf("%d %d %d\n", a, ++a, a++) and does not categorize it as undefined behaviour as it should. It also consistently promotes unportable and buggy coding practices, such as using gets, %[\n]s in scanf, storing return value of getchar in a variable of type char or using fflush on stdin.

This book has been read and reviewed by at least two well-known and respected C gurus on this site and the claims above show that this book is not just outdated - it is spectacularly bad. Counting yours sincerely as well, there's now at least three 100k+ rep C tag users of SO giving a big fat anti-recommendation against this book.

You need to stop reading this harmful book immediately. Please show this post to your teacher.

Lundin
  • 195,001
  • 40
  • 254
  • 396