C Basics Examples

Begin with printing, swapping, sizes, character checks, and simple control flow. These concise programs build confidence with C syntax and the standard library.

C Program to Calculate Difference Between Two Time Periods

Program (C)
Example & Output

Subtracts times with borrow.

01:19:40

C "Hello, World!" Program

Program (C)
Example & Output

Prints a greeting to the console.

Hello, World!

C Program to Print an Integer (Entered by the User)

Program (C)
Example & Output

Demonstrates integer I/O.

You entered: 123

C Program to Add Two Integers

Program (C)
Example & Output

Adds two integers and prints the sum.

Sum = 8

C Program to Multiply Two Floating-Point Numbers

Program (C)
Example & Output

Multiplies two floats.

Product = 10.00

C Program to Find ASCII Value of a Character

Program (C)
Example & Output

Prints ASCII of a character.

ASCII of A = 65

C Program to Compute Quotient and Remainder

Program (C)
Example & Output

Integer division and modulo.

Quotient = 6
Remainder = 2

C Program to Find the Size of int, float, double and char

Program (C)
Example & Output

Uses sizeof operator.

sizeof(char) = 1
sizeof(int) = 4
sizeof(float) = 4
sizeof(double) = 8

C Program to Demonstrate the Working of Keyword long

Program (C)
Example & Output

Shows long type usage.

long x = 1234567890

C Program to Swap Two Numbers

Program (C)
Example & Output

Swaps using temp variable.

a = 4, b = 3

C Program to Check Whether a Number is Even or Odd

Program (C)
Example & Output

Checks parity using modulo.

7 is odd

C Program to Check Whether a Character is a Vowel or Consonant

Program (C)
Example & Output

Checks vowel set.

e is vowel

C Program to Find the Largest Number Among Three Numbers

Program (C)
Example & Output

Finds max of three.

Largest = 17

C Program to Find the Roots of a Quadratic Equation

Program (C)
Example & Output

Quadratic formula for ax^2+bx+c.

Roots: 2.00, 1.00

C Program to Check Leap Year

Program (C)
Example & Output

Checks leap year rules.

2024 is leap year

C Program to Check Whether a Number is Positive or Negative

Program (C)
Example & Output

Checks sign.

-10 is negative

C Program to Check Whether a Character is an Alphabet or not

Program (C)
Example & Output

Uses isalpha.

# is not alphabet

C Program to Display Characters from A to Z Using Loop

Program (C)
Example & Output

Iterates ASCII letters.

ABCDEFGHIJKLMNOPQRSTUVWXYZ

C Program to Calculate Standard Deviation

Program (C)
Example & Output

Computes population std dev.

StdDev = 2.00

C Program Swap Numbers in Cyclic Order Using Call by Reference

Program (C)
Example & Output

Cyclic swap a->b->c.

a=3 b=1 c=2

C Program to Find Largest Number Using Dynamic Memory Allocation

Program (C)
Example & Output

Uses malloc for array.

Largest = 9

C Program to Find the Frequency of Characters in a String

Program (C)
Example & Output

Counts frequency of a given char.

'l' occurs 3 times

C Program to Count the Number of Vowels, Consonants and so on

Program (C)
Example & Output

Classifies characters.

Vowels=3 Consonants=7 Digits=3 Spaces=2 Others=1

C Program to Remove all Characters in a String Except Alphabets

Program (C)
Example & Output

Filters only letters.

HllWrld

C Program to Find the Length of a String

Program (C)
Example & Output

Uses strlen.

Length = 5

C Program to Concatenate Two Strings

Program (C)
Example & Output

Uses strcat safely.

Hello World

C Program to Copy String Without Using strcpy()

Program (C)
Example & Output

Manual copy loop.

Sample

C Program to Sort Elements in Lexicographical Order (Dictionary Order)

Program (C)
Example & Output

Sorts array of strings.

apple banana cherry

C Program to Store Information of a Student Using Structure

Program (C)
Example & Output

Defines struct and prints.

Alice 20 92.5

C Program to Add Two Distances (in inch-feet system) using Structures

Program (C)
Example & Output

Adds feet-inch distances.

9 feet 7 inch

C Program to Add Two Complex Numbers by Passing Structure to a Function

Program (C)
Example & Output

Adds complex numbers.

4.0 + 5.0i

C Program to Store Data in Structures Dynamically

Program (C)
Example & Output

Allocates struct via malloc.

1 3.14

C Program to Write a Sentence to a File

Program (C)
Example & Output

Writes a line to file.

Written.

C Program to Read the First Line From a File

Program (C)
Example & Output

Reads first line of file.

First line: Hello file

C Program to Display its own Source Code as Output

Program (C)
Example & Output

Prints contents of source via argv[0].

(program source content)

C Program to Print Pyramids and Patterns

Program (C)
Example & Output

Simple star pyramid.

    *
   ***
  *****
 *******
*********

Frequently Asked Questions

Use a standard C compiler such as gcc or clang. For example: gcc program.c -o program && ./program. On Windows, you may use MinGW or tdm-gcc; on macOS/Linux, the default package managers provide compilers.

The programs are compatible with the C99 standard and above. If you use older compilers, ensure the appropriate flags are set or update your toolchain.

Yes. These examples are intended for learning. Copy the code, experiment with changes (e.g., different inputs), and observe the output to deepen understanding.

The examples use standard library functions and portable constructs, and have been tested on major platforms (Windows, macOS, Linux). They should compile and run with any standards-compliant C compiler.

Learn C the Practical Way

C remains one of the most influential programming languages, powering operating systems, embedded devices, and performance-critical software. Practicing small, focused programs builds muscle memory and clarity with syntax and concepts such as data types, control flow, functions, arrays, pointers, and memory management.

On this page, you will find clean, well-formatted examples that compile across platforms. Each example includes a short description and expected output so you can quickly verify your understanding. Explore the topics via the sidebar and extend the code to experiment further.

Whether you’re preparing for interviews, working through coursework, or refreshing fundamentals, these examples offer a practical path to mastering C. Bookmark the page and return often to continue improving.