Very simple C syntax question

Thread Starter

count_volta

Joined Feb 4, 2009
435
Okay so I have an assignment to write some mips assembly code from C code. I sorta forgot my C and just trying to remember it.

My question is about syntax. In fact my textbook used incorrect syntax, and so its a little hard for me to understand what this code is doing.

Here is the code the textbook gives. This is a function, subroutine.

Rich (BB code):
 int find( int a[], int n, int x)
{
    int i;
  
    for (i=0; i!=n; i++)
        if(a==x)
            
                     return i;
return -1;


}


There are obviously curly brackets { } missing in the if statement. Thus I have no clue what this code is supposed to do and when. I mean when does it return i, and when does it return -1?

Obviously I need to understand the code if I am to turn it into mips. Can someone please help? Thanks guys.
 

indianhits

Joined Jul 25, 2009
86
the program is correct there are no errors

first it loops from i=0 to i=n along with the looping it checks for whether the array has the value of x if the array has the value of x then it returns the value i

but if loop keeps on incrementing and the array doesn't have the value equal to x then the loop ends and the function returns -1

i.e if array has the value of x then it returns i but if it doesn't then it returns -1
 

mik3

Joined Feb 4, 2008
4,843
a[] is an array and the for loop compares some values of the array until i does not equal (!=) n.

Then I guess, it checks a==x and if this is true it returns i.
Otherwise, it will reutrn -1. I think the book forgot the else statment which comes after the if.
 

Thread Starter

count_volta

Joined Feb 4, 2009
435
Yea its definitely missing an else. Also shouldn't there be curly brackets? Syntax is there for a reason. The absence of curly brackets means the program does something entirely different.
 

mik3

Joined Feb 4, 2008
4,843
Well, I think curly brackets should be included for the for loop. For the if statement they are not necessari if there is just one instruction after the if statement. Also, I thought it better and the else statment is not really needed. This becuse if the if statement is true then it will return i and the subroutine will end. Otherwise, it will not execute return i but it will execute return -1 because it continuous with the next instruction.
 

Papabravo

Joined Feb 24, 2006
21,225
Curly braces are only required when the if-part or the else-part consist of a compound statement. A compound statement is semantically the same as the single statement when there are no curly braces.

As a matter of style I ALWAYS use the curly braces for the case where I come back to a piece of code six months later and decide that a single statement really needs to be two or three statements.
 

indianhits

Joined Jul 25, 2009
86
The function only returns i when a==x hence

if there is an else then it will check only for i=0 and ends the function

the above program is correct

like for example this is not correct
int find( int a[], int n, int x)
{
int i;

for (i=0; i!=n; i++)
if(a==x) return i;
else return -1;
//it loops only for i=0 and returns depending on the if condition

}

 

Thread Starter

count_volta

Joined Feb 4, 2009
435
The function only returns i when a==x hence

if there is an else then it will check only for i=0 and ends the function

the above program is correct

like for example this is not correct
int find( int a[], int n, int x)
{
int i;

for (i=0; i!=n; i++)
if(a==x) return i;
else return -1;
//it loops only for i=0 and returns depending on the if condition

}



So the else basically says to not increment i by 1, and just exit the loop? That is weird and tricky. And you know what, I don't remember learning about such things properly in my C course.

Can you guys suggest somewhere I can read about proper C condition statement syntax and how it effects what the program does?
 

Thread Starter

count_volta

Joined Feb 4, 2009
435
By the way, my professor replied and said that this means the same thing as what I wrote in my first post,

Rich (BB code):
int find( int a[], int n, int x)
{
    int i;
  
    for (i=0; i!=n; i++){
        if(a==x){
            
                     return i;
}
}
return -1;


}
Umm, I don't think thats right. Isn't this positioning of the curly brackets saying, return i, if a == x. Then once you exit the for loop return -1. Just the action of exiting the for loop means to return -1.

That sounds weird. Is that what the code in my first post does also?
 

mik3

Joined Feb 4, 2008
4,843
int find is a subroutine and the return instruction ends the subroutine. Thus, it will not execute the return -1 if a==x.
 

Thread Starter

count_volta

Joined Feb 4, 2009
435
int find is a subroutine and the return instruction ends the subroutine. Thus, it will not execute the return -1 if a==x.


Okay I see, so if a == x, it will return i and go back to main program, but if a never equals to x, it will get to the end of the loop, jump out of the loop and return -1 and then go back to main program.

Cool thanks. Now I can do that in mips assembly pretty easily.
 

Harrington

Joined Dec 19, 2009
85
Hi this should give you a better idea of how this works

I've rewritten the code for you so that it makes more sense then you can see what it does and hopefully get a better understanding

#include <stdio.h>


// declare global functions

int find( int a[], int n, int x) ;

int Array[] = { 1,3,5,4,10} ;


// define the function in full here


int find( int a[], int n, int x)
{
int i = -1 ;

for (i=0; i!=n; i++)
{

if(a==x){

printf("Match found %d at index X = %d \n \n ",i,x);

return i;


} // end if

}// end for


if (i == -1 )
{
printf("\n No Match found \n") ;

}

return i;



}// end function






void main()
{


char ch =' ';

int x = 0;

int n = 0 ;

int match = 0 ;

int j = 0 ;

int flag = 1 ;


printf("Enter q to quit or r to run \n\n");


while(flag ==1){


ch = getchar(); // get user input


if(ch == 'r')
{
printf(" Enter a value for x please followed by enter\n ") ; // prompt the user for input

scanf ("%d",&n);

printf("Enter a value for n please in the range of 0 to 5 followed by return\n");

scanf ("%d",&x); // get user input

match = find(Array,n,x); // simply return our value here



printf("\n Array contents were \n");
for( j = 0 ; j < 5 ; j ++)

{

printf(" %d " ,Array[j]);
}




printf("\n\n q to quit or r to run\n\n");

}

if (ch =='q')
{

flag = 0 ;

}



} // end while


printf("\n User typed q \n **** Quitting program **** \n" );




}// end main method
 
Top