Kangaroo07
Badges: 0
Rep:
?
#1
Report Thread starter 4 years ago
#1
How do you use both scanf and gets in this given program? What's the difference between scanf and gets function?
0
reply
Kangaroo07
Badges: 0
Rep:
?
#2
Report Thread starter 4 years ago
#2
Name:  Screenshot (19).png
Views: 29
Size:  104.4 KBAttachment 663656663658
Attached files
0
reply
Kangaroo07
Badges: 0
Rep:
?
#3
Report Thread starter 4 years ago
#3
[QUOTE=JCCO;72152766] Do you have an answer? If not, you can show your ass somewhere else!
0
reply
username3079870
Badges: 19
Rep:
?
#4
Report 4 years ago
#4
On my phone atm but avoid using scanf (if you absolutlely have to use scanf) like this. Its not secure as you aren't doing any kind of bounds check. Left unchecked you could have a buffer overflow.

I know its not part of the question but its best practice.
0
reply
NecroKamios
Badges: 9
Rep:
?
#5
Report 4 years ago
#5
This offline practise sheet you're using is not appropriate for best practice programming.
I may end up reiterating some things you already know here:
* Both scanf() and gets() read input into a c-string.
* gets() is delimited by newline and EOF, scanf() is delimited by whitespace, newline and EOF.
* scanf() can take a parameter for the length of the input as to prevent buffer overflow, gets() just reads until it's delimited.

Buffer Overflow = When writing data, data overruns boundary and overwrites adjacent memory location data.
Example: You assigned a c-string of length 40 chars, gets() doesn't get delimited before reaching 40 characters), so it keeps reading into the c-string.
A c-string is an array of chars with a terminating 0 character on the end. Primitive array's in C have no tracking on how long they are (no bound checking).
The notation of arr[20] to access an array defined char arr[40] is equivalent to arr[0] + 20. Hence if you decided to read/write on arr[41], you would not get an error for trying to do so, but you may have overwritten something else that was at the memory location defined by arr[0] + 41.

The current recommended practice in C when taking input is to put a buffer limit on the input method (like how scanf() allows) or to use fgets() instead of gets(). Also, gets() is a deprecated method. No buffer check methods may exist only for explicit cases where the input is guaranteed to not overflow.

So... to answer your question, in your case, almost no difference, I think that exercise sheet should be ditched.
0
reply
username3079870
Badges: 19
Rep:
?
#6
Report 4 years ago
#6
(Original post by NecroKamios)
So... to answer your question, in your case, almost no difference, I think that exercise sheet should be ditched.
It's a really badly constructed question because as you say, it's not encouraging best practice and I'm not exactly sure whoever it is who wrote that question wants the student to demonstrate.

I mean you could read it to do something like:


#include <stdio.h>

int main()

{
char daylabel[3];
char monthlabel[3];
char yearlabel[5];

printf( "Enter a Day in DD format :");
gets( daylabel );

printf( "Enter a Month in MM format :");
gets( monthlabel );

printf( "Enter a Year in YYYY format :");
gets( yearlabel );

printf("Date: %s \nMonth:%s \nYear:%s \n", daylabel, monthlabel, yearlabel);

return 0;
}

To do what it is basically asking. That is a totally insecure solution though and even using scan isn't secure.

P.S. I haven't compiled the above code, did it during my tea break on Notepad lol!
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Do you think receiving Teacher Assessed Grades will impact your future?

I’m worried it will negatively impact me getting into university/college (12)
40%
I’m worried that I’m not academically prepared for the next stage in my educational journey (4)
13.33%
I’m worried it will impact my future career (2)
6.67%
I’m worried that my grades will be seen as ‘lesser’ because I didn’t take exams (6)
20%
I don’t think that receiving these grades will impact my future (3)
10%
I think that receiving these grades will affect me in another way (let us know in the discussion!) (3)
10%

Watched Threads

View All