Introduction to C Assignments — 13 Programs with Solutions
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

Introduction to C Assignments in C Language with Explanation and Solutions

📑 Jump to a category:
  • Comments & Structure (1–3)
  • Variables & Data Types (4, 8, 12)
  • Operators & Arithmetic (5, 10, 11)
  • Input/Output & Calculations (6, 7, 9, 13)
  • Previous / Next Topic
📚 What you'll practice across these 13 assignments:
  • Writing single-line and multi-line comments — and when to use each
  • Declaring and initializing variables of different data types (int, float, char, double)
  • Using the sizeof operator to inspect type sizes
  • Reading user input with scanf and printing with printf
  • Swapping two variables without a third temporary variable
  • Performing basic arithmetic — addition, subtraction, multiplication, division
  • Converting between temperature units with proper type handling
  • Calculating areas of circles and rectangles
  • Computing averages from multiple inputs
  • Mixing strings and numeric inputs in the same program

Every C program you'll ever write rests on the same handful of foundations: comments, variables, data types, operators, and the input/output functions printf and scanf. This page collects 13 introduction-to-C assignments, arranged from the simplest (a program with a single comment) to the more practical (a program that reads employee data and prints it back). Every assignment links to a full tutorial with the program code, sample output, and a line-by-line explanation.

If you're learning C for the first time, work through these in order. Each assignment builds on the previous ones — assignment 1 shows you how to comment, assignment 2 uses those comments in a real program, assignment 3 combines comments with expressions, and by assignment 13 you're reading multiple inputs, doing calculations, and displaying formatted output. By the end, you'll have written 13 working programs and understood every line.

Introduction to C assignments — basic practical assignments with solutions in the C programming language.

🔹 Comments & Program Structure

Start here — learn to write comments, then use them to document a real program.

  • 1. Write a program in C to find the area of a circle using single-line comments.
  • 2. Write a C program to find the addition of two numbers with multi-line comments that document the purpose of every step.
  • 3. Write a program in C that describes a complex mathematical formula with nested comments.

🔹 Variables & Data Types

Declare, initialize, and inspect the fundamental data types of C.

  • 4. Write a C program to demonstrate the declaration and initialization of variables of different data types.
  • 8. Write a program in C to demonstrate and decide the size of int, float, char, and double data types.
  • 12. Write a program in C to input a user's name and age and display them.

🔹 Operators & Arithmetic

Apply arithmetic operators, perform a swap without a temporary variable, and compute basic expressions.

  • 5. Write a program in C to swap the values of two variables without using a third variable.
  • 10. Write a C program to perform basic arithmetic operations on any two numbers of various data types.
  • 11. Write a program in C that accepts any two numbers from the user and calculates addition, multiplication, subtraction, and division, then shows the result.

🔹 Input/Output & Calculations

Read data from the user, perform real-world calculations, and format the output.

  • 6. Write a program in C to find the area of a rectangle by accepting the length and width as variables.
  • 7. Write a program that accepts user input for employee name and salary, then displays them using those variables.
  • 9. Write a program in C to convert temperature from Celsius to Fahrenheit using proper data types.
  • 13. Write a program in C that asks the user to enter the marks obtained in three subjects, reads them with scanf, calculates the average marks, and displays the student details.

How to Get the Most Out of These Assignments

Reading a solution is not the same as writing one. For each assignment, follow this three-step workflow:

  1. Attempt first. Open a blank editor and try to solve the problem using only the assignment description. Even if you get stuck, the act of trying cements the concept.
  2. Compare carefully. Open the solution page and compare your approach to the reference. Look for differences in variable naming, type choices, and output formatting.
  3. Re-implement from scratch. Close the solution page and write the program again without looking. This is the step most learners skip — and it's the one that actually builds fluency.

💡 Tip: Compile with gcc -Wall -Wextra -std=c11 -g so the compiler flags mistakes as you make them. Use printf to trace variable values between statements — for a beginner, seeing the actual value at each step is the fastest way to understand what the code is doing. Also run each program under valgrind to catch memory bugs early, though these introductory programs rarely have any.

❓ Introduction to C Viva Questions & Quick Answers

Q1. What is the difference between single-line and multi-line comments in C?

Single-line comments start with // and extend to the end of the line. Multi-line comments start with /* and end with */ — they can span many lines. Use single-line for short remarks, multi-line for longer explanations. Modern C (since C99) supports both; older C (C89) only supported multi-line, but almost every compiler accepts // now.

Q2. What's the difference between a declaration and an initialization?

A declaration reserves memory and associates a name with a type — e.g., int x;. An initialization gives the variable its first value — e.g., int x = 42;. You can declare without initializing, but if you then read the variable before assigning to it, you get an indeterminate value.

Q3. Why does scanf("%d", x) crash, and scanf("%d", &x) doesn't?

Because scanf needs an address to write into. Passing x passes its value, not its address — scanf would try to write to whatever address the value happens to look like, which is almost always a segfault. Always pass &x for scalar types. For strings (character arrays), you can pass the array name directly because it decays to a pointer.

Q4. Why does sizeof(int) sometimes print 4 and sometimes 8?

Because the C standard does not fix the size of int — it only says it must be at least 16 bits. On most modern 64-bit systems, int is 32 bits (4 bytes). But on some embedded systems, it's 16 bits (2 bytes). The long type varies even more. If you need a guaranteed size, use <stdint.h> types like int32_t or int64_t.

Q5. How can I swap two numbers without a third variable?

Two common ways. Method 1: arithmetic — a = a + b; b = a - b; a = a - b;. This works but can overflow if a + b exceeds INT_MAX. Method 2: XOR — a ^= b; b ^= a; a ^= b;. This is overflow-free for integers, but not for floating-point. Both are interesting puzzle solutions; in real code, prefer the plain temp = a; a = b; b = temp; — it's clearer and works for any type.

Q6. Why does Celsius-to-Fahrenheit sometimes print the wrong answer?

The formula is F = C * 9 / 5 + 32. The trap is integer division: if C is an integer and you write C * 9 / 5 with C = 100, you get 900 / 5 = 180 — correct here, but for C = 37, you get 333 / 5 = 66 instead of 66.6. Use floating-point types or cast explicitly: F = C * 9.0 / 5.0 + 32. Always be aware of what type each operand is in an expression.

📖 Related Tutorials

  • What is Program?
  • Structure of a C Program
  • Working of a C Program
  • Character Set
  • Variables and Identifiers
  • Built-in Data Types
  • Operators and Expressions
  • Constants and Literals
  • Basic Input/Output Statements
  • Simple C Programs
  • Introduction to C FAQ
  • Introduction to C MCQ

Previous Topic: -->> Dynamic Memory FAQ   ||   Next topic: -->> Operators Assignments in C


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.