this post was submitted on 25 Apr 2026
17 points (84.0% liked)

Ask Lemmy

39277 readers
2222 users here now

A Fediverse community for open-ended, thought provoking questions


Rules: (interactive)


1) Be nice and; have funDoxxing, trolling, sealioning, racism, toxicity and dog-whistling are not welcomed in AskLemmy. Remember what your mother said: if you can't say something nice, don't say anything at all. In addition, the site-wide Lemmy.world terms of service also apply here. Please familiarize yourself with them


2) All posts must end with a '?'This is sort of like Jeopardy. Please phrase all post titles in the form of a proper question ending with ?


3) No spamPlease do not flood the community with nonsense. Actual suspected spammers will be banned on site. No astroturfing.


4) NSFW is okay, within reasonJust remember to tag posts with either a content warning or a [NSFW] tag. Overtly sexual posts are not allowed, please direct them to either !asklemmyafterdark@lemmy.world or !asklemmynsfw@lemmynsfw.com. NSFW comments should be restricted to posts tagged [NSFW].


5) This is not a support community.
It is not a place for 'how do I?', type questions. If you have any questions regarding the site itself or would like to report a community, please direct them to Lemmy.world Support or email info@lemmy.world. For other questions check our partnered communities list, or use the search function.


6) No US Politics.
Please don't post about current US Politics. If you need to do this, try !politicaldiscussion@lemmy.world or !askusa@discuss.online


Reminder: The terms of service apply here too.

Partnered Communities:

Tech Support

No Stupid Questions

You Should Know

Reddit

Jokes

Ask Ouija


Logo design credit goes to: tubbadu


founded 2 years ago
MODERATORS
 

Edit: After prompting the user to choose between "login (1)" and "create an account (2)", I am trying to have the program check that:

  • the input is an integer
  • that the input, in case of an integer, is only valid if one (1) or two (2)
  • (optionally) that the data size of the user input is not greater than the integer number one (1) or two (2), as a security measure, to hinder overflow exploits

If I allocate [] or [1] to intCheck the program goes into an endless loop from the very start. No user input required. If, however, I allocate [2] or more, the program works as intended, as long as the total size of bytes of the user input is less than the number of bytes previously allocated. If, however, the user input results in a byte size greater than what was preallocated, the program repeats the else condition of the while loop - here, printf - that number of times. Is this an overflow of choice or what?

//Bank of Haruhi  
 
//TODO  
// - Create user account: require user first name, last name, user name, password, age (deny if < 20 yrs)  
// - Prompt login (deny if != password && username)  
// - Display menu ("About the Bank of Haru", "Account Settings", "Check your balance", "Deposit", "Withdraw", "Close account", "Logout")  
 
#include <stdio.h>  
#include <string.h>  
#include <stdbool.h>  
 
int main(void) {  
 
//Base 
char intCheck[3] = ""; //Will "if (sscanf(char,%d,&int) == true)" check for ANY "char" or a predetermined amount of "char"?  
 
//Registration and login  
char firstName[] = "";  
char lastName[] = "";  
char password[] = "";  
int age = 0;  
 
//Menues and selections  
int choice = 0;  
int arithmeticChoice = 0;  
 
//Balance, deposit and withdrawal  
float balance = 0.0;  
float deposit = 0.0;  
float withdraw = 0.0;  
 
printf("Welcome to the Bank of Haruhi! Please login (1) or create an account (2): ");  
 
while (fgets(intCheck, sizeof(intCheck),stdin)) {  
intCheck[strlen(intCheck) - 1] = '\0';  
if (sscanf(intCheck, "%d", &choice) == true && sizeof(intCheck) == 4 && choice == 1 || choice == 2) break; //I'm keeping "sizeof(intCheck)" in order to excercise byte size input validation, but I could just remove it and let "if choice == 1 || 2" restrict the valid input.  
else printf("Please enter ""1"" to login or ""2"" to create a new account: ");  
}  
 
return 0;  
 
}  

The program is, of course, not nearly complete. I just stopped doing anything else, as soon as I stumbled upon this phenomenon.

you are viewing a single comment's thread
view the rest of the comments
[–] akunohana@piefed.blahaj.zone 4 points 2 days ago (1 children)

Wow! I didn't even realize that functions have these man pages looking docs!!! Complete beginner here... I also had no idea that there are debuggers that can help with this. I'll look it up tomorrow! Thanks! :)

[–] AudaciousArmadillo@piefed.blahaj.zone 6 points 2 days ago (1 children)

Ah ok, glad I could help. The debugger can be confusing if you have never used it before. If you are a complete beginner to programming, I'd recommend starting with something other than C. Python, Java or Go are a lot more forgiving in most aspects. Especially dealing with strings is not easy in C as it involves a lot of low level details already. But don't be discouraged, its a fun journey learning C but there are a lot of gotchas.

[–] akunohana@piefed.blahaj.zone 3 points 2 days ago (2 children)

Yeah, a lot of people have told me so, he he. But I started my journey into IT with a pretty low level - albeit very beginner friendly - book by Charles Petzold: Code: The Hidden Language of Computer Hardware and Software. I have also been greatly influenced by Linux. I guess these two aspects combined made me pick up C. 😊

[–] kryptonianCodeMonkey@lemmy.world 4 points 1 day ago* (last edited 1 day ago) (1 children)

C is unforgiving and... let's call it "quirky" with anything that resembles UI. It's just not built for it. C sits just above Assembly in low level code, and it has many pitfalls in that regard.

It's not that it's not valuable to learn, because it is. But learning C is more about learning about the backend of how programming languages work (pointers, heaps/stacks, memory allocation and cleanup, etc.) and the specific quirks and dangers of C itself than it is about teaching you good programming skills.

Modern programming isn't about guarding against overflow, managing memory, or dealing with pointers and memory manipulation. Having a deep understanding of those things will do very little for your actual programming skills. It's far more important to understand data structures and algorithms, and having some exposure to different paradigms is a good idea too.

Python is an easy entry point, and frankly, many have made their careers off of python alone. It is often slow at scale and has its own quirks, good and bad, but it's far easier to focus on the logic than the quirks with Python than compared to C. It has built in tools for basically any paradigm, though it is built primarily for object oriented and procedural, but has some nice declarative tools like list comprehensions too. Java is also a common language in the industry and gives you more exposure to Object Oriented Programming. Try out Haskell for pure functional and prolog for logic/declarative. C lacks all of those things.

[–] Zwuzelmaus@feddit.org 1 points 1 day ago

Some very reasonable points, but I do not support what you say about C itself.

But learning C is more about learning about the backend of how programming languages work (pointers, heaps/stacks, memory allocation and cleanup, etc.)

A bunch of languages work this way, and then you can understand them better if you have learned C before.

But other languages simply don't work this way.

This is not specific about C, it is just coincidentally that the creators of newer languages had learned their C before, and then rebuilt some of the basic concepts into their new languages.

Java is also a common language in the industry and gives you more exposure to Object Oriented Programming.

But then you should actually skip Java and go directly to Kotlin.

Try out Haskell for pure functional and prolog for logic/declarative. C lacks all of those things.

C already has the basics of these concepts, that have been developed further a little later, while C was developed into C++ (which I don't recommend anymore today).

[–] Zwuzelmaus@feddit.org 4 points 2 days ago* (last edited 1 day ago)

I have learned C >30 years ago from the book "The C programming language" (Kernighan/Ritchie) and I still recommend it because it really teaches the language itself.

Besides that, you also want to learn about algorithms and data structures and software architecture and design patterns.