Introduction

In this tutorial, we will demonstrate how to write a C program that displays a random quote from a predefined list of quotes.
This program uses basic features of the C programming language such as arrays, the rand() function, and simple control structures.
The purpose of this program is to give you a practical example of how to generate random output, which can be useful in many applications.

Objective

The objective of this program is to generate a random quote from a list of quotes each time it is run. The program will:

  • Define a list of motivational or inspirational quotes.
  • Use the rand() function to select a random quote.
  • Display the selected quote on the screen.

Code

#include 
#include 
#include 

int main() {
    // Define an array of quotes
    const char *quotes[] = {
        "The only way to do great work is to love what you do. - Steve Jobs",
        "Success is not the key to happiness. Happiness is the key to success. - Albert Schweitzer",
        "Don't watch the clock; do what it does. Keep going. - Sam Levenson",
        "The best time to plant a tree was 20 years ago. The second best time is now. - Chinese Proverb",
        "It always seems impossible until it's done. - Nelson Mandela"
    };

    // Initialize random number generator
    srand(time(NULL));

    // Generate a random index
    int randomIndex = rand() % 5; // Since there are 5 quotes

    // Display the random quote
    printf("Random Quote: \n%s\n", quotes[randomIndex]);

    return 0;
}

Explanation

The program begins by defining an array of quotes. Each quote is stored as a string in the array quotes[].
We use the rand() function to generate a random index from the array. The expression rand() % 5
ensures that the random number will be between 0 and 4, which corresponds to the indices of the quotes array.

The srand(time(NULL)) statement initializes the random number generator with the current time, ensuring that the random sequence differs each time the program runs.

Finally, the selected quote is printed to the console using printf().

How to Run the Program

  1. Write the C code in a text editor (such as Notepad++ or Visual Studio Code) and save the file with a .c extension (e.g., random_quote.c).
  2. Open a terminal or command prompt window.
  3. Navigate to the directory where you saved your C program file.
  4. Compile the program using a C compiler. If you’re using GCC, the command is:
    gcc random_quote.c -o random_quote
  5. Run the compiled program:
    ./random_quote
  6. You should now see a random quote displayed in your terminal or command prompt window.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)