C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Hello World Program

C Programming

In this tutorial we will learn about the basic structure of a C program.

Open your favourite IDE and lets write a C program to print Hello World.

Save the file as helloworld.c or any other filename that you like.

#include <stdio.h>

int main(void) {
  printf("Hello World");
  return 0;
}

Now if we compile and run this code we will get Hello World as output.

Lets talk about each line of the program.

#include Directive

The first line of the program #include <stdio.h> is a preprocessor directive which instructs the compiler to include stdio.h header file from the C library for input/output.

C programs are generally divided into modules or functions. Some of these functions are written by us programmers and some of the functions are stored in the C library.

Functions stored in C library are grouped in categories and saved in files known as header files.

Header files are saved with a .h file extension.

Note! preprocessor directives are added at the beginning of a program.

The main function

Next we have the main() function which is a special function in C programming as it tells about the starting point of the program.

Every C program must have only one main function.

If we create more than one main() function in a C program then we will get an error as the compiler will not know which one represent the starting point of the program.

What is a function?

A function in C is a block of code that can take in some value, perform some tasks and can return a value.

We represent the start and end of a function block using the opening and closing curly { } brackets.

int main(void) {

}

Function code is written inside the opening and closing curly brackets.

Return type

Functions can return value and the int keyword before the main function tells us that the main function can return integer value.

If a function is not going to return any value then we use the void keyword.

We will learn about functions in the Functions tutorial.

Function Argument

Within the opening and closing parenthesis ( ) of a function we pass some values.

In the above code we have main(void) which means the main function takes no value.

void is a special keyword.

The printf function

The printf() function in C is used to print output. It is a standard C function.

So, in the above code we are printing a string message that is enclosed within double quotes "Hello World".

The semicolon ;

Every sentence in English ends with a full stop . similarly, every statement in C ends with a semicolon ; mark.

So, in the above program we end the statement with a semicolon.

printf("Hello World");

return 0;

The last line inside the main function is a return statement.

Since the return type of the main function in the above program is set to int so, we are returning an integer value 0 at the end of the function.

We will learn about return values and functions in the Functions tutorial.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT