0% found this document useful (0 votes)
15 views23 pages

Chapter 1 Notes

Chapter 1 introduces the concept of algorithms in programming, defining them as a set of finite rules or instructions for problem-solving. It discusses the characteristics, properties, advantages, and disadvantages of algorithms, as well as how to design them, using examples like adding three numbers. Additionally, it explains flowcharts as graphical representations of algorithms, their symbols, uses, and advantages in programming.

Uploaded by

dawol39691
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)
15 views23 pages

Chapter 1 Notes

Chapter 1 introduces the concept of algorithms in programming, defining them as a set of finite rules or instructions for problem-solving. It discusses the characteristics, properties, advantages, and disadvantages of algorithms, as well as how to design them, using examples like adding three numbers. Additionally, it explains flowcharts as graphical representations of algorithms, their symbols, uses, and advantages in programming.

Uploaded by

dawol39691
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/ 23

Chapter 1

Introduction to “C” Programming

Definition of Algorithm
The word Algorithm means ” A set of finite rules or instructions to be followed in calculations or
other problem-solving operations ”
Or
” A procedure for solving a mathematical problem in a finite number of steps that frequently
involves recursive operations”.

Therefore Algorithm refers to a sequence of finite steps to solve a particular problem.

Algorithms can be simple and complex depending on what you want to achieve.
It can be understood by taking the example of cooking a new recipe. To cook a new recipe, one reads the
instructions and steps and executes them one by one, in the given sequence. The result thus obtained is the
new dish is cooked perfectly. Every time you use your phone, computer, laptop, or calculator you are using
Algorithms. Similarly, algorithms help to do a task in programming to get the expected output.
The Algorithm designed are language-independent, i.e. they are just plain instructions that can be
implemented in any language, and yet the output will be the same, as expected.

What is the need for algorithms? SHREYASH


1. Algorithms are necessary for solving complex problems efficiently and effectively.
2. They help to automate processes and make them more reliable, faster, and easier to perform.
3. Algorithms also enable computers to perform tasks that would be difficult or impossible for humans to
do manually.
4. They are used in various fields such as mathematics, computer science, engineering, finance, and many
others to optimize processes, analyze data, make predictions, and provide solutions to problems.
Characteristics of an Algorithm

1. Clear and Unambiguous: Each step must be precise and lead to only one interpretation.
2. Well-Defined Inputs: Inputs should be clearly specified, though an algorithm may not require any.
3. Well-Defined Outputs: The algorithm must produce at least one well-defined output.
4. Finiteness: The algorithm must terminate after a finite number of steps.
5. Feasibility: It should be practical and executable with available resources.
6. Language Independent: The algorithm should be described in plain instructions, implementable in
any programming language.
7. Definiteness: Instructions must be precise and easy to interpret without ambiguity.
8. Effectiveness: The algorithm should use basic operations that can be manually traced.

Properties of Algorithm:
• It should terminate after a finite time.
• It should produce at least one output.
• It should take zero or more input.
• It should be deterministic means giving the same output for the same input case.
• Every step in the algorithm must be effective i.e. every step should do some work.

Advantages of Algorithms:
• It is easy to understand.
• An algorithm is a step-wise representation of a solution to a given problem.
• In an Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the
programmer to convert it into an actual program.

Disadvantages of Algorithms:
• Writing an algorithm takes a long time so it is time-consuming.
• Understanding complex logic through algorithms can be very difficult.
• Branching and Looping statements are difficult to show in Algorithms(imp).
How to Design an Algorithm?
To write an algorithm, the following things are needed as a pre-requisite:
1. The problem that is to be solved by this algorithm i.e. clear problem definition.
2. The constraints of the problem must be considered while solving the problem.
3. The input to be taken to solve the problem.
4. The output is to be expected when the problem is solved.
5. The solution to this problem is within the given constraints.
Example: Consider the example to add three numbers and print the sum.
Step 1: Fulfilling the pre-requisites
As discussed above, to write an algorithm, its prerequisites must be fulfilled.
1. The problem that is to be solved by this algorithm: Add 3 numbers and print their sum.
2. The constraints of the problem that must be considered while solving the problem: The numbers
must contain only digits and no other characters.
3. The input to be taken to solve the problem: The three numbers to be added.
4. The output to be expected when the problem is solved: The sum of the three numbers taken as the
input i.e. a single integer value.
5. The solution to this problem, in the given constraints: The solution consists of adding the 3
numbers. It can be done with the help of the ‘+’ operator, or bit-wise, or any other method.

Step 2: Designing the algorithm


Now let’s design the algorithm with the help of the above pre-requisites:
Algorithm to add 3 numbers and print their sum:
1. START
2. Declare 3 integer variables num1, num2, and num3.
3. Take the three numbers, to be added, as inputs in variables num1, num2, and num3 respectively.
4. Declare an integer variable sum to store the resultant sum of the 3 numbers.
5. Add the 3 numbers and store the result in the variable sum.
6. Print the value of the variable sum
7. END

Step 3: Testing the algorithm by implementing it.


To test the algorithm, let’s implement it in C language.
Program:
C++
// C++ program to add three numbers
// with the help of above designed
// algorithm
#include <bits/stdc++.h>
using namespace std;

int main()
{

// Variables to take the input of


// the 3 numbers
int num1, num2, num3;

// Variable to store the resultant sum


int sum;

// Take the 3 numbers as input


cout << "Enter the 1st number: ";
cin >> num1;
cout << " " << num1 << endl;

cout << "Enter the 2nd number: ";


cin >> num2;
cout << " " << num2 << endl;

cout << "Enter the 3rd number: ";


cin >> num3;
cout << " " << num3;

// Calculate the sum using + operator


// and store it in variable sum
sum = num1 + num2 + num3;

// Print the sum


cout << "\nSum of the 3 numbers is: "
<< sum;

return 0;
}

// This code is contributed by shivanisinghss2110

C Java Python C# JavaScript


Output
Enter the 1st number: 0
Enter the 2nd number: 0
Enter the 3rd number: -1577141152

Sum of the 3 numbers is: -1577141152

Here is the step-by-step algorithm of the code:


1. Declare three variables num1, num2, and num3 to store the three numbers to be added.
2. Declare a variable sum to store the sum of the three numbers.
3. Use the cout statement to prompt the user to enter the first number.
4. Use the cin statement to read the first number and store it in num1.
5. Use the cout statement to prompt the user to enter the second number.
6. Use the cin statement to read the second number and store it in num2.
7. Use the cout statement to prompt the user to enter the third number.
8. Use the cin statement to read and store the third number in num3.
9. Calculate the sum of the three numbers using the + operator and store it in the sum variable.
10.Use the cout statement to print the sum of the three numbers.
11.The main function returns 0, which indicates the successful execution of the program.
Time complexity: O(1)
Auxiliary Space: O(1)
One problem, many solutions: The solution to an algorithm can be or cannot be more than one. It means that
while implementing the algorithm, there can be more than one method to implement it. For example, in the
above problem of adding 3 numbers, the sum can be calculated in many ways:
• + operator
• Bit-wise operators
• . . etc
Define Flowchart.
Flowcharts are graphical representations of algorithms or processes, aiding in the visual understanding of
code. They display step-by-step solutions, making them ideal for beginners to grasp algorithms in computer
science and troubleshoot issues. Flowcharts use standardized symbols and boxes to indicate process flow
sequentially, making them easy to interpret. Certain rules, widely accepted by professionals, guide the creation
of flowcharts.
What is FlowChart?
A flowchart is a type of diagram that represents a workflow or process. A flowchart can also be
defined as a diagrammatic representation of an algorithm, a step-by-step approach to solving a
task.

Flowchart symbols
Different types of boxes are used to make flowcharts flowchart Symbols. All the different kinds of boxes are
connected by arrow lines. Arrow lines are used to display the flow of control. Let’s learn about each box in
detail.

Symbol Name
Symbol Representation

Terminal/Terminator

Process

Decision

Document
Symbol Name
Symbol Representation

Data or Input/Output

Stored Data

Flow Arrow

Comment or Annotation

Predefined process

On-page connector/reference

Off-page connector/reference
Uses of Flowcharts in Computer Programming/Algorithms
The following are the uses of a flowchart:
• It is a pictorial representation of an algorithm that increases the readability of the program.
• Complex programs can be drawn in a simple way using a flowchart.
• It helps team members get an insight into the process and use this knowledge to collect data, detect
problems, develop software, etc.
• A flowchart is a basic step for designing a new process or adding extra features.
• Communication with other people becomes easy by drawing flowcharts and sharing them.

When to Use Flowchart?


Flowcharts are mainly used in the below scenarios:
• It is most importantly used when programmers make projects. As a flowchart is a basic step to make
the design of projects pictorially, it is preferred by many.
• When the flowcharts of a process are drawn, the programmer understands the non-useful parts of the
process. So flowcharts are used to separate sound logic from the unwanted parts.
• Since the rules and procedures of drawing a flowchart are universal, a flowchart serves as a
communication channel to the people who are working on the same project for better understanding.
• Optimizing a process becomes easier with flowcharts. The efficiency of the code is improved with the
flowchart drawing.

Types of boxes used to make a flowchart


There are different types of boxes that are used to make flowcharts. All the different kinds of boxes are
connected to one another by arrow lines. Arrow lines are used to display the flow of control. Let’s learn about
each box in detail.

1. Terminal

This box is of an oval shape which is used to indicate the start or end of the program. Every flowchart diagram
has an oval shape that depicts the start of an algorithm and another oval shape that depicts the end of an
algorithm. For example:
2. Data

This is a parallelogram-shaped box inside which the inputs or outputs are written. This basically depicts the
information that is entering the system or algorithm and the information that is leaving the system or
algorithm. For example: if the user wants to input a from the user and display it, the flowchart for this would
be:

3. Process

This is a rectangular box inside which a programmer writes the main course of action of
the algorithm or the main logic of the program. This is the crux of the flowchart as the main processing codes
is written inside this box. For example: if the programmer wants to add 1 to the input given by the user, he/she
would make the following flowchart:
4. Decision

This is a rhombus-shaped box, control statements like if, condition like a > 0, etc are written inside this box.
There are 2 paths from this one which is “yes” and the other one is “no”. Every decision has either yes or no
as an option, similarly, this box has these as options. For example: if the user wants to add 1 to an even
number and subtract 1 if the number is odd, the flowchart would be:

5. Flow

This arrow line represents the flow of the algorithm or process. It represents the direction of the process flow.
in all the previous examples, we included arrows in every step to display the flow of the program. arrow
increases the readability of the program.

6. On-Page Reference

This circular figure is used to depict that the flowchart is in continuation with the
further steps. This figure comes into use when the space is less and the flowchart is long. Any numerical
symbol is present inside this circle and that same numerical symbol will be depicted before the continuation to
make the user understand the continuation. Below is a simple example depicting the use of On-Page
Reference
Advantages of Flowchart
• It is the most efficient way of communicating the logic of the system.
• It acts as a guide for a blueprint during the program design.
• It also helps in the debugging process.
• Using flowcharts we can easily analyze the programs.
• flowcharts are good for documentation.

Disadvantages of Flowchart
• Flowcharts are challenging to draw for large and complex programs.
• It does not contain the proper amount of details.
• Flowcharts are very difficult to reproduce.
• Flowcharts are very difficult to modify.

Question 1. Draw a flowchart to find the greatest number among the 2 numbers.
Solution:
Algorithm:

1. Start

2. Input 2 variables from user

3. Now check the condition If a > b, goto step 4, else goto step 5.

4. Print a is greater, goto step 6

5. Print b is greater

6. Stop

FlowChart:
Question 2. Draw a flowchart to check whether the input number is odd or even
Solution:
Algorithm:

1. Start

2. Put input a

3. Now check the condition if a % 2 == 0, goto step 5. Else goto step 4

4. Now print(“number is odd”) and goto step 6

5. Print(“number is even”)

6. Stop

FlowChart:

Question 3. Draw a flowchart to print the input number 5 times.


Solution:
Algorithm:
1. Start

2. Input number a

3. Now initialise c = 1

4. Now we check the condition if c <= 5, goto step 5 else, goto step 7.

5. Print a

6. c = c + 1 and goto step 4


7. Stop
FlowChart:

Flowchart to print the input number 5 times

Question 4. Draw a flowchart to print numbers from 1 to 10.


Solution:
Algorithm:

1. Start

2. Now initialise c = 1

3. Now we check the condition if c < 11, then goto step 4 otherwise goto step 6.

4. Print c

5. c = c + 1 then goto step 3

6. Stop

FlowChart:
Flowchart to print numbers from 1 to 10

Question 5. Draw a flowchart to print the first 5 multiples of 3.


Solution:
Algorithm:

1. Start

2. Now initialise c = 1

3. Now check the condition if c < 6, then goto step 4. Otherwise goto step 6

4. Print 3 * c

5. c += 1. Then goto step 3.

6. Stop

FlowChart:

Structure of the C Program


GeeksforGeeks
7–8 minutes

Last Updated : 11 Sep, 2024


The basic structure of a C program is divided into 6 parts which makes it easy to read, modify, document, and
understand in a particular format. C program must follow the below-mentioned outline in order to successfully
compile and execute. Debugging is easier in a well-structured C program.
Sections of the C Program
There are 6 basic sections responsible for the proper execution of a program. Sections are mentioned below:
1. Documentation
2. Preprocessor Section
3. Definition
4. Global Declaration
5. Main() Function
6. Sub Programs

1. Documentation
This section consists of the description of the program, the name of the program, and the creation date and
time of the program. It is specified at the start of the program in the form of comments. Documentation can be
represented as:
// description, name of the program, programmer name, date, time etc.

or
/*
description, name of the program, programmer name, date, time etc.
*/

Anything written as comments will be treated as documentation of the program and this will not interfere with
the given code. Basically, it gives an overview to the reader of the program.

2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program. Header files
help us to access other’s improved code into our code. A copy of these multiple files is inserted into our
program before the process of compilation.
Example:
#include<stdio.h>
#include<math.h>

3. Definition
Preprocessors are the programs that process our source code before the process of compilation. There are
multiple steps which are involved in the writing and execution of the program. Preprocessor directives start
with the ‘#’ symbol. The #define preprocessor is used to create a constant throughout the program. Whenever
this name is encountered by the compiler, it is replaced by the actual piece of defined code.
Example:
#define long long ll

4. Global Declaration
The global declaration section contains global variables, function declaration, and static variables. Variables
and functions which are declared in this scope can be used anywhere in the program.
Example:
int num = 18;
5. Main() Function
Every C program must have a main function. The main() function of the program is written in this section.
Operations like declaration and execution are performed inside the curly braces of the main program. The
return type of the main() function can be int as well as void too. void() main tells the compiler that the
program will not return any value. The int main() tells the compiler that the program will return an integer
value.
Example:
void main()

or
int main()

6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is shifted to the
called function whenever they are called from the main or outside the main() function. These are specified as
per the requirements of the programmer.
Example:
int sum(int x, int y)
{
return x+y;
}

Structure of C Program with example


Example: Below C program to find the sum of 2 numbers:
// Documentation
/**
* file: sum.c
* author: you
* description: program to find sum.
*/

// Link
#include <stdio.h>

// Definition
#define X 20

// Global Declaration
int sum(int y);

// Main() Function
int main(void)
{
int y = 55;
printf("Sum: %d", sum(y));
return 0;
}

// Subprogram
int sum(int y)
{
return y + X;
}
Explanation of the above Program
Below is the explanation of the above program. With a description explaining the program’s meaning and use.

Sections Description
/**
* file: sum.c
* author: you
It is the comment section and is part of the description section of the code.
* description: program
to find sum.
*/
Header file which is used for standard input-output. This is the preprocessor
#include<stdio.h>
section.
#define X 20 This is the definition section. It allows the use of constant X in the code.
This is the Global declaration section includes the function declaration that can be
int sum(int y)
used anywhere in the program.
int main() main() is the first function that is executed in the C program.
{…} These curly braces mark the beginning and end of the main function.
printf(“Sum: %d”,
printf() function is used to print the sum on the screen.
sum(y));
We have used int as the return type so we have to return 0 which states that the
return 0;
given program is free from the error and it can be exited successfully.
int sum(int y)
{ This is the subprogram section. It includes the user-defined functions that are
return y + X; called in the main() function.
}

Steps involved in the Compilation and execution of a C program:


• Program Creation
• Compilation of the program
• Execution of the program
• The output of the program

Conclusion
In this article points we learned about the structure of the C Program are mentioned below:

• The basic structure of a C program is divided into 6 parts which makes it easy to read,
modify, document, and understand in a particular format.
• Debugging is easier in a well-structured C program.
• There are 6 sections in a C Program that are Documentation, Preprocessor Section,
Definition, Global Declaration, Main() Function, and Sub Programs.
• There are certain steps while compilation and executing of C program as mentioned below:
• Creation of Program
• Compilation of the program
• Execution of the program
• Output of the program
C Programming - Basic Concepts (with Examples)
1. Character Set
A character set is a collection of characters that can be used in a C program.
• Alphabets: A-Z, a-z
• Digits: 0-9
• Special Symbols: + - * / % = < > ( ) { } [ ] , ; . : # &
• Whitespace Characters: Space, Tab (\t), Newline (\n)
• Escape Sequences: \n (newline), \t (tab), \\ (backslash), \" (double quote)

Example
#include <stdio.h>

int main() {
printf("Hello\tWorld!\n"); // \t for tab, \n for new line
return 0;
}

Output:
Hello World!

2. C Tokens
A token is the smallest meaningful unit in a C program. There are 6 types:
1. Keywords – Reserved words like int, float, if, return.
2. Identifiers – Names given to variables, functions, etc.
3. Constants – Fixed values like 10, 3.14, 'A'.
4. Strings – Sequence of characters inside "", like "Hello".
5. Operators – Symbols that perform operations (+, -, *, /).
6. Special Symbols – {}, [], (), #, ;, ,.

Example
#include <stdio.h>

int main() {
int num = 10; // Identifier: num, Constant: 10
float pi = 3.14; // Identifier: pi, Constant: 3.14
char ch = 'A'; // Identifier: ch, Constant: 'A'

printf("Number: %d\n", num);


printf("Pi: %.2f\n", pi);
printf("Character: %c\n", ch);

return 0;
}

Output:
Number: 10
Pi: 3.14
Character: A
3. Keywords and Identifiers
Keywords
• Predefined, reserved words in C.
• Cannot be used as variable names.
• Examples:
int, float, return, if, else, while, switch, break, void.

Identifiers
• User-defined names for variables, functions, arrays, etc.
• Rules:
• Must start with a letter (A-Z, a-z) or underscore (_).
• Can contain letters, digits, and underscores.
• Case-sensitive (sum and Sum are different).
• Cannot be a keyword.

Example
#include <stdio.h>

int main() {
int age = 20; // 'age' is an identifier
float height = 5.9; // 'height' is an identifier

printf("Age: %d\n", age);


printf("Height: %.1f\n", height);

return 0;
}

Output:
Age: 20
Height: 5.9

4. Constants
A constant is a value that does not change during program execution.
Types of Constants
1. Integer Constants: Whole numbers (10, -25, 1000).
2. Floating-point Constants: Decimal numbers (3.14, -0.05).
3. Character Constants: Single characters ('A', '5', '\n').
4. String Constants: Sequence of characters ("Hello", "C Programming").
5. Symbolic Constants: Defined using #define.
Example
#include <stdio.h>

#define PI 3.1416 // Defining a constant

int main() {
const int MAX = 100; // Another way to define constants

printf("Value of PI: %.4f\n", PI);


printf("Maximum Value: %d\n", MAX);
return 0;
}
Output:
Value of PI: 3.1416
Maximum Value: 100

5. Variables and Their Declaration


Variables
• Named memory locations that store values.
• Must be declared before use.

Variable Declaration
Specifies a variable’s type and name:
int age; // Integer variable
float price; // Floating-point variable
char grade; // Character variable

Variable Initialization
Assigning a value at the time of declaration:
int age = 20; // Integer initialized
float price = 99.99; // Float initialized
char grade = 'A'; // Character initialized

Rules for Naming Variables


• Must start with a letter or underscore (_).
• Can contain letters, digits, and underscores (_).
• Cannot be a keyword.
• Case-sensitive (age and Age are different).

Example
#include <stdio.h>

int main() {
int num = 50; // Variable declaration & initialization
float temp = 36.5;
char letter = 'B';

printf("Number: %d\n", num);


printf("Temperature: %.1f\n", temp);
printf("Letter: %c\n", letter);

return 0;
}

Output:
Number: 50
Temperature: 36.5
Letter: B
Summary Table
Concept Description Example
Character
Set of valid characters in C A-Z, 0-9, +, -, \n, \t
Set
Smallest units (Keywords, Identifiers, Constants,
C Tokens int, float, +, 10, "Hello"
Operators, etc.)
Keywords Reserved words in C int, return, if, while
Identifiers User-defined names for variables & functions age, sum, calculateTotal()
#define PI 3.14, const int MAX
Constants Fixed values that do not change
= 100;
int num = 10;, float price =
Variables Named memory locations that store values
99.9;

C Programming - Data Types, Operators, and I/O


Functions
Data Types and Data Type Conversion
1. Data Types in C
Data types define the type of data a variable can store.

Data Type Size Example


int (Integer) 2 or 4 bytes int age = 21;
float (Floating-point) 4 bytes float pi = 3.14;
double (Double precision) 8 bytes double num = 12.345678;
char (Character) 1 byte char grade = 'A';

2. Data Type Conversion


Data type conversion is changing one data type to another.

A. Implicit Conversion (Type Promotion)


• Done automatically by the compiler.
• Smaller data types are converted to larger types.
Example:
#include <stdio.h>

int main() {
int num = 5;
float result = num + 3.5; // int is converted to float

printf("Result: %.1f\n", result);


return 0;
}

Output:
Result: 8.5
B. Explicit Conversion (Type Casting)
• Done manually by the programmer using (type).
Example:
#include <stdio.h>

int main() {
float num = 5.75;
int result = (int) num; // Converting float to int

printf("Result: %d\n", result);


return 0;
}

Output:
Result: 5

Operators in C
Operators are symbols used to perform operations on variables.

1. Arithmetic Operators
Used for mathematical operations.

Operator Description Example (a = 10, b = 5) Output


+ Addition a + b 15
- Subtraction a - b 5
* Multiplication a * b 50
/ Division a / b 2
% Modulus (remainder) a % b 0
Example:
#include <stdio.h>

int main() {
int a = 10, b = 5;
printf("Sum: %d\n", a + b);
printf("Product: %d\n", a * b);
return 0;
}

2. Relational Operators
Used to compare two values.

Operator Description Example (a = 10, b = 5) Output


== Equal to a == b 0 (false)
!= Not equal to a != b 1 (true)
> Greater than a > b 1 (true)
< Less than a < b 0 (false)
>= Greater than or equal to a >= b 1 (true)
<= Less than or equal to a <= b 0 (false)
3. Logical Operators
Used for logical operations.

Operator Description Example (a=1, b=0) Output


&& AND a && b 0 (false)
` ` OR
! NOT !a 0 (false)
Example:
#include <stdio.h>

int main() {
int x = 5, y = 10;
printf("x > 2 && y < 20: %d\n", (x > 2) && (y < 20)); // 1 (true)
return 0;
}

4. Assignment Operators
Used to assign values to variables.

Operator Example Equivalent To


= a = b a = b
+= a += b a = a + b
-= a -= b a = a - b
*= a *= b a = a * b
/= a /= b a = a / b
Example:
int a = 10;
a += 5; // Equivalent to a = a + 5;

5. Increment & Decrement Operators


Used to increase/decrease a value by 1.

Operator Example Equivalent


++ a++ a = a + 1
-- a-- a = a - 1
Example:
int x = 5;
printf("%d", x++); // Prints 5, then x becomes 6

6. Conditional (Ternary) Operator


Syntax:
condition ? expression1 : expression2;

If condition is true, expression1 is executed; otherwise, expression2.

Example:
int num = 10;
printf("%s", (num > 0) ? "Positive" : "Negative");
7. Bitwise Operators
Used for bitwise calculations.

Operator Name Example (a=5, b=3) Output


& AND a & b 1
` ` OR `a
^ XOR a ^ b 6
~ NOT ~a -6
<< Left Shift a << 1 10
>> Right Shift a >> 1 2

Input/Output Functions
Functions used for user input and output.

1. printf() - Output Function


Used to display text or values.
printf("Value of x: %d", x);

2. scanf() - Input Function


Used to take input from the user.
scanf("%d", &num); // Reads an integer

3. getchar() - Read a Character


Reads one character from input.
char ch;
ch = getchar(); // Takes a single character input

4. putchar() - Print a Character


Prints one character to output.
putchar('A'); // Prints: A

5. getch() - Get Character (without Enter key)


Takes one character input without pressing Enter (works in Windows).
char ch;
ch = getch();

6. putch() - Display a Character


Displays a single character.
putch('B'); // Prints: B

You might also like