0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語 簡略記法を身につけるためのfor文

Last updated at Posted at 2020-03-03

できる人に質問したらこういう書き方で返答が返ってきた。
知っているfor文と違うので、解読してみる。

for ( ; str != p && str != --p; ++str ) {
    char tmp = *str;
    *str = *p;
    *p = tmp;
}

for文の基本書式はこちら。コンパイラによるけど「;」が2個ないとダメだと理解すればOK。「;」によって何の式が書かれているのか、機械が理解している。

for (<初期設定>; <継続条件> ; <再設定>) {
         <処理>
}

つまりforの書式の中では下記のように書かれている。

// 初期設定
// なし

// 継続条件
str !=p;
// かつ
p = p - 1;
str !=p;

// 再設定
str = str + 1;

while文で書き換えてみる。
継続条件の中でインクリメントが使われているのが驚きだったけど、前回書いた、後置・前置インクリメントの記法の知識で理解できた。

while(str !=p && str !=--p){
    char tmp = *str;
    *str = *p;
    *p = tmp;
    ++str;
}

参考

C言語 インクリメント演算子を使用した簡略記法 - Qiita
https://qiita.com/Kchan_01/items/9e8c65926735db65c254

もう一度基礎からC言語 第8回 制御構造と変数(4)~forとwhileによる繰り返し(反復)処理 forによる繰り返しの構造
https://www.grapecity.com/developer/support/powernews/column/clang/008/page02.htm

for文の()内の初期化式、条件式、継続処理方法を書いたり書かなかったりするとどうなるでしょうか モンスター数値さん、お久しぶりです 0.1 - 天国にいけるC言語入門 ヘキサ構造体 ver1.1620(@solarplexuss) - カクヨム
https://kakuyomu.jp/works/1177354054881541503/episodes/1177354054882548172

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?