How about: How can I take in integers in C without using scanf?

  • Thread starter Simon Clement
  • Start date
In summary: Read = fread(buffer, sizeof(char), sizeof(buffer) - 1, stdin); //-1 to save room for the null terminatorbuffer[charactersRead] = getc(stdin); In summary, getc() reads one character from stdin.
  • #1
Simon Clement
18
3
Hello
So please I'm quite new at C programming. I've been using scanf a lot in my codes but I also encounter some problems with it and I have been told to try and avoid using scanf.
Now I'm learning getc function, but don't quite get it fully. I've surfed the net and all I see are very basic examples.

Please why does the code below compiles well but doesn't execute fully? Also is there a way to take in integers without using scanf i.e without using something like

Code:
scanf("%d",&x);
Code:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

int main()
{
 char *name;
 printf("Enter full names: ");
 gets(name);
 
 printf("Thank you Mr. %s",name[0]);
 
 return 0;
}
 
Last edited:
Technology news on Phys.org
  • #2
People may object to scanf because it is vulnerable to string overflows.
Your code uses gets. I think it has the same problem as scanf. If you are worried about string overflows, you should treat it like scanf.
For my own use and use by trusted code, I never worried about string overflows. But string overflows are a way that hackers can insert their own code into your program. So you should avoid it in distributed public code.

Microsoft compilers include a scanf_s (the '_s' stands for safe), which includes a parameter for the maximum number of characters to read. That is safe to use. Other compilers might have something similar; I don't know.

getc is very basic and is tedious to work with directly unless you need the program to respond before the user hits Enter. But you can use it in a utility that gives you a safe scanf. If you are not using a Microsoft compiler, you can write your own scanf_s.
 
  • #3
C has several input functions getc() gets one character from the terminal. You are responsible for taking the character and appending it to a string or validating it depending on your needs.

https://www.tutorialspoint.com/c_standard_library/c_function_getc.htm

I often used gets() to get a string of input terminated when a \r character (CR or carriage return) was entered then I would validate it and if it was numeric then I'd use the sscanf() or atoi() or atof() to decode it depending on what I was trying to do.

Every programmer has a different take on it:

http://stackoverflow.com/questions/7021725/converting-string-to-integer-c
 
  • Like
Likes Simon Clement and FactChecker
  • #4
Simon Clement said:
Hello
So please I'm quite new at C programming. I've been using scanf a lot in my codes but I also encounter some problems with it and I have been told to try and avoid using scanf.
Now I'm learning getc function, but don't quite get it fully. I've surfed the net and all I see are very basic examples.

Please why does the code below compiles well but doesn't execute fully? Also is there a way to take in integers without using scanf i.e without using something like

Code:
scanf("%d",&x);
Code:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

int main()
{
 char *name;
 printf("Enter full names: ");
 gets(name);
 
 printf("Thank you Mr. %s",name[0]);
 
 return 0;
}
There are a couple things wrong with your code.
1. name is declared as a pointer to a char, but no space is allocated for the storage of whatever characters will eventually be read in. Since you're new at C, I won't suggest using malloc() to allocate space on the heap. Instead, declare name as an array of type char, with enough space in the declaration to hold the full name.
Something like this:
C:
char name[30];
Now, as long as someone enters a name with 29 or fewer characters, the above will be fine. The reason for 29 rather than 30 is to leave room for the terminating null character.
2. name[0] represents only a single character, not the address of all of the characters in the string. Instead, do this:
Code:
printf("Thank you Mr. %s", name);
 
  • Like
Likes Simon Clement, QuantumQuest and FactChecker
  • #5
I would not recommend using either one of them, because as said above, both risk buffer overflow.

It's important to understand what the keyboard input actually is. It actually has nothing to do with the keyboard, you probably know that you can pipe the output of one process as the input of another. So what's actually happening?

It's a file descriptor ;) All programs have at least three "files" open at any given time: stdin, stdout, and stderr. You can treat them exactly like FILE descriptors. So in order to read from stdin (which is your keyboard in this case) with a known buffer size:

Code:
char buffer[30];
size_t charactersRead = fread(buffer, sizeof(char), sizeof(buffer) - 1, stdin);  //-1 to save room for the null terminator
buffer[charactersRead] = '\0';  //You have to add your own null terminator in this case
 
  • Like
Likes Simon Clement and DrClaude

1. What is the main difference between gets and scanf?

The main difference between gets and scanf is the way they handle input. Gets reads input from the keyboard until it reaches a newline character, while scanf reads input until it reaches a whitespace character.

2. Can I use gets and scanf interchangeably?

No, gets and scanf are not interchangeable. Gets is meant for reading strings, while scanf can read various types of data such as integers, floats, and characters.

3. Why is gets considered unsafe?

Gets is considered unsafe because it does not check the length of the input string, which can lead to buffer overflow and potential security vulnerabilities.

4. Are there any alternatives to using gets?

Yes, there are alternative functions such as fgets and gets_s that provide safer ways of reading input from the keyboard.

5. Can I use gets and scanf together in the same program?

Yes, you can use gets and scanf together in the same program. However, it is recommended to use them carefully and properly validate user input to prevent any potential issues or vulnerabilities.

Similar threads

  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
4
Views
730
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
3
Views
993
  • Programming and Computer Science
Replies
6
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
875
  • Engineering and Comp Sci Homework Help
Replies
3
Views
661
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
Back
Top