CP Unit 2P1
CP Unit 2P1
TO
PROGRAMMING
[BTECH-JNTUGV-R23]
BY
CH. RAMACHANDRA REDDY
CEC@CSE-DEPARTMENT
BLOCK DIAGRAM OF COMPUTER AND ITS FUNCTIONAL UNITS EXPLAINATION
A computer system can be broadly divided into the following functional units:
1. Input Unit
2. Central Processing Unit (CPU)
o Arithmetic and Logic Unit (ALU)
o Control Unit (CU)
o Memory Unit (MU)
3. Output Unit
Types:
Primary Memory (RAM and ROM): Fast and directly
accessible by the CPU. RAM is volatile, meaning it loses its
data when the power is off, while ROM retains data
permanently.
Introduction,
Structure of a C Program
Comments
Keywords
Identifiers
Data Types
Variables
Constants
Input/output Statement.
Operators
Type Conversion
Control Flow, Relational Expressions:
Conditional Branching Statements: if, if-else, if-else—if, switch. Basic
Loop Structures: while, do-while loops, for loop, nested loops.
Break and Continue Statements, goto statements.
C as a mother language
C language is considered as the mother language of all the modern programming
languages because most of the compilers, JVMs, Kernels, etc. are written in C
language, and most of the programming languages follow C syntax, for example,
C++, Java, C#, etc.
It provides the core concepts like the array, strings, functions, file handling, etc.
that are being used in many languages like C++, Java, C#, etc.
C as a procedural language
A procedure is known as a function, method, routine, subroutine, etc. A
procedural language specifies a series of steps for the program to solve the
problem.
A procedural language breaks the program into functions, data structures, etc.
C is a procedural language. In C, variables and function prototypes must be
declared before being used.
4. Structured Language
Organized Code: C supports structured programming, which helps you
organize your code into small, manageable sections. This makes your
programs easier to understand, debug, and modify. For example, you can
break a complex task into simpler parts by creating functions, each
performing a specific job.
5. Low-Level Access
Direct Hardware Control: C allows you to interact directly with the
computer’s memory and hardware. This level of control is beneficial for
tasks that need to manage resources precisely, such as developing operating
systems or embedded systems.
Use of Pointers: Pointers are a powerful feature in C that allows you to
manipulate memory directly, giving you fine-grained control over how
your program uses system resources.
Advantages of C Programming:
1. Portability: C programs can be executed on various machines with
minimal changes. This makes C highly portable and a preferred choice for
developing system software.
2. Efficient and Fast: C is a low-level language, which allows developers to
write programs that are highly efficient and close to hardware, leading to
fast execution speeds.
3. Rich Library Support: C provides a wide range of built-in functions and
libraries that simplify the development process.
4. Structured Language: C follows a structured approach, allowing for
better organization of code through functions, making it easier to
understand, debug, and maintain.
5. Memory Management: C allows dynamic memory allocation, providing
more control over system memory, unlike many higher-level languages.
6. Low-Level Access: With C, you can directly manipulate bits, bytes, and
addresses, giving more control over hardware components, which is
essential in system programming.
7. Extensibility: C code can be easily extended and integrated with other
programming languages like C++ or Java.
7. Return Statement
The main() function typically ends with a return 0; statement, which
signifies successful completion of the program.
In other functions, return is used to return values.
Comments in C
Definition: Comments are non-executable statements i.e., non printable in a
program that are used to explain code or describe about the program and make it
more readable & understandable for programmers. The compiler ignores these
lines during execution.
Types of Comments:
Single-line comments: Begin with // and continue until the end of the line.
o Example: // This is a single-line comment
Multi-line comments: Enclosed between /* and */. Can span multiple
lines.
Example: /* This is
a multi-line
comment */
Importance:
Makes code easier to understand.
Helps in debugging by explaining logic.
Can be used to disable parts of code temporarily.
Keywords in C
Definition:
Keywords in C are reserved words that have specific meanings in the C language.
They are predefined in the C language syntax, which means that they cannot be
used for any other purpose, such as naming variables or functions. Keywords
serve as building blocks of C's syntax and control the flow of a program, data
types, operations, and more.
Characteristics of Keywords:
They are written in lowercase (e.g., int, float, if, else).
Keywords are the foundation of C syntax, as they define operations, control
structures, data types, and more.
Each keyword has a specific, well-defined meaning that the compiler
understands.
Reserved: The C language reserves keywords are those keywords that
cannot be used as identifiers in programs. Using a keyword as a variable
name or other identifier will cause a compilation error.
Predefined Meaning: Each keyword has a specific meaning that is
assigned by the C language. These meanings are built into the C language's
grammar and syntax and the compiler interprets them accordingly.
Specific Use: Keywords are designed for specific purposes and contexts
within the C language. They define control structures, data types, flow
control, and other language constructs. Attempting to use a keyword
outside of its intended purpose will result in a compilation error.
Examples:
Control flow: if, else, switch, for, while, do
Data types: int, char, float, double
Miscellaneous: return, void, sizeof
NOTE:
There are 32 keywords in the c language is given below:
auto break case char const continue default do
double else enum extern float for goto if
int long register return short signed sizeof static
struct switch typedef union unsigned void volatile while
Identifiers in C
Definition:
Identifiers are names given to various elements in a C program, such as variables,
functions, arrays, and structures. An identifier allows the programmer to refer to
these elements in the code. Unlike keywords, identifiers can be chosen by the
programmer, following certain rules, to name data and functions.
Key Points:
Identifiers are case-sensitive, meaning count, Count, and COUNT are
treated as three different identifiers.
They must begin with a letter (A-Z, a-z) or an underscore (_), followed by
letters, digits (0-9), or underscores.
Identifiers should be descriptive to make the code more readable and
maintainable (e.g., totalMarks rather than tm).
1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable
Global Variable
A variable that is declared outside the function or block is called a global variable.
Any function can change the value of the global variable. It is available to all the
functions.
Static Variable
A variable that is declared with the static keyword is called static variable.
Automatic Variable
All variables in C that are declared inside the block, are automatic variables by
default. We can explicitly declare an automatic variable using auto keyword.
External Variable
Input/Output Statements in C
Definition:
Input/Output (I/O) statements allow a C program to interact with the user by
receiving input and displaying output. The standard I/O functions are part of the
stdio.h library/header file and are essential for communicating data between the
user and the program.
Common Input/Output Functions:
1. printf():
o Used to output data to the console.
o Takes a format string and optional variables.
o Example: printf("The value is %d", number);
2. scanf():
o Used to take input from the user.
o Requires the address of the variable to store the input using the &
operator.
o Example: scanf("%d", &number);
*few functions: puts(); , get(); , putchar(); , getchar(); also comes under i/o
statements.
Operators in C
Definition:
Operators in C are special symbols that perform operations on operands (variables
and values). They are essential for performing calculations, comparisons, and
logical operations. Operators are divided into different categories based on the
type of operation of particular operator they perform.
Classifications of Operators
Unary operator: Only one operand is required to perform calculation.
Binary Operator: Two operands are required to perform calculation.
Ternary Operator: Three operands are required to perform calculation.
3. Logical Operators:
Used to combine multiple conditions. Includes AND (&&), OR (||), and
NOT (!).
4. Assignment Operators:
Used to assign values to variables. The basic assignment operator is =, but
there are also compound assignment operators like +=, -=, *=.
6. Bitwise Operators:
Perform operations at the bit level, such as AND (&), OR (|), XOR (^),
NOT (~), shift left (<<), and shift right (>>).
Advantages:
1. Concise: The conditional operator can replace an if-else block in a single
line.
2. Readable: Improves readability when used for simple conditions.
3. Inline Use: Can be used directly in expressions and assignments.
Example:
int num = 5;
char *result = (num % 2 == 0) ? "Even" : "Odd";
// Output: result will point to "Odd" since 5 is not divisible by 2.