Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 150525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:18:16+00:00 2026-05-11T09:18:16+00:00

I don’t get why I get 0 when I use printf and %d to

  • 0

I don’t get why I get 0 when I use printf and %d to get the size of my vector:

vector<long long> sieve; int size; ... //add stuff to vector ... size = sieve.size(); printf('printf sieve size: %d \n', size); //prints 'printf sieve size: 0' std::cout << 'cout sieve size: '; std::cout << size; std::cout << ' \n '; //prints 'cout sieve size: 5 (or whatever the correct sieve size is)' 

If I iterate through the vector via

if(i=0;i<sieve.size();i++)  

I get the correct number of iterations.

What am I doing wrong or what is up with printf? size() returns an int right??


Here’s my entire little script:

#include <iostream> #include <vector> #include <stack> #include <math.h>  int main (int argc, char * const argv[]) {     unsigned long long answer = 0;     unsigned long long cur = 2;     std::vector<long long> sieve;     unsigned long long limit;     unsigned long long value;     unsigned int i;     int size;     bool isPrime;     std::cout << 'Provide a value to find its largest prime factor: ';     std::cin >> value;     limit = ceil(sqrt(value));     sieve.push_back(2);     while(cur++ < limit){       isPrime = true;       sieve.begin();       for(i=0; i<sieve.size();i++){         if(!(cur % sieve[i])){           isPrime = false;           break;         }       }       if(isPrime){           if(!(value % cur)){           std::printf('Is prime factor: %d\n', cur);           sieve.push_back(cur);           answer = sieve[sieve.size() - 1];           size = sieve.size();           std::printf('current last: %d sieve size: %ld\n', answer, size);           for(i=0; i<sieve.size();i++){             std::printf('sieve iter: %d sieve val: %d\n', i, sieve[i]);             std::cout << size;             std::cout << ' wtf\n';           }         }       }     }     answer = sieve[sieve.size() - 1];     size = sieve.size();     std::printf('Limit: %d Answer: %d sieve size: %ld\n', limit, answer, size);     return 0; } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:18:17+00:00Added an answer on May 11, 2026 at 9:18 am

    Now, with the complete source, it is clear.

    You declared:

    int size; 

    Then you used:

    std::printf('current last: %d sieve size: %ld\n', answer, size); std::printf('Limit: %d Answer: %d sieve size: %ld\n', limit, answer, size); 

    If size is int, you should use ‘%d’, not ‘%ld’. A good compiler would have warned you about this. GCC gives these warnings for your original version:

    test.cpp: In function ‘int main(int, char* const*)’: test.cpp:17: warning: converting to ‘long long unsigned int’ from ‘double’ test.cpp:30: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:34: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:34: warning: format ‘%ld’ expects type ‘long int’, but argument 3 has type ‘int’ test.cpp:36: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long long int’ test.cpp:45: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long long unsigned int’ test.cpp:45: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long long unsigned int’ test.cpp:45: warning: format ‘%ld’ expects type ‘long int’, but argument 4 has type ‘int’ 

    This tells a lot.

    You should declare size as:

    std::vector<long long>::size_type size; 

    Then you should use it as:

    std::printf('current last: %llu sieve size: %llu\n', (unsigned long long) answer, (unsigned long long) size); std::printf('Limit: %llu Answer: %llu sieve size: %llu\n', (unsigned long long) limit, (unsigned long long) answer, (unsigned long long) size); 

    Of course, using iostream avoids you these problems, specially the ugly casting in printf() to transform size to a type known to printf.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As you already discovered yourself, this isn't a bug but… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer Taking it quite literally, it can be that the class… May 11, 2026 at 8:24 pm
  • Editorial Team
    Editorial Team added an answer The best bet if you must use raw pointers is… May 11, 2026 at 8:24 pm

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I don't know when to add to a dataset a tableadapter or a query
I don't edit CSS very often, and almost every time I need to go
I don't understand where the extra bits are coming from in this article about
I don't want PHP errors to display /html, but I want them to display

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.