为什么以下for循环不能正常工作?

时间:2015-08-17 05:47:33

标签: c char

#include<stdio.h>

int main() 
{
    char ch;
    for(ch='0';ch<=127;ch++)
    printf("%c %d",ch,ch);
    return 0;
}

执行时如何成为无限循环。我想知道背后的确切原因吗?类型是从int转换为char到char的原因吗?

还有一个小问题

是否所有具有.c扩展名的文件都需要在所有文件中都有main()

6 个答案:

答案 0 :(得分:7)

在这里,您要将charint

进行比较
for(ch='0';ch<=127;ch++)

char的int值可以是0到255或-128到127,具体取决于实现。

在您的情况下,charsigned char,可以将范围的值保存为-128到+127。因此,一旦你的情况下值达到127,它就会溢出并且你得到负值并且循环继续永远,因为它的值永远不会超过127.once它达到127,在下一次迭代中值将是-128,并且循环继续进行。

您可以使用unsigned char ch;

答案 1 :(得分:2)

除了answer已经提供的Mr. Nishant之外,我还想补充一点,确实,对于您的平台,charsigned类型,如果是

for(ch='0';ch<=127;ch++)

ch保持值等于 127时,则循环条件满足,控制进入正文,并且在执行正文后,执行ch++

现在,基本上,在这里你溢出一个签名类型,结果是undefined behaviour

答案 2 :(得分:2)

问题很明显,上面提到的nishant,你可以改变你的代码来执行那个

#include<stdio.h>
#include <math.h>

int main() 
{
    char ch;
    for(ch='0';abs(ch)<=127;ch++)
    printf("%c %d",ch,ch);
    return 0;
}

答案 3 :(得分:2)

代码中需要进行一项更改 - 将char ch更改为unsigned char ch

unsigned char ch;
for(ch='0';ch<=127;ch++)
    printf("%c %d\n",ch,ch);
return 0;

这是因为char值的范围是-128到+127,当你从127增加1时,它变成-128。

答案 4 :(得分:0)

_placesClient = [GMSPlacesClient sharedClient];


[_placesClient currentPlaceWithCallback:^(GMSPlaceLikelihoodList *likelihoodList, NSError *error) {
    if (error != nil) {
        NSLog(@"Current Place error %@", [error localizedDescription]);
        return;
    }
    int cnt =0;
    for (GMSPlaceLikelihood *likelihood in likelihoodList.likelihoods) {
        cnt++;
        if(cnt==1)
        {

            GMSPlace* place = likelihood.place;
            NSLog(@"Current Place name %@ at likelihood %g", place.name, likelihood.likelihood);
            NSLog(@"Current Place address %@", place.formattedAddress);
            NSLog(@"Current Place attributions %@", place.attributions);
            NSLog(@"Current PlaceID %@", place.placeID);

            CLLocationCoordinate2D center = CLLocationCoordinate2DMake(place.coordinate.latitude,place.coordinate.longitude);
            //                CLLocationCoordinate2D center = CLLocationCoordinate2DMake([lat doubleValue],[lng doubleValue]);

            CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001);
            CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001);
            GMSCoordinateBounds *viewport = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
                                                                                 coordinate:southWest];
            GMSPlacePickerConfig *config = [[GMSPlacePickerConfig alloc] initWithViewport:viewport];
            _placePicker = [[GMSPlacePicker alloc] initWithConfig:config];

            [_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
                if (error != nil) {
                    NSLog(@"Pick Place error %@", [error localizedDescription]);

                }
                if (place != nil) {
                    [_placesClient reportDeviceAtPlaceWithID:place.placeID];

                    NSLog(@"Place name %@", place.name);
                    NSLog(@"Place address %@", place.formattedAddress);
                    NSLog(@"Place placeID %@", place.placeID);
                    NSLog(@"Place number %@", place.phoneNumber);
                    NSLog(@"Place website %@", place.website);

                    self.placeInfo = [[SHPlaceInfo alloc]init];
                    self.placeInfo.address=place.formattedAddress;
                    self.placeInfo.phnNumber=place.phoneNumber;
                    self.placeInfo.website = [NSString stringWithFormat:@"%@",place.website];
                    self.placeInfo.idStr = place.placeID;
                    self.placeInfo.lat =[NSString stringWithFormat:@"%f", place.coordinate.latitude];
                    self.placeInfo.lng =[NSString stringWithFormat:@"%f", place.coordinate.longitude];
                    self.placeInfo.type = [place.types componentsJoinedByString:@","];
                    self.placeInfo.name = place.name;
                    [self savePlaceDatainDatabase:self.placeInfo];

                    //            [self getNearDetailPlaces];

                    //if place id gives phone number then need to use below flow and remove api call above getNearDetailPlaces

                    selectedPlaceDetailViewController *placeDetailVC=[self.storyboard instantiateViewControllerWithIdentifier:@"selectedPlaceDetailViewController"];

                    placeDetailVC.placeInfo = self.placeInfo;
                    placeDetailVC.strCalledFromPage=@"Home";
                    [self.navigationController pushViewController: placeDetailVC animated:YES];


                } else {
                    NSLog(@"No place selected");
                }
            }];
        }

    }

}];

char的最大值为127,因此for(ch='0';ch<=127;ch++) 永远不会结束。它始终是真的。

答案 5 :(得分:0)

如上所述,max为127,因此循环变为无限。 每个c程序都需要主要功能。其他明智的编译器不能识别哪个块首先执行以及要执行的操作...... !!