LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 11-16-2004, 10:00 AM   #1
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Rep: Reputation: 30
what does "prog7.c:11: error: invalid operands to binary %" mean


hey

I've started learning C and one below is the code for one of the exercises I'm doing:

#include <stdio.h>

double daysAndHours(char prompt[]);

void main(void)
{
double numOfHours, days, hours;

numOfHours = daysAndHours("Enter number of days: ");
days = numOfHours / 24;
hours = numOfHours % 24;
printf("You've entered %0.0f,\nwhich makes it %0.0f days and %0.0f hours.\n", numOfHours, days, hours);
}

double daysAndHours(char prompt[])
{
char string[BUFSIZ];
double aNumber = 0.0;

printf("%s", prompt);
gets(string);
sscanf(string, "%lf", &aNumber);
return aNumber;
}
The question of an exercise is as follows:

Write a program which will input a whole number of hours and convert it into days and hours, e.g. 27 hours input converts to one day and three hours output.

When i try to compile it (cc -o prog7 prog7.c), it gives the following warnings and the error:

rog7.c: In function `main':
prog7.c:11: error: invalid operands to binary %
prog7.c:6: warning: return type of `main' is not `int'

can anyone help, please?

thanks
 
Old 11-16-2004, 10:13 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
To deal with the second error first:

The main() method should always be declared with a return value of int, not void. You don't need to use a return value for main, as it will return 0 normally. Note that this is a relativly recent change to the C standard, and I wouldn't be surprised if that wasn't listed in various text-books etc. that you might be using.

In C, the modulus (%) operator returns the remainder of an integer division. That is,
Code:
7 % 3
would evaluate as 1 (the remainder of 7 divided by 3). However, you're trying to take the modulus of a double-precision number, which is floating-point, and can have a fractional part. You can't do integer division on numbers unless they are an integer type, so it produces an error.

Thirdly, your program does not account for daylight savings time; not all days are 24 hours long.
 
Old 11-16-2004, 10:31 AM   #3
javakid
LQ Newbie
 
Registered: Nov 2004
Location: Mesa, AZ
Distribution: Redhat
Posts: 8

Rep: Reputation: 0
hours needs to be an int
 
Old 11-16-2004, 11:21 AM   #4
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Original Poster
Rep: Reputation: 30
thanks for your help, it works and I have another question.

Why the `gets' function is dangerous and should not be used?
 
Old 11-16-2004, 11:45 AM   #5
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
gets() does not specify the size of the buffer, so it can be very easily used to create a buffer overrun where you write data past the end of the buffer, into whatever happens to be in the next chunk of memory.

In the worst case, an attacker can give maliciously coded data to overwrite your program's code segments with their own, allowing them to easily infect your system with root kits, viruses, etc.

The safe option is to use fgets(), for which you must specify the length of the buffer. You can use the stdin stream to read from the console if that's what you want to do.
 
Old 11-16-2004, 12:08 PM   #6
hubabuba
Member
 
Registered: Jan 2002
Location: UK
Distribution: Slack 14.1
Posts: 193

Original Poster
Rep: Reputation: 30
I see. The definition "char string[BUFSIZ];" in function (see code below) which uses the BUFSIZ to specify the maximum number of characters to be stored (typically 512) and then the gets(string); places string of characters in the "string" variable. So, does the gets() ignore the BUFSIZ specification?

double daysAndHours(char prompt[])
{
char string[BUFSIZ];
double aNumber = 0.0;

printf("%s", prompt);
gets(string);
sscanf(string, "%lf", &aNumber);
return aNumber;
}
 
Old 11-16-2004, 12:13 PM   #7
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 67
BUFSIZ is a convenient block size for reading data in. gets() and fgets() will both read in as much data as they can; internally, they may call read() using BUFSIZ as the block size (so no, they don't technically ignore it), but they will call it repeatedly.

Also, remember that C strings are null-terminated; if you have a buffer size 512, then it can hold only 511 characters (and the terminating NULL character).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Cedega from Fat32 (Invalid path "." given for "--use-dos-cwd") bdox Linux - Software 0 03-30-2005 12:24 PM
Cedega and Fat32 (Invalid path "." given for "--use-dos-cwd") bdox Linux - Games 0 03-26-2005 03:48 AM
invalid binary "/usr/bin/postgres" while running postgresql ukrainet Linux - Newbie 1 12-01-2004 09:36 AM
Linux Emulation problem "ELF file OS ABI invalid Error" man26 *BSD 0 08-04-2004 12:57 AM
Partition problem windows not booting "Invalid disk error" julian_s Linux - Newbie 1 04-02-2004 10:26 PM


All times are GMT -5. The time now is 11:08 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration