print each string

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Hi,

I am looking help in my code. My code supposed to store six name of student's and print the each student names

I have written following code

Code:
// language c and GCC
#include <stdio.h>

int main()
{
    char name[6];

    int i, j;

    printf("Enter name: \n");

    for (i = 0; i <5; i++)
    
        {
      
          scanf("%s", name);      

        }
    
           
    for (j = 0; j <6; j++)
    
        {
       
           printf("Your name is %s. \n", name[j]);
      
        }
    

    return 0;
}
When I run this code on my PC It gives following output

Enter name:
gajya
kamlesh
padmya
sujit
anil
'sunil' is not recognized as an internal or external command,
operable program or batch file.

someone can help me in my code. Why the name of student's are not printing on the screen?
 

djsfantasi

Joined Apr 11, 2010
9,160
Is this homework ?

When using character arrays, then individual characters are stored in each element of the array. So you have defined one name with a maximum length of 6 characters stored in position 0-5. The name must end with a null character.

Also you are reading each successive value into the same array. Not only because your array is only one row, but because you use the array pointer in your scanf statement.

How would you fix this?
 

bogosort

Joined Sep 24, 2011
696
I have written following code
On line 6 -- "char name[6];" -- you declare an array of 6 character bytes. In other words, the 'name' array can hold a single string of up to 5 characters (plus the terminating null character), which is clearly not what you need.

This seems like a homework problem, which we won't solve for you (if we did, you wouldn't learn anything). If you're having trouble understanding a particular aspect of C, feel free to ask about that.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Is this homework ?
On line 6 -- "char name[6]
This seems like a homework problem, which we won't solve for you (if we did, you wouldn't learn anything). If you're having trouble understanding a particular aspect of C, feel free to ask about that.
That's not the homework assignment. I don't know why this on homework forum.

I was implementing my logic to see result but my code failed to do this


My code failed if I write same code to store the six characters

Code:
#include <stdio.h>
int main()
{
    char name[6];
    int i, j;
    printf("Enter name: \n");
    for (i = 0; i <5; i++)
 
        {
   
          scanf("%c", &name);    
        }
 
         
    for (j = 0; j <6; j++)
 
        {
     
           printf("Your name is %c. \n", name[j]);
   
        }
 
    return 0;
}
a
b
c
Your name is c.
Your name is .
Your name is .
Your name is .
Your name is .
Your name is .
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,160
That's not the homework assignment. I don't know why this on homework forum.

I was implementing my logic to see result but my code failed to do this


My code failed if I write same code to store the six characters

Code:
#include <stdio.h>
int main()
{
    char name[6];
    int i, j;
    printf("Enter name: \n");
    for (i = 0; i <5; i++)

        {
  
          scanf("%c", name);   
        }

        
    for (j = 0; j <6; j++)

        {
    
           printf("Your name is %c. \n", name[j]);
  
        }

    return 0;
}
a
b
c
Your name is c.
Your name is .
Your name is .
Your name is .
Your name is .
Your name is .
Both of us have pointed out to you that you’ve defined a character array that will only store names up to 5/6 characters long.
  • You enter an ‘a’. It only fills the first array element. The remaining elements are undefined.
  • Then you enter a ‘b’ It overwrites the previous value in the name array which was an ‘a’.
  • Then you enter a ‘c’. It overwrites the ‘b’.
So in your example, the last value read was ‘c’. The remaining array elements are undefined and represented by the ‘.’s in the output.

Read up on character arrays. You seem to have a misunderstanding on how they are used. They DO NOT store a series of strings.
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Read up on character arrays. You seem to have a misunderstanding on how they are used. They DO NOT store a series of strings.
@djsfantasi I am not agree with you
look this
Code:
#include<stdio.h>

int main(void)
{
     int i;
    
     char name[5][5]= {"jay","viru","deck", "gaju","sinu"};
   
    for(i=0; i <5;i++)
    {
        printf(" name = %s \n",name[i]);
    }
   
   
    return 0;
   
}
see the results

name = jay
name = viru
name = deck
name = gaju
name = sinu
 

djsfantasi

Joined Apr 11, 2010
9,160
@djsfantasi I am not agree with you
look this
Code:
#include<stdio.h>

int main(void)
{
     int i;
   
     char name[5][5]= {"jay","viru","deck", "gaju","sinu"};
  
    for(i=0; i <5;i++)
    {
        printf(" name = %s \n",name[i]);
    }
  
  
    return 0;
  
}
see the results

name = jay
name = viru
name = deck
name = gaju
name = sinu
Yes, I see that code. I’m not changing my comments. They are correct.

I see a major difference between this code and your program. Compare the definitions of the character arrays in each program.

Then get back to me...
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Yes, I see that code. I’m not changing my comments. They are correct.

I see a major difference between this code and your program. Compare the definitions of the character arrays in each program.

Then get back to me...
Can you see why it takes only three input while I have declare five variables

Code:
#include <stdio.h>
int main(void)
{
   
    char letter1, letter2, letter3, letter4, letter5;
    printf("Enter name: \n");
  
  
    scanf("%c", &letter1);
    scanf("%c", &letter2);
    scanf("%c", &letter3);
    scanf("%c", &letter4);
    scanf("%c", &letter5);   
  
    return 0;
}
Enter name:
a
b
c

Edit: when I add new line it scan all user input
Code:
#include <stdio.h>
int main(void)
{
  
    char letter1, letter2, letter3, letter4, letter5;
    printf("Enter name: \n");
  
  
    scanf("%c\n", &letter1);
    scanf("%c\n", &letter2);
    scanf("%c\n", &letter3);
    scanf("%c\n", &letter4);
    scanf("%c", &letter5);   
  
    return 0;
}
Enter name:
a
b
c
d
e
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,160
Can you see why it takes only three input while I have declare five variables

Code:
#include <stdio.h>
int main(void)
{
   
    char letter1, letter2, letter3, letter4, letter5;
    printf("Enter name: \n");
  
  
    scanf("%c", &letter1);
    scanf("%c", &letter2);
    scanf("%c", &letter3);
    scanf("%c", &letter4);
    scanf("%c", &letter5);   
  
    return 0;
}
Enter name:
a
b
c
No, I can’t see that. Nor do I see you outputting what you have input. I’m not totally familiar with the scanf function. But I have many questions.

Did you do what I asked, before writing that additional code? Did you research character arrays? Did you compare the character array definitions in your original code to the same in the code of post #6?

Until you respond directly to those two questions, I might hold off on further comment. My observation is that you aren’t familiar with character arrays and character processing in general. To get Homework Help, you need to do the research. I can’t tell you.

To repeat, here are my questions.

Did you research character arrays? Did you compare the character array definitions in your original code to the same in the code of post #6?
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
Just to be sure, could you describe to me what a variable if type char is?
char is data type that tells compiler to allocate memory location to store character. also It is used to store the string and string is group of characters

This is example of five character array

char letters[5];

4 characters maximum with a null terminator
 

bogosort

Joined Sep 24, 2011
696
Edit: when I add new line it scan all user input
The scanf family of functions was designed to parse well-formatted input (like CSV files), not arbitrary user strings. If you really want to prompt the user for data from the console -- something you should almost never do -- learn how to use the fgets function, which is far more robust.
 

djsfantasi

Joined Apr 11, 2010
9,160
char is data type that tells compiler to allocate memory location to store character. also It is used to store the string and string is group of characters
And there is where you are wrong.

A string is very different from a char. A char is a single byte and hence the compiler reserves one byte of memory. You cannot store a group of characters in a single byte.

That is where character arrays come in. You define an array longer than the longest group of characters you expect to be stored (+1).

But if you want to store more than one group of characters, you need to add another dimension to the array. Which is what that sample program did.
 

djsfantasi

Joined Apr 11, 2010
9,160
I got nothing...

The programs in post #8 don’t have any code to output, so I don’t get how you expect to see results printed out..
 

Thread Starter

Gajyamadake

Joined Oct 9, 2019
310
I got nothing...

The programs in post #8 don’t have any code to output, so I don’t get how you expect to see results printed out..
I don't think you understand my question

I want to get this output

Enter name: // get this number from user
a
b
c
d
e

// print each number entered by user
name : a
name : b
name : c
name : d
name : e

I tried to make program code #4 for it but couldn't do that
 

djsfantasi

Joined Apr 11, 2010
9,160
I understood the question. I don’t think you are understanding my answers.

You referred to post #8 in your last post. Now you are saying post #4. #8 has no output!

For each execution of scanf(), you are overwriting the previous value of “name”. The value in scanf() for the output, is the entire name array. When you use an array name as a parameter without braces, you get the address of the array. So, each scanf() writes to all of the array elements.

if you want to read one character and store it in an array element, I’d change your scanf() to this:
scanf(“%c”,&name[ i ])

That will return the result to one element of the array, rather than the entire array. I’m not 100% sure because I don’t use scanf(), but I think it’s worth a try.
 
Top