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

  • Thread starter Thread starter Simon Clement
  • Start date Start date
Click For Summary
The discussion focuses on issues related to input handling in C programming, particularly the use of `scanf` and `gets`. The original poster expresses confusion about using `getc` and seeks alternatives to `scanf` for reading integers. The code provided has several problems: the `name` variable is declared as a pointer without allocated memory, leading to undefined behavior, and the use of `gets` is discouraged due to buffer overflow risks. Instead, it is recommended to declare `name` as a character array with sufficient size. Additionally, the discussion highlights that both `scanf` and `gets` are vulnerable to security issues. For safer input handling, alternatives like `scanf_s` (in Microsoft compilers) or custom implementations are suggested. The importance of understanding input as a file descriptor and using functions like `fread` to manage input safely is also emphasized.
Simon Clement
Messages
18
Reaction score
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
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.
 
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
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
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
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 20 ·
Replies
20
Views
2K
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
47
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
5
Views
2K