This blog is about programming codes for beginners and professionals

Translate

Saturday, April 16, 2022

C++ Data Types

C++ Data Types

·         Difficulty Level : Basic

·         Last Updated : 28 Jan, 2022

All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory.
 



Data types in C++ is mainly divided into three types: 
 

1.      Primitive Data Types: These data types are built-in or predefined data types and can be used directly by the user to declare variables. example: int, char , float, bool etc. Primitive data types available in C++ are: 

·         Integer

·         Character

·         Boolean

·         Floating Point

·         Double Floating Point

·         Valueless or Void

·         Wide Character

2.      Derived Data Types: The data-types that are derived from the primitive or built-in datatypes are referred to as Derived Data Types. These can be of four types namely: 

·         Function

·         Array

·         Pointer

·         Reference

3.      Abstract or User-Defined Data Types: These data types are defined by user itself. Like, defining a class in C++ or a structure. C++ provides the following user-defined datatypes: 

·         Class

·         Structure

·         Union

·         Enumeration

·         Typedef defined DataType

This article discusses primitive data types available in C++. 
 

·         Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and ranges from -2147483648 to 2147483647. 
 

·         Character: Character data type is used for storing characters. Keyword used for character data type is char. Characters typically requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255. 
 

·         Boolean: Boolean data type is used for storing boolean or logical values. A boolean variable can store either true or false. Keyword used for boolean data type is bool
 

·         Floating Point: Floating Point data type is used for storing single precision floating point values or decimal values. Keyword used for floating point data type is float. Float variables typically requires 4 byte of memory space. 
 

·         Double Floating Point: Double Floating Point data type is used for storing double precision floating point values or decimal values. Keyword used for double floating point data type is double. Double variables typically requires 8 byte of memory space. 
 

·         void: Void means without any value. void datatype represents a valueless entity. Void data type is used for those function which does not returns a value. 
 

·         Wide Character: Wide character data type is also a character data type but this data type has size greater than the normal 8-bit datatype. Represented by wchar_t. It is generally 2 or 4 bytes long. 
 

 

Datatype Modifiers

As the name implies, datatype modifiers are used with the built-in data types to modify the length of data that a particular data type can hold. 
 



Data type modifiers available in C++ are: 
 

·         Signed

·         Unsigned

·         Short

·         Long

Below table summarizes the modified size and range of built-in datatypes when combined with the type modifiers:
 

Data Type

Size (in bytes)

Range

short int

2

-32,768 to 32,767

unsigned short int

2

0 to 65,535

unsigned int

4

0 to 4,294,967,295

int

4

-2,147,483,648 to 2,147,483,647

long int

4

-2,147,483,648 to 2,147,483,647

unsigned long int

4

0 to 4,294,967,295

long long int

8

-(2^63) to (2^63)-1

unsigned long long int

8

0 to 18,446,744,073,709,551,615

signed char

1

-128 to 127

unsigned char

1

0 to 255

float

4

 

double

8

 

long double

12

 

wchar_t

2 or 4

1 wide character

Note : Above values may vary from compiler to compiler. In the above example, we have considered GCC 32 bit.
We can display the size of all the data types by using the sizeof() operator and passing the keyword of the datatype as argument to this function as shown below: 
 

·         CPP

  1. // C++ program to sizes of data types
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     cout << "Size of char : " << sizeof(char)
  8.       << " byte" << endl;
  9.     cout << "Size of int : " << sizeof(int)
  10.       << " bytes" << endl;
  11.     cout << "Size of short int : " << sizeof(short int)
  12.       << " bytes" << endl;
  13.     cout << "Size of long int : " << sizeof(long int)
  14.        << " bytes" << endl;
  15.     cout << "Size of signed long int : " << sizeof(signed long int)
  16.        << " bytes" << endl;
  17.     cout << "Size of unsigned long int : " << sizeof(unsigned long int)
  18.        << " bytes" << endl;
  19.     cout << "Size of float : " << sizeof(float)
  20.        << " bytes" <<endl;
  21.     cout << "Size of double : " << sizeof(double)
  22.        << " bytes" << endl;
  23.     cout << "Size of wchar_t : " << sizeof(wchar_t)
  24.        << " bytes" <<endl;
  25.      
  26.     return 0;
  27. }

Output: 
 

Size of char : 1 byte

Size of int : 4 bytes

Size of short int : 2 bytes

Size of long int : 8 bytes

Size of signed long int : 8 bytes

Size of unsigned long int : 8 bytes

Size of float : 4 bytes

Size of double : 8 bytes

Size of wchar_t : 4 bytes

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

 


Share:

C++ Programming Basics

 C++ Programming Basics

C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc. 
However to become proficient in any programming language, one Firstly needs to understand the basics of that language. 
Therefore, below are the basics of C++ in the format in which it will help you the most to get the headstart: 

  1. Basic Syntax and First Program in C++: Learning C++ programming can be simplified into writing your program in a text editor and saving it with correct extension(.CPP, .C, .CP) and compiling your program using a compiler or online IDE. The “Hello World” program is the first step towards learning any programming language and also one of the simplest programs you will learn.
  2. Basic I/O in C++:C++ comes with libraries which provides us with many ways for performing input and output. In C++ input and output is performed in the form of a sequence of bytes or more commonly known as streams. The two keywords cin and cout are used very often for taking inputs and printing outputs respectively. These two are the most basic methods of taking input and output in C++.
  3. Comments in C++: A well-documented program is a good practice as a programmer. It makes a program more readable and error finding become easier. One important part of good documentation is Comments. In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. These are statements that are not executed by the compiler and interpreter.
  4. Data Types and Modifiers in C++: All variables use data-type during declaration to restrict the type of data to be stored. Therefore, we can say that data types are used to tell the variables the type of data it can store. Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-type with which it is declared. Every data type requires a different amount of memory.
  5. Uninitialized variable in C++: “One of the things that has kept C++ viable is the zero-overhead rule: What you don’t use, you don’t pay for.” -Stroustrup. The overhead of initializing a stack variable is costly as it hampers the speed of execution, therefore these variables can contain indeterminate values. It is considered a best practice to initialize a primitive data type variable before using it in code.
  6. Undefined Behaviour in C++: If a user starts learning in C/C++ environment and is unclear with the concept of undefined behaviour then that can bring plenty of problems in the future like while debugging someone else code might be actually difficult in tracing the root to the undefined error.
  7. Variables and Types in C++: A variable is a name given to a memory location. It is the basic unit of storage in a program. The value stored in a variable can be changed during program execution. A variable is only a name given to a memory location, all the operations done on the variable effects that memory location. In C++, all the variables must be declared before use.
  8. Variable Scope in C++: In general, scope is defined as the extent up to which something can be worked with. In programming also the scope of a variable is defined as the extent of the program code within which the variable can be accessed or declared or worked with. There are mainly two types of variable scopes, Local and Global Variables.
  9. Constants and Literals in C++: As the name suggests the name constants is given to such variables or values in C++ programming language which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constants like integer, float, octal, hexadecimal, character constants, etc. Every constant has some range. The integers that are too big to fit into an int will be taken as long. Now there are various ranges that differ from unsigned to signed bits. Under the signed bit, the range of an int varies from -128 to +127 and under the unsigned bit, int varies from 0 to 255. Literals are kind of constants and both the terms are used interchangeably in C++.
  10. Types of Literals in C++: In this article we will analyse the various kind of literals that C++ provides. The values assigned to each constant variables are referred to as the literals. Generally, both terms, constants and literals are used interchangeably. For eg, “const int = 5;“, is a constant expression and the value 5 is referred to as constant integer literal. 
  11.  
  12. Access Modifiers in C++: Access modifiers are used to implement an important feature of Object-Oriented Programming known as Data Hiding. Access modifiers or Access Specifiers in a class are used to set the accessibility of the class members. That is, it sets some restrictions on the class members not to get directly accessed by the outside functions.
  13. Storage Classes in C++: Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility, and life-time which help us to trace the existence of a particular variable during the runtime of a program.
  14. Operators in C++: Operators are the foundation of any programming language. Thus the functionality of C/C++ programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands.
  15. Loops in C++: Loops in programming comes into use when we need to repeatedly execute a block of statements. For example: Suppose we want to print “Hello World” 10 times. This can be done in two ways, Iterative method and by using Loops.
  16. Decision Making in C++:There comes situations in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations arise in programming also where we need to make some decisions and based on these decisions we will execute the next block of code. Decision-making statements in programming languages decide the direction of flow of program execution.
  17. Forward declarations in C++: It refers to the beforehand declaration of the syntax or signature of an identifier, variable, function, class, etc. prior to its usage (done later in the program). In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this.
  18. Errors in C++: Error is an illegal operation performed by the user which results in abnormal working of the program. Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before compiling and executing.

بناء الجملة والبرنامج الأول في C ++: يمكن تبسيط تعلم برمجة C ++ في كتابة برنامجك في محرر نصوص وحفظه بامتداد صحيح (.CPP ، .C ، .CP) وتجميع برنامجك باستخدام مترجم أو IDE عبر الإنترنت. برنامج "Hello World" هو الخطوة الأولى نحو تعلم أي لغة برمجة وأيضًا أحد أبسط البرامج التي ستتعلمها.

I / O الأساسي في C ++: يأتي C ++ مع مكتبات توفر لنا العديد من الطرق لأداء الإدخال والإخراج. في لغة C ++ ، يتم تنفيذ المدخلات والمخرجات في شكل سلسلة من البايتات أو المعروفة باسم التدفقات. يتم استخدام الكلمتين الأساسيتين cin و cout كثيرًا لأخذ المدخلات ومخرجات الطباعة على التوالي. هاتان الطريقتان هما أبسط الطرق لأخذ المدخلات والمخرجات في C ++.
التعليقات في C ++: يعد البرنامج الموثق جيدًا ممارسة جيدة كمبرمج. فهو يجعل البرنامج أكثر قابلية للقراءة ويصبح اكتشاف الأخطاء أسهل. جزء مهم من التوثيق الجيد هو التعليقات. في برمجة الكمبيوتر ، التعليق هو شرح أو شرح مقروء من قبل المبرمج في الكود المصدري لبرنامج الكمبيوتر. هذه هي العبارات التي لم يتم تنفيذها من قبل المترجم والمترجم.
أنواع ومعدلات البيانات في C ++: تستخدم جميع المتغيرات نوع البيانات أثناء الإعلان لتقييد نوع البيانات المراد تخزينها. لذلك ، يمكننا القول أن أنواع البيانات تُستخدم لإخبار المتغيرات بنوع البيانات التي يمكن تخزينها. عندما يتم تعريف متغير في C ++ ، يخصص المترجم بعض الذاكرة لذلك المتغير بناءً على نوع البيانات الذي تم الإعلان عنه. يتطلب كل نوع بيانات مقدارًا مختلفًا من الذاكرة.
متغير غير مهيأ في C ++: "أحد الأشياء التي أبقت C ++ قابلة للتطبيق هي قاعدة صفر النفقات العامة: ما لا تستخدمه ، لا تدفع مقابله." -ستروستروب. يعتبر الحمل الزائد لتهيئة متغير مكدس مكلفًا لأنه يعيق سرعة التنفيذ ، وبالتالي يمكن أن تحتوي هذه المتغيرات على قيم غير محددة. يعتبر من أفضل الممارسات تهيئة متغير نوع بيانات بدائي قبل استخدامه في الكود.
سلوك غير محدد في C ++: إذا بدأ المستخدم التعلم في بيئة C / C ++ ولم يكن واضحًا مع مفهوم السلوك غير المحدد ، فقد يؤدي ذلك إلى حدوث الكثير من المشكلات في المستقبل ، مثل أثناء تصحيح أخطاء رمز شخص آخر ، قد يكون من الصعب بالفعل تتبع الجذر إلى خطأ غير محدد.
المتغيرات والأنواع في C ++: المتغير هو الاسم الذي يطلق على موقع الذاكرة. إنها وحدة التخزين الأساسية في البرنامج. يمكن تغيير القيمة المخزنة في متغير أثناء تنفيذ البرنامج. المتغير هو فقط اسم يُعطى لموقع الذاكرة ، كل العمليات التي تتم على التأثيرات المتغيرة لموقع الذاكرة. في C ++ ، يجب التصريح عن جميع المتغيرات قبل الاستخدام.
النطاق المتغير في C ++: بشكل عام ، يُعرّف النطاق على أنه المدى الذي يمكن العمل به مع شيء ما. في البرمجة أيضًا ، يتم تعريف نطاق المتغير على أنه مدى كود البرنامج الذي يمكن من خلاله الوصول إلى المتغير أو الإعلان عنه أو العمل معه. هناك نوعان رئيسيان من النطاقات المتغيرة ، المتغيرات المحلية والعالمية.
الثوابت والحرفية في C ++: كما يوحي الاسم ، يتم إعطاء ثوابت الاسم لمثل هذه المتغيرات أو القيم في لغة البرمجة C ++ والتي لا يمكن تعديلها بمجرد تعريفها. إنها قيم ثابتة في البرنامج. يمكن أن يكون هناك أي أنواع من الثوابت مثل الأعداد الصحيحة ، والعائمة ، والثمانية ، والسداسية العشرية ، وثوابت المحارف ، وما إلى ذلك ، ولكل ثابت بعض النطاق. الأعداد الصحيحة التي تكون أكبر من أن تتناسب مع عدد صحيح سيتم أخذها طويلة. توجد الآن نطاقات مختلفة تختلف من البتات غير الموقعة إلى البتات الموقعة. تحت البتة الموقّعة ، يختلف نطاق int من -128 إلى +127 وتحت البت غير الموقعة ، يختلف int من 0 إلى 255. تعد القيم الحرفية نوعًا من الثوابت ويتم استخدام كلا المصطلحين بالتبادل في C ++.
أنواع الحروف في C ++: سنحلل في هذه المقالة الأنواع المختلفة من المعطيات الحرفية التي توفرها C ++. يشار إلى القيم المخصصة لكل متغير ثابت بالقيم الحرفية. بشكل عام ، يتم استخدام كلا المصطلحين والثوابت والحرفية بالتبادل. على سبيل المثال ، "const int = 5 ؛" ، هو تعبير ثابت ويشار إلى القيمة 5 على أنها حرفية عدد صحيح ثابت.

معدِّلات الوصول في C ++: تُستخدم معدِّلات الوصول لتنفيذ ميزة مهمة في البرمجة الموجهة للكائنات تُعرف باسم إخفاء البيانات. تُستخدم مُعدِّلات الوصول أو محددات الوصول في فئة ما لتعيين إمكانية الوصول لأعضاء الفئة. أي أنه يضع بعض القيود على أعضاء الفصل لعدم الوصول مباشرة إلى الوظائف الخارجية.
فئات التخزين في C ++: تُستخدم فئات التخزين لوصف ميزات المتغير / الوظيفة. تتضمن هذه الميزات بشكل أساسي النطاق والرؤية ووقت الحياة والتي تساعدنا على تتبع وجود متغير معين أثناء وقت تشغيل البرنامج.
العوامل في C ++: العوامل هي أساس أي لغة برمجة. وبالتالي فإن وظائف لغة البرمجة C / C ++ غير مكتملة بدون استخدام عوامل التشغيل. يمكننا تعريف العوامل على أنها رموز تساعدنا في إجراء حسابات رياضية ومنطقية محددة على المعاملات. بعبارة أخرى ، يمكننا القول أن المشغل يشغل المعاملات.
الحلقات في C ++: يتم استخدام الحلقات في البرمجة عندما نحتاج إلى تنفيذ مجموعة من العبارات بشكل متكرر. على سبيل المثال: لنفترض أننا نريد طباعة "Hello World" 10 مرات. يمكن القيام بذلك بطريقتين ، الطريقة التكرارية وباستخدام الحلقات Loops.
اتخاذ القرار في C ++: هناك مواقف في الحياة الواقعية عندما نحتاج إلى اتخاذ بعض القرارات وبناءً على هذه القرارات ، نقرر ما يجب أن نفعله بعد ذلك. تظهر مواقف مماثلة في البرمجة أيضًا حيث نحتاج إلى اتخاذ بعض القرارات وبناءً على هذه القرارات سننفذ الكتلة التالية من التعليمات البرمجية. تحدد بيانات صنع القرار في لغات البرمجة اتجاه تدفق تنفيذ البرنامج.
التصريحات إلى الأمام في C ++: يشير إلى الإعلان المسبق عن بناء الجملة أو توقيع المعرف ، المتغير ، الوظيفة ، الفئة ، إلخ قبل استخدامه (يتم ذلك لاحقًا في البرنامج). في لغة C ++ ، تُستخدم الإعلانات إلى الأمام عادةً للفئات. في هذا ، يتم تعريف الفئة مسبقًا قبل استخدامها بحيث يمكن استدعاؤها واستخدامها من قبل الفئات الأخرى التي تم تحديدها قبل ذلك.
أخطاء في C ++: الخطأ هو عملية غير قانونية يقوم بها المستخدم وينتج عنها عمل غير طبيعي للبرنامج. غالبًا ما تظل أخطاء البرمجة غير مكتشفة حتى يتم تجميع البرنامج أو تنفيذه. تمنع بعض الأخطاء البرنامج من التجميع أو التنفيذ. وبالتالي يجب إزالة الأخطاء من قبل

Share:

Introduction to C++ Programming Language

Introduction to C++ Programming Language

 C++ is a general-purpose programming language that was developed as an enhancement of the C language to include object-oriented paradigm. It is an imperative and a compiled language. 

 

C++ is a middle-level language rendering it the advantage of programming low-level (drivers, kernels) and even higher-level applications (games, GUI, desktop apps etc.). The basic syntax and code structure of both C and C++ are the same. 

Some of the features & key-points to note about the programming language are as follows:

  • Simple: It is a simple language in the sense that programs can be broken down into logical units and parts, has a rich library support and a variety of data-types.
  • Machine Independent but Platform Dependent: A C++ executable is not platform-independent (compiled programs on Linux won’t run on Windows), however they are machine independent.
  • Mid-level language: It is a mid-level language as we can do both systems-programming (drivers, kernels, networking etc.) and build large-scale user applications (Media Players, Photoshop, Game Engines etc.)
  • Rich library support: Has a rich library support (Both standard ~ built-in data structures, algorithms etc.) as well 3rd party libraries (e.g. Boost libraries) for fast and rapid development.
  • Speed of execution: C++ programs excel in execution speed. Since, it is a compiled language, and also hugely procedural. Newer languages have extra in-built default features such as garbage-collection, dynamic typing etc. which slow the execution of the program overall. Since there is no additional processing overhead like this in C++, it is blazing fast.
  • Pointer and direct Memory-Access: C++ provides pointer support which aids users to directly manipulate storage address. This helps in doing low-level programming (where one might need to have explicit control on the storage of variables).
  • Object-Oriented: One of the strongest points of the language which sets it apart from C. Object-Oriented support helps C++ to make maintainable and extensible programs. i.e. Large-scale applications can be built. Procedural code becomes difficult to maintain as code-size grows.
  • Compiled Language: C++ is a compiled language, contributing to its speed.

Applications of C++: 

C++ finds varied usage in applications such as:

Operating Systems & Systems Programming. e.g. Linux-based OS (Ubuntu etc.)
Browsers (Chrome & Firefox)
Graphics & Game engines (Photoshop, Blender, Unreal-Engine)
Database Engines (MySQL, MongoDB, Redis etc.)
Cloud/Distributed Systems

Some interesting facts about C++: 

Here are some awesome facts about C++ that may interest you:

  1. The name of C++ signifies the evolutionary nature of the changes from C. “++” is the C increment operator.
  2. C++ is one of the predominant languages for the development of all kind of technical and commercial software.
  3. C++ introduces Object-Oriented Programming, not present in C. Like other things, C++ supports the four primary features of OOP: encapsulation, polymorphism, abstraction, and inheritance.
  4. C++ got the OOP features from Simula67 Programming language.
  5. A function is a minimum requirement for a C++ program to run.(at least main() function)

يشير اسم C ++ إلى الطبيعة التطورية للتغييرات من C. "++" هو عامل زيادة C.
C ++ هي واحدة من اللغات السائدة لتطوير جميع أنواع البرامج التقنية والتجارية.
يقدم C ++ البرمجة الموجهة للكائنات ، غير الموجودة في C. مثل الأشياء الأخرى ، يدعم C ++ الميزات الأساسية الأربعة لـ OOP: التغليف ، وتعدد الأشكال ، والتجريد ، والوراثة.
حصلت C ++ على ميزات OOP من لغة برمجة Simula67.
الوظيفة هي الحد الأدنى من المتطلبات لتشغيل برنامج C ++. (على الأقل الدالة main ())

Share:

Setting up C++ Development Environment

Setting up C++ Development Environment

C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. 

C++ runs on lots of platform like Windows, Linux, Unix, Mac, etc. Before we start programming with C++. We will need an environment to be set-up on our local computer to compile and run our C++ programs successfully. If you do not want to set up a local environment you can also use online IDEs for compiling your program.

Using online IDE: IDE stands for integrated development environment. IDE is a software application that provides facilities to a computer programmer for developing software. There are many online IDEs available which you can use to compile and run your programs easily without setting up a local development environment.

ide.geeksforgeeks.org is one such IDE provided by GeeksforGeeks. 

  1. #include<iostream>
  2. using namespace std;
  3. main()
  4. {
  5. cout << "Learning C++ at GeekforGeeks";
  6. }

Setting up local environment

For setting up your own personal development environment on your local machine you need to install two important softwares: 

  1. Text Editor: Text Editors are type of programs used to edit or write texts. We will use text-editors to type our C++ programs. The normal extension of a text file is (.txt) but a text file containing C++ program should be saved with ‘.CPP’ or ‘.C’ extension. Files ending with the extension ‘.CPP’ and ‘.C’ are called source code files and they are supposed to contain source code written in C++ programming language. These extension helps the compiler to identify that the file contains a C++ program. 
  2. Before beginning programming with C++, one must have a text-editor installed to write programs. 
  3. C++ Compiler: Once you have installed text-editor and typed and save your program in a file with ‘.CPP’ extension, you will need a C++ compiler to compile this file. A compiler is a computer program which converts high-level language into machine understandable low-level language. In other words, we can say that it converts the source code written in a programming language into another computer language which the computer understands. For compiling a C++ program we will need a C++ compiler which will convert the source code written in C++ into machine codes. Below are the details about setting up compiler on different platforms.

  • Linux Installation: We will install the GNU GCC compiler on Linux. To install and work with the GCC compiler on your Linux machine, proceed according to below steps: 
  • You have to first run the below two commands from your Linux terminal window: 

  • sudo apt-get update
  • sudo apt-get install gcc
  • sudo apt-get install g++

  • This command will install the GCC compiler on your system. You may also run the below command: 
  • sudo apt-get install build-essential
  • This command will install all the libraries which are required to compile and run a C++ program. 
  •  After completing the above step, you should check whether the GCC compiler is installed in your system correctly or not. To do this you have to run the below-given command from Linux terminal: 
  • g++ --version
  • If you have completed the above two steps without any errors, then your Linux environment is set up and ready to be used to compile C++ programs. In further steps, we will learn how to compile and run a C++ program on Linux using GCC compiler. 
  • Write your program in a text file and save it with any file name and.CPP extension. We have written a program to display “Hello World” and saved it in a file with the filename “helloworld.cpp” on desktop. 
  • Now you have to open the Linux terminal and move to the directory where you have saved your file. Then you have to run the below command to compile your file: 
  • g++ filename.cpp -o any-name
  • filename.cpp is the name of your source code file. In our case, the name is “helloworld.cpp” and any-name can be any name of your choice. This name will be assigned to the executable file which is created by the compiler after compilation. In our case, we choose any-name to be “hello”. 
  • We will run the above command as: 
  • g++ helloworld.cpp -o hello
  • After executing the above command, you will see a new file is created automatically in the same directory where you have saved the source file and the name of this file is the name you chose as any-name. 
  • Now to run your program you have to run the below command: 
  • ./hello
  • This command will run your program in the terminal window. 
  • Windows Installation: There are lots of IDE available for windows operating system which you can use to work easily with C++ programming language. One of the popular IDE is Code::Blocks. To download Code::Blocks you may visit this link. Once you have downloaded the setup file of Code::Blocks from the given link open it and follow the instruction to install. 
  • After successfully installing Code::Blocks, go to File menu -> Select New and create an Empty file. 
  • Now write your C++ program in this empty file and save the file with a ‘.cpp’ extension. 
  • After saving the file with ‘.cpp’ extension, go to Build menu and choose the Build and Run option. 
  • Mac OS X Installation: If you are a Mac user,you have to download Xcode. To download Xcode you have to visit the apple website or you can search it on apple app store. You may follow the link developer.apple.com/technologies/tools/ to download Xcode. You will find all the necessary install instructions there. 
  • After successfully installing Xcode, open the Xcode application. 
  • To create a new project. Go to File menu -> select New -> select Project. This will create a new project for you. 
  •  Now in the next window you have to choose a template for your project. To choose a C++ template choose Application option which is under the OS X section on the left side bar. Now choose command-line tools from available options and hit Next button. 
  • On the next window provide all the necessary details like ‘name of organisation’, ‘Product Name’ etc. But make sure to choose the language as C++ . After filling the details hit the next button to proceed to further steps. 
  • Choose the location where you want to save your project. After this choose the main.cpp file from the directory list on the left side-bar. 
  • Now after opening the main.cpp file, you will see a pre written c++ program or template is provided. You may change this program as per your requirement. To run your C++ program you have to go to Product menu and choose the Run option from the dropdown. 

  • This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
  • Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 

هذا المقال من مساهمة هارش أغاروال. إذا كنت تحب GeeksforGeeks وترغب في المساهمة ، فيمكنك أيضًا كتابة مقال باستخدام Contrib.geeksforgeeks.org أو إرسال مقالتك بالبريد إلى Contrib@geeksforgeeks.org. شاهد مقالتك تظهر على صفحة GeeksforGeeks الرئيسية وساعد المهوسين الآخرين.
يرجى كتابة التعليقات إذا وجدت أي شيء غير صحيح ، أو إذا كنت ترغب في مشاركة المزيد من المعلومات حول الموضوع الذي تمت مناقشته أعلاه.

Share:

E-mail: Bader.jb111@gmail.com