KEMBAR78
Fundamentals of computers - C Programming | PPTX
PROGRAMMING
C
UNIT-1
Fundamentals of
computers:
What is a Computer?
A computer is an electronic device capable of performing calculations, processing data, and following
instructions with incredible speed and accuracy. It's essentially a tool that manipulates information under
the control of programs.
Basic Functions of a Computer
1.Input
2.Processing
3.Output
4.Storage
Components of a
Computer
• Hardware
• Software
Types of Computers
• Personal Computers (PCs): Desktop and laptop computers for individual use.
• Mainframes: Large, powerful computers used by organizations for complex tasks.
• Supercomputers: Extremely fast computers used for scientific research and complex
calculations.
Computer Generations
Computers have evolved through several generations, each characterized by significant
technological advancements:
• First Generation: Vacuum tubes
• Second Generation: Transistors
• Third Generation: Integrated circuits
• Fourth Generation: Microprocessors
• Fifth Generation: Artificial intelligence
Classification of programming language
What is a programming language?
A programming language defines a set of instructions that are compiled together to perform a specific task
by the CPU (Central Processing Unit). The programming language mainly refers to high-level languages
such as C, C++, Pascal, Ada, COBOL, etc.
They can be classified into two categories:
• Low-level language
• High-level language
Problem Solving Techniques in Programming
Problem-solving is the core of programming. It involves breaking down a complex problem
into smaller, manageable sub-problems and then developing a logical solution. Here are some
fundamental techniques:
• Understanding the Problem
• Developing a Solution
• Implementing the Solution
• Common Problem-Solving Techniques
Over view of
C:
What is C?
• High-level, general-purpose programming language.
• Developed by Dennis Ritchie at Bell Labs in 1972.
• Known for efficiency, speed, and portability.
Characters are used in forming either words or numbers or even expressions in C
programming. Characters in C are classified into 4 groups
Character set in C:
• Letter(alphabets)
• Digit(numbers)
• Special character
• White Space(space)
Tokens in
C:
A token in C can be defined as the smallest individual element of the C programming
language that is meaningful to the compiler. It is the basic component of a C program.
Types of Tokens in
C: 1.Keywords
2.Identifiers
3.Constants
4.Strings
5.Special Symbols
6.Operators
Keyword &
Identifiers:
Keywords: Predefined reserved words with specific meanings in a programming language,
used to define its syntax structure.
Example: int, float, char, if, else, for, while, return.
Identifiers: User-defined names given to program elements like variables, functions, or data
types for identification.
Example: age, name, total, calculate_area, my_variable.
Constant in C:
Constants in C are fixed values that cannot be modified once assigned. They are used to
represent values that remain constant throughout the program.
Syntax:
const data_type constant_name = value;
Variables are essentially named storage locations in a computer's memory used to hold data
values that can be changed during program execution. Think of them as containers that can
store different values of a specific data type.
Variables in
C:
Syntax:
data_type variable_name;
Datatype in
C:
A data type in C specifies the kind of data a variable can hold and the operations that can be
performed on it. It determines how the data is stored in memory and interpreted by the
compiler.
Declaration of
variables:
Variable declaration is the process of informing the compiler about the name and data type of
a variable before using it in a program. This allows the compiler to allocate appropriate
memory space for the variable.
Syntax:
data_type variable_name;
Example for Declaration:
int age;
float height;
char initial;
Assigning values to
variables:
Assigning a value to a variable in C means storing a specific data item in the memory
location represented by that variable. This is done using the assignment operator (=).
Syntax:
variable_name = value;
Example:
int age = 25;
float height = 1.75;
char initial = 'A';
Defining Symbolic
Constants:
A symbolic constant is a name given to a fixed value that cannot be changed during program
execution. It improves code readability and maintainability. There are two primary methods
to define symbolic constants in C:
1. Using the “#define” Preprocessor Directive
The #define directive is used to replace a symbolic name with a constant value during the
preprocessing stage.
Syntax:
#define CONSTANT_NAME value
2. Using the const Keyword
The const keyword declares a read-only variable, which essentially acts as a constant.
Syntax:
const data_type CONSTANT_NAME = value;
Types of Operators in C
Operators are symbols that perform specific operations on one or more operands. C offers a
variety of operators to manipulate data effectively.
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Increment and Decrement Operators
• Conditional Operator
• Special
Arithmetic
Operator:
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations on numerical values.
List of Arithmetic Operators:
• + : Addition
• - : Subtraction
• * : Multiplication
• / : Division
• % : Modulus (remainder)
Example:
int a = 10, b = 3;
int sum = a + b;
int difference = a - b;
int quotient = a / b;
Relational
Operator:
Relational operators compare two operands and return a boolean value (true or false).
List of Relational Operators:
• < : Less than
• > : Greater than
• <= : Less than or equal to
• >= : Greater than or equal to
• == : Equal to
• != : Not equal to
Example:
int a = 10, b = 5;
bool isGreater = a > b;
bool isEqual = a == b;
Logical
Operator:
Logical Operators
Logical operators combine boolean expressions.
List of Logical Operators:
• && : Logical AND
• || : Logical OR
• ! : Logical NOT
Example:
bool isEven = (num % 2) == 0;
bool isPositive = num > 0;
bool isEvenPositive = isEven && isPositive;
bool isEvenOrPositive = isEven || isPositive;
bool isNotEven = !isEven;
Assignment Operators:
Assignment operators are used to assign values to variables.
List of Assignment Operators:
• = : Simple assignment (assigns the right-hand value to the left-hand variable)
• += : Add and assign (adds the right-hand value to the left-hand variable and assigns the
result back to the left-hand variable)
• -= : Subtract and assign
• *= : Multiply and assign
• / : Divide and assign
• %= : Modulus and assign
Example:
int x = 10;
x += 5;
Conditional Operator (Ternary Operator):
The conditional operator is a shorthand for an if-else statement.
Example:
int max;
if (a > b) {
max = a;
}
else {
max = b;
}
Bitwise operators operate on individual bits of their operands.
List of Bitwise Operators:
• & : Bitwise AND
• | : Bitwise OR
• ^ : Bitwise XOR
• ~ : Bitwise complement (unary)
• << : Left shift
• >> : Right shift
Example:
int x = 5, y = 3;
int result1 = x & y;
int result2 = x | y;
int result3 = x ^ y;
int result4 = ~x; //
int result5 = x << 2;
int result6 = x >> 1;
Bitwise
Operator:
Special
Operator:
• sizeof : Returns the size of a data type or variable in bytes.
• comma : Separates expressions in a statement.
Example:
int sizeOfInt = sizeof(int);
int a = (5, 6);
Increment and Decrement
Operator:
These operators are used to increase or decrease the value of a variable by
1.
Types:
• Prefix increment (++): Increments the value of the variable before
using it in the expression.
• Postfix increment (++): Increments the value of the variable after using
it in the expression.
• Prefix decrement (--): Decrements the value of the variable before using it in the
expression.
• Postfix decrement (--): Decrements the value of the variable after using it in the
expression.
Example:
int x = 5;
int y = ++x;
int z = x++;
int a = --x;
int b = x--;
Arithmetic
Expressions:
Arithmetic Expressions in C
An arithmetic expression is a combination of operands (variables, constants, or function
calls) and arithmetic operators that evaluate to a numerical value.
Example:
int x = 10, y = 5;
int result = (x + y) * 2 - 3;
Evaluation of
Expressions:
Expression evaluation is the process of determining the value of an expression based on the values
of its operands and the operators involved.Order of Evaluation
The order in which operators are applied to operands is crucial for correct evaluation. It follows the
standard order of operations (PEMDAS/BODMAS):
1.Parentheses: Expressions within parentheses are evaluated first.
2.Exponents: Exponentiation is performed next.
3.Multiplication and Division: Performed from left to right.
4.Addition and Subtraction: Performed from left to right.
Complex Expressions:
For more complex expressions, use parentheses to explicitly define the order of evaluation:
int result = (5 + 3) * (2 - 4);
Procedure of Arithmetic
operator:
Procedure of Arithmetic Operators in C
Arithmetic operators perform basic mathematical operations on numerical values. The
process involved in their execution can be broken down into the following steps:
1.Operand Evaluation
2.Operator Identification
3. Data Type Conversion (if necessary)
4.Operation Execution
5.Result Assignment (if applicable)
C provides a rich set of mathematical functions to perform complex calculations. These
functions are primarily defined in the math.h header file.
Common Mathematical Functions
Here's a list of some commonly used mathematical functions:
• Trigonometric functions
• Exponential and logarithmic functions
• Rounding and absolute value functions
• Other functions
Mathematical
Function:
Example:
#include <stdio.h>
#include <math.h>
int main() {
double x = 3.14159;
double result;
result = sin(x); // Calculate sine of x
printf("sin(x) = %fn", result);
result = sqrt(16); // Calculate square root of 16
printf("sqrt(16) = %fn", result);
return 0;
}
Formatted input and
output:
Formatted input and output functions in C allow you to read and write data in a specific
format. The most commonly used functions for this purpose are printf() and scanf().
printf() Function
The printf() function is used to display formatted output on the console. It takes a format
string as its first argument, followed by optional arguments representing the values to be
printed.
Syntax:
printf(format_string, argument1, argument2, ...);
scanf() Function
The scanf() function is used to read formatted input from the console. It takes a format string
as its first argument, followed by pointers to variables where the input values will be stored.
Syntax:
scanf(format_string, &variable1, &variable2, ...);
Format Specifiers
• %d : Integer (signed decimal integer)
• %i : Integer (same as %d)
• %u : Unsigned integer (unsigned decimal integer)
• %ld : Long integer (signed long integer)
• %lu : Unsigned long integer (unsigned long integer)
• %lld : Long long integer (signed long long integer)
• %llu : Unsigned long long integer (unsigned long long integer)
• %f : Float (floating-point number)
• %lf : Double (double-precision floating-point number)
• %Lf : Long double (long double-precision floating-point number)
• %c : Character (single character)
• %s : String (null-terminated character array)
• %p : Pointer (pointer address)
• %x : Hexadecimal integer (lowercase)
• %X : Hexadecimal integer (uppercase)
• %o : Octal integer
THANK YOU

Fundamentals of computers - C Programming

  • 1.
  • 2.
    Fundamentals of computers: What isa Computer? A computer is an electronic device capable of performing calculations, processing data, and following instructions with incredible speed and accuracy. It's essentially a tool that manipulates information under the control of programs. Basic Functions of a Computer 1.Input 2.Processing 3.Output 4.Storage
  • 3.
    Components of a Computer •Hardware • Software Types of Computers • Personal Computers (PCs): Desktop and laptop computers for individual use. • Mainframes: Large, powerful computers used by organizations for complex tasks. • Supercomputers: Extremely fast computers used for scientific research and complex calculations.
  • 4.
    Computer Generations Computers haveevolved through several generations, each characterized by significant technological advancements: • First Generation: Vacuum tubes • Second Generation: Transistors • Third Generation: Integrated circuits • Fourth Generation: Microprocessors • Fifth Generation: Artificial intelligence
  • 5.
    Classification of programminglanguage What is a programming language? A programming language defines a set of instructions that are compiled together to perform a specific task by the CPU (Central Processing Unit). The programming language mainly refers to high-level languages such as C, C++, Pascal, Ada, COBOL, etc. They can be classified into two categories: • Low-level language • High-level language
  • 6.
    Problem Solving Techniquesin Programming Problem-solving is the core of programming. It involves breaking down a complex problem into smaller, manageable sub-problems and then developing a logical solution. Here are some fundamental techniques: • Understanding the Problem • Developing a Solution • Implementing the Solution • Common Problem-Solving Techniques
  • 7.
    Over view of C: Whatis C? • High-level, general-purpose programming language. • Developed by Dennis Ritchie at Bell Labs in 1972. • Known for efficiency, speed, and portability. Characters are used in forming either words or numbers or even expressions in C programming. Characters in C are classified into 4 groups Character set in C: • Letter(alphabets) • Digit(numbers) • Special character • White Space(space)
  • 8.
    Tokens in C: A tokenin C can be defined as the smallest individual element of the C programming language that is meaningful to the compiler. It is the basic component of a C program. Types of Tokens in C: 1.Keywords 2.Identifiers 3.Constants 4.Strings 5.Special Symbols 6.Operators
  • 9.
    Keyword & Identifiers: Keywords: Predefinedreserved words with specific meanings in a programming language, used to define its syntax structure. Example: int, float, char, if, else, for, while, return. Identifiers: User-defined names given to program elements like variables, functions, or data types for identification. Example: age, name, total, calculate_area, my_variable.
  • 10.
    Constant in C: Constantsin C are fixed values that cannot be modified once assigned. They are used to represent values that remain constant throughout the program. Syntax: const data_type constant_name = value; Variables are essentially named storage locations in a computer's memory used to hold data values that can be changed during program execution. Think of them as containers that can store different values of a specific data type. Variables in C: Syntax: data_type variable_name;
  • 11.
    Datatype in C: A datatype in C specifies the kind of data a variable can hold and the operations that can be performed on it. It determines how the data is stored in memory and interpreted by the compiler.
  • 12.
    Declaration of variables: Variable declarationis the process of informing the compiler about the name and data type of a variable before using it in a program. This allows the compiler to allocate appropriate memory space for the variable. Syntax: data_type variable_name; Example for Declaration: int age; float height; char initial;
  • 13.
    Assigning values to variables: Assigninga value to a variable in C means storing a specific data item in the memory location represented by that variable. This is done using the assignment operator (=). Syntax: variable_name = value; Example: int age = 25; float height = 1.75; char initial = 'A';
  • 14.
    Defining Symbolic Constants: A symbolicconstant is a name given to a fixed value that cannot be changed during program execution. It improves code readability and maintainability. There are two primary methods to define symbolic constants in C: 1. Using the “#define” Preprocessor Directive The #define directive is used to replace a symbolic name with a constant value during the preprocessing stage. Syntax: #define CONSTANT_NAME value
  • 15.
    2. Using theconst Keyword The const keyword declares a read-only variable, which essentially acts as a constant. Syntax: const data_type CONSTANT_NAME = value; Types of Operators in C Operators are symbols that perform specific operations on one or more operands. C offers a variety of operators to manipulate data effectively. • Arithmetic Operators • Relational Operators • Logical Operators • Bitwise Operators • Assignment Operators • Increment and Decrement Operators • Conditional Operator • Special
  • 16.
    Arithmetic Operator: Arithmetic Operators Arithmetic operatorsare used to perform mathematical calculations on numerical values. List of Arithmetic Operators: • + : Addition • - : Subtraction • * : Multiplication • / : Division • % : Modulus (remainder) Example: int a = 10, b = 3; int sum = a + b; int difference = a - b; int quotient = a / b;
  • 17.
    Relational Operator: Relational operators comparetwo operands and return a boolean value (true or false). List of Relational Operators: • < : Less than • > : Greater than • <= : Less than or equal to • >= : Greater than or equal to • == : Equal to • != : Not equal to Example: int a = 10, b = 5; bool isGreater = a > b; bool isEqual = a == b;
  • 18.
    Logical Operator: Logical Operators Logical operatorscombine boolean expressions. List of Logical Operators: • && : Logical AND • || : Logical OR • ! : Logical NOT Example: bool isEven = (num % 2) == 0; bool isPositive = num > 0; bool isEvenPositive = isEven && isPositive; bool isEvenOrPositive = isEven || isPositive; bool isNotEven = !isEven;
  • 19.
    Assignment Operators: Assignment operatorsare used to assign values to variables. List of Assignment Operators: • = : Simple assignment (assigns the right-hand value to the left-hand variable) • += : Add and assign (adds the right-hand value to the left-hand variable and assigns the result back to the left-hand variable) • -= : Subtract and assign • *= : Multiply and assign • / : Divide and assign • %= : Modulus and assign Example: int x = 10; x += 5;
  • 20.
    Conditional Operator (TernaryOperator): The conditional operator is a shorthand for an if-else statement. Example: int max; if (a > b) { max = a; } else { max = b; }
  • 21.
    Bitwise operators operateon individual bits of their operands. List of Bitwise Operators: • & : Bitwise AND • | : Bitwise OR • ^ : Bitwise XOR • ~ : Bitwise complement (unary) • << : Left shift • >> : Right shift Example: int x = 5, y = 3; int result1 = x & y; int result2 = x | y; int result3 = x ^ y; int result4 = ~x; // int result5 = x << 2; int result6 = x >> 1; Bitwise Operator:
  • 22.
    Special Operator: • sizeof :Returns the size of a data type or variable in bytes. • comma : Separates expressions in a statement. Example: int sizeOfInt = sizeof(int); int a = (5, 6); Increment and Decrement Operator: These operators are used to increase or decrease the value of a variable by 1. Types: • Prefix increment (++): Increments the value of the variable before using it in the expression. • Postfix increment (++): Increments the value of the variable after using it in the expression.
  • 23.
    • Prefix decrement(--): Decrements the value of the variable before using it in the expression. • Postfix decrement (--): Decrements the value of the variable after using it in the expression. Example: int x = 5; int y = ++x; int z = x++; int a = --x; int b = x--;
  • 24.
    Arithmetic Expressions: Arithmetic Expressions inC An arithmetic expression is a combination of operands (variables, constants, or function calls) and arithmetic operators that evaluate to a numerical value. Example: int x = 10, y = 5; int result = (x + y) * 2 - 3;
  • 25.
    Evaluation of Expressions: Expression evaluationis the process of determining the value of an expression based on the values of its operands and the operators involved.Order of Evaluation The order in which operators are applied to operands is crucial for correct evaluation. It follows the standard order of operations (PEMDAS/BODMAS): 1.Parentheses: Expressions within parentheses are evaluated first. 2.Exponents: Exponentiation is performed next. 3.Multiplication and Division: Performed from left to right. 4.Addition and Subtraction: Performed from left to right. Complex Expressions: For more complex expressions, use parentheses to explicitly define the order of evaluation: int result = (5 + 3) * (2 - 4);
  • 26.
    Procedure of Arithmetic operator: Procedureof Arithmetic Operators in C Arithmetic operators perform basic mathematical operations on numerical values. The process involved in their execution can be broken down into the following steps: 1.Operand Evaluation 2.Operator Identification 3. Data Type Conversion (if necessary) 4.Operation Execution 5.Result Assignment (if applicable)
  • 27.
    C provides arich set of mathematical functions to perform complex calculations. These functions are primarily defined in the math.h header file. Common Mathematical Functions Here's a list of some commonly used mathematical functions: • Trigonometric functions • Exponential and logarithmic functions • Rounding and absolute value functions • Other functions Mathematical Function: Example: #include <stdio.h> #include <math.h> int main() { double x = 3.14159; double result; result = sin(x); // Calculate sine of x printf("sin(x) = %fn", result); result = sqrt(16); // Calculate square root of 16 printf("sqrt(16) = %fn", result); return 0; }
  • 28.
    Formatted input and output: Formattedinput and output functions in C allow you to read and write data in a specific format. The most commonly used functions for this purpose are printf() and scanf(). printf() Function The printf() function is used to display formatted output on the console. It takes a format string as its first argument, followed by optional arguments representing the values to be printed. Syntax: printf(format_string, argument1, argument2, ...);
  • 29.
    scanf() Function The scanf()function is used to read formatted input from the console. It takes a format string as its first argument, followed by pointers to variables where the input values will be stored. Syntax: scanf(format_string, &variable1, &variable2, ...); Format Specifiers • %d : Integer (signed decimal integer) • %i : Integer (same as %d) • %u : Unsigned integer (unsigned decimal integer) • %ld : Long integer (signed long integer) • %lu : Unsigned long integer (unsigned long integer) • %lld : Long long integer (signed long long integer)
  • 30.
    • %llu :Unsigned long long integer (unsigned long long integer) • %f : Float (floating-point number) • %lf : Double (double-precision floating-point number) • %Lf : Long double (long double-precision floating-point number) • %c : Character (single character) • %s : String (null-terminated character array) • %p : Pointer (pointer address) • %x : Hexadecimal integer (lowercase) • %X : Hexadecimal integer (uppercase) • %o : Octal integer
  • 31.