-2

I am trying to get a very simple and basic logic up and running using the C language. The following code gets it right on python, but I am not able to get the same results using C.

//Python Code
a = 2
while True:
    a=a*2
    print(a)

With the above Python code I am able to generate the particular sequence, but with the following C code I am not able to generate the same results.

//C Code
#include <stdio.h>
int main(){
long int a = 2;
while (1){
    a=a*2;
    printf(a);
}
return 0;
}

I am getting 0 infinitely. Why am I not able to get the sequence in C.

EDIT: I am running this program with slightly modified code on an Arduino, so I can't use printf.

4 Answers4

0

printf in C is not the same as print in Python. This is a page describing how to use printf. Basically, you provide a format string describing where, what, and how to print the arguments, and then a variable number of arguments to be printed. You want to use printf("%ld\n", a);.

Alyssa Haroldsen
  • 3,652
  • 1
  • 20
  • 35
  • Please understand that I won't be able to use printf() in the program as it will be running on an Arduino. The printf() function was just a reference/stub for another function [Serial.println()]. – Vishal Subramanyam Aug 02 '15 at 07:51
  • First off, that's very important information to not include. Second, read the documentation for that function. If it doesn't match the documentation, then submit a question regarding that function. However, if they aliased printf, it likely has very similar usage. – Alyssa Haroldsen Aug 03 '15 at 09:07
0
    #include <stdio.h>
int main()
{
  int count, n, t1=0, t2=1, display=0;
  printf("Enter number of terms: "); /** Series for how many terms **/
  scanf("%d",&n);
  printf("Fibonacci Series: %d+%d+", t1, t2); 
  count=2;
  while (count<n)  
  {
      display=t1+t2;
      t1=t2;
      t2=display;
      ++count;
      printf("%d+",display);
  }
  return 0;
}
Krunal Shah
  • 3
  • 1
  • 5
  • Please understand that I won't be able to use printf() in the program as it will be running on an Arduino. The printf() function was just a reference/stub for another function [Serial.println()]. – Vishal Subramanyam Aug 02 '15 at 07:51
0

The sequence that you're printing is not the fibonacci sequence. It is a geometric sequence with ratio 2. See Wikipedia: Fibonacci numbers, Geometric sequences

Assuming that you actually want the sequence 2,4,8....,

The usage of printf is different. Use

printf("%ld", a);

Also C doesn't handle integers over 2^32. Using long, it may handle up to 2^64, But either case you would get an overflow, ie. the numbers will eventually get out of range after generating 63 numbers. Python has builtin Big Integers, C doesn't.

Rohcana
  • 359
  • 4
  • 13
  • @ Vishal, Use an external Big Integer Library such as [GNU MP](https://gmplib.org) or [InfInt](https://github.com/sharan01/infint). I would recommend InfInt since it is easier to use. – Rohcana Aug 02 '15 at 16:36
0

Well, maybe you are using the other print() on Arduino wrong.. post this on Arduino tags and see.