Write a c program to print "Hello" without using main() function.

Ram Pothuraju

 In C, the main() function is the entry point of the program, so it is not possible to run a C program without a main() function. However, there is a non-standard way to write a program that appears to not use main() by utilizing the __attribute__((constructor)) function attribute in GCC and Clang.


Here's an example program that prints "Hello" using this attribute:



#include <stdio.h>


void print_hello() __attribute__((constructor));


void print_hello()

{

    printf("Hello\n");

}



In this program, we declare a function print_hello() and specify the __attribute__((constructor)) attribute, which tells the compiler to call this function automatically at program startup, before main() is called. Inside the print_hello() function, we use the printf() function to print "Hello".

When we compile and run this program, it will print "Hello" without explicitly calling main(). However, it is important to note that this is non-standard and platform-specific behavior, and may not work on all compilers or platforms. It is generally recommended to write C programs using the standard main() function.
Tags

Post a Comment

0Comments

Post a Comment (0)