I have a simple program in mind where it reads the input first name and last name respectively and shows the number of characters present in each, but I am getting both the values as 5.
Here is the code:
#include<stdio.h>
void main()
{
char fname[20];
char lname[20];
int count=0;
int counter=0;
printf("enter your first name");
scanf("%s",fname);
printf("enter your last name");
scanf("%s",lname);
while((fname[count]>0) && (lname[counter]>0))
{
count++;
counter++;
}
printf("the no of char in ur fname are %d ",count);
printf(" the no of char in ur lname are %d ",counter);
}
Any advice would be of great help.
void main()' is at best non-standard and is typically wrong. You should probably specify how long the name arrays in thescanf()formats ("%19s") to prevent overlong names causing crashes. You should print a newline at the end of the output (probably at the end of each of the last twoprintf()statements. If you are using a C99 compiler, omittingreturn 0;from the end of the program is 'OK' (he says with gritted teeth), but it is better to be explicit and return the correct status. You should probably make the twoprintf()statements more symmetric (no stray blanks).mainsignature. From the C99 standard (5.1.2.2.1): "It shall be defined with a return type of int and with no parameters ... or with two parameters ... or in some other implementation-defined manner"void main()does exit - and the MS web site is silent too.