TutorialsCC Introduction
Share:

C Introduction

beginner

Part of C Fundamentals

Theory

C is a general-purpose, procedural programming language created by Dennis Ritchie at Bell Labs in 1972. It was developed to write the UNIX operating system and remains one of the most influential languages in computing history.

Why learn C:

  • Foundation — C influences many modern languages (C++, Java, C#, Python)
  • Performance — C produces efficient, fast machine code
  • System programming — operating systems, embedded systems, compilers
  • Memory management — direct control over memory allocation
  • Portability — C compilers exist for virtually every platform

A basic C program has this structure:

#include <stdio.h>
 
int main() {
    // code here
    return 0;
}

The #include directive includes header files. <stdio.h> provides input/output functions like printf(). The main() function is the program's entry point — execution starts here. return 0 indicates successful execution.

The compilation process transforms C source code into an executable:

  1. Preprocessor — handles #include, #define, and other directives
  2. Compiler — translates C code to assembly language
  3. Assembler — converts assembly to machine code (object file)
  4. Linker — combines object files into a single executable

Comments in C:

// Single-line comment (C99)
/* Multi-line
   comment */

Tokens are the smallest elements of a C program — keywords, identifiers, constants, string literals, and operators. Keywords are reserved words like int, if, return, void, while.

Basic syntax rules:

  • Statements end with semicolons (;)
  • Code blocks are enclosed in curly braces {}
  • C is case-sensitive (main vs Main)
  • Whitespace is generally ignored (unlike Python)

To compile and run a C program with GCC:

gcc program.c -o program
./program

Practical Examples

Example 1: Your First C Program
c
Example 2: Program Structure and printf
c

Exercises

Print a Pattern

easy

Write a C program that prints a right-angle triangle pattern of asterisks with 5 rows using multiple printf() statements.

Starter Code:

#include <stdio.h>\n\nint main() {\n  // Your code here\n  return 0;\n}

Expected Output:

*\n**\n***\n****\n*****

Personal Information Cards

easy

Write a C program that prints your name, age, and favorite programming language in a formatted card with a border. Use at least 3 printf() statements.

Starter Code:

#include <stdio.h>\n\nint main() {\n  // Your code here\n  return 0;\n}

Expected Output:

+------------------+\n| Name: Alice      |\n| Age: 25          |\n| Language: C      |\n+------------------+

Fix Compilation Errors

medium

Fix the following C code that has multiple errors: \n\n#include <stdio.h>\n\nint main() {\n printf('Hello World\\n')\n return 0\n}\n

Starter Code:

#include <stdio.h>\n\nint main() {\n  printf('Hello World\\n')\n  return 0\n}

Expected Output:

Hello World

Mini Quiz

Mini Quiz

Mini Project

Mini Project: ASCII Art Banner Generator

Create a C program that prints a large ASCII art banner displaying the letters 'C' in a 5x7 grid pattern using printf().

Requirements:

    Bonus Challenge

    Make the banner dynamic by printing the user's first initial instead of 'C'.