PDA

View Full Version : some Fundamental C questions


!Marc!
2010-02-27, 08:40
After a week of banging my head against a wall trying to follow xCode tuts that are obviously out of date and contain errors and typos - and my biggest annoyance - authors that just want to lead you like a zombie telling you to do this and do that without explaining 'why' or what you are trying to achieve, I thought I'd go right back to basics, and learn xCode from the beginning my way.

That means Terminal apps and the fundamentals of the C and objective-C language. I thought if I keep it simple, I can work out what is going on in xCode, and how it works

so consider this simple app in C - this is the main()

#include <stdio.h>
#include <math.h>
#include "sinfunc.h"
#include "cosfunc.h"


int main (void){

for (int i=0; i<=360; i+=30)
{
printf("%i : sin=%.2f : cos=%.2f \n", i, sinfunc((float)i), cosfunc((float)i));
}

return 0;
}


this is the sinfunc.h

float sinfunc(float number);


and this is sinfunc.c

//#include <math.h>

float sinfunc(float number)
{
return sin(number);
}


The code for cosfunc is identical to sinfunc except for the obvious difference!

You may have noticed that in sinfunc.c that I commented out the #include, which generates 2 warnings in the code - and if I uncomment it the warnings go away, but Im confused as to why I need to include it at all, as I previously included the math.h file in the main() before I included my custom functions.

I was also wondering why, when in xcode I create a new .h and .c file, that in the .c file it includes an #include to the .h file - I deleted it and the program compiled with no fuss at all.

I dont understand the logic of that, the main() includes the .h file, which the compiler knows corresponds with the .c file, so why does the .c file include an #include to the .h file? It seems all a bit 'circular'

Also, whats really annoying is that in some tutorial I followed, I have the output going to a built in window in xcode on the debug page. I cant remember how I did this, and would like it to launch terminal if possible - but what do I need to add to a program to stop the terminal window automatically quitting at the end of the program? Its impossible to read the output when it goes away

Thanks

!Marc!
2010-02-27, 08:51
Also as an aside, I understand that the math.c library code works in radians - but I'd like the output to be the same as when I type into my scientific calculator in degrees mode.

ie sin(30) should output '0.5', not '-0.988...'

ast3r3x
2010-02-27, 09:11
Are you ever happy…about anything? Maybe I've just come at the wrong times, but so far I've seen post after post of you complaining. Christ that is annoying.


I'll let someone else answer your other questions, as I'm not 100% sure and I wouldn't want to give you the wrong information.


See output:
Shift + Control + R brings up the terminal

chucker
2010-02-27, 09:28
What ast3r3x said.

I dont understand the logic of that, the main() includes the .h file, which the compiler knows corresponds with the .c file

The compiler doesn't know that; it's purely a convention.

, so why does the .c file include an #include to the .h file? It seems all a bit 'circular'

Because the compiler goes linearly through files. When compiling sinfunc.c and encountering a function call there, it already wants to know the signature so it can check if you called it correctly.

!Marc!
2010-02-27, 09:37
I pressed shift-control and R and nothing happened....
I then went to the menu and went Run->Console and nothing happened....

!Marc!
2010-02-27, 09:43
What ast3r3x said.



The compiler doesn't know that; it's purely a convention.



Because the compiler goes linearly through files. When compiling sinfunc.c and encountering a function call there, it already wants to know the signature so it can check if you called it correctly.

so how come if you delete the #include .h in the .c file, the program compiles normally?

Also if I include math.h in 3 places, haven't I introduced bloat to my code? Why do I need to include it in more than one place to stop the warnings appearing?

scratt
2010-02-27, 10:00
Buy a book on programming in C, C++ or Obj C.
Read it.

The questions you are asking show a basic lack of understanding of the most fundamental things about programming, and how you structure files for a compiler.

If we answer these questions for you, and you then show the same approach to the next step we will forever be answering questions which you really should have learned the answers to by investing some of your own time in reading a damn book.... starting at page 1 and progressing from there linearly through the book until the end.

I would recommend "C++ Primer Plus". Google it.
It has lessons you can follow, and tests you can take, and answers in the back.

!Marc!
2010-02-27, 10:01
I can see now that if you are already in the debug page that if you ask it for the console it just skips the cursor to the internal output - this is called GDB, right?

Im pretty sure that when I first installed xcode that the output went to the terminal, but I had a problem of the terminal auto quitting before I could see the results - Is this correct? I would just like to put it back to this behavior.

Also, my next idea was to change the code so I could supply an argument to the program at launch. I added the necessary argc to the main(), and googled supplying arguments, and found this...

http://www.meandmark.com/xcodetips.html


1. Select the executable from the Groups and Files list.
2. Click the Info button in the project window toolbar to open the executable's information panel.
3. Click the Arguments tab in the information panel.
4. At the top of the information panel, you will see a section titled Arguments to be passed on launch. This section is where you add the launch arguments. Click the + button to add the arguments.

Im assuming that the executable is the entry with the small black icon with some green dots, to represent the terminal, so I selected that and pressed the info button...

But then there ISNT an arguments tab!, so Im stumped at step 3.

!Marc!
2010-02-27, 10:22
Buy a book on programming in C, C++ or Obj C.
Read it.

The questions you are asking show a basic lack of understanding of the most fundamental things about programming, and how you structure files for a compiler.

If we answer these questions for you, and you then show the same approach to the next step we will forever be answering questions which you really should have learned the answers to by investing some of your own time in reading a damn book.... starting at page 1 and progressing from there linearly through the book until the end.

I would recommend "C++ Primer Plus". Google it.
It has lessons you can follow, and tests you can take, and answers in the back.

I have been and am reading many books thankyou, and I am trying to understand xcode and learn a whole bunch of things in parallel, unfortunately, lots of things I am reading seems to be out of date or just blatantly wrong. Hence the need to ask questions, and start right at the beginning.

Also, reading books in isolation isn't the answer to everything, some things you just wont get until you ask the question, sometimes you interpret things slightly off, and just need a little correction to make the link.

I dont think C, or ob-C is especially difficult, and once I can find a reliable source on xcode, im sure thats not especially difficult either. However, I have got 2 books on xcode, and both are full of errors, that make it especially difficult to follow, I gave up on the books and decided to use the web...same problem as illustrated above - im assuming that xcode is in rapid development, so things are liable to change. Hence I have to ask questions.

Sorry if asking simple questions about stuff offends you, but neither you nor anyone else is under duress to reply. If you dont like my questions, dont click on my threads, if you dont like me, dont click on my threads. If you cant relate to the frustrations of learning several difficult things at once, dont click on my threads.

If you have a problem with that, please petition Brad to ban my account, and i'll go elsewhere.

scratt
2010-02-27, 10:27
The questions you are asking show a basic lack of understanding of the most fundamental things about programming, and how you structure files for a compiler.

Probably the key sentence in my post.

Part of your problem is that you are probably trying to do too much at once.

Have you tried building a simple "Hello World" project in the latest version of Xcode and then bit by bit modifying that?

!Marc!
2010-02-27, 10:33
Probably the key sentence in my post.

Part of your problem is that you are probably trying to do too much at once.

Have you tried building a simple "Hello World" project in the latest version of Xcode and then bit by bit modifying that?

Hence, why I am going back to the start and doing terminal apps...

I assume that is sarcasm, seeing as xcode has a built in HelloWorld program on launch...please, if you just want to sit on the high ground and snipe, why bother?

chucker
2010-02-27, 10:35
Given how awfully defensive you are, I'm not sure why you expect any help at all. And saying "there's already a built in HelloWorld program on launch" reaaaaally misses the point.

!Marc!
2010-02-27, 10:42
Really missing the point :lol: - sure, I modified the helloWorld program, bit by bit until I got stuck on the questions I posted today, but hell, if reading comprehension fails you....

OK, I concede :lol:

All the best everyone. I'll move on...Bye

Brad - if you can do me the honour of making my absence permanent...;)

JohnnyTheA
2010-02-27, 19:22
so how come if you delete the #include .h in the .c file, the program compiles normally?


Probably because you already linked. If you did a clean, you would have problems.



Also if I include math.h in 3 places, haven't I introduced bloat to my code? Why do I need to include it in more than one place to stop the warnings appearing?

No, the linker will link to the appropriate math.h functions once. Each module will link to the one function. You have to expressly include math.h in every file that uses a math.h function because the compiler compiles each file individually.

Slow down, don't worry about bloat, take a deep breathe, and you will be fine..

!Marc!
2010-02-27, 20:06
Probably because you already linked. If you did a clean, you would have problems.



No, the linker will link to the appropriate math.h functions once. Each module will link to the one function. You have to expressly include math.h in every file that uses a math.h function because the compiler compiles each file individually.

Slow down, don't worry about bloat, take a deep breathe, and you will be fine..

Thanks, a decent explanation, - actually, I spent a couple of hours looking up the process of compiling after the 'snipers' had their fun, and I discovered that each file I create is compiled seperately, and linked into one executable at the end - which is why I need to include the .h's in several places.

Seeing as theres a 2 line explanation - you have to wonder why its not possible for these clowns to explain that, rather than snipe?

But hey, ive spent my Saturday on a 14 hour stint of trying to learn Xcode, - add that to the 4-5 hours I spent every night last week after work aswell - Sadly because of the issues of things and processes moving about all over the place in Xcode, I've still not managed to follow a single tutorial all the way through without running into unsolvable problems. Im practically learning the app blind.

14 hours today, and nothing to show for it. And all they can do is berate me for being a bit negative. Well, im sorry Im not really moved to be gushing with praise for Apple or Xcode. Fortunately, i think that when I do figure out what the fuck is going on in Xcode, I think it might well be a piece of cake.

Now, its off to bed with my book on objective-c and read it until the thing smacks me in the face as I drop off.:D just like every other night last week.

ast3r3x
2010-02-27, 22:18
I pressed shift-control and R and nothing happened....
I then went to the menu and went Run->Console and nothing happened....

My bad, Shift + Command + R brings up the console.