← Back to C Course | Chapter 5: Functions | Lesson 8 of 8

C के Math Functions

math.h library C का scientific calculator है, square roots, powers और rounding जैसी चीज़ों के साथ। यह वह math करता है जो simple operators नहीं कर सकते।
Syntax
c
#include <math.h>

sqrt(number);
pow(base, exponent);
fabs(number);

math.h Include करना

math.h का हर function floating-point numbers पर operate करता है और आपको compile time पर math library link करने की ज़रूरत होती है (ज़्यादातर Unix compilers पर -lm से)।

header भूल जाना आपको implicit-declaration warnings और कुछ platforms पर undefined behavior के साथ छोड़ देता है।

उदाहरण: Including math.h

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", sqrt(16.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Square Root (sqrt)

sqrt() सिर्फ non-negative arguments accept करता है -- एक negative number pass करना एक compile error की बजाय NaN produce करता है, इसलिए इसे call करने से पहले input validate करना अच्छी practice है।

यह आमतौर पर distance formulas और standard deviation जैसी statistical calculations के लिए इस्तेमाल होता है।

उदाहरण: Square Root (sqrt)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.2f", sqrt(25.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Power Function (pow)

Python जैसी languages में exponent operator के उलट, C में कोई ** syntax नहीं है, इसलिए pow(base, exponent) एक number को एक power तक raise करने का इकलौता built-in तरीका है। Arguments और return value दोनों doubles हैं, यहाँ तक कि whole numbers के साथ काम करते हुए भी।

उदाहरण: Power Function (pow)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.1f", pow(2.0, 3.0));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Rounding Functions (ceil और floor)

ceil(4.1) 5.0 return करता है जबकि floor(4.9) 4.0 return करता है -- दोनों हमेशा nearest integer तक round करने की बजाय number line पर zero से दूर या इसकी ओर move करते हैं।

ये ज़रूरी हैं जब आपको predictable rounding behavior चाहिए, जैसे एक printout को कितने pages चाहिए यह compute करना।

उदाहरण: Rounding Functions (ceil and floor)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.1f %.1f", ceil(4.1), floor(4.9));
	// Return 0 to signal that the program finished successfully
	return 0;
}

Absolute Value (fabs)

fabs() abs() का floating-point counterpart है, जो सिर्फ integers पर काम करता है; एक double पर abs() इस्तेमाल करना absolute value लेने से पहले चुपचाप इसे truncate कर देता है, subtle bugs का एक common source।

जब भी आप एक tolerance के against floating-point differences compare कर रहे हों fabs() इस्तेमाल करें।

उदाहरण: Absolute Value (fabs)

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include math functions (sqrt, pow, ...)
#include <math.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%.1f", fabs(-7.5));
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. #include <math.h> भूल जाना, ताकि sqrt और pow declared न हों।
  2. कुछ systems पर -lm से link करना भूल जाना, जो undefined reference errors cause करता है।
  3. एक negative number पर sqrt call करना, जो NaN return करता है।
चैप्टर सारांश
  • Functions reusable code group करते हैं, parameters लेते हैं, और return values से results वापस भेजते हैं।
  • Prototypes एक function को इस्तेमाल होने से पहले declare करते हैं, और recursion एक function को खुद को call करने देता है।
  • Scope, lifetime, और storage classes decide करते हैं कि variables कहाँ visible हैं और कितनी देर रहते हैं।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.