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

Discussion Overview

The discussion revolves around alternative methods for taking integer input in C programming without using the scanf function. Participants explore various input functions, their safety, and the implications of using certain functions like gets and getc.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses frustration with scanf and seeks alternatives, mentioning their exploration of the getc function.
  • Another participant warns about the vulnerabilities associated with scanf and gets, particularly regarding string overflows, suggesting that these should be avoided in public code.
  • A different viewpoint suggests that getc is tedious for direct use but can be utilized in a utility that provides a safer scanf alternative.
  • One participant shares their experience using gets to read input until a carriage return is encountered, followed by validation and conversion using sscanf or atoi.
  • A later reply identifies issues in the original code, noting that the pointer for the name variable lacks allocated space and suggests using a character array instead.
  • Another participant emphasizes the importance of understanding keyboard input as a file descriptor and proposes using fread to read from stdin with a defined buffer size.

Areas of Agreement / Disagreement

Participants express differing opinions on the safety and usability of various input functions, with no consensus reached on the best approach to take integers without scanf.

Contextual Notes

Some participants highlight limitations related to buffer sizes, potential for buffer overflow, and the need for null termination in character arrays. There are unresolved issues regarding the best practices for input handling in C.

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   Reactions: 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   Reactions: 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   Reactions: Simon Clement and DrClaude

Similar threads

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