Raven49
I am sorry if I am too repetitive but I am a newbie in the world of programming
Anonymous
Add "unsigned" infront of all ur ints
Anonymous
I, d suggest going to codewars.com to practise ur skills
Anonymous
No not with int main
Anonymous
Except int main
Anonymous
Just with f
Anonymous
F and arr2 is fine
Anonymous
Rest the same
Raven49
Just with f
I did what you said Its still wrong
Anonymous
Why we use the int main in C except of main () or void main !
Anonymous
The return type of main can only be int
Anonymous
void main is wrong
Anonymous
Ye cause void main cannot return anything
Anonymous
Main is actually the linker entry point
Anonymous
Its where the compiler first starts looking at
Anonymous
When a program "calls" main
Anonymous
main must return control to the caller when it is done
Anonymous
Okay 👍
Anonymous
But the caller needs to know if the program executed successfully
Anonymous
This is why along with returning control main sends a value to the caller
Anonymous
Anonymous
Now how the value is sent
Anonymous
That depends on the architecture you're compiling for
Anonymous
You don't need to concern yourself with that unless you're dealing with the operating system or the compiler itself
Anonymous
Who is anuprita?
Anonymous
No one
Anonymous
And why is she no. 1?
артем
Who is anuprita?
I wish I could know
Anonymous
And why is she no. 1?
The name reads "anuprita is no one"
Anonymous
Anyway continuing
Serenity
int flag = 1; int choice; do { printf(ENTERING_QUESTION); scanf("%d", &choice); switch (choice) { case 0: flag = 0; break; case 1: printf(QUESTION1_INPUT_MESSAGE); char bin_num[MAX_SIZE]; int FLAG = 1; scanf("%s", bin_num); int i = 0; while (bin_num[i] != '\0') { if ((bin_num[i] != '1') && (bin_num[i] != '0')) { printf(QUESTION1_ERROR_MESSAGE); FLAG = 0; break; } i++; } if (FLAG == 0) break; else { unsigned long long int ret; ret = strtoul(bin_num,NULL,10); printf("%lli", ret); } break;
Serenity
I want to ask a question about the last part of my code.
Anonymous
You definitely do need to check with caller about your return value Just because it says int main doesn't mean you can return something like (~0U) >> 1 Some callers can only accept 8 bits for the return value You definitely should check what values to return with your caller
Serenity
As I enter a string longer than 10 binary digits , the function produces undefined behavior.
Serenity
The main purpose of the function is to convert a string with binary digits to a decimal number ( that still has just 1's and 0's)
Serenity
MAX_SIZE is pre-defined
Serenity
The max number of bits I want to have converted is 31.
Serenity
Why does it even matter if I may ask ?
Serenity
I changed it but the functionality's still the same.
Anonymous
Smbdy cn help u, smbdy isnt me
Serenity
Maybe should I use fgets ?
артем
Otherwise you corrupt memory. Solution: use smth like %11s, where 11 is size of buffer + 1
артем
Maybe should I use fgets ?
nonono, it's wrong
Serenity
Okay
Serenity
Sorry for asking lame questions, I am new to C.
артем
char buffer[20 + 1] scanf("%20s", buffer);
Anonymous
But with fgets if your buffer is too large than what fgets is copying
Anonymous
Then the next time you call fgets
Serenity
I can assume that the input string won't be longer than 31.
Anonymous
It might automatically take input on your behalf
Anonymous
Then use fgets
Anonymous
Remember that fgets counts the null byte too
артем
You can
fgets does not ignore whitespace characters as far as I know
Serenity
scanf("%31s",bin_num)
Serenity
I have made this change but the output still wouldn't change.
Serenity
32
Anonymous
32
Doubt
Serenity
Serenity
11 digits —» undefined behavior.
Anonymous
Like I said
Anonymous
Use 32 instead of max size
Serenity
sec
Serenity
still undefined
Anonymous
still undefined
So this isn't part of some assignment right?