max()

설명

두 수의 최대값 계산.

문법

max(x, y)

매개변수

  • x: 첫번째 숫자, 자료형
  • y: 두번째 숫자, 자료형

반환값

  • 두 매개변수 값 중 큰 값

예제 코드

void setup() { Serial.begin(9600); int x = 2; int y = 3; int z = max(x, y); Serial.print("x = "); Serial.println(x); Serial.print("y = "); Serial.println(y); Serial.print("max = "); Serial.println(z); } void loop() {}

The result in Serial Monitor:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
x = 2 y = 3 max = 3
Ln 11, Col 1
Arduino Uno on COM15
2

※ 주의 및 경고:

  • max() 는 주로 낮은 값을 제한하는 데 쓰이고, min() 은 높은 값을 제한하는 데 쓰임.
  • max() 함수가 구현되는 방식 때문에, 괄호 안에 다른 함수를 쓰는 것을 피하시오. 그른 결과를 얻을 수 있음
max(a--, 0); // 이것을 피하세요 - 그른 결과 나옴 max(a, 0); // 대신 이렇게 쓰세요 - a--; // 함수 밖 수학 지킴

더보기

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables .

※ OUR MESSAGES