Writing our first C++ program - easy4tuts.blogspot.com

Hot Contents To Know

Post Top Ad

Your Ad Spot

Thursday, September 28, 2023

Writing our first C++ program

You've decided to learn how to program in C++ but don't know where to start. Here's a brief overview of how you can get started. 
आपने C++ में प्रोग्राम करना सीखने का निर्णय लिया है, लेकिन यह नहीं जानते कि कहां से शुरू करें। यहां इसका संक्षिप्त विवरण दिया गया है कि आप कैसे शुरुआत कर सकते हैं।

 Get a C++ Compiler 

    This is the first step you have to do before starting learning to program in C++. There are several free C++ compilers available for all major OS platforms. Download one that suits your platform. Now we are downloading and installing code Block.
यह पहला कदम है जो आप C++ में प्रोग्राम करने से पहले करना चाहेंगे। सभी प्रमुख Operating System के लिए  C++ कंपाइलर उपलब्ध हैं। वह डाउनलोड करें जो आपके operating system के अनुकूल हो। अब हम Code Block compiler को  डाउनलोड और इंस्टॉल कर रहे हैं।

Write a C++ program 

    Now that you have a compiler installed, its time to write a C++ program. We'll print hello world to the screen using C++ in this example. Open Code Block, go to the File menu and select New option and then empty file option. Create a new file called hello.cpp and write the following code to it −
अब जब आपने एक कंपाइलर स्थापित कर लिया है, तो अब C++ प्रोग्राम लिखने का समय आ गया है। इस उदाहरण में हम C++ का उपयोग करके स्क्रीन पर हैलो वर्ल्ड प्रिंट करेंगे। Code Block खोलें, File मेनू पर जाएं और New विकल्प चुनें और फिर Empty File विकल्प चुनें। hello.cpp नाम से एक नई फ़ाइल बनाएं और उसमें निम्नलिखित कोड लिखें -

#include<iostream>
using namespace std;
int main() {
cout<< "Hello World\n";
}

#include <iostream>

int main()
{
  std::cout << "Hello World\n";
  return 0;
}

#include <iostream>
using namspace std;
int main()
{
  cout << "Hello World\n";
  return 0;
}


Let's discuss this program. 

 Line 1−

     We start with the #include line which essentially tells the compiler to copy the code from the iostream file(used for managing input and output streams) and paste it in our source file. Header iostream, that allows performing standard input and output operations, such as writing the output of this program (Hello World) to the screen. Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. 

हम #include लाइन से शुरू करते हैं जो अनिवार्य रूप से कंपाइलर को iostream फ़ाइल (इनपुट और आउटपुट stream को manage करने के लिए उपयोग किया जाता है) से कोड कॉपी करने और इसे हमारी source file में पेस्ट करने के लिए कहता है। Header iostream, जो standard इनपुट और आउटपुट operation करने की अनुमति देता है, जैसे कि इस प्रोग्राम के आउटपुट (हैलो वर्ल्ड) को स्क्रीन पर लिखना। हैश चिह्न (#) से शुरू होने वाली लाइनें को  preprocessor directive statement कह्ते है।

 Line 2− 

    A blank line: Blank lines have no effect on a program. 

 Line 3− 

    We then declare a function called main with the return type of int. main() is the entry point of our program. Whenever we run a C++ program, we start with the main function and begin execution from the first line within this function and keep executing each line till we reach the end. We start a block using the curly brace({) here. This marks the beginning of main's function definition, and the closing brace (}) at line 5, marks its end. All statements between these braces are the function's body that defines what happens when main is called.
 
फिर हम int के रिटर्न प्रकार के साथ  main नामक एक फ़ंक्शन declare करते हैं। main() हमारे प्रोग्राम का प्रवेश बिंदु है। जब भी हम C++ प्रोग्राम चलाते हैं, तो हम main फ़ंक्शन से शुरू करते हैं और इस फ़ंक्शन के भीतर पहली पंक्ति से execution शुरू करते हैं और अंत तक पहुंचने तक प्रत्येक पंक्ति को execute करते रहते हैं। हम यहां कर्ली ब्रेस({) का उपयोग करके एक ब्लॉक शुरू करते हैं। यह main फ़ंक्शन के definition की शुरुआत को चिह्नित करता है, और पंक्ति 5 पर समापन ब्रेस (}), इसके अंत को चिह्नित करता है। इन ब्रेसिज़ के बीच के सभी statements main function के part हैं जो परिभाषित करते हैं कि main कॉल करने पर क्या करना है।

 Line 4− 

 cout << "Hello World\n"; This line is a C++ statement. This statement has three parts: First, cout, which identifies the standard console output device. Second the insertion operator << which indicates that what follows is inserted into cout. Last, we have a sentence within quotes that we'd like printed on the screen. This will become more clear to you as we proceed in learning C++.
 In short, we provide a cout object with a string "Hello world\n" to be printed to the standard output device. 
Note that the statement ends with a semicolon (;). This character marks the end of the statement 

cout << "हैलो वर्ल्ड\n"; यह पंक्ति  C++ का एक Statement है। इस Statement के तीन भाग हैं: पहला, cout, जो standard कंसोल आउटपुट डिवाइस (monitor) की पहचान करता है। दूसरा insertion ऑपरेटर (<< ) जो इंगित करता है कि निम्नलिखित को cout में डाला गया है। अंत में, हमारे पास quotes sign के भीतर एक वाक्य है जिसे हम स्क्रीन पर print करना चाहेंगे। जैसे-जैसे हम C++ सीखते जाएंगे, यह आपके लिए और अधिक स्पष्ट होता जाएगा।
 संक्षेप में,  cout object के सहायता से "Hello Word\n" स्ट्रिंग को हम standard आउटपुट डिवाइस पर print  करते हैं।
ध्यान दें कि C++ का statement  semicolon  (;) से समाप्त होता है। यह statement के अंत का प्रतीक है

Compile the Program 

Now that we've written the program, we need to translate it to a language that the processor understands, ie, in binary machine code. We do this using a compiler we installed in the first step. You need to open your terminal/cmd and navigate to the location of the hello.cpp file using the cd command. Assuming you installed the GCC, you can use the following command to compile the program − $ g++ -o hello hello.cpp This command means that you want the g++ compiler to create an output file, hello using the source file hello.cpp. 

अब जब हमने प्रोग्राम लिख लिया है, तो हमें इसे उस भाषा में अनुवाद करने की ज़रूरत है जिसे प्रोसेसर समझता है, यानी बाइनरी मशीन कोड में। हम इसे उस कंपाइलर का उपयोग करके करते हैं जिसे हमने पहले चरण में स्थापित किया था। आपको अपना टर्मिनल/cmd खोलना होगा और cd कमांड का उपयोग करके hello.cpp फ़ाइल के स्थान पर नेविगेट करना होगा। यह मानते हुए कि आपने GCC स्थापित किया है, आप प्रोग्राम को संकलित करने के लिए निम्नलिखित कमांड का उपयोग कर सकते हैं - $ g++ -o hello hello.cpp इस कमांड का अर्थ है कि आप चाहते हैं कि g++ कंपाइलर एक आउटपुट फ़ाइल बनाए, स्रोत फ़ाइल hello.cpp का उपयोग करके हेलो।

 Run the program 

Now that we've written our program and compiled it, time to run it! You can run the program using − 
अब जब हमने अपना प्रोग्राम लिख लिया है और इसे संकलित कर लिया है, तो इसे चलाने का समय आ गया है! आप - का उपयोग करके प्रोग्राम चला सकते हैं
 $ ./hello 

Output 


You will get the output− Hello world

No comments:

Post a Comment

Post Top Ad

Your Ad Spot