TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: First C Program Simplified for You
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
C Tutorials

First C Program Simplified for You

Last updated: Jun 03, 2024 1:23 pm
By Meenakshi Agarwal
Share
6 Min Read
Write your first C program in simple and easy steps.
Learn To Write Your First C Program
SHARE

This tutorial outlines the simple steps to write your first C program. You will learn what the fundamental components that make it work are.

Contents
C Program StructureInclude Statement – #include<stdio.h>Entry Function – int main()Print a Message – printf()Wait for User Input – getchar()Exit from the C Program – return(0)The curly braces {}Execute the Code

Your First C Program in Simple Steps

Below is the C program coding snippet which prints “Hello Computer” on the console. You can use any text editor to write this code. But we recommend and use Codeblocks to create, compile, and execute any C program.

#include<stdio.h>

int main()
{
    printf("Hello Computer");
    getchar();
    return(0);
}

C Program Structure

Let’s now see what every individual line in the above code means.

Include Statement – #include<stdio.h>

In the big brain of C, there are some pre-defined instructions to help users execute commands faster. One example of that is the “printf” instruction, which we will see soon. There are specific libraries in C that contain information on these instructions. Therefore, “#include<stdio.h>” tells the computer to include the information of these pre-defined instructions so that you can use them in your programs. “stdio” stands for “standard input-output.”

Note: You can observe what happens when you do not write this line. It will show an error that the function “printf” is not defined. It is because the compiler does not know what it means unless you provide the header file “#include<stdio.h>” which has its definition.

Entry Function – int main()

Every C program has a set of instructions to be executed. We usually write them inside the body of the main() function. It is the entry point routine for all programs to begin execution. From the main function, we call all other functions to perform a set of tasks. So, there has to be a “main” function in a C program.

In C programming, the main always returns an integer type value. The “int” before the main represents the type of value returned.

A C program can also have user-defined functions which we’ll cover in the later tutorials. However, in C programming, using the “main” is compulsory.

Note: You may have noticed that after every function, we give parentheses (). It is because while calling, the program can pass a set of values or parameters to it. They also send back data using the “return” statement. If there is nothing to return, then we may specify the 0 as a return value or not use it at all.

Print a Message – printf()

It is a pre-defined function in C that tells the computer to display the text as it is. It helps the compiler to differentiate between command and actual text intended for printing. The text inside the parentheses should appear as it is on the screen

Note: In the printf() function, the actual text to be displayed should be enclosed in double inverted commas “_.” Something like printf(“Hello Computer”).

Wait for User Input – getchar()

It is a C input function that makes the program wait until the user presses a key.

Exit from the C Program – return(0)

It represents the return code of the main() function. A non-zero value means error whereas the zero indicates the program gets executed successfully.

In our example, we return a zero value which evaluates success.

The curly braces {}

The curly brackets let us create multiple units of code in a C program.

They define the boundaries of a function such as the main() and include instructions for execution in a C program.

A function can have multiple nested blocks of code delineating using curly brackets. Each such section has access to variables from the parent block, but any of its variables will not be visible from the outside.

We’ll provide a detailed description of the variable scope in the next classes.

Execute the Code

Now that we have successfully understood what every line means. Let us type this code on our newly set up IDE and Run it.

When you type this code and run, you should see something like this.

Use Codeblocks to write your first C program

Now click on the upper green triangle, and you should see a screen like this after a few seconds.

Execute your first C program

Congratulations, You have successfully written, compiled, and run a C program. By the way, if you love using the Linux command line, then go through our makefile tutorial to compile your first C program.

Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.

You Might Also Like

C Programming Language “Struct”

20 C Programs for Beginners to Practice

Learn C Programming – How To Guide for Beginners

Python vs C++ – Is Python or C++ Better?

The Best Top-Down Approach Guide for You in C

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Introduction to C Programming using codeblocks Set up C Programming Environment using Code::Blocks
Next Article Understanding C Variables

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x