Re: i need help about Turbo C
- From: Simon Biber <news@xxxxxxxxx>
- Date: Mon, 17 Jul 2006 19:16:51 GMT
Manmeet Mittal wrote:
>> printf("\n Name"); scanf("%s",name);Hi Pipito, This is manmeet Mittal, U can also write the code as follows :-
Bad idea. The %s has no limit to how many characters it will attempt to read and place in the name array. It might overflow the array. This kind of bug is the cause of most of the security issues in programs around the world. It's important to be vigilant and always consider buffer overflow, no matter how simple or trivial the program is.
>>> In input of age u can use ' & ' ampersand.printf("\n Address"); scanf("%s",address);
printf("\n Age"); scanf("%d",&age);
' \n ' is an escape sequence which causes the output in new line.
If age has type int, then you are right, you need the ampersand to give the address of age, so that scanf can store into your age variable.
U can also write code to input a string as follows :-
puts("\n Name"); gets(name);
NO!!! NEVER USE gets!!!
Didn't I just tell you above, how important it is to consider buffer overflows? The gets function is bad, evil and wrong. It is unsafe, and it cannot be made safe. Do you see why?
One alternative is to use fgets:
fgets(name, sizeof name, stdin);
char *p = strchr(name, '\n');
if(p != NULL) *p = 0;
The two extra lines are to find and remove the newline character from the end of the string, since fgets doesn't do that for you.
I think it will serve your purpose. If u have any query then send ur queris.Name :-Kumar Gaurav
address :-India
age 19
A man of your age should know better than to use juvenile abbreviations like ‘u’ when talking to adults.
--
Simon.
(for fun, ...)
Name :- Simon Biber
Address :- Sydney Australia
Age :- 23
.
- References:
- i need help about Turbo C
- From: pipito
- Re: i need help about Turbo C
- From: sarathy
- Re: i need help about Turbo C
- From: Manmeet Mittal
- i need help about Turbo C
- Prev by Date: Help for designing outlines in c language
- Next by Date: Re: How to write a small graceful gcd function?
- Previous by thread: Re: i need help about Turbo C
- Next by thread: Simple RegExp function
- Index(es):
Relevant Pages
|