Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label Print “javaabhigyan” with empty main() in C. Show all posts
Showing posts with label Print “javaabhigyan” with empty main() in C. Show all posts

Sunday, 2 June 2019

Print “javaabhigyan” with empty main() in C


Write a program that prints “javaabhigyan” with empty main() function.You are not allowed to write anything in main().


1.) One way of doing this is to apply GCC constructor attribute to a function so that it executes before main()

#include <stdio.h> 
  
/* Apply the constructor attribute to myStartupFun()  
   so that it is executed before main() */
void myStartupFun(void) __attribute__((constructor)); 
  
/* implementation of myStartupFun */
void myStartupFun(void) 
{ 
    printf("javaabhigyan"); 
} 
  
int main() 
{ 
} 

Output:
javaabhigyan


2.) In linux, just override the default definition of _start() function so that it would work as a custom startup code. See this article to understand more.

#include <stdio.h> 
#include <stdlib.h> 
  
int main(void) 
{ 
} 
  
// _start() function 
void _start(void) 
{ 
    printf("javaabhigyan"); 
  
    // Call main() function 
    int var = main(); 
    exit(var); 
} 

Now compile this by following command

gcc -nostartfiles -o file file.c

Output:
javaabhigyan