Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
36 views78 pages

C Programming Basics for Beginners

Uploaded by

sahilcarry42121
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)
36 views78 pages

C Programming Basics for Beginners

Uploaded by

sahilcarry42121
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/ 78

PROGRAMMING WITH C

MODULE 1
CHAPTER 1 - INTRODUCTION
ALGORITHMS
● A computer is a powerful tool for solving a wide range of problems
● Writing computer programs is essential to instruct a computer to perform tasks
● An algorithm is a finite sequence of steps designed for solving a problem in
arithmetic, computer science and related fields
● It is defined as “ a procedure that performs a sequence of operations to solve a
given problem”
● It takes one or more inputs and produces the desired output
Use of Algorithms
1. Computer Science
2. Mathematics
3. Operations Research
4. Artificial Intelligence
5. Data Science
What is the need of Algorithms?
1. necessary for solving complex problems
2. help to automate processes and make them more reliable, faster
and easier to perform
3. enable computers to perform tasks that would be difficult for
humans to do manually
4. used in various fields
How to design an Algorithm?
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.
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
Qualities of a Good Algorithm
1. Exact definitions of input and output are necessary
2. All steps should be distinct and straightforward
3. All possible solutions to a problem, algorithms should be the
most efficient
HISTORY OF C
ALGOL
- The root of all modern languages is ALGOL introduced in the early 1960s
- ALGOL was the first computer language to use a block structure
BCPL
- In 1967, Martin Richards developed a language called (Basic Combined
Programming Language)
- In 1970, Ken Thompson created a language using many features of BCPL
and called it simply B
- In 1972 C was evolved from ALGOL, BCPL and B by Dennis Ritchie at the Bell
Laboratories
- C uses many concepts from these languages and added the concept of data types and
other powerful features
- It is strongly associated with UNIX
- UNIX is one of the most powerful network operating system in use today

ANSI C

- In 1983 American National Standards Institute (ANSI) appointed a technical committee


to define a standard for C
- In December 1989 the committee approved a version of C which is known as ANSI C
- It was then approved by the International Standards Organization (ISO) in 1990
- Today C is in widespread use with a rich library of functions
FLOWCHARTS
SYMBOLS OF FLOWCHARTS
FLOWCHART TO FIND THE SIMPLE INTEREST
FLOWCHART TO FIND LARGER AMONG 2 NUMBERS
STRUCTURE OF C
- A set of guidelines that programmers must adhere to while creating C
programs determine the structure
DESIRABLE PROGRAM CHARACTERISTICS
1. Integrity
2. Clarity
3. Simplicity
4. Efficiency
5. Modularity
6. Generality
FEATURES OF C
- provides low level features
- easier to write assembly language code
- programs are portable
- provides wide variety of Data types and built - in functions
- provides useful loop and control statements
- more user friendly
- supports use of pointer
- consists of different modules that are integrated to form
complete program
CHAPTER 2 - C PROGRAMMING
FUNDAMENTALS
PROGRAM STRUCTURE
- C programs are built with the help of functions
- It is essential that one function must have name main
- C program has following programming elements:
Preprocessor Commands - Directives like #include<stdio.h> tell
compiler to include necessary files before compilation.
Functions - Building blocks of C programs , including main()
function, which returns an integer value.
Statements & Expressions - Statements are made up of
expressions, assignments, function calls and control flow structures
enclosed in {}.
Variables - Used to store data such as numbers, strings and complex
information for computations.
Comments - Used to provide additional information. They can be
multi - line (/* */) or single - line(//).
C CHARACTER SET
- A character denotes any alphabet, digit or special symbol used
to represent information.
White Spaces
- Tab or New Line or Space
Escape Sequences
- Enables the inclusion of special characters and control patterns
in strings and character constants that are difficult to represent
or write directly.
Escape Sequence Name Description
\a Alarm or beep It is used to generate a bell sound

\b Backspace It is used to move the cursor one place backward

\f Form feed Moves initial position of next page

\0 Null Represents the null character

\t Horizontal tab Moves next horizontal tab

\v Vertical tab It is used to insert vertical space

\? Question Mark It is used to display Question Mark

\n New Line It moves the cursor to next line

\’ Single quote It is used to display a single quotation mark

\’’ Double quote It is used to display a double quotation mark


CONSTANTS, VARIABLES AND KEYWORDS
- The alphabets, numbers and special symbols when
properly combined form constants, variables and
keywords.
- A constant is an entity that doesn’t change whereas a
variable is an entity that may change.
- The results of calculations are stored in computers
memory.
- Like human memory the computer memory also consists
of millions of cells.
- Here 3 is stored in a memory location and a name x is given to
it.
- Then we are assigning a new value 5 to the same memory
location.
- Since the location whose name is x can hold different values at
different times x is known as a variable. As against this, 3 or 5
do not change, hence are known as constants.
Integer Constants
- An integer constant must have at least one digit.
- It must not have a decimal point.
- It can be either positive or negative.
- If no sign precedes an integer constant it is assumed to be
positive.
- No commas or blanks are allowed within an integer constant.
- The allowable range for integer constants is -32768 to 32767.
- Eg: 426, +782 , -8000, -7605
Floating Point Constants
- The real constants could be written in two forms—Fractional
form and Exponential form.
- A real constant must have at least one digit.
- It must have a decimal point.
- It could be either positive or negative.
- Default sign is positive.
- No commas or blanks are allowed within a real constant.
- Eg: +325.34, 426.0 , -32.76 , -48.5792 , 4.1e8
Character Constants
- A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single inverted
commas.
- Eg: 'A', 'I', 'z'
- In C programming, there are two ways to define a constant:
1. By using const keyword
- Eg: const float PI = 3.14;
1. By using #define preprocessor
- Constants can also be defined by using #define preprocessor
directive.
- Syntax: #define CONSTANT_NAME value
- Eg: #define PI 3.14159
IDENTIFIERS
- Identifiers are the names given to program elements such as
variables, functions, structures etc.
- It must be unique so that easily identified during execution of a
program.
- Eg: int total;
double remainingBalance;
Rules
1. First character must be an alphabet (a-z,A-Z) or an underscore. It
can be followed by any number of alphabets, digits or underscore.
2. It must not begin with a digit(0-9).
3. Uppercase and lowercase letters are distinct, making them
case-sensitive.
4. Commas, spaces or any special characters are not allowed.
5. Keywords cannot be used as identifiers.
6. It should not exceed 31 characters in length.
7. It must be meaningful, short, easy and easily readable.
KEYWORDS
- Keywords are predefined, reserved words used in
programming that have special meaning.
- They are part of syntax and cannot be used as identifiers.
- Eg: int result;
- All keywords must be written in lowercase
Data Qualifiers Storage Classes Loop Others User-defined
Types

int signed auto for const typede

char unsigned extern while volatile enum

float short register do sizeof

double long static


Decision Jump Derived function

if goto struct void

else continue union return

switch break

case

default
VARIABLES
- An entity that may vary during program execution is called a
variable.
- Variable names are names given to locations in memory.
- These locations can contain integer, real or character constants.
- A particular type of variable can hold only the same type of
constant.
- For example, an integer variable can hold only an integer constant,
a real variable can hold only a real constant and a character
variable can hold only a character constant.
Rules
- A variable name is a combination of alphabets, digits and
underscore
- The first character in the variable name must be an alphabet
or underscore.
- No commas or blanks are allowed within a variable name.
- No special symbol other than an underscore (as in gross_sal)
can be used in a variable name.
EXPRESSIONS
- An expression is any legal combination of symbols
that represents a value.
- Every expression consists of at least one operand and
can have one or more operators.
- Operands are values, whereas operators are symbols
that represent particular actions.
Valid C Programming Expressions
- C programming code gets compiled first before execution.
- In the different phases of compiler, expression is checked for
validity.

Expressions Validity
a+b Expression is valid since it contain + operator
++a+b Invalid Expression
Priority and Expressions
- In C expression is solved according to
Operators used and their priorities.
Types of Expression
- Expressions can be classified on the basis of position of
operators in an expression:
Type Explanation Example

Infix Operator is in between a+b


operands
Prefix Operator is written before +ab
operands
Postfix Operator is written after ab+
operands
Examples

Expression Explanation
n1+n2 This will add two numbers and we can assign result of addition to
another variable

x=y This will assign the value of right hand side operand to left side
variable

v=u+a*t Multiplying two numbers and result is added to ‘u’ and total result is
assigned to v

x<=y This expression will return Boolean value because comparison


operator will give us output either true or false

++j This is having pre increment operator, it is used to increment the


value of j before using it in expression
STATEMENTS
- In C programming, statements are the building blocks
of a program.
- They represent individual instructions that the
computer executes.
- There are three different classes of statements:
1. Expression
2. Compound
3. Control
Expression Statement
- These are statements that consist of an
expression followed by a semicolon.
- They perform operations or assignments.
- Eg:
int a = 5; // Assigns the value 5 to variable 'a' a
= a + 10; // Updates 'a' to be 15
Compound Statement
- Also known as blocks, these statements group multiple
statements into one. They are enclosed in curly braces { }.
- Eg:
{
int x = 10;
x = x * 2;
printf("Value of x: %d\n", x);
}
Control Statement
- Control Statements are elements in source
code that control the flow of program
execution
- There are blocks using { and }, loops using
for, while and do-while and decision making
using if and switch.
VARIABLE SCOPE
- In C programming, scope refers to the region of the
code where a variable or function is accessible.
- It determines the visibility and lifetime of variables and
functions within different parts of a program.
- Types of scope:
1. Global Scope
2. Local Scope
Global Scope
- Variables and functions declared outside of all functions
are known as having file scope.
- These are accessible from the point of declaration to the
end of the file where they are defined.
- By default, these variables and functions are global and
can be accessed by any function within the same file.
Eg:
int globalVar = 100; // Global variable with file scope
void function()
{
printf("Global variable: %d\n", globalVar);
}
Local Scope
- Variables declared inside a block (enclosed by {}) are
known only within that block.
- This includes variables declared inside functions or
control structures.
- These variables are local to the block and are not
accessible outside of it.
void function()
{
int x = 10;
if (x > 5)
{
int y = 20;
printf("x: %d, y: %d\n", x, y);
}
// y is not accessible here }
CHARACTER AND CHARACTER STRINGS
- A single character value is referred to as a character.
- The char data type in C allows the programmer to
store a single character
- Majority of compilers only require one byte of RAM
to store a character
- Eg:
char letter = 'A';
String
- ‘0’ serves as the null character at the conclusion of a
string of characters or string
- A string is a collection of characters
- String size should be greater than the number of
characters in the string
- String should terminate with a null character
- Declaration and initialization of a string
char greeting[8] = {‘W’, ‘e’, ‘l’, ‘c’, ‘o’, ‘m’, ‘e’,‘\0’}
char greeting []= “Welcome”;
#include<stdio.h>
#include<conio.h>
int main()
{
char greeting [6]= “{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
printf(“Welcome message: %s\n”,greeting);
return 0;
}

You might also like