← Back to C Course | Chapter 1: Introduction & Basics | Lesson 5 of 21

C Syntax और Structure

C के strict spelling और punctuation rules हैं, careful grammar वाली किसी language की तरह। Statements semicolons से खत्म होते हैं, curly braces steps को group करते हैं, और capital letters मायने रखते हैं।
Syntax
c
#include <header.h>

return_type function_name() {
  statement;
  statement;
}

Semicolons

हर C statement को semicolon से खत्म होना चाहिए ताकि compiler को exactly पता हो कि एक instruction कहाँ रुकता है और अगला कहाँ शुरू होता है; एक भूल जाना beginners की सबसे common errors में से एक है, और यह अक्सर अगली line पर एक confusing error produce करता है।

उदाहरण: Semicolons

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 10
	int x = 10;
	// Print formatted text to the screen
	printf("%d", x);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Curly Braces

Curly braces { } कई statements को एक single block में group करते हैं, एक function, loop, या conditional की body define करते हुए। compiler matching pair के बीच की हर चीज़ को एक unit मानता है जो साथ execute होती है (या नहीं होती)।

उदाहरण: Curly Braces

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	{
		// Print formatted text to the screen
		printf("Grouped inside braces");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Tokens

एक token सबसे छोटा meaningful हिस्सा है जिसे compiler आपकी source code पढ़ते समय पहचानता है -- keywords, variable names, numbers, + या ; जैसे symbols, और string literals सब individual tokens हैं जिन्हें compiler एक बार में एक parse करता है।

उदाहरण: Tokens

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare total and set it to 100
	int total = 100;
	// Print formatted text to the screen
	printf("%d", total);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Case Sensitivity

C uppercase और lowercase letters को पूरी तरह अलग characters मानता है, इसलिए main को exactly lowercase में लिखना होता है, और total और Total नाम के variables same program में दो अलग, independent variables हैं।

उदाहरण: Case Sensitivity

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare total and set it to 5
	int total = 5;
	// Declare Total and set it to 10
	int Total = 10;
	// Print formatted text to the screen
	printf("%d %d", total, Total);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Indentation

Consistent indentation इस पर असर नहीं डालता कि compiler आपका code कैसे चलाता है -- C whitespace को ignore करता है -- लेकिन यह आपके और दूसरे developers के लिए visually trace करना ज़रूरी है कि कौन से statements किस block के अंदर हैं, खासकर nested loops या conditionals में।

उदाहरण: Indentation

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Check whether 1
	if (1) {
		// Print formatted text to the screen
		printf("Indentation shows nesting");
	}
	// 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. किसी statement के अंत में semicolon भूल जाना, ताकि compiler अगली line पर एक error report करे।
  2. Printf या INT लिखना जब C case-sensitive है और सिर्फ printf और int exist करते हैं।
  3. एक { को उसके closing } के बिना छोड़ना, जिससे 'expected } at end of input' error आती है।

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.