This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions.
This article has been viewed 116,782 times.
Learn more...
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.
Steps
Getting Started
-
1Install 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.
-
2Install 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.[1] To do so, open a terminal window, and run this command: sudo xcode-select --install.
Advertisement -
3Set 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
-
1Create 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.
-
2Type 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<stdio.h> int main() { printf("I love wikiHow!"); return 0; }
-
3Learn 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.[2]
In our code, we're including stdio.h, which is a header file that defines core input and output functions.
#include<stdio.h>
- The main function. All C programs begin with a function called main().[3]
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.
}
- 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.[2]
In our code, we're including stdio.h, which is a header file that defines core input and output functions.
Running Your Program
-
1Click 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.
-
2Click 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.
-
3Debug 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. [4]
Community Q&A
-
QuestionWhat is the difference between C and C++?Community AnswerC is an in-depth language, everything you need to write. C++ is object-oriented base needed to create objects for classes.
-
QuestionHow do I create a declaration of variables?Community AnswerUse "int x;" without quotes will declare an integer variable x. You can replace 'int' with 'char', 'float', 'double', and many other variable types. You can also use "int x = 5;" to both declare x as an integer and set it to the value 5.
-
QuestionHow do I make an HTML program?Community AnswerCode the program in Notepad, then save the file with an .htm extension on your desktop. After that, you can run the html program in your web browser.
References
About This Article
1. Install an IDE.
2. Install a compiler.
3. Set up your IDE.
4. Create a new C project.
5. Paste the sample code.
6. Compile and run the code.
7. Debug to find errors.