Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.
readline char array limit in C
#include <stdio.h>
int main (void)
{ int i; char line[81]; void readLine (char buffer[]);
for ( i = 0; i < 3; ++i )
{
readLine (line);
printf ("%s\n\n", line);
}
return 0;
}
void readLine (char buffer[] ) { char character; int i = 0;
do
{
character = getchar ();
buffer[i] = character;
++i;
}
while ( character != '\n' );
buffer[i-1] = '\0';
}
The problem is I want to set a limit on the array so if more than 81 characters are put in with no new line character, it will store the null character at buffer[80] and then display the 80 characters. I've tried several things, but I really need to go to dinner right now so if you want I can fill you guys in later with more detail. Thanks in advance for any help.
edit- Sorry I noticed a couple syntax errors from when I was copying the code down into this thread. Like I said I was in a hurry, but it's fixed now.
#include <stdio.h>
int main (void)
{
int i;
char line[81];
void readLine (char buffer[]);
for ( i = 0; i < 3; ++i )
{
readLine (line);
printf ("%s\n\n", line);
}
return 0;
}
void readLine (char buffer[], )
{
char character;
int i = 0;
do
{
character = getchar ();
if(i<=80)
{
buffer[i] = character;
++i;
}
}
while ( character != '\n' );
{buffer[i-1] = '\0';
}
How is that?