0% found this document useful (0 votes)
3 views25 pages

C Manual

The document is a practical guide for learning C programming, covering fundamental concepts such as arithmetic operations, operators, decision statements, loops, functions, arrays, pointers, and structures. It includes detailed explanations, examples, and practice problems for each topic to enhance understanding and application of C programming. The guide also provides instructions on setting up an Integrated Development Environment (IDE) for coding in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

C Manual

The document is a practical guide for learning C programming, covering fundamental concepts such as arithmetic operations, operators, decision statements, loops, functions, arrays, pointers, and structures. It includes detailed explanations, examples, and practice problems for each topic to enhance understanding and application of C programming. The guide also provides instructions on setting up an Integrated Development Environment (IDE) for coding in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Let’s C: A Practical

Guide to Learn C
programming
CONTENTS

Acknowledgments i

1 What is Programming 1

2 Arithmetic Operations in C Pg #

3 Operators in C Pg #

4 Decision Statements Pg #

5 Loops Pg #

6 Functions Pg #

7 Arrays Pg #

8 Pointers and Strings Pg #

9 File operations Pg #

10 Object Oriented Programming Pg #


ACKNOWLEDGMENTS

Thanks to the writer of the book “Capsules of C Programming”.


Most of the guided problems in this book are taken from
“Capsules of C Programming”.

i
1 WHAT IS PROGRAMMING

Programming is talking to computer. It is a language to communicate


with your computer. The main unit of a computer is microprocessor.
Each microprocessor is designed to execute a certain set of
instructions. Actually, through programming, we pass these
instructions to computer and computer executes these instructions
in given order.
What is inside a Microprocessor?
A microprocessor contains millions of transistors and capacitive
elements. All these components operate after providing voltages.
Presence of voltage means a signal 1 and absence of voltage means
signal 0. So, a computer is only able to understand presence and
absence of voltages.
Machine Language to C:
Computer only understands machine language also called binary
language. A machine language instruction consists of only 0s and 1s.
For example, to add two numbers, machine language instruction can
be 00011001. It is very difficult to remember and understand these
machine language instructions for programmers.
To make the instructions understandable to the programmer,
assembly language was formed. Assemble language consists of some
simple statements i.e. sum a, b. This instruction is supposed to add
a and b. Each processor has its own assembly language instruction
set. So, it can only execute instructions from its instruction set.

1
Let’s C: A Practical Guide to Learn C programming

Whenever an assembly language instruction is executed, it is first


converted into equivalent machine language instruction (consisting
of 0s and 1s).
Although assembly language is more easy to understand for the
programmers, there is still missing information in assembly language
instructions, for example, sum a, b is equivalent to a = a + b, but it
cannot be inferred from the instruction that result will be assigned
to a. To facilitate the programmers further, another language was
designed named “C Language”. This language was implemented to
write programs in mathematical language. In C language, if we have
to sum a and b and store the result in a, the instruction will be: a =
a + b. There is no hidden information in this statement.
What is Programming:
When a C program is executed, it is first translated into equivalent
assembly language code, which is further translated into equivalent
machine language code. The computer finally executes machine
language instructions being translated from C program. Hence a C
program is “a set of sequence of commands being sent to
microprocessor for execution.”
One thing should be noticed here that programming is not only
writing instructions to the computer but it is instructions in the right
format to form a required logic (i.e. to sum a and b etc.).
Integrated Development Environment for C:
Compilation of a C program converts C code to machine
language instructions. There are many software available for
compiling and running C programs. Some famous IDEs are
Microsoft Visual Studio, Visual C++ and Eclipse etc. Online
compilers can also be explored. In this book, we will only learn to
use Microsoft Visual Studio.
Getting Started with Microsoft Visual Studio:
1. Download the setup from official visual studio website.
2. Install visual studio on your computer.
3. Open the application
4. Click new project

2
Let’s C: A Practical Guide to Learn C programming

5. Select Empty Project and name the project.

6. Click on add new source

3
Let’s C: A Practical Guide to Learn C programming

7. Select C++ file and name this file

8. Write this code in your source file

9. Compile/build the code.

4
Let’s C: A Practical Guide to Learn C programming

10. Debug the Program

11. See the output

5
Let’s C: A Practical Guide to Learn C programming

Step-in debugging in Microsoft Visual Studio:


One very useful feature of Microsoft Visual Studio is the step
wise debugging of code. You can run your code line by line to find
the out the reason for errors rather than running the code at once.

6
2 ARITHMETIC OPERATIONS IN C

This chapter covers the basic understanding of arithmetic operations


in C and some guided problems to get hands on experience on
performing arithmetic operations in C.
Arithmetic Operations:
Arithmetic operations means performing simple computations
using mathematical expressions in C. These expressions may include
sum, subtraction, multiplication and division operators etc. Some
guided problems for practice are provided in next sections.
Practice Problems:
1. Write a program to display hello world in C.
2. Write a program to add two numbers (5&7) and display its sum.
3. Write a program to multiply two numbers (10&8) and display its
product.
4. Write a program to calculate area of a circle having its radius (r=5).
5. Write a program to calculate area of an ellipse having its axes
(minor=4cm, major=6cm).
6. Write a program to calculate simple interest for a given P=4000,
T=2, R=5.5. (I = P*T*R/100)

7
3 OPERATORS IN C

This chapter covers the data types and operators available in C. It


also provides some guided problems to get hands on practice on
solving different expressions in C.
Data types:
Data types are the available types of variables that can be declared
in C. Different types of data require different types of variables to
store data in a convenient form. Some common examples of the data
types are: int, char, uint8_t, float, double and long etc.
Operators:
Operators are used to solve expressions in a programming
language such as sum, subtract, divide, multiply, modulus and
bitwise operators etc.
Practice Problems:
1. Write a program to take input of name, rollno and marks obtained
by a student in 5 subjects each have its 100 full marks and display
the name, roll no with percentage score secured.
2. Write a program to declare two integer and one float variables
then initialize them to 10, 15, and 12.6. Also print the variable values
in the screen.
3. Write a C program to prompt the user to input 3 integer values
and print these values in forward and reversed order.
4. Write a program to calculate simple and compound interest.
5. Write a program to swap two variables values with and without

9
Let’s C: A Practical Guide to Learn C programming

using third variables.


6. Write a program to check odd or even number (a) using modulus
operator (b) using bitwise operator (c) without using bitwise and
modulus operator (d) using conditional operator.
7. Print the value of y for given x=2 & z=4 and analyze the output.
a. y = x++ + ++x; b. y= ++x + ++x;
c. y= ++x + ++x + ++x; d. y = x>z; e. y= x>z? x:z;
f. y = x&z; g. y= x>>2 + z<<1;
8. Write a program to print the size of char, float, double and long
double data types in C.

10
4 DECISION STATEMENTS IN C

Decision statements are used when you have to use between two
different types of sequence of commands and the decision is based
on a condition.
Practice Problems:
1. Write a program to input marks of 5 subjects (Physics, Chemistry,
Math, English & Biology) for a student. Display the rank of each
subjects and also the result of total marks and percentage obtained
with his/her rank in the class. The rank is categorized as fail (marks
< 40%), pass & third division (marks between 40 to 55%), second
(marks between 55 to 65%), first (marks between 65 to 80%),
Distinction (marks between 80 to 95%), extra ordinary (marks above
95 to 100%).
2. Write a program to find the largest and smallest among three
entered numbers and also display whether the identified
largest/smallest number is even or odd.
3. Write a program to check whether input alphabet is vowel or not
using if-else and switch statement.
4. Write a program to get input of two or higher digit integer number
and display in reverse order.
5. Write a program that asks a number and test the number whether
it is multiple of 5 or not, divisible by 7 but not by eleven.
6. Write a program to check whether the entered year is leap year or
not (a year is leap if it is divisible by 4 and divisible by 100 or 400.)
7. Write a program to read the values of coefficients a, b and c of a

12
Let’s C: A Practical Guide to Learn C programming

quadratic equation ax2+bx+c=0 and find roots of the equation.

13
5 LOOPS

Practice Problems:
1. Write a program to find sum as Y of the following series excluding
prime numbers in the series:
1 22 32 102
𝑌 =1+ + + + ⋯+
1! 2! 3! 10!
2. Write a program to input two integer numbers and display the sum
of even numbers between these two input numbers.
3. Write a program to display Fibonacci series of last term up to 300.
4. Write a program to display the flag of Nepal using symbolic/HEX
character in C.

5. Write a program to display the following:

16
Let’s C: A Practical Guide to Learn C programming

17
6 FUNCTIONS

Practice Problems:
1. Write a program to find sum as Y of the following series excluding
prime number in the series. (Write function program to check
whether the number is prime or not. also write recursive function to
calculate the factorial of the series numbers).
1 22 32 102
𝑌 =1+ + + + ⋯+
1! 2! 3! 10!
2. Write a program to add, subtract, multiply and divide two integers
using user defined type function with return type.
3. Write a program to calculate sum of first 50 natural numbers using
recursive function.
4. Define a function named fact() to calculate factorial of a number
n and then write a program that uses this function fact() to calculate
combination and permutation.
5. Write a recursive function to generate Fibonacci series.
6. Write a program that illustrates use of local, global and static
variables.

18
7 ARRAYS

Practice Problems:
1. Write a program to find separately the sum of the positive and
negative integer elements of an array of size 10. Pass this array to a
function called sortarray(int[]) and display the array elements into
ascending order.
2. Write a program to enter 10 floating numbers in an array and
display it.
3. Write a program to display largest and smallest element of an array
defined in Q.No. 2.
4. Write a program to initialize one dimensional array of size 8 and
display the sum and average of array elements.
5. Write a program to read two matrices of order 3 * 2, add them
and display the resultant matrix in matrix form.
6. Write a program to multiply two 3*3 matrix.
7. Write a program to read a string and check for palindrome without
using string related function (a string is palindrome if its half is
mirror by itself eg: abcdcba).

22
8 POINTERS AND STRINGS

Practice Problems:
1. Write a program to find separately the sum of the positive and
negative integer elements of an array of size 10. Pass the positive and
negative elements to separate functions eg: sumpositive(int*),
sumnegative(int*) to carry out its sum. Also pass this array to a
function called sortarray(int[]) and display the array elements into
ascending order using pointer.
2. Write a program to find biggest among three numbers using
pointer
3. Write a program to find the sum of all the elements of an array
using pointers.
4. Write a program to swap value of two variables using pointer.
5. Write a program to read a sentence and count the number of
characters &words in that sentence.
6. Write a program to read a sentence & delete all the white spaces.
Replace all “.” by “:”.
7. Write a program to copy one string to another string with and
without using string handling function.
8. Write a program to concatenate two strings.
9. Write a program to compare two strings.
10. Write a program to sort 5 string words stored in an array of
pointers.
11. Write a program to print the following pattern:

26
Let’s C: A Practical Guide to Learn C programming

UN
UNIV
UNIVER
UNIVERSI
UNIVERSITY
UNIVERSI
UNIVER
UNIV
UN

27
9 STRUCTURES

Practice Problems:
1. Write a program to read RollNo, Name, Address, Age & marks in
physics, C, math in 1st semester of three students in BCT and display
the student details with average marks achieved.
2. Create a structure named company which has name, address,
phone and noOfEmployee as member variables. Read name of
company, its address, phone and noOfEmployee. Finally display
these members’ value.
3. Write a program to enter to Cartesian coordinate points and
display the distance between them.
4. Write a function which accepts structure as argument and returns
structure to the calling program.
5. Pass the structures defined in Question 1 into a function and read
the structure member and display the values from the function (use
structure pointer).
6. Define a structure “complex” (typedef) to read two complex
numbers and perform addition, subtraction of these two complex
numbers and display the result.
7. Write a program to read RollNo, Name, Address, Age & average-
marks of 12 students in the BCT class and display the details from
function.
8. Write a program to show programming examples with union and

31

You might also like