• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Compilation and Execution of C Program | Hello World Program in C

Aniruddha Chaudhari/30147/0
C / C++Code

C is one of the basic languages fo a beginner. It is invented by Dennis Ritchie. In this article, you are going to write your first program. You will be educated with the step-by-step procedure for compilation and execution of C program with hello world program.

Writing Hello World program is very easy and everyone can code it.  But it is important to understand how C program runs and what are the purpose of each line of code in the program.

In this program, we are adding stdio.h file and using printf() statement to print the string message “hello World” on output console.

Writing Hello World program in C:

#include <stdio.h>
int main()
{
  //print output string on console
  printf("Hello, World!\n");
  return 0;
}

This program runs all the operating systems including Windows, Linux, Mac OS. The only prerequisite as you should have GCC compiler installed on your system.

How does Hello World program run?

step by steps…

  • The first line of code #include<stdio.h> is a preprocessor. stdio.h is a header file which includes multiple functions defined in it such as scanf(), printf() functions. Adding this header file in our program, allow us to use functions defined in the header file. Using existing function makes our job pretty easy.
  • Every C  program execution starts with main() function. Your program may have multiple functions, the code inside the main() function block is executed first. Here, the return type of the main() function is int.
  • As like all other function, main() function block starts with a { and end with a } delimiter.
  • Write printf() statement by passing a string as an input. It will print the string message on the output console.
  • As we have defined the main with return int value, write return 0; It returns a SUCCESS message to the operating system.
  • \n in string message moves the cursor to a new line.

Compilation and Execution of C Program

Follow the steps given below.

  • Save the program as helloWorld.c (with .c extension).
  • Open a command prompt.
  • Go to the current directory where the program is saved using. You can use cd command to change the current directory.
  • Compile the program with the following command.
gcc helloWorld.c

It will compile the program file and create an executable file.

(a.exe if you are running program on the window system. a.out for Linux system).

  • Running executable file.

For Windows,

a.exe

For Linux,

./a.out

The output of the Program after execution:

It will print “Hello, World!” on output console.

Hello, World!

Compilation and Execution of C Program

In the above program, we are passing “Hello world” string as input to the printf() statement. You can save this string as a character array and then you can pass this character array (strMsg) as input.

#include <stdio.h>

int main()
{
  char strMsg[] = "Hello, World!";
  printf("%s\n", strMsg);
  return 0;
}

If you ar new to the C programming, don’t worry about char array (string).

Frequently asked questions:

Covering some important questions every newbie program faces.

Why do we use return 0?

The main() is the first function that runs. “return <vlaue>” returns value to the OS.

This value can be 0 or 1.

As a part of good practice, you can use EXIT_SUCCESS instead of 0 and EXIT_FAILURE instead of 1.

EXIT_SUCCESS and EXIT_FAILURE are defined in the stdlib.h header file as follow…

#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1

If you want to use EXIT_SUCCESS or  EXIT_FAILURE, you have to add stdlib.h header file.

In our hello world program, we are using “return 0”. It means program runs successfully.

Where does the header file stdio.h file present in C?

The stdio.h header file is located in GCC directory at .../include/stdio.h
You can find the location of all the header files by running the following command.

gcc -H -fsyntax-only helloWorld.c

It lists complete path for all the header files included in helloWorld.c.

Wrapping Up…

This is all about running your first program in C. Now you are free to go ahead and start learning C.

I tried my best to describe each and every aspect of the compilation and execution of the C program. As this is the first program you are writing, you may have doubts.

It is very important to clear your doubts before heading to your next program. So feel free to write in a comment.

Happy Programming!

Codecpp
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

C Programming

  1. C- Introduction
  2. C- Compile & Execute Program
  3. C- Data Types
  4. C- if-else statement
  5. C- While, do-while, for loop
  6. C- Array
  7. C- Function (Types/Call)
  8. C- strlen() vs sizeof()
  9. C- Nested Switch Statement
  10. C- Recursion
  11. C- Dynamic Programming
  12. C- Storage Classes
  13. C- Creating Header File
  14. C- Null Pointer
  15. C- Stack and Queue
  16. C- Implement Stack using Array
  17. C- Implement Linked List in C
  18. C- File Handling
  19. C- Makefile Tutorial

Object Oriented Concepts in C++

  • C++: C vs OOPs Language
  • C++: Introduction to OOPs Concepts
  • C++: Inheritance

Sorting Algorithms

  • Different Types of Sorting Algo
  • Selection Sort
  • Bubble Sort
  • Quick Sort

Programming for Practice

  1. Online C/C++ Compiler

String Handling:

  1. Remove White Spaces from String
  2. Implement strstr Function in C
  3. Convert String to Int – atoi()
  4. Check if String is Palindrome
  5. Check if Two Strings are Anagram
  6. Split String in using strtok_r()
  7. Undefined reference to strrev

Array:

  1. Check if Array is Sorted

Bit Manipulation:

  1. Count Number of 1’s in Binary

Linked List:

  1. Reverse a Linked List Elements

Number System:

  1. Program to Find 2’s Complement
  2. Convert Decimal to Binary in C

Tricky Questions:

  1. Add Two Numbers without Operator
  2. Find Next Greater Number
  3. Swap values without temp variable
  4. Print 1 to 100 without Loop

Interview Coding Questions

  • 50+ Interview Coding Questions

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard