C Program Structure

Write your first C program.

* Before writing a C program you must learn the fundamentals or syntax of the system input & output process.

/*comments*/

Comments are a way of explaining what makes a program. Comments are ignored by the compiler and used by others to understand the code. Syntax below:

Single-line comments: Use '//' to start a single-line comment.

// This is a single-line comment in C/C++
int x = 5; // This is a comment at the end of a line

Multi-line comments: Use '/*' to start a multi-line comment and '*/' to end it.

/* This is a
   multi-line
   comment */

<stdio.h>

'<stdio.h>' is a header file in the C programming languages. It stands for "standard input-output header." This header file is part of the C Standard Library and provides a set of functions and macros for performing input and output operations.

Some of the key functions and macros provided by '<stdio.h>' include:

  • printf(): Used to display formatted output on the console.
  • scanf(): Used to read formatted input from the console.
  • fopen(), fclose(): Functions for opening and closing files.
  • fread(), fwrite(): Functions for reading and writing binary data to and from files.
  • fgets(), fputs(): Functions for reading and writing text data to and from files.
  • fprintf(), fscanf(): Functions for formatted input and output to and from files.

main()

In the C programming language, 'main()' is a special function that serves as the entry point of a C program. It is where program execution begins. The 'main()' function is mandatory in every C program, and it has a specific signature:

int main(void) {
    // Your C code here
    return 0; // Optional return statement
}

'int': This specifies the return type of the 'main()' function. It indicates that the function returns an integer value. The integer returned by 'main()' is often used as an exit status code, where a return value of '0' typically indicates successful execution, and a non-zero value indicates an error or abnormal termination.

'void': The 'void' inside the parentheses specifies that 'main()' takes no parameters. In other words, it doesn't accept any command-line arguments. If you want to accept command-line arguments, you can replace 'void' with int argc, char *argv[], where argc is an integer representing the number of command-line arguments, and argv is an array of strings containing the actual arguments.

'{}': The curly braces '{}' denote the beginning and end of the 'main()' function block. Inside these braces, you write the actual C code that makes up your program.

'return 0;' (optional): You can use the return statement to exit the 'main()') function and return an integer value to the operating system. A return value of '0' is often used to indicate successful execution. However, it's not required to include this line, and if omitted, C compilers typically assume an implicit 'return 0'; at the end of the 'main()' function.

;

In the C programming language, the semicolon (;) also a crucial element used to terminate statements. The semicolon is used to mark the end of a line, indicating that the statement is complete.

int x = 5;  // Statement 1
int y = 10; // Statement 2

Hello, World!

Let's write your first C program:

c Copy Code
//Write "Hello, World!" program in C
#include<stdio.h>
int main(void) {
    printf("Hello, world!\n");
    return 0;
}

In this example, the program prints "Hello, world!" to the console and exits with a return value of '0'.

The program 'C:\hello.exe' has exited with code 0 (0x00000000).

Output:
Hello, world!

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.