Beginning with C Programming

Beginning with C Programming

1️⃣ Finding a Compiler

Before starting C programming, you need a compiler to compile and run programs.

✔ Online C Compilers (No Installation Needed)

You can use:

These allow you to write and run C programs directly in the browser.


2️⃣ Compilers for Different Operating Systems

🪟 Windows

Free C compilers available:


🐧 Linux

  • GCC usually comes bundled with Linux

  • Code::Blocks can also be used

This guide is based on Linux (Ubuntu) and examples are compiled on Ubuntu.


3️⃣ Check if GCC is Installed (Linux/UNIX)

Open terminal and type:

$ gcc -v
  • If GCC is installed → version information will be displayed

  • If not installed → follow instructions:

https://gcc.gnu.org/install/


4️⃣ Writing Your First C Program

Example Program

/* my first program */ #include <stdio.h> int main(void) { printf("Hello World"); return 0; }

Output

Hello World

5️⃣ Program Explanation (Line by Line)

Line 0

/* my first program */
  • Multi-line comments use /* */

  • Comments are ignored by the compiler

  • Improve readability and documentation

  • Single-line comments use //


Line 1

#include <stdio.h>
  • Lines starting with # are processed by the preprocessor

  • Preprocessor converts the program before compilation

  • stdio.h is a header file

  • Contains declarations for functions like printf()


Line 2

int main(void)
  • Entry point of the C program

  • Execution begins from main()

  • void → no parameters passed

  • int → return type of the function

  • Returned value indicates program termination status


Line 3 and Line 6

{ }
  • Curly braces define scope

  • Used in functions, loops, conditions

  • Every function must start and end with {}


Line 4

printf("Hello World");
  • Standard library function for output

  • Prints text to screen

  • Semicolon ; marks end of statement

  • Mandatory in C


6️⃣ Compile and Execute a C Program

Follow these steps:

Step 1: Open a text editor

Examples:

vi, emacs, gedit

Step 2: Save file as

hello.c

Step 3: Go to the directory containing the file

Step 4: Compile the program

$ gcc hello.c

If no errors occur, an executable file is created:

a.out

Step 5: Run the program

$ ./a.out

Output

Hello, World!

✔ Important Notes

  • Ensure gcc is installed

  • Ensure gcc is in your system PATH

  • Run the command from the directory containing hello.c

Comments

Popular posts from this blog

Programming in C GXEST204 - KTU 2024 scheme syllabus notes pdf ------- Dr Binu V P

Structure of a C Program

Single and Multi Dimensional Arrays