View Full Version : Basic C questions
Arowa704
08-19-2004, 03:12 AM
I'm a newbie at C
1) If you set an array like:
char name[20];
That means it's only set for 20 characters, right?
So technically, the user shouldn't be able to enter 20 characters, if i were to prompt the user and store it in that array.
If so, when does it crash?
I tried it out and sometimes it will crash immediately, sometimes not.
2) What's the difference betweent printf and gets?
I believe gets is simpler and doesn't have any formats?
3) Anyone knows any good forums specificaly for C?
Thanks
rahulkothari
08-21-2004, 02:07 AM
1) If you set an array like:
char name[20];
That means it's only set for 20 characters, right?
hehehe... even i got a bit confused, and tried out a simple proggie which accepts characters in a char array[5]. Result is exactly same as yours sometimes it will crash immediately, sometimes not.
After some googling, found that, if you try to enter more than 19 characters (last character is reserved for '\0' that is, for terminating the string) the extra characters may end up being used for the next item of input or may be thrown away. The program may breeze right past the next input statement due to the extra characters. In any case, the results are not dependable if too many characters are entered.
2) What's the difference betweent printf and gets?
I believe gets is simpler and doesn't have any formats?
gets() is like scanf() that is both receive input from keyboard, the only difference is that scanf() has some limitations while receiving strings of characters, as the foll example illustrates
main()
{
char name[50];
printf("\nEnter name ");
scanf("%s",name);
printf("%s",name);
}
and herez the output..
Enter name Rahul Kothari
Rahul
"Kothari" never got stored in the array name[], because the moment the blank was typed after "Rahul" scanf() assumed that the name being entered has ended. The solution is to use gets(). Thus spaces and tabs are perfectly acceptable as part of the input string in gets().
The limitation of gets() is that it can be used to read only 1 string at a time.. unlike scanf("%d %c", &age, &initial).. u get the point.
printf() and puts() work exactly opposite to the above. Try it out for yourself ;). Again, the difference between the two is puts() can output only one string at a time.
3) Anyone knows any good forums specificaly for C?
www.programmersheaven.com
Also, you may want to check out this book.. LET US C by Yashwant Kanetkar, excellent book for beginners.
http://www.bpbonline.com/books/aspBookDetail.asp?ID=1634
utpal2006
09-16-2006, 04:56 AM
The difference between printf and gets :
printf is used for print.
you can used puts just like a printf.
gets is used just like a scanf.:)
utpal2006
09-16-2006, 06:08 AM
what is the means of this funtion?
char*s
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.