I've been trying to write something similar to tari's mkg3a for a different format recently, but I've run into some troubles. The main trouble is that I'm, how shall I put it, completely and utterly incompetent in any normal or usable language ("Hello world" took me the better part of an hour to understand...).

By some miracle, I managed to get GCC to work in both Cygwin and Linux, which means that I can successfully compile stuff.


So, at this point, I should probably post what I have so far. I would do that if I had anything, but I don't. Here's an empty box showing where it would otherwise go:


Code:
/* Code goes here*/


What I don't really understand is the entirety of C. What's with the Main() thing? Why do you have to declare machine variables? How do you do string comparison? What the heck is this mess with pointers that don't appear to behave like the integers they are? What are all of these %n or \n things that make absolutely no sense?

So, I'm a newbie/n00b with no basic understanding of what I'm trying to do Razz
It sounds to me like we might be better off trying to get you to write some simpler programs first, but I'll try to answer all your questions as well as I can. I'd suspect that you're so used to working in low-level languages like SH3 that you're underestimating what C can do for you. Anyway, answers to your questions, in more-or-less the proper order:

1) What is main()? When you give your compiler a chunk of source code, it needs a way to make sense of it. You have your include files, your global variables, great. Then you have lots of functions, since code has to be in a function, it can't just be floating around outside the function. Good enough. But when the program is actually run, which function should run? In C, main() is the function that gets run, which can do anything, such as execute code and call other functions. In addition, the arguments provided by the operating system to main() tell you about the arguments provided to your program, namely how many there are and what they are. Do you want to know more about this?

2) Why do you have to declare machine variables? I don't follow this question, and from the several possible things I think you might be asking, I'm inclined to say "you don't".

3) How do you do string comparison? Simple! You use the strcmp(str1,str2) function. It returns 0 if the strings match, nonzero otherwise. Are you familiar with how to storage strings as arrays of characters?

4) Pointers are very, very logical, and you have a HUGE advantage by being an ASM coder and therefore understanding indirection. Can you clarify your question?

5) \n is a newline, \t is a tab, \r is a carriage return. I think you're referring to the printf(), sprintf(), fprintf(), scanf(), sscanf(), fscanf() family of functions, which use special formatting sequences like %d, %f, %s, %x, etc to represent integers, floating-point numbers, strings, hex numbers, etc.
KermMartian wrote:

...

2) Why do you have to declare machine variables? I don't follow this question, and from the several possible things I think you might be asking, I'm inclined to say "you don't".

3) How do you do string comparison? Simple! You use the strcmp(str1,str2) function. It returns 0 if the strings match, nonzero otherwise. Are you familiar with how to storage strings as arrays of characters?

4) Pointers are very, very logical, and you have a HUGE advantage by being an ASM coder and therefore understanding indirection. Can you clarify your question?

5) \n is a newline, \t is a tab, \r is a carriage return. I think you're referring to the printf(), sprintf(), fprintf(), scanf(), sscanf(), fscanf() family of functions, which use special formatting sequences like %d, %f, %s, %x, etc to represent integers, floating-point numbers, strings, hex numbers, etc.



2) What I meant was stuff like

Code:
int check,var,bullet;


Shouldn't the code default to int or other integer data types?

3) Cool, so that's how you do it. I was using something like


Code:
if ("%s" == "var\n")
                           printf("Hello world\n");


4) I don't understand stuff like this:


Code:
/* Don't you love not having context for code? */
fgets(buf,1000, ptr_file)


5) I think I understood the first half of that.
2) The compiler makes no assumptions about your data. Stop thinking it terms of registers, it's no longer relevant. You can have signed and unsigned chars, signed and unsigned shorts, signed and unsigned ints and longs and long longs, floats, doubles, arrays of any of those things, even variables that are actually structures, big conglomerations of other data types. That line happens to define three integers.

3) Yeah, that would compare the address of those two constants in your program's text section, not exactly what you want. Smile

4) When in doubt, always read the man pages. For that, try a Google for "man fgets" (this is a good result for the search). That line gets at most 1000 bytes from the file descriptor named in ptr_file, a FILE* (which is basically an integer) into the storage buffer pointed to be buf.
For future reference, while there are lots of great free C resources on the web the best thing I've found for helping one learn ANSI C is K&R, while there have been lots of updates to C since then almost all C compilers will support a minimum of ANSI C syntax.
TheStorm wrote:
For future reference, while there are lots of great free C resources on the web the best thing I've found for helping one learn ANSI C is K&R, while there have been lots of updates to C since then almost all C compilers will support a minimum of ANSI C syntax.
Exactly this. Smile I get the impression that Fishbot has at least a vague idea of program flow inside functions and such, though, he just needs more guidance and examples to understand it more precisely.
jfgi

http://lmgtfy.com/?q=C+tutorial
Kllrnohj wrote:
That's not particularly helpful in this situation, since he has vague knowledge but specific questions. Razz
@Kllrnohj: Yes, that was so hard. I had to enable Javascript and everything.

Here's what it came up with
Qwerty.55 wrote:
@Kllrnohj: Yes, that was so hard. I had to enable Javascript and everything.

Here's what it came up with
a, you got me with that. ANYWAY, shall we go back to discussing C questions? Got any new ones for us, Qwerty?
Um, how does GCC work Razz

Ridiculous requests aside, how does one work with things like bytes that don't seem to be native data types (unsigned Short is 16 bit).
How do you use Prizm calls in C? What .h files are necessary and what do you need to make a Hello World .g3a? How do you make it?

@Qwerty: GCC, once you install it, works like this:


Code:
gcc <input file>
Raylin, take a look at the .h files, you'll see which system calls are provided by each .h file. I'll post a simple Hello World program on Thursday if you remind me and if no one has already done so.

Qwerty, unsigned short is indeed 16 bits; you just use it. Add unsigned shorts together, bitmask them, multiply and divide them, whatever. Set a long equal to the value in a short! No worries, your compiler will figure it all out for you. The only place you need to be careful is converting between datatypes of different sizes.
Kerm, I was asking how you work with individual bytes. Shorts are pretty much like Longs from what I can tell.
Qwerty.55 wrote:
Kerm, I was asking how you work with individual bytes. Shorts are pretty much like Longs from what I can tell.
Bytes are no harder to work with than shorts, ints, longs, and long longs. The 8-bit datatype is "char", so you can have signed and unsigned chars. Ignore the fact that you're not writing strings with them; that's ok. However, if you're using chars instead of ints because you're trying to optimize for space, I wouldn't worry unless you're creating massive arrays.
5) printf is just a regular subroutine (subroutines and functions are called functions in C, regardless of whether they return a value to the caller). printf is used to PRINT Formatted output. The first argument is the format string, which tells printf HOW to print the text. For example:

Code:
printf("hello %d %x\n", 42, 42);

The format string contains %d which tells printf to grab the next integer (type int) and print it as decimal, then the %x tells printf to grab the next integer and print it as hexadecimal, and finally the \n is the newline character, which simply goes to the next line. Here is the output of the above statement:

Code:
hello 42 2a

42 decimal is the same as 2a hexadecimal, which is why it would print 2a here.

The d and x are called conversion specifiers. There are a number of other conversion specifiers, and some tell printf to grab different data types other than integers. For example, %s takes a string (pointer to char), and %f takes a double (floating point) value.

Does that answer your question?

Also, bytes would generally be represented by the char type, which is the smallest data type in C. char is at least 8 bits wide, and on most mainstream hardware it is exactly 8 bits wide.
KermMartian wrote:
That's not particularly helpful in this situation, since he has vague knowledge but specific questions. Razz


No he didn't, he was asking questions pretty much every tutorial would easily answer. He had no knowledge whatsoever, that is exceedingly obvious.
Kllrnohj wrote:
KermMartian wrote:
That's not particularly helpful in this situation, since he has vague knowledge but specific questions. Razz


No he didn't, he was asking questions pretty much every tutorial would easily answer. He had no knowledge whatsoever, that is exceedingly obvious.
Fair enough, but some diplomacy is still a good idea, such as explictly suggesting that he go through a few tutorials, perhaps suggesting some tutorials, and mentioning how writing and running and debugging a few simple programs might help with his understanding.
Ignoring the above, since I was aware of the existence of tutorials, how is caching done in C? Is it written in by the compiler (and hence Volatile data) or can it be done explicitly by the programmer?
Qwerty.55 wrote:
Ignoring the above, since I was aware of the existence of tutorials, how is caching done in C? Is it written in by the compiler (and hence Volatile data) or can it be done explicitly by the programmer?


Depends on what cache you are talking about
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 2
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement