We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,280 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

About the for loop

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.

6
Contributors
6
Replies
3 Days
Discussion Span
9 Months Ago
Last Updated
7
Views
Orymeyer
Newbie Poster
2 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

++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).

nmaillet
Posting Pro
537 posts since Aug 2008
Reputation Points: 111
Solved Threads: 103
Skill Endorsements: 4

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.

Ancient Dragon
Achieved Level 70
Team Colleague
32,145 posts since Aug 2005
Reputation Points: 5,836
Solved Threads: 2,577
Skill Endorsements: 69

>

 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.

`

abhishekde.nasa
Newbie Poster
2 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

or u can try like

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

this because string end character will be terminated by null

rithish
Posting Whiz in Training
268 posts since Apr 2011
Reputation Points: 23
Solved Threads: 9
Skill Endorsements: 0

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

rithish
Posting Whiz in Training
268 posts since Apr 2011
Reputation Points: 23
Solved Threads: 9
Skill Endorsements: 0

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);
Vish0203
Junior Poster
144 posts since Apr 2012
Reputation Points: -6
Solved Threads: 9
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0798 seconds using 2.71MB