Considering the following lines of C code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void print_upper(char *string);

int main()
{
    char s[80];
    printf("enter a string\n");
    gets(s);
    print_upper(s);
    printf("\ns is now in upper case: %s",s);
    return 0;
}
void print_upper(char *string)
{
    register int t;
    int k;
    for(t=0;string[t];++t)
{

    string[t]=toupper(string[t]);
    putchar(string[t]);
}
}

In the function,
print_upper(char *string)

there is a for loop which looks like:

for(t=0;string[t];++t)
{

    string[t]=toupper(string[t]);
    putchar(string[t]);
}
}

Now,how exactly does this for loop work?
I knew the structure of for loop to be like

    for(i=0;i<10;i+=1)
    {
    \\code;
    }

So I am kind of confused about it.
Thanks in advance.

++t is an increment operator, it is essentially equivalent to t+=1. Although it does not matter in this situation, note that there is a difference between ++t and t++ (just Google it if you're not sure about it).

As for the string[t], strings (in C) are an array of characters ending in 0 (not the character 0, which has a ASCII value of 32 I think, but the actual value 0, sometimes known as the null character) to indicate the end of the string. This is done since C strings do not implicitly have a length value like C# or Java. Now in C, Boolean values are simply integers where 0 indicates false, and any other value indicates true (generally speaking, I don't think this is actually stated in the standard). So the for loop iterates through each character until it reaches a character which evaluates to false. In this case it is the null character (ie. the end of the string).

which has a ASCII value of 32 I think

No. The character '0' has an ascii value of 48. C string null terminator 0 is an ascii value of 0.

>

 void print_upper(char *string)
 {
     register int t;
      int k;
      for(t=0;string[t];++t){      
     string[t]=toupper(string[t]);
     putchar(string[t]);
}
}

Here, the for loop simply accepts char[element],i.e, an array of characters supplied by the call of the function in main(void) as print_upper(char *string) where the string is supplied as a char pointer parameter. Now, the limiting condition is as long as the char elements of the array is supplied the string is parsed as an array of chars. In languages as C# you simply have the privelage of writing:

for(int i=0;i<string.Length;i++){
    //code
}

where array.length simply calculates the length of the string.

`

or u can try like

for(int i=0;string[i]!='\0';i++)
{
\\code;
}

this because string end character will be terminated by null

and try to use fgets dont use gets() .because now only i learned this topic recently

commented: True -- but at least explain why. And "there is do and do not. There is no try!" +0
commented: abolutely right +14

and try to use fgets dont use gets() .because now only i learned this topic recently

we use fgets() because gets() doesn't allow to specify the length of buffer to store string whereas in fgets we are allowed to give max string length.

here's the syntax:

fgets(s,sizeof(s),stdin);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.