Integer with 32 bits.

How can I create a integer with 32 bits?
And how can I use scanf and printf with that?
Thanks!
An integer is, by its nature, 32 bits. Note that 4 bytes=32 bits.
Is it a long? or anything?
My doubt is how I can represent it in scanf and printf (like %d for int).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <cstdint>
#include <cinttypes>
#include <cstdio>
#include <iostream>

int main()
{
    // http://en.cppreference.com/w/cpp/header/cstdint
    std::int32_t a ; // signed integer type, exactly 32 bits (if available)
    std::int_fast32_t b ; // fastest available signed integer type, at least 32 bits
    std::int_least32_t c ; // smallest available signed integer type, at least 32 bits

    // http://en.cppreference.com/w/cpp/header/cinttypes
    std::scanf( "%" SCNd32 " %" SCNdFAST32 " %" SCNdLEAST32, &a, &b, &c ) ; // decimal
    std::printf( "%" PRId32 " %" PRIdFAST32 " %" PRIdLEAST32 "\n", a, b, c ) ;

    std::cin >> a >> b >> c ;
    std::cout << a << ' ' << b << ' ' << c << '\n' ;
}
Last edited on
An int is NOT guaranteed to be 32 bits. If you want a 32-bit integer then #include <cstdint> and use int32_t: http://www.cplusplus.com/reference/cstdint/

You can scan/print an int like this:
1
2
3
4
int i;
if (scanf("%d", &i) == 1) {
    printf("The int scanned is %d\n", i);
}


scanf returns the number of items scanned. It's very easy to mess up the arguments and C++ purists will skin you alive for even mentioning it.



scanf is complicated enough to please even the C++ purists!
Thanks!
Topic archived. No new replies allowed.