Introduction
In this guide, we will learn how to track and display stock prices using an API in C programming. By leveraging stock market APIs, you can retrieve real-time stock data, such as the current price of a stock, its opening price, and more. We will focus on using a stock price API that allows fetching data in real time.
Objective
The goal of this program is to:
- Retrieve real-time stock price data from an API.
- Parse and display the relevant stock information on the screen.
- Use a simple and effective C program to fetch and show stock prices.
Code
#include
#include
#include
#include <curl/curl.h>
#define API_KEY "your_api_key" // Replace with your API key
#define STOCK_SYMBOL "AAPL" // Replace with the desired stock symbol
// Function to write the response data into a string
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) {
size_t total_size = size * nmemb;
strncat(userp, contents, total_size);
return total_size;
}
void get_stock_price(const char *symbol) {
CURL *curl;
CURLcode res;
char url[256];
char response[1024] = ""; // Buffer to store the response
// Construct the URL with the stock symbol and API key
snprintf(url, sizeof(url), "https://api.example.com/stock/%s?apikey=%s", symbol, API_KEY);
// Initialize CURL
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
// Set the URL for the request
curl_easy_setopt(curl, CURLOPT_URL, url);
// Set the function to write the response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);
// Perform the request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
} else {
printf("Stock Data: %s\n", response); // Display the response
}
// Clean up
curl_easy_cleanup(curl);
}
curl_global_cleanup();
}
int main() {
printf("Fetching stock price for symbol: %s\n", STOCK_SYMBOL);
get_stock_price(STOCK_SYMBOL);
return 0;
}
Explanation
This C program fetches the stock price of a specified symbol using a stock market API. Here’s an overview of the main components:
- API_KEY: Replace this with your actual API key from the stock market provider.
- STOCK_SYMBOL: The stock symbol for which you want to track the price (e.g., AAPL for Apple).
- WriteCallback function: This function is used to store the API response into a string, which will later be processed or displayed.
- get_stock_price function: This function constructs the API URL, initializes the CURL library, performs the API call, and prints the response to the screen.
- curl_easy_setopt and curl_easy_perform: These are the key CURL functions that set options and perform the HTTP request.
How to Run the Program
- Install CURL library on your system (if not already installed).
- Replace the API_KEY with a valid key from the API provider (such as Alpha Vantage, Yahoo Finance, etc.).
- Set the desired stock symbol in STOCK_SYMBOL.
- Compile the C program using a C compiler, e.g., gcc stock_tracker.c -o stock_tracker -lcurl.
- Run the program using ./stock_tracker in your terminal.
- The program will output the stock data in the console, depending on the response format from the API.