I have created two template project in Eclipse with CDT plugin(one is C project, another C++), and have compiled two very similar projects(as for me) but I get absolutely different console outputs. Why this outputs so different? C code:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
int n;
for (n=0; n<5; n++)
printf("%c ",numbers[n]);
return EXIT_SUCCESS;
}
output some garbage
C++ code:
#include <iostream>
using namespace std;
int main() {
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << " ";
return 0;
}
output
10, 20, 30, 40, 50
You are printing int
as char
in C.
Change
printf("%c ",numbers[n]);
to
printf("%d ",numbers[n]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With