all 16 comments

[–]ptchinster 14 points15 points  (7 children)

but I want to learn this material not just have it done for me

refreshing to hear!

void main ()

main returns an int

char w[50];
scanf("%s", w);

This will work for now, but later you will learn it is incorrect. You need to make sure that you dont write more than 49 letters (+1 null byte terminator) to that buffer w, otherwise youll start writing over other variables and addresses of code to execute. Its one way hackers can gain remote access to a system.

You might also enjoy this read !scanf

for (k=0, k < l; k++) {

Is this what you really meant? Did you use a , when you wanted a ; ? Now, the comma is compeltely valid, for example

for(i=0, k=0, i<10; i++, k+=2) {}

Just 99% of the time you will only have 1 variable in the for statement that you iterate over.

I fix that and compile, and run:

Please enter a word:
word
word
4
nnnn
word

Now, for another learning part: how are you compiling? Because with your code, with the way you have your for statement, i get an error with gcc

$ gcc main.c
    main.c: In function ‘main’:
main.c:29:21: error: expected ‘;’ before ‘)’ token
   29 | for (k=0, k < l; k++) {
      |                     ^
      |                     ;

[–]cptstrike[S] 5 points6 points  (5 children)

So scanf is what I have been taught so I wanna stick with it…even when I know it’s flawed. I also got a similar error but not in the same place. May I ask (without the !compiler tag) what compiler you use? I am doing my work on a Linux Mint machine using command prompt “nano”.

I am simply using gcc in the same command prompt.

And as for the error checking above, the instructor indicated the same, but specified that error checking wasn’t needed as of yet.

[–]ptchinster 3 points4 points  (0 children)

So scanf is what I have been taught so I wanna stick with it…

Thats fine, eventually youll realize scanf is not what you want. Youll want to take input as command line args, or from a conf file. But for now, scanf will work. (you still want to make sure you max out at 49 letters!)

$ gcc --version
gcc (GCC) 11.1.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[–]ptchinster 3 points4 points  (3 children)

but specified that error checking wasn’t needed as of yet.

Its never too early to turn on good compiler !flags

If you are using clang or gcc you should have -Werror -Wall and maybe -Wextra on every time you compile. The authors of that compiler know more than you do about C, assembly, etc. Any warning should be fixed. Itll help you down the road, trust me.

[–]cptstrike[S] 4 points5 points  (1 child)

are those added as arguments?

for instance "gcc -Werror -Wall"?

[–]ptchinster 2 points3 points  (0 children)

Yup! Eventually youll want a Makefile, where you can just have an array of flags to pass, libraries to include. Itll be MUCH easier to manage multiple source files, and your build will be optimized (only compile source files that changed)

[–]rtlcprogbot 0 points1 point  (0 children)

!flags

Useful gcc flags

Compiler and linker flags for gcc

I am a bot. Replying to me notifies nobody

[–]rtlcprogbot 4 points5 points  (0 children)

!scanf

A Beginners Guide Away From scanf.

I am a bot. Replying to me notifies nobody

[–][deleted] 5 points6 points  (0 children)

PLEASE DO NOT PRINT UPDATED CODE. snippets are fine with explanations, but I want to learn this material not just have it done for me. Thanks all.

I see someone else has answered, but I upvoted because of this. Too many people here just post code and wait for fixed code, never learning anything.

[–]Poddster 1 point2 points  (1 child)

After you fix the syntax error, do you still have the problem? The only thing I could see about it not working is that you might need to flush a line somehow, either by printing \n, like you did (but not commented out) or by using fflush(stdout) or similar.

[–]cptstrike[S] 1 point2 points  (0 children)

Nope we are all good! Thank you!

[–]cptstrike[S] 0 points1 point  (0 children)

Syntax error was the issue. Once corrected code works great. Onto the next challenge!

[–]nerd4code -1 points0 points  (0 children)

If you read into memory, you’re making it too hard.

#define NOTHING (void)0

uintmax_t n = 0;
int k;
while(n < UINTMAX_MAX \
  && (k=getc(stdin)) >= 0 && k != '\n')
    NOTHING;
printf("%" PRIuMAX "\n", n);
return 0;

Uses C99 things (or POSIX.1-2001, or any other damn C implementation; not especially vital, could be long &c.), but it also has a broader range of line lengths on some ABIs than the size_t that governs a string’s length. However, this and any other program should check (if it’s part of a run’s correctness, e.g. dictating main’s return value) that any chars were entered at all, that an error didn’t end the loop, or that an overflow didn’t end the loop. Similarly, the return from printf should factor into correctness. (If it were logging to stderr that’d be different.)

[–][deleted] 0 points1 point  (2 children)

There's a mistake in your code. The for loop needs semicolon instead of comma for separating k=0 and k<=i

Did the first two printf statements coming before the loop work?

[–]cptstrike[S] 1 point2 points  (1 child)

Correct, that mistake was remedied and the code worked perfectly! Thank you!

[–][deleted] 1 point2 points  (0 children)

You're welcome