The operation of the "for" function in this example

Thread Starter

richard3194

Joined Oct 18, 2011
193
My main interest actually is to be able to understand how to write code for MCUs, not MPUs. But I figured it would be helpful at this stage to have a compiler that simply would work with Windows, that would not represent overkill (like Windows 7 SDK does). I mean, I'm likely never going to be involved with any large and complex source code. On Windows itself, likely just these small source code examples that help with understanding of principles - more or less.

I have an Arduino clone that I'll eventually be messing with, which of course is more about controlling stuff. Some posts go to an Arduino forum, some here.

EDIT: Oh, and I see we have the Embedded Systems and Microcontrollers forum here.
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,273
You are trying to learn 2 completely different things, which is fine, but identify them as separate and I think your life will be easier.

First you are trying to learn the language C. There are many many C compilers, we all have favorites. If you want to learn all the nitty gritty stuff, start with gcc and a makefile, and gdb debugger. The learning curve will be steep and slow at first, but you will learn a lot of the details. If you want to jump direct to writing code, Microsoft Visual Studio is fantastic and will make learning the actual programming part very easy.

The second thing you want to learn is how to program micro controllers. This is separate from the C language. My advice is to learn the C language first, at least the basics, get comfortable with it, THEN learn how to write code for a micro controller. Trying to do both at the same time is going to be frustrating. Why? Because it's easier to step through your code in the debugger on a PC, so that you can see what is actually happening line-by-line. Yes you can do this on a micro controller, but it's more complicated and there are sometimes caveats that will add complexity that you don't need at this point.
 

BobaMosfet

Joined Jul 1, 2009
2,211
Hi. I read that the for function is a loop function. But, in reading up about the types of defined functions, I've come across the use of the for function to determine whether a number is prime or not. Here is the code (from programiz.com):

Code:
#include <stdio.h>

void checkPrimeNumber();

int main()
{
    checkPrimeNumber();    // no argument is passed to prime()
    return 0;
}

// return type of the function is void because no value is returned from the function
void checkPrimeNumber()
{
    int n, i, flag=0;

    printf("Enter a positive integer: ");
    scanf("%d",&n);

    for(i=2; i <= n/2; ++i)
    {
        if(n%i == 0)
        {
            flag = 1;
        }
    }
    if (flag == 1)
        printf("%d is not a prime number.", n);
    else
        printf("%d is a prime number.", n);
}
What I'm not sure about is how the function works in this example - as to a loop action. If I set n to 12 (keyboard-in 12), then "i" initially will have the value of 2 I believe. In that case, "i" will be smaller than 6 (12/2) and the test turns out true. So, "i" is incremented to 3 and the code below the for function runs. When printf() right at the end executes, I believe the program goes back to main() at line 7. Which again calls the function checkPrimeNumber(). Of course, that sets things in motion again and again the program halts until an integer is entered in from the keyboard and Enter is pressed. I see that whatever "i" is, it gets reset to 2 again as the for function again executes.

So, it looks like as it is, the code that is below the for function only executes once after a keyboeard entry. But of course, "I" only got to the value of 3, so I'm wondering (not withstanding what I've just said) what stopped the for function continuing to run the code below it. I see that normally "I" would get to 7, at which point the test would result in false and stop the for function from running the code below it. I hope my thinking makes some sense. :) Thanks. Rich
Before you can understand 'for' looping statements, you should learn about operators and their meaning when placed before or after a variable, and how your compiler will interpret them. Note the '++i' in your loop. Go learn what that does to help you better understand what's going on in your for loop compound-statement.
 

BobaMosfet

Joined Jul 1, 2009
2,211
My main interest actually is to be able to understand how to write code for MCUs, not MPUs. But I figured it would be helpful at this stage to have a compiler that simply would work with Windows, that would not represent overkill (like Windows 7 SDK does). I mean, I'm likely never going to be involved with any large and complex source code. On Windows itself, likely just these small source code examples that help with understanding of principles - more or less.

I have an Arduino clone that I'll eventually be messing with, which of course is more about controlling stuff. Some posts go to an Arduino forum, some here.

EDIT: Oh, and I see we have the Embedded Systems and Microcontrollers forum here.
Language is a means to implement logic in a way that it can be submitted to a program to compile or interpret it into machine language on the platform you're on. Binary is binary, and virtually all hardware operates the same, irregardless of scale, with today's technology. Master the basics, and the deviations will become manageable details.
 
Top