Programming in C using gcc in Linux


Installing GCC and Writing Your First C Program

1️⃣ Check Whether GCC Is Installed

If you are using Linux or UNIX, check if GCC is installed:

$ gcc -v

2️⃣ Install GCC on Ubuntu

Method 1: Install GCC Directly

Open terminal and run:

$ sudo apt-get install gcc

(Example: Ubuntu 18.04 Bionic Beaver / 20.04 Focal Fossa)


Method 2: Install Using build-essential (Recommended)

This installs:

  • gcc compiler

  • g++ compiler

  • required libraries

$ sudo apt-get install build-essential

3️⃣ Check GCC Version After Installation

$ gcc --version

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 in C are written between /* */

  • Comments are ignored by the compiler

  • Improve readability and documentation

  • Single-line comments use //


Line 1

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

  • Preprocessor processes header files before compilation

  • stdio.h contains declarations for functions like printf()

  • Header files usually end with .h


Line 2

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

  • Execution begins from main()

  • void → no parameters passed

  • int → return type of the function


Line 3 & 6

{ }
  • Curly brackets define scope

  • Used in functions, loops, conditions

  • Every function must start and end with { }


Line 4

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

  • Semicolon ; marks end of statement

  • Mandatory in C


6️⃣ Compile and Execute a C Program

Step 1: Save the file

Save as:

hello.c

Step 2: Compile

$ gcc hello.c
  • If successful → creates executable:

a.out

Step 3: Run Program

$ ./a.out

Output:

Hello World

Specify Output File Name Using -o

$ gcc -o hello hello.c $ ./hello

Output:

Hello World

✔ Ensure:

  • gcc is in PATH

  • you are in the same directory as hello.c


7️⃣ Important GCC Compiler Options


1. Specify Output Executable Name (-o)

$ gcc hello.c -o hello

Creates executable named hello.


2. Enable All Warnings (-Wall)

$ gcc -Wall hello.c

Shows all compiler warnings.


3. Produce Only Preprocessor Output (-E)

$ gcc -E hello.c > hello.i

Generates preprocessed file.


4. Produce Assembly Code (-S)

$ gcc -S hello.c > hello.s

Creates assembly output.


5. Produce Object File Only (-c)

$ gcc -c hello.c

Creates:

hello.o

(Object/machine-level compiled code without linking)


6. Save All Intermediate Files (-save-temps)

$ gcc -save-temps hello.c $ ls

Output may include:

a.out hello.c hello.i hello.o hello.s

Stores all compilation stages.


7. Link With Shared Libraries (-l)

$ gcc -Wall hello.c -o main -lCPPfile

Links program with shared library libCPPfile.


8. Show Verbose Compilation Steps (-v)

$ gcc -Wall -v hello.c -o hello

Displays detailed compilation steps.


9. Provide GCC Options From a File (@ option)

Create options file:

$ cat opt_file -Wall -o hello

Compile using options file:

$ gcc main.c @opt_file

8️⃣ Online C Compiler

You can also run C programs online:

https://www.onlinegdb.com/




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