Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C again...

Status
Not open for further replies.

Marks256

New Member
Yesterday i was looking at some C code, and i fell in love with it. The only thing i still have a problem with is the brackets {}.

Take the following code, for example, how do you know where you need the brackets?

Code:
/*  LCD Module Software                                               */
/*  17th May 1997			                              */
/*  Copyright 1997 Craig Peacock                                      */
/*  WWW     - http://www.senet.com.au/~cpeacock                       */
/*  Email   - cpeacock@senet.com.au                                   */
/*                                                                    */
/*  Register Select must be connected to Select Printer (PIN 17)      */
/*  Enable must be connected to Strobe (PIN1)                         */
/*  DATA 0:7 Connected to DATA 0:7                                    */

#include <dos.h>
#include <string.h>

#define PORTADDRESS 0x378  /* Enter Your Port Address Here */

#define DATA PORTADDRESS+0
#define STATUS PORTADDRESS+1
#define CONTROL PORTADDRESS+2

void main(void)
{
 char string[] = {"Testing 1,2,3                           "
		  "It' Works !                             "};
 char init[10];
 int count;
 int len;
 init[0] = 0x0F; /* Init Display */
 init[1] = 0x01; /* Clear Display */
 init[2] = 0x38; /* Dual Line / 8 Bits */

 outportb(CONTROL, inportb(CONTROL) & 0xDF); /* Reset Control Port - Make sure Forward Direction */

 outportb(CONTROL, inportb(CONTROL) | 0x08); /* Set Select Printer (Register Select) */

 for (count = 0; count <= 2; count++)
  {
   outportb(DATA, init[count]);
   outportb(CONTROL,inportb(CONTROL) | 0x01); /* Set Strobe (Enable)*/
   delay(20);                                 /* Larger Delay for INIT */
   outportb(CONTROL,inportb(CONTROL) & 0xFE); /* Reset Strobe (Enable)*/
   delay(20);                                 /* Larger Delay for INIT */
  }

 outportb(CONTROL, inportb(CONTROL) & 0xF7);  /* Reset Select Printer (Register Select) */

 len = strlen(string);

 for (count = 0; count < len; count++)
  {
   outportb(DATA, string[count]);
   outportb(CONTROL,inportb(CONTROL) | 0x01); /* Set Strobe */
   delay(2);
   outportb(CONTROL,inportb(CONTROL) & 0xFE); /* Reset Strobe */
   delay(2);
  }
}
 
Ha ha! I figured it out!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I kept stairing at some other coding, and i figured out that the brackets {} hold the FORs, the CONDITIONALs, the WHILEs, etc. They also define where functions begin and end. Correct?

Oh, also, i think i figured out what the ";" are for. I think they are there to define the end of a command, right?

What i mean is;

Printf("Hello, World!\n"); // <- the semicolon says that the printf command is done
 
Marks256.

You got it. I hate C for that reason, but you have it.

{} wraps things otherwise it will only do the next command you have.

It is like while wend, THEN ENDIF, in pascal the begin end.. It encapsulates the code to run.

Think of the ; as a continuation of the string of commands.

BASIC does not need it. That is why I like it better. :)
 
Yes, i am native to BASIC. It is the first programming language i learned. Really, i would love to keep using BASIC, but i guess most people are programming microprocessors in C, and quite a few good programs have been made in C, so i figured i would learn it.

I am also tackling ASSEMBLY right now, too. For some reason ASM is pretty easy, and that scares me....(i always thought that assembly was difficult, but no, it is pretty cool)



You actually like BASIC, huh? Wow, you are the first person i have EVER heard(read) say that. Most people i know absolutely HATE BASIC. :( What losers.....just kidding. :D
 
If you have a project that is big and can go to market with it, assembler for sure. If you want to do something fast on the weekend, BASIC is fast.

C is great, used it for years and our Goverment here in the US paid us over and over to remove all the mistakes made in it (you do not want to know how much). We never made that money when we used assembler before that.

But the company got the money, not me. So I would have rather stayed with ASM :)

They are all the same (basic, C, pyton, pascal, forth, blah blah), pick one you like. They all look (ok work) about the same, just I like the BASIC syntax. I like writing code and not have to hold shift a lot and I can read it like a book. I'd rather type "endif" and see it is the end of that "if" I typed above. } is good too..

And one "=" is good enough for me to see if something is equal. And not needing () around the compare. I mean if it was math I could see using it. Not to compare strings.

BASIC and C to me are about the same on larger CPUs, on micros, well... I could go either way as well.

You basically have to find what works for you. But BASIC is clean.. I just can not find a good one for the microcontrollers far as a trail. Other than MikroBASIC.

20 years ago or so I met people at computer clubs telling me BASIC was for beginners (I was a hardware and software designer back then, I am old and retired now; yes back in the K&R days of C). I can tell you, when I told them I only use BASIC when I had no time for assembler for a small task.

They normally backed off. As they learned C because assmbler was to hard for them.

So if assembler is easy for you, I would use that. Otherwise I use BASIC.
 
Last edited:
ASM is nice and efficient as long as you know what you're doing, but I think I would be scared to death to have to tackle a LARGE project with it; it would take a lot of time to write, and I think I'd have an increasingly harder time keeping it all straight in my head. BASIC is definitely easy to write and read, but I still feel like it ends up being more verbose and the structure is a bit less clear.

For me, C is the easiest to read and follow; once you get used to them, the semicolons and {}'s make it pretty easy to keep track of the structure and flow of the program. Also, the relaxed whitespace rules allow you to stick multiple things on one line, or spread them out as much as you want. With all the operators, etc that are available, you can also perform a lot of tasks with very compact lines of commands, at the cost of them sometimes appearing 'cryptic' to a beginner at C. But at some point, when you are working on a very large, complicated program, it is often helpful to be able to fit a large portion of your code on the computer screen at once, in which case this sort of brevity is beneficial. For getting started and ease of use, the more rigid formats of languages like ASM and BASIC can be helpful, as it forces you to keep your code in a standard format so you can more easily understand it... Whereas C 'gives you enough rope to hang yourself' so to speak - there's no reason you can't write clear, verbose programs that are easy to follow, but when you're comfortable enough with it there's also nothing stopping you from structuring things the way you feel is best. I think part of the reason some people claim C is too cryptic is that they've based their opinions on programs in which the programmer took such liberties, to which I answer "blame the programmer, not the language". It's kind of like why I drive a car with decent power and handling: if I'm trying to show someone how to drive (like writing example code for others to read), I'll drive real cautiously and courteously, but if I'm on a twisty back road all by myself (like writing code for my own personal projects), I have the freedom to have some more fun. I'm sure there aren't many people who intentionally drive a slow and stable car for the sole purpose of forcing themselves to 'behave'.

I started programming when I was pretty young, playing around with QBASIC on a 486, and since then I've done ASM, C, C++, C#, VB, etc, and I still prefer the variants of C the most. And, they're the most common so it's easiest to get example code - as Marks discovered.

Just FYI, I found this the other day: "Teach yourself C in 24 hours"
**broken link removed**
and bunch of other C-related books:
https://freecomputerbooks.com/langCBooksIndex.html

Might be good references for getting started.
 
Last edited:
On a PC C and BASIC (I like PBwin at powerbasic.com) are about the same to me. On the micro, the BASIC is easier for me to follow.

When I was in R&D we did out own OSs, state machines, etc. Large projects are really not that bad once you lay it all out first. I just seem to have to look at the microchip commands all the time. The Atmel was a lot easier for me. And the small projects I do, BASIC is quicker.
 
Keep in mind C parsers/compilers don't rely on standard spaces, returns...you could write you entire program without hitting the return key once if you wanted.

I was shocked when I found out I didn't have to use line numbers with newer versions of BASIC a couple years ago...I went from my Apple II to a Basic Stamp II.

I'd always add lines of code, and go from my usual ten number spacing to one, with lots of gosubs and gotos. Those were the good times...
 
Not really. When i was taught QBASIC, i was thought that i HAD to have line numbers. What a bunch of bull. Thankfully i realized that i didn't need them, which later enabled me to make some pretty freakin sweet programs!
 
My Apple II's BASIC used line numbers to differ from executed commands:

If I typed PRINT 1+1, it would return 2 on the screen

If I typed 10 PRINT 1+1, a new program would be created with that as its first line...
 
Oh, the equivalent of that for the PC is GWBASIC. Now THAT is hardcore!!!!
 
...and to think the only thing that stopped me from writing an asteroids clone was the fact that I couldn't use the INKEY$ command...and I didn't know what assembly was at the time so writing a routine to scan the keyboard was out of the question...*sigh*
 
Pff, that is apple for you..... :D
 
You can pretty much get all the old apples for free now days. He he, actually, i helped the tech department at my high school, um, "dispose of" about 35 Apple IIe's. And guess what?!?!? THEY LET ME KEEP ONE!!!! :( lol.
It was fun to desolder....... :D
 
The IIe can be a good 'bot platform. I saw one on the net a while ago (lost the bookmark - wiped my hard drive) and it could navigate its way through a house, retrieve stuff, speak and listen...all through a 65c02 and some ASM routines tied together in BASIC. pretty cool stuff.
 
Hmm, sounds interesting. i would like to see that. Do you remember what you were looking for when you found it? I think i will google it quick. THIS I GOT TO SEE!!!! :)
 
Status
Not open for further replies.

Latest threads

Back
Top