I've been trying to compile this C program (in GCC, Ubuntu):
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return(0);
}
When I tried compiling it, I got an error message saying that stdio.h doesn't exist! What ever could be wrong?
(I'm follwing the instructions from here exactly and I'm trying to compile the program from my home folder. Should I move it to the C library folder or something?)
Joined: 11/22/2004
Posts: 1468
Location: Rotterdam, The Netherlands
Yeah, Bisqwit is right. libc-dev needs to be on your system and GCC must be able to find it. If you're on a system with a package manager, that's really easy, though the package might have a nonstandard name. It might be called libc6-dev.
For providing the machine code that is used by programs using libc.
Library provides the machine code. Dev library (which is, the #include files) tells a compiler how to use the library. (Also, the dev package includes those libraries that are normally linked statically to the binary, as opposed to loaded runtime from the disk.)
Parens don't force a boolean context, or you'd have a whole lot of math troubles in everything.
And last I programmed in C (many moons ago) there was no boolean type- you just used zero and nonzero, and any boolean operation returned zero or nonzero.
Actually the C99 standard does define 'bool', 'true' and 'false', but AFAIK not as reserved keywords (ie. internal types), but as symbols defined in <stdbool.h>.
Joined: 2/28/2006
Posts: 2275
Location: Milky Way -> Earth -> Brazil
wow, swedishmartin, that's gotta be the most kickass program ever.
You , like, say hello to the world... wow
"Genuine self-esteem, however, consists not of causeless feelings, but of certain knowledge about yourself.
It rests on the conviction that you — by your choices, effort and actions — have made yourself into the
kind of person able to deal with reality. It is the conviction — based on the evidence of your own volitional
functioning — that you are fundamentally able to succeed in life and, therefore, are deserving of that success."
- Onkar Ghate
More troubles!
I was surfing the internet when I came upon this little thing:
unsigned int fib(unsigned int n){
if (n < 2)
return n;
else
return fib(n - 1) + fib(n - 2);
}
I borrowed it for use in my own code. And this is how my code looks at the moment:
int main()
{
int fib(int x)
{
if x<2
return 1;
else
return fib(x-1)+fib(x-2);
}
int n;
printf("Integer goes here, yo: ");
scanf("%i", &n);
printf("\nThe Fibbonacci number of %i is %i", n, fib(n));
return 0;
}
The program fails when trying to print the numbers. Why?
(Because %i signifies an integer, right? So I have to use %i two times, right?)
And would it be possible to put the \n line break in the scanf, to make the code neater?
And what's up with signed/unsigned integers? What does it mean?
You shouldn't, because that's the most inefficient possible way of calculating fibonacci numbers. It's a classical example of using complex recursion (ie. more complicated than simple tail recursion), but also a classical example of where a recursive solution can lead to extremely inefficient results.
A much more efficient way of calculating fibonacci numbers is to use a simple loop.
Because you are not printing a "\n" at the end, and your shell prompt is probably overwriting what it is printing.
In printf, instead of %i, I usually use %d, but that is really a matter of taste.
However, in scanf there is a difference between %i and %d: %i can read hexadecimals (written as 0x7F for example) whereas %d can only read normal base-10 integers.
Your code has a syntax error. The if clause should have parentheses in it. Other than that, it looks okay.
unsigned/signed controls the matter of whether the values can be negative or not.
For ease of thinking, let's demonstrate with a 4-bit integer:
So you see, if an integer is "signed", it sacrifices half of its range (i.e. one bit) to represent negative values; whereas if it is "unsigned", it uses the full range (i.e. all bits) for positive values.
"int" is generally 32 bits, so its unsigned range is 0..(2^32-1) and its signed range is -(2^31)..(2^31-1)
firstly, it's %d, not %i. http://www.cplusplus.com/reference/clibrary/cstdio/printf.html provides a nice reference for all the format identifiers.
As for signed int vs unsigned int, it's pretty simple.
A signed int (assuming a 32-bit int size) can be any value from -2147483648 to 2147483647.
an unsigned int can be any value from 0 to 4294967295.
A slightly more in-depth explanation is that it's a different way of interpreting the same binary data:
-1 in a signed int is the same binary value as 4294967295 in an unsigned int (11111111111111111111111111111111)
and -2147483648 in a signed int is the same binary value as 2147483648 in an unsigned int (10000000000000000000000000000000).
"int" by itself usually defaults to signed, although I believe most compilers allow you to customize this in their options somewhere.
Also, if that code block is accurate, I'm surprised it gets to the printf before choking. The fib function definition should go above main, not inside it.
How fleeting are all human passions compared with the massive continuity of ducks.
The reason for this was that I was typing up the code from memory. I have it on my laptop, you see, and I'm posting from my desktop.
Original code:
#include <stdio.h>
int main()
{
int fib(int x)
{
if(x<2)
return 1;
else
return fib(x-1)+fib(x-2);
}
int n;
printf("Integer goes here, yo: ");
scanf("%i", n);
printf("\nThe Fibbonacci number of %i is %i\n", n, fib(n)); //I really believe that this line is faulty in the "...\n", n, fib(n))" declaration.
return 0;
}
It compiles fine, but after inputting the first integer the program stops and prints "Segmentation fault".
You can see my comment in the code. Does anyone know how outputting several variables in one line of code should look like?
Yes, but I adore its elegance. To me, it's really simple to see what it does.
I've declared functions in main before, and I believe that has worked. I'll check this tomorrow.