LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2008, 09:12 AM   #1
kyle4579
LQ Newbie
 
Registered: May 2008
Posts: 13

Rep: Reputation: 0
why does this program not compile for me?


i wrote this program as an exercise involved in learning the c programming language:

Quote:
#include <stdio.h>

int conversion(int n);

main()
{

int celsius, fahr;

for (celsius = 0; celsius <= 300; celsius = celsius + 20)
printf("%3d %3d\n", celsius, conversion(celsius));

}

int conversion (int n)
{
int i;


for (i=0; i <= 300; ++i)
n = (9*n)/5 + 32;
return = n;
}
Unfortunately I get the following error message from the compiler:

Quote:
function.c: In function 'conversion':
function.c:22: error: expected expression before '=' token
why is this!?

any help is greatly appreciated, and please feel free to throw in any tips about programming or c, whether related or not! thanks
 
Old 07-13-2008, 09:44 AM   #2
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Quote:
Originally Posted by kyle4579 View Post
i wrote this program as an exercise involved in learning the c programming language:

Code:
#include <stdio.h>

int conversion(int n);

main()
{

    int celsius, fahr;

    for (celsius = 0; celsius <= 300; celsius = celsius + 20)
        printf("%3d %3d\n", celsius, conversion(celsius));

}

int conversion (int n)
{
    int i;


    for (i=0; i <= 300; ++i)
    n = (9*n)/5 + 32;
    return = n;
}

Unfortunately I get the following error message from the compiler:



why is this!?

any help is greatly appreciated, and please feel free to throw in any tips about programming or c, whether related or not! thanks



Almost at the end of your program, you are assigning n to return:

Code:
return = n;
That is wrong. You will just want to return n, like this:
Code:
return n;
Regards!

Last edited by Mega Man X; 07-13-2008 at 05:14 PM.
 
Old 07-17-2008, 04:11 AM   #3
kyle4579
LQ Newbie
 
Registered: May 2008
Posts: 13

Original Poster
Rep: Reputation: 0
ah yes, that solves it! Thankyou very much
 
Old 07-18-2008, 09:06 AM   #4
FranDango
Member
 
Registered: Jun 2008
Posts: 101

Rep: Reputation: 15
My following notes are rather hints on C programming in general:

Quote:
for (i=0; i <= 300; ++i)
pre-incrementing 'i' is valid statement, but for human-readability it would be easier to use
Quote:
for (i=1; i <= 300; i++)
as it makes clearer that the first used value for i will be 1 and not 0.

I would rather write
Quote:
for (i=0; i <= 300; ++i) {
n = (9*n)/5 + 32;
}
return = n;
to make clear what happens inside the loop and what happens afterwards.

These remarks are not relevant for your quite simple code, but can be helpful for a larger codebase.

Linux Archive

Last edited by FranDango; 09-20-2008 at 05:38 AM.
 
Old 07-18-2008, 09:17 AM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
You really need to take another look at conversion(). I don't think it is doing quite what you want it to do.

Why is there a for loop in it? You are computing the same value 300 times, then returning it.
 
Old 07-18-2008, 09:58 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by FranDango View Post
Code:
for (i=0; i <= 300; ++i)
pre-incrementing 'i' is valid statement, but for human-readability it would be easier to use

Code:
for (i=1; i <= 300; i++)
as it makes clearer that the first used value for i will be 1 and not 0.
In the pre-incrementing loop the first value for i would be 0, so those are not equivalent. The ++i is C++'ism stemming from the fact that the pre increment operator usually more efficient on iterator objects (C++ Operator: Why should I use '++i' instead of 'i++'?). Although as jiml8 said, the loop shouldn't be there at all...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How does one compile a program? What do you need? TheMusicGuy Linux - Newbie 4 03-12-2004 01:10 AM
Compile Program mattp Linux - Newbie 4 03-09-2004 11:13 AM
program want compile dlm4444 Programming 10 09-26-2003 08:08 PM
Compile qt program. hfawzy Linux - General 2 02-18-2003 10:25 AM
compile program Eddie9 Linux - General 1 05-21-2002 10:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration