Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

C Programming Question

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
12 posts • Page 1 of 1
Author
Message
pyro120
n00b
n00b
Posts: 14
Joined: Mon Feb 21, 2005 4:20 am

C Programming Question

  • Quote

Post by pyro120 » Wed Mar 09, 2005 10:42 pm

I am fairly new to C as a programming language, I have a little experience with it and robotics, however I was trying to write a simple program that would take a distance(in inches) and convert it to the number of sprockets that would be counted by a hall effect sprocket counter. I am having some trouble however with how to input the integer value for inches. I tried scanf but that didnt work. What else can I try to fix this problem?

A.J.
Top
Hal[PT]
n00b
n00b
Posts: 33
Joined: Sun Feb 09, 2003 1:02 pm
Location: Lisbon

  • Quote

Post by Hal[PT] » Wed Mar 09, 2005 10:44 pm

You can try scanf again...

Code: Select all

int my_input;

...

scanf(" %d", &my_input);
No?[/code]
Top
pyro120
n00b
n00b
Posts: 14
Joined: Mon Feb 21, 2005 4:20 am

  • Quote

Post by pyro120 » Thu Mar 10, 2005 2:09 am

No, I just tried a scanf again and it comes up with a runtime error. I am using lcc compiler for windows.. I havent fully switched to gentoo yet. Here is my code. There are probably a few errors in it if anyone would like to check it for me.

Code: Select all

#include <stdheaders.h>

int main(void){
	int inches;
	int revolutions;
	int sprockets;

	printf("Inches:");
	scanf("%d",inches);

	revolutions = inches / 25;
	sprockets = revolutions * 4;

	printf("sprockets %d",sprockets);
}
Thanks in advance.. AJ
Top
EzInKy
Veteran
Veteran
User avatar
Posts: 1742
Joined: Fri Oct 11, 2002 10:24 pm
Location: Kentucky

  • Quote

Post by EzInKy » Thu Mar 10, 2005 3:05 am

printf("Inches:");
scanf("%d",inches);
You need to pass the address of "inches", not it's value. Look at Hal's example again.

Here is the relevant section from the Gnu C library info page:

int scanf (const char *template, ...)

The scanf function reads formatted input from the stream stdin under the control of the template string template. The optional arguments are pointers to the places which receive the resulting values.

The return value is normally the number of successful assignments. If an end-of-file condition is detected before any matches are performed, including matches against whitespace and literal characters in the template, then EOF is returned.
Time is what keeps everything from happening all at once.
Top
bll0
Apprentice
Apprentice
User avatar
Posts: 178
Joined: Sat May 22, 2004 10:54 pm
Contact:
Contact bll0
Website

  • Quote

Post by bll0 » Thu Mar 10, 2005 6:47 am

scanf versus printf:

Code: Select all

scanf("%d",&inches);

                versus

	printf("sprockets %d",sprockets);
I need a girl whose name doesn't end in .jpg
Top
voidengineer
n00b
n00b
Posts: 54
Joined: Fri Feb 11, 2005 11:31 pm

blah blah blah, <insert something cool here>

  • Quote

Post by voidengineer » Thu Mar 10, 2005 10:08 am

You should try man scanf first! Then consider trying sscanf :)

Code: Select all

#include <stdheaders.h>
#define STRBUF 20 /* or whatever */
int main(void)
{
        int inches;
        int revolutions;
        int sprockets;
        char readLine[STRBUF];

        printf("Inches:");
        fgets(readLine, STRBUF*sizeof(char), stdin);
        sscanf(readLine, "%d", &inches);
   
        revolutions = inches / 25;
        sprockets = revolutions * 4;

        printf("sprockets %d", sprockets);
}
I didn't actually compile this, but it should work. Please pay your respects to the people that bring you linux, and the men that created C, and make tabstops 8 spaces please.
Top
bll0
Apprentice
Apprentice
User avatar
Posts: 178
Joined: Sat May 22, 2004 10:54 pm
Contact:
Contact bll0
Website

Re: blah blah blah, <insert something cool here>

  • Quote

Post by bll0 » Thu Mar 10, 2005 11:03 am

voidengineer wrote:Then consider trying sscanf :)
sscanf is not secure because it may cause buffer underruns. I think your program might not be very security relevant, but don't get used to sscanf.
And if you really want to have a good compiler, try coding in your gentoo and run gcc, as lcc is only an adaption of it for windows.
I need a girl whose name doesn't end in .jpg
Top
voidengineer
n00b
n00b
Posts: 54
Joined: Fri Feb 11, 2005 11:31 pm

roflmao buffer underruns

  • Quote

Post by voidengineer » Thu Mar 10, 2005 11:39 am

Ok, well do what you will with your super secure code, but remember one thing. When people go blabbing about secure code and what not; remember that it's not what functions were chosen, it's how they were used.

Code: Select all

 ...,STRBUF*sizeof(char), ... 
rofl buffer underruns
Top
emil
n00b
n00b
Posts: 44
Joined: Mon Feb 10, 2003 1:17 pm
Location: Ireland

Re: blah blah blah, <insert something cool here>

  • Quote

Post by emil » Thu Mar 10, 2005 5:38 pm

voidengineer wrote:Please pay your respects to the people that bring you linux, and the men that created C, and make tabstops 8 spaces please.
Or just use whatever suits _you_. :)
Top
Genone
Retired Dev
Retired Dev
User avatar
Posts: 9656
Joined: Fri Mar 14, 2003 6:02 pm
Location: beyond the rim

Re: blah blah blah, <insert something cool here>

  • Quote

Post by Genone » Thu Mar 10, 2005 7:25 pm

Azazel_7 wrote:And if you really want to have a good compiler, try coding in your gentoo and run gcc, as lcc is only an adaption of it for windows.
I think you're wrong with that statement.
Top
bll0
Apprentice
Apprentice
User avatar
Posts: 178
Joined: Sat May 22, 2004 10:54 pm
Contact:
Contact bll0
Website

Re: blah blah blah, <insert something cool here>

  • Quote

Post by bll0 » Thu Mar 10, 2005 11:49 pm

Genone wrote:
Azazel_7 wrote:And if you really want to have a good compiler, try coding in your gentoo and run gcc, as lcc is only an adaption of it for windows.
I think you're wrong with that statement.
Sorry, my mistake. Mixed-up accidently lcc and cygwin... So you're right.
I need a girl whose name doesn't end in .jpg
Top
pyro120
n00b
n00b
Posts: 14
Joined: Mon Feb 21, 2005 4:20 am

  • Quote

Post by pyro120 » Fri Mar 11, 2005 2:01 am

Ok.. After fixing my scanf statement everything worked fine Thanks for the help.

A.J.
Top
Post Reply

12 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic