1 2
7 8
Joined: 11/26/2005
Posts: 285
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: 2/26/2007
Posts: 1365
Location: Minnesota
Well, your 0 in "return (0)" should not be in parenthesis. Other than that, I don't know.
adelikat wrote:
I very much agree with this post.
Bobmario511 wrote:
Forget party hats, Christmas tree hats all the way man.
Post subject: Re: Hello, I suck at computers :)
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
Swedishmartin wrote:
When I tried compiling it, I got an error message saying that stdio.h doesn't exist! What ever could be wrong?
Install libc-dev.
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.
Post subject: Re: Hello, I suck at computers :)
Joined: 11/26/2005
Posts: 285
Bisqwit wrote:
Install libc-dev.
(Doing this now) This got me wondering, though. If libc-dev is for source code compilation, then what's the regular libc for?
Post subject: Re: Hello, I suck at computers :)
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
Swedishmartin wrote:
This got me wondering, though. If libc-dev is for source code compilation, then what's the regular libc for?
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.)
Joined: 11/22/2004
Posts: 1468
Location: Rotterdam, The Netherlands
If you want to do some reading up on libc, try here: http://www.gnu.org/software/libc/ http://packages.ubuntu.com/hardy/libc6 - the libc6 package http://packages.ubuntu.com/hardy/libc6-dev - the libc6-dev package
Joined: 11/11/2006
Posts: 1235
Location: United Kingdom
stickyman05 wrote:
Well, your 0 in "return (0)" should not be in parenthesis. Other than that, I don't know.
I'm pretty sure it can be if he so wishes. It doesn't really make a difference.
<adelikat> I am annoyed at my irc statements ending up in forums & sigs
Joined: 11/26/2005
Posts: 285
Hey, program is compiled and working! Thanks for helping me, Bisqwit and Omega! :)
Joined: 3/7/2006
Posts: 720
Location: UK
Yeah, return(0); doesn't mean 'call return with argument 0', it means, return (0), i.e. 'evaluate (0) as an expression and return it'
Voted NO for NO reason
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
And, more precisely, in C the expression "(0)" is completely equivalent to the expression "0".
Joined: 7/2/2007
Posts: 3960
Doesn't C have boolean types? So (0) would be false, which is distinct from 0 even though 0 evaluates to false in a boolean context.
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
Joined: 3/11/2008
Posts: 583
Location: USA
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.
Joined: 3/7/2006
Posts: 720
Location: UK
Yeah C never implicitly casts to boolean unless you do something like bool x = 1;
Voted NO for NO reason
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
LagDotCom wrote:
Yeah C never implicitly casts to boolean unless you do something like bool x = 1;
And even then, bool is a C++ type, not C. C does not have a boolean type.
nfq
Player (94)
Joined: 5/10/2005
Posts: 1204
Swedishmartin wrote:
Hello, I suck at computers :)
not really. compare to me, i hardly even know what "compile" means.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
Bisqwit wrote:
And even then, bool is a C++ type, not C. C does not have a boolean type.
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: 7/2/2007
Posts: 3960
It's been a long time since I did serious programming in C (hacking FCEU's controls doesn't count), so fair enough.
Pyrel - an open-source rewrite of the Angband roguelike game in Python.
Active player (312)
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
Bisqwit wrote:
Drama, too long, didn't read, lol.
Joined: 11/26/2005
Posts: 285
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?
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
Swedishmartin wrote:
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.
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.
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 you are not printing a "\n" at the end, and your shell prompt is probably overwriting what it is printing.
Editor, Active player (297)
Joined: 3/8/2004
Posts: 7469
Location: Arzareth
Swedishmartin wrote:
More troubles! I was surfing the internet when I came upon this little thing: <snip> I borrowed it for use in my own code. And this is how my code looks at the moment: <snip> 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?
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:
Binary  SignedValue  UnsignedValue
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 -8 8
1001 -7 9
1010 -6 10
1011 -5 11
1100 -4 12
1101 -3 13
1110 -2 14
1111 -1 15
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)
upthorn
He/Him
Emulator Coder, Active player (391)
Joined: 3/24/2006
Posts: 1802
Swedishmartin wrote:
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?
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.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
upthorn wrote:
firstly, it's %d, not %i. http://www.cplusplus.com/reference/clibrary/cstdio/printf.html provides a nice reference for all the format identifiers.
The very url you gave shows that %d and %i are completely equivalent.
Joined: 11/26/2005
Posts: 285
Warp wrote:
Because you are not printing a "\n" at the end, and your shell prompt is probably overwriting what it is printing.
Bisqwit wrote:
Your code has a syntax error. The if clause should have parentheses in it. Other than that, it looks okay.
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?
Warp wrote:
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.
Yes, but I adore its elegance. To me, it's really simple to see what it does.
upthorn wrote:
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.
I've declared functions in main before, and I believe that has worked. I'll check this tomorrow.
1 2
7 8