A Guide to Writing a Simple Program in C
A Guide to Writing a Simple Program in C
Want to write your first program in C? You'd be surprised at how easy it is, even if you don't have any experience with coding. The simplest way to get started is to install an IDE (a program for writing code) and a C compiler, and then experiment with some sample code. This wikiHow article will teach you how to write a basic C program that displays a string of text on the screen.
Things You Should Know
  • Begin by installing an Integrated Development Environment (IDE), such as Visual Studio Code, NetBeans, or Eclipse.
  • Install a C compiler as well, as few IDEs come with one. You will need to open your IDE and tell it where to find your compiler.
  • Select the "New Project" option within your IDE to begin inputting your sample C script.

Getting Started

Install an Integrated Development Environment (IDE). IDEs are applications you can use to write, debug, and compile programs. Some common free IDEs you can try are Visual Studio Code, NetBeans, Eclipse, and Code::Blocks. If you're using a Mac, install Xcode from the Mac App Store. The primary components of an IDE are: The source code editor (where you'll write your code). Source code editors support syntax highlighting and checking, which makes it a lot easier to write code that works. Build automation tools, which can help you automate tasks. A debugger, which allows you to test and find bugs in your code. If you don't want all of these features in one tool, you can opt to write your code in a plain text editor and compile it at the command line.

Install a compiler. A compiler turns the code you write into a program you can run. Your IDE will usually have a compiler built in, but many (including Code::Blocks and Netbeans) require a compiler like GCC to compile programs. If you're using Linux, you have the best compiler already built in—GCC. If you're using Windows, you can use Mingw-w64, which allows you to use GCC on Windows. If you're using Xcode on a Mac, you'll want to install Clang. To do so, open a terminal window, and run this command: sudo xcode-select --install.

Set up your IDE. Before you start writing code, you'll want to make sure your IDE is ready to go. This usually involves opening your IDE for the first time and telling it where it can find your C compiler (if it doesn't come with its own). Some IDEs know where to look for a C compiler on a computer, while others may need your assistance. Once your IDE knows where to find the compiler, you'll be ready to write your first program.

Writing Your Program

Create a new project in your IDE. You'll usually find a New Project option in your IDE's File or main menu. This will allow you to choose a programming language environment (such as C). It will also open a blank sheet into which you can type your code.

Type this code into your source code editor. Your first program will display the words "I love wikiHow!" on the screen. Start by entering this code into your editor: #include int main() { printf("I love wikiHow!"); return 0; }

Learn the structure of your code. Now that you have some code to look at, you'll want to know what it all means: Link to the header file. The first component of any C program is the inclusion of header files, which contain the definitions for the functions in your code. In our code, we're including stdio.h, which is a header file that defines core input and output functions. #include The main function. All C programs begin with a function called main(). Your program won't actually run without this function. int main() The opening curly bracket. The statements of a function are surrounded by curly brackets. Our first program only has one statement, so we'll only have one set of curly brackets. { The function and its arguments. We're using the function printf to display the words I love wikiHow! on the screen. We start by naming the statement printf, and then entering its arguments into parenthesis. We surround "I love wikiHow!" in quotation marks because it's one string of text. You can replace this text with anything you want to display on the screen. printf("I love wikiHow!"); The return statement. This statement exits the main function. return 0; The closing curly bracket. Remember, we used a curly bracket right after the main() function, which means we'll need to close it. }

Running Your Program

Click the Compile option in your IDE. The steps to do this are different depending on your IDE. The option you're looking for is usually Compile or Make.

Click the Run option in your IDE. This will run your newly compiled C program and display the results ("I love wikiHow!") in a terminal or command prompt window.

Debug your code. Did you get an error when you tried to run your program? That's where the debugger comes in. Run your IDE's debugging tool to find out exactly where your code failed, which can help you fix the issue before you compile it again.

What's your reaction?

Comments

https://ugara.net/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!