How to portably print a int64_t type in C

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to portably print a int64_t type in C

How to Portably Print an int64_t Type in C 😎

Are you working with the C99 standard and dealing with the int64_t type? Fantastic! But wait, are you struggling with printing these types portably without compiler warnings? Don't worry, we've got you covered! πŸš€

The Problem πŸ€”

Let's take a look at the code snippet you posted:

#include <stdio.h>
#include <stdint.h>

int64_t my_int = 999999999999999999;
printf("This is my_int: %I64d\n", my_int);

You mentioned that you receive a compilation warning:

warning: format β€˜%I64d’ expects type β€˜int’, but argument 2 has type β€˜int64_t’

Sounds like a typical problem! The %I64d format specifier is specific to certain compilers and platforms (like Windows), but it's not portable across different systems. 😟

Easy Solutions πŸ’‘

Here are a few simple solutions to help you print your int64_t variable without any hassle:

Solution 1: "PRI" Macros from <inttypes.h> πŸ’ͺ

C99 introduced the <inttypes.h> header, which defines format macros for different integer types. These macros make it easier to print integer types in a portable way. Let's modify your code:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int64_t my_int = 999999999999999999;
printf("This is my_int: %" PRId64 "\n", my_int);

No more warning! πŸŽ‰ Using the PRId64 macro ensures that the correct format specifier is used for your int64_t variable, regardless of the platform and compiler you're using.

Solution 2: Casting to long long πŸ™Œ

If you don't have access to the <inttypes.h> header or prefer a simpler solution, you can cast your int64_t variable to long long and use the %lld format specifier. Here's what it looks like:

#include <stdio.h>
#include <stdint.h>

int64_t my_int = 999999999999999999;
printf("This is my_int: %lld\n", (long long) my_int);

This workaround helps you avoid the compiler warning and ensures consistency across different systems.

πŸŽ‰ You Did It! πŸŽ‰

Now you know how to print an int64_t type in C without encountering those pesky compiler warnings! Give yourself a pat on the back! πŸ‘

Your Turn! πŸ“

Did these solutions work for you? Have any other ideas or questions? Let us know in the comments section below! We're here to help you out. πŸ”

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

πŸ”₯ πŸ’» πŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! πŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings πŸ’₯βœ‚οΈ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide πŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? πŸ€” Well, my