Programming With C
Programming With C
Introduction to computer
software
INTRODUCTION
TO C
PROGRAMMIN Program Design
G
Tools
UNIT - I Structure of a C
program
Keywords,
Identifiers
AGENDA
Basic Data Types in C
INTRODUCTION
TO C
PROGRAMMIN Variables, Constants
G
Operators in C
Can do X, /, +, -
Can do X, /, +, -
Babbage
in 1848
This was the worlds
first truly programmable device,
and therefore the
world’s first true computer
Father of Modern Digital
Computer
Alan
Turing
•English Mathematician
•Logician
•Cryptanalyst and
•Computer Scientist
•Father of Computer Science
and Artificial Intelligence
Turing
machine
• ADA
Types of
Computer
Components of
Computer System
Organization of
Computer
Computer organization consist of following parts
▶ CPU – central processing unit
▶ Memory
▶ Input devices
▶ Output devices
Central
processing
• Alternatively referred to as theunit
brain of the computer,
processor, central processor,or microprocessor, the
CPU
• first developed at Intel in the early 1970’s
• The computer CPU is responsible for handling all
instructions it receives from hardware and software
running on the computer
• CPU performs all types of data processing
operations.
• It stores data, intermediate results and instructions
• It controls the operation of all parts of computer
CPU itself has following three components
1. ALU (Arithmetic Logic Unit)
All arithmetic calculations and logical operation are
performed using the Arithmetic/Logical Unit or ALU
2. Memory Unit
• A memory is just like a human brain.
• It is used to store data and instruction Computer
memory is use to Stores information being processed by
the CPU
3. Control Unit
unit help to perform operations of input unit, output
unit, Memory unit and ALU in a sequence.
Input Devices
• A device that can be used to insert data into a
computer system is called as input device.
• Examples : Keyboards, mouse, scanners and
digital cameras
Output Devices
• A device which is used to display result from a
computer is called as output device
• Examples: Printer, Scanner, Monitor, etc.
Examples of software and
Hardware
Hardwa Softwa
Hardwarere
is further divided into Software isre
further divided into
four main categories: two main categories:
•Input Devices •Application Software
•Output Devices •System Software
•Secondary Storage Devices
•CPU
Developed using electronic and Developed by using a
other materials programming language
When damaged, it can be When damaged it can be installed
replaced with a new component once more using a backup copy
Hardware is physical in nature The software cannot be physically
and hence one can touch and see touched but still can be used and
hardware seen
Hardware cannot be infected by The software can be infected by
Viruses Viruses
Types of Computer
Language
•Computer language is defined as code or syntax which is
used to write programs or any specific applications
•The computer language is used to communicate with
computers
•Three categories assembly language, machine language,
and high-level language
1. Machine Language
•The Machine language is considered a low-level language
•Other name -machine code or object code
•Which is set of binary digits 0 and 1
•These binary digits are understood and read by a computer system
•Example of machine language for the text “Hello World”.
01001000 0110101 01101100 01101100 01101111 00100000
01010111 01101111 01110010 01101100 01100100
2. Assembly Language
•Intermediate-level language for microprocessors
•It is second-generation language
3. High-Level Language
•The high-level language is easy to understand and
•human-readable program
•Examples: C++, C, JAVA, FORTRAN, etc..
Computer –
Software
Software is a set of programs,
which is designed to perform a
well-defined function.
A program is a sequence of
instructions written to solve a
particular problem.
There are two types of software
• System Software
• Application Software
System
•
Software
The system software is a collection of programs
• designed to operate, control, and supports the
process of computer
• System software –Inbuilt in System
• System software written in low-level languages
• Use to Interact with the hardware and software
• It Serves as the interface between the hardware and
end users
• Examples: Operating System, Compilers, Interpreter,
Assemblers, etc..
System
Software
Some examples of system software are Operating
• Fast in speed
• Difficult to design
• Difficult to understand
• Less interactive
• Smaller in size
• Difficult to manipulate
following −
• Payroll Software
• Microsoft Word
• Microsoft Excel
• Microsoft PowerPoint
Key System Application
Software Software
Definition System Software is the Application Software is the
type of software which is type of software which runs
the interface between as per user request. It runs
application software and on the platform which is
system provide by system software
Developm low level language high level language
e
nt
Language
Usage System software is used Application software is used
for operating computer by user to perform specific
hardware task
Installation Installed on the Application software are
computer
when operating system is installed according to user’s
Program
• Design Tools
Program Design tools are the tools used to
develop a program. A program is the expression of
an algorithm in a programming language.
01 Algorithms
02 Flowcharts
03 Pseudo
codes
Algorith
m
An algorithm in general is a sequence of steps to
solve a particular problem. Algorithms are
universal.
• It cannot be compiled or
run like a regular
program.
• an algorithm.
Acts as a bridge between the
program and the algorithm or
flowchart.
1. Preprocessor
2. Compiler
3. Assembler
4. Linker
Compilation process in c
Compilation process in c
Preprocessor
• The source code is the code which is
written in a text editor and the source code
file is given an extension ".c".
• This source code is first passed to the
preprocessor, and then the preprocessor
expands this code.
• After expanding the code, the
expanded code is passed to the compiler.
Compilation process in c
Compiler
• The code which is expanded by
the preprocessor is passed to the
compiler. The compiler converts this code
into assembly code.
hello.c
#include <stdio.h>
int main()
printf("Hello javaTpoint");
return 0;
}
Compilation process in c
Structure of c program
A C program involves the following sections:
• Local Declarations
#include<stdio.h>
Void main()
printf("Hello, World!\
n");
}
/* Comments */ - Comments are a way of explaining what
makes a program. The compiler ignores comments and
used by others to understand the code.
main function.
statements.
•Alt + F9 – Compile
• For example:
• 1. int money;
• 2. double accountBalance;
type variable_list;
• int a;
• float b;
• char c;
Variables in C
• Here, a, b, c are variables. The int, float, char are
the data types.
• float f=20.8;
• char c='A';
Variables in C
Rules for defining variables
• No whitespace is allowed
within the variable name.
program, for example: 10, 20, 'a', 3.4, "c programming" etc.
Syntax:
#define LENGTH 10
#define WIDTH 5
Using const keyword.
Syntax:
const type var
const int LENGTH = 10; const int WIDTH = 5;
Data Types in C
A data type specifies the type of data that
a variable can store such as integer,
floating, character
Data Types in C
EXAMPLE:
#include <stdio.h>
int main()
{
// Displays the string inside quotations printf("C
Programming");
return 0;
}
Output
statement in C
printf() function to display output to the user
• Output:Number = 5
• We use %d format specifier to print int types.
• Here, the %d inside the quotations will be
replaced by the value of testInteger
Example 3: float and double
Output
#include <stdio.h> void main()
{
float number1 = 13.5; double
number2 = 12.4;
printf("number1 = %f\n", number1);
printf("number2 = %lf", number2);
}
Print
Characters
#include <stdio.h> void main()
{
char chr = 'a'; printf("character = %c",
chr);
}
Output
character = a
To print char, we use %c format specifier
INPUT OUTPUT (I/O) STATEMENTS
scanf() function - to take input from the user,
Syntax: scanf(“%X”, &variableOfXType);
where %X is the format specifier in c
& is the address operator in C.
EXAMPLE:
#include <stdio.h>
void main()
{
int a;
// Displays the string inside quotations printf("C
Programming");
//getting input from the user
scanf(“%d”,&a);
printf(“Given data is %d”,a);
}
C Input
• In C programming, scanf() is one of the commonly used function to take
input from the user.
• The scanf() function reads formatted input from the standard input
such as keyboard
char chr;
printf("Enter a character: ");
scanf("%c", &chr);
// When %c is used, a character is displayed
printf("You entered %c.\n",chr);
/*When %d is used, ASCII value is displayed */
printf("ASCII value is % d.", chr);
return 0;
}
Output
Enter a character: g You entered g.
ASCII value is 103.
I/O Multiple
Values
Here's how you can take multiple inputs from the user and displaythem.
#include <stdio.h>
int main()
{
int a; float b;
printf("Enter integer and then a float: ");
// Taking multiple inputs Page13 scanf("%d%f", &a, &b);
printf("You entered %d and %f", a, b);
return 0;
}
Output
Enter integer and then a float: -3
3.4
You entered -3 and 3.400000
Format Specifiers
for I/Oint
Data Type Format Specifier
char %d
float double %c
short int %f
unsigned int %lf
long int long %hd
long int %u
unsigned long int %li
%lli
%lu
unsigned long long int %llu
signed char %c
unsigned char %c
C
Operator
An operator is simply a symbol that is used to
perform operations.
There scan be many types of
operations like arithmetic, logical, bitwise,
etc.
There are following types of operators
perform different types of to
language. operations
Arithmetic Operators in C
Relational Operators
Shift Operators
Logical Operators
Bitwise Operators
Ternary or Conditional Operators
Assignment Operator
Misc Operator
Arithmetic
Operators
• The following table shows all the arithmetic operators
supported by the C language.
• Assume variable A holds 10 and variable B holds 20 then
Arithmetic
Operators
EXAMPLE:
Relational
•Operators
The following table shows all the relational operators
supported by C. Assume variable A holds 10 and
variable B holds 20 then
Relational
Operators
A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
Relational
Operators
Logical
Operators
An expression containing logical operator returns either 0 or
1 depending upon whether expression results true or false.
Logical operators are commonly used in decision making in
C programming.
• (a == b) && (c > 5) evaluates to 1 because both
operands (a == b) and (c > b) is 1 (true).
40.500000.
2) Explicit Type
Conversion
• Explicit type conversion rules out
the use of compiler for converting
one data type to another instead
the user explicitly defines within the
program the datatype of the
operands in the expression.
2) Explicit Type
Conversion
Output:
1. Thus, in the above example we find that the output result is
12 because in the result expression the user has explicitly
defined the operands (variables) as integer data type.
2. Hence, there is no implicit conversion of data type by the
compiler.If in case implicit conversion was used the result
would be 13.
C - Type Casting
• (type_name) expression
Example: