Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a C program to print 'ABCD' repeatedly without using loop, recursion and any control structure
In this problem, we have to write a program in C that will print a string 'ABCD' repeatedly without using loop, recursion and any control structure.
So, we will have to call or run the same block of code infinite times but without using loop, recursion or control structure which are the most common methods to perform the task. For this, we will run the same program multiple times instead of looping. This will perform our task within the given constraints. The system() method can be employed inside the code that will call the program infinite times.
Syntax
int system(const char *command);
Approach: Using system() Function
We will pass the executable file name to the system() method to run the program repeatedly −
Note: To compile and run this program, save it as
main.c, compile withgcc main.c -o main, then run./main(Linux/Mac) ormain.exe(Windows).
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("ABCD\t");
fflush(stdout); /* Ensure output is displayed immediately */
system("main"); /* Call the same program again */
return 0;
}
Output
ABCD ABCD ABCD ABCD ABCD ABCD ABCD ABCD ABCD ABCD ... (The program will print ABCD infinite times until you stop the program execution)
How It Works
- The program prints "ABCD" followed by a tab character
-
fflush(stdout)ensures the output is displayed immediately -
system("main")executes the compiled program again - This creates an infinite chain of program executions
- Each execution prints "ABCD" and spawns a new process
Key Points
- This approach uses process spawning instead of traditional loops
- The executable name passed to
system()must match your compiled program name - Use Ctrl+C to terminate the infinite execution
- Each call creates a new process, which may consume system resources
Conclusion
Using the system() function provides a creative solution to print repeated output without loops, recursion, or control structures. However, this approach should be used cautiously as it spawns infinite processes.
