Maker Pro
Maker Pro

Print the string of structure

What do you mean by this? This makes no sense as "i=0" is no variable and therefore nothing can be stored there.
I have studied loop, statement , array Where I am doing mistake What I am missing in program please tell me. I will re-read it and come back to this thread
 
I have an idea what you are looking for, but to tell you where you are wrong requires that you first answer the questions at the end of post #4.
I know the program but the problem is without #include <string.h> how to do this
Code:
#include<stdio.h>
#include <string.h>
struct profile
{
   char name[10];
   int number;
};
 
int main()
{
    struct profile account = {0};
    account.number = 132;
    strcpy(account.name, "Joy");

 
   printf(" name %s \n",account.name);
 
   printf(" number %d \n",account.number);
 
   return 0;
}

name Joy
number 132

I saw this idea while searching information
 

Harald Kapp

Moderator
Moderator
without #include <string.h>
Why? String.h is a library of functions that makes your life as programmer easier. If you don't want to use it, you can always write the same routines as in string.h as your own subroutines into your code. These selfmade subroutines are probably not optimized, may have errors, are not well tested and finally will take at least as much memory in your program as the library routines.

If you want to do this, you find the code e.g. here.

Not using string.h was not part of your original question.
And you still haven't answered my questions.
Instead you're jumping around different topics within this thread. I get the impression that you don't know what youre after yourself.
Maybe it's time for you to lean back, take a deep breath (or more) and reconsider your original problem. Then state this problem and when you get answers, specially answers that ask for more detail, follow them up as closely as possible instead of throwing one piece of code after another at us.
 
Maybe it's time for you to lean back, take a deep breath (or more) and reconsider your original problem. Then state this problem and when you get answers, specially answers that ask for more detail, follow them up as closely as possible instead of throwing one piece of code after another at us.
This is my last effort and I am sure This is what you are asking in post #4
Code:
#include <stdio.h>
int main()
{
   int i;
    char name[10] = "Joy";
 
   printf(" \n Print name : %s ", name);
 
   for (i = 0; i <10; i++)
     
       {
           printf(" \n Print element of array  :  %c ", name[i]);
       }
 
    return 0;
}
}
Print name : Joy
Print element of array : J
Print element of array : o
Print element of array : y
Print element of array :
Print element of array :
Print element of array :
Print element of array :
Print element of array :
Print element of array :
Print element of array :


If we want to store and print string There is no need a loop to store string

If we want to print the element of character then we have to use loop
 

Harald Kapp

Moderator
Moderator
You're getting at it.
And without using string.h.

BTW: printf is also a library function and probably requires more memory and computing power than the simple string functions in string.h
 
You're getting at it.
And without using string.h.

BTW: printf is also a library function and probably requires more memory and computing power than the simple string functions in string.h
Thanks to both of you for being up to the end. it's okay I will use string.h function

@Harald Kapp @Steve

Do you think I have a understanding of loop and statement
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
If you understand the for statement

Code:
For (a; b; c)
{
   d;
}

Then you can trivially give the mapping of a,b,c, and d to w, x, y, and z below

Code:
z;
While (y)
{
   x;
   w;
}

The answer is something like a = w, b = x, c = y, d = z <-- but it's not that.
 

Harald Kapp

Moderator
Moderator
Do you think I have a understanding of loop and statement
I don't think your problem was with the loop statement. I think you got that part right. In my view it's your understanding of the handling of strings and arrays in C. I hope you're coming to grips with that now.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
@vead, can you answer the question possessed in post 28?

The c for statement is especially completely and subtle compared to other languages. I feel you're using it in a very cookie cutter style without understanding it.

What would you make of:

Code:
for (int i = 0; i++ < 10;)
  printf('%d', i);

I do not want you to just show me the output! I want to know what it happening in the three parts of the for statement, but I want the answer to post 28 first, because if you can't answer that, then you can't understand this.
 
Last edited:
@vead, can you answer the question possessed in post 28?

The c for statement is especially completely and subtle compared to other languages. I feel you're using it in a very come cutter style without understanding it.
Still I didn't understand your question but I am telling you what I understand
for loop
Code:
for ( initialize ; condition; increment/decrements )
{
   statement;
}

and while loop
Code:
initialize

while (condition )
{
   statement;
   increment/decrements;
}

Code:
#include <stdio.h>
int main(void)
{
int i;

for (i=0; i < 10; i++)
{
    printf(" number %d \n", i);

}
return 0;
}

Code:
#include<stdio.h>
int main ()

{
  int i = 0;
  while(i< 10)
  {
    printf("%d \n", i);
    i++;
   }
return 0;
}

if I run both program , they show same output
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Still I didn't understand your question but I am telling you what I understand
for loop

In that question a, b, c, and d represent initialize, condition, increment/decrements, and statement, respectively. You've shown the same thing in your while loop. All I was asking you to do is match up which of z, y, x, and w were initialize, condition, increment/decrements, and statement?

I'll accept that you've got that basic concept in your head.

now if we look at this:

Code:
#include <stdio.h>
int main(void)
{
int i, j;

j = 10;

for (i=0; i < j; i++)
{
   printf(" number %d \n", i);

   j = 3;
}
return 0;
}

Can you figure out what the output will be BEFORE you execute the program?

Why?

I want the answer in words written by you, not as a program with sample output. The former should show understanding, the latter only shows that you can compile and run a program. (remember my car example).
 
In that question a, b, c, and d represent initialize, condition, increment/decrements, and statement, respectively. You've shown the same thing in your while loop. All I was asking you to do is match up which of z, y, x, and w were initialize, condition, increment/decrements, and statement?

I'll accept that you've got that basic concept in your head.

now if we look at this:



Can you figure out what the output will be BEFORE you execute the program?

Why?

I want the answer in words written by you, not as a program with sample output. The former should show understanding, the latter only shows that you can compile and run a program. (remember my car example).
Code:
int i, j;

j = 10;
in first line you are declaring i and j
then you are assigning value 10 to variable j

Code:
for (i=0; i < j; i++)
{
   printf(" number %d \n", i);

   j = 3;
}
return 0;
}

loop start with i = 0 and check the condition i < j so i = 0 and j = 10 so condition is true because the value of i is less then the value of j, then you are printing the value of i which is i = 0 then you are assigning value 3 to j variable
i = 0 and j = 3
after that loop increment by 1

now i become i = 1 and check the condition i < j so i = 1 and j = 3 because the value of i is less then the value of j, condition is true then you are printing the value of i which is i = 1 then you are assigning value 3 to j variable
i = 1 and j = 3
after that loop increment by 1

now i become i = 2 and check the condition i < j so i = 2 and j = 3 because the value of i is less then the value of j, condition is true then you are printing the value of i which is i = 2 then you are assigning value 3 to j variable
i = 2 and j = 3
after that loop increment by 1


now i = 3 and check the condition i < j so i = 3 and j = 3 because the value of i is not less then the value of j, condition is false you don't print anything

so at the end i = 2 and j = 3
 
Last edited:

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Then you don't understand how the for statement works.

Perhaps you can write the program and try it out.

And when you discover that something unusual happens you need to be able to explain it. But before you do, read the last two paragraphs of your post 33 and see if you can see where you made the mistake.

It is far more obvious if you understand the C for statement in terms of a while loop. And I've tried to lead you to this, but you refuse to go there.
 
Then you don't understand how the for statement works.
I don't see anything wrong in this
now i become i = 2 and check the condition i < j so i = 2 and j = 3 because the value of i is less then the value of j, condition is true then you are printing the value of i which is i = 2 then you are assigning value 3 to j variable
i = 2 and j = 3

Exit loop


This is wrong statement I wrote this to explain false condition. This doesn't happen
now i = 3 and check the condition i < j so i = 3 and j = 3 because the value of i is not less then the value of j, condition is false you don't print anything
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Then do what I say and write a program which prints the value of the indeed loop after the loop.

I have asked you to do this three times and I won't ask you to do it again.

Code:
    int i, j;

    j = 10;

    for (i=0; i < j; i++)
    {
       printf(" number %d \n", i);

       j = 3;
    }

    printf(" (i=%d, j=%d \n", i,j);
 
Top