Download Article
Display a string of text with this introductory C program from wikiHow
Download Article

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.
Part 1
Part 1 of 3:

Getting Started

Download Article
  1. 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.
  2. 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
  3. 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.
  4. Advertisement
Part 2
Part 2 of 3:

Writing Your Program

Download Article
  1. 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.
  2. 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;
    }
    
  3. 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.
      }
      
  4. Advertisement
Part 3
Part 3 of 3:

Running Your Program

Download Article
  1. The steps to do this are different depending on your IDE. The option you're looking for is usually Compile or Make.
  2. This will run your newly compiled C program and display the results ("I love wikiHow!") in a terminal or command prompt window.
  3. 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]
  4. Advertisement

Community Q&A

Search
Add New Question
  • Question
    What is the difference between C and C++?
    Community Answer
    Community Answer
    C is an in-depth language, everything you need to write. C++ is object-oriented base needed to create objects for classes.
  • Question
    How do I create a declaration of variables?
    Community Answer
    Community Answer
    Use "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.
  • Question
    How do I make an HTML program?
    Community Answer
    Community Answer
    Code 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.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Video

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Thanks for submitting a tip for review!

You Might Also Like

Learn to Program in CA Beginner's Guide to Programming in C
Compile a C Program Using the GNU Compiler (GCC)How to Use GCC to Compile a C Program on Linux and Windows
Edit DLL Files in Visual StudioEdit DLL Files in Visual Studio
Delay in CDelay in C
Run CUDA C or C++ on Jupyter (Google Colab)Run CUDA C or C++ on Jupyter (Google Colab)
Set Up SDL with Visual StudioSet Up SDL with Visual Studio
Get Color in C ProgramGet Color in C Program
Set Up OpenGL GLFW GLEW GLM on a Project with Visual StudioSet Up OpenGL GLFW GLEW GLM on a Project with Visual Studio
Print in C2 Easy Ways to Print in C and C++ Programming
Set Up OpenGL‐GLFW‐GLAD on a Project with Visual StudioSet Up OpenGL‐GLFW‐GLAD on a Project with Visual Studio
Set Up SFML in a Project on Visual StudioSet Up SFML in a Project on Visual Studio
Start Learning C Programming in Turbo C++ IDEStart Learning C Programming in Turbo C++ IDE
Check Null in CCheck Null in C
Set Up an OpenGL FreeGLUT GLEW Template Project in Visual StudioSet Up an OpenGL FreeGLUT GLEW Template Project in Visual Studio
Advertisement

About This Article

Nicole Levine, MFA
Written by:
wikiHow Technology Writer
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 128,023 times.
How helpful is this?
Co-authors: 21
Updated: May 2, 2023
Views: 128,023
Article SummaryX

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.

Did this summary help you?

Thanks to all authors for creating a page that has been read 128,023 times.

Is this article up to date?

Advertisement