0% found this document useful (0 votes)
4 views39 pages

PROG101_Lecture01

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views39 pages

PROG101_Lecture01

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

PROG - 101

Structured Programming Language


Lecture-01
What is programming?
 Definition: Giving a computer a set of instructions to perform
a task
 Examples of programming languages: C, Python, Java, C++
 Real-life applications: Web apps, AI, IoT, games
 Key concept: Programming = logic + syntax + problem-
solving

2
Why need programming…
 Operating structures
 Web browsers
 Mobile apps
 Desktop packages
 Video games
 General Software program
 Business-related software programs
 Embedded structures

3
What is a Program?
A computer program is nothing but a set of instructions (smallest unit of execution) that are
used to execute particular tasks to get particular results. It is required for programmers to learn
basic concepts of mathematics to write programs. For different types of tasks, we have to write
different programs.
The set of instructions used to perform a specific task to obtain a specific result is termed a
computer program. The computer program is generated by programmers or software
developers. The code is then processed and executed to provide the output of the program.

Components of a Program
 Input → Data provided to the program
 Process → Computations, logic, decision-making
 Output → Results after processing
 Storage → Saving data for later use

Simple example diagram: Input → Processing → Output

4
Computer Program Terminologies

Below are the terminologies related to computer programs:


Source Code: It is nothing but code written in High-level language that humans
easily understand.
Machine code: It is binary code that is easy to understand by computers or
machines.
Compiler: It is used to translate the source code into machine code all at once.
Interpreter: It is used to execute the source code line by line converting it into
machine code.
Algorithm: It is nothing but a set of instructions to solve a problem.

5
What is Problem Solving?
Problem solving in programming is the process of
formulating a solution to a computational problem using a
computer program.

It involves understanding the problem, designing an


algorithm (a step-by-step solution), and then translating that
algorithm into a programming language to create a functional
program.

This process often includes breaking down complex problems


into smaller, manageable sub-problems.

6
7
Why Programming Languages?
A computer "speaks" in a "language" using strings of the numerals "1" and "0"—
known as binary code. A programming language allows us to translate the
computer's binary code into something humans can understand and write.

These languages enable us to communicate with computers, build and organize


code, specify functionality and behavior, automate and improve efficiency,
collaborate and share code, and solve problems creatively.

8
Language Types

9
Language Trends

10
Generation of Programming Languages

11
What is C?
C is a general-purpose, procedural, high-level programming language used in the
development of computer software and applications, system programming, games, and more.
•C language was developed by Dennis M. Ritchie at the Bell Telephone Laboratories
in 1972.
•It is a powerful and flexible language which was first developed for the programming
of the UNIX operating System.
•C is one of the most widely used programming languages.
C programming language is known for its simplicity and efficiency. It is the best choice to
start with programming as it gives you a foundational understanding of programming.

12
1. Foundation for Other Languages
Many modern languages (C++, Java, Python) are influenced by C.
Builds strong fundamentals for learning advanced languages.
2. Close to Hardware
Direct access to memory using pointers.

Why Suitable for writing device drivers, firmware, and embedded systems.
3. High Performance
Compiled into machine code → faster execution.

Learn Used in performance-critical applications.


4. Portability
Write once, compile anywhere with minimal changes.

C? Works across different operating systems and devices.


5. Essential for System Programming
Operating systems (Linux, Windows) and file systems are written in C.
Networking and embedded software often use C.
6. Improves Problem-Solving Skills
Encourages logical thinking and understanding of memory management.
Helps in competitive programming and algorithmic challenges.

13
Features of C

14
C Program Compilation Process
Structure of a C Program

16
Introduction to C
#include <stdio.h>

int main ()
{
printf(“Welcome to CS 1621!\n”);

return 0;
}
17
Outline
II. Program Basics
A. Program skeleton
preprocessor directives
global declarations
functions
local declarations
statements
B. Comments and Documentation
C. Names (identifiers)
reserved words
18
Outline (cont)
II. Program Basics (cont)
D. Variable declarations
1. Memory allocation
2. Atomic types
void, int, float, char
E. Constants
1. literal
2. defined
3. memory
19
Outline (cont)
II. Program Basics (cont)
F. Formatted input/output
1. Files
2. Printf (monitor output)
a. format strings
field specifications
b. data list
3. Scanf (keyboard input)
a. format strings
b. address list
4. Prompting for Input
20
Execution Process of a C Program

22
Best C IDEs

23
History of C
C Program Structure
Preprocessor Directives
• Program defined by:
– global declarations
Global Declarations
– function definitions
Function Definitions • May contain preprocessor
int main () {
directives
Local Declarations
• Always has one function
Statements named main, may contain
}
others

25
Parts of a Program

#include <stdio.h> Preprocessor Directive

int x; Global Declaration

int main () {
int y; Local Declaration
Function printf("Enter x and y: ");
scanf(&x,&y); Statements
printf("Sum is %d\n",x+y);
}

26
Preprocessor Directives
• Begin with #
• Instruct compiler to perform some
transformation to file before compiling
• Example: #include <stdio.h>
– add the header file stdio.h to this file
– .h for header file
– stdio.h defines useful input/output functions

27
Declarations
• Global
– visible throughout program
– describes data used throughout program
• Local
– visible within function
– describes data used only in function

28
Functions
• Consists of header and body
– header: int main ()
– body: contained between { and }
• starts with location declarations
• followed by series of statements
• More than one function may be defined
• Functions are called (invoked) - more later

29
Main Function
• Every program has one function main
• Header for main: int main ()
• Program is the sequence of statements
between the { } following main
• Statements are executed one at a time from
the one immediately following to main to
the one before the }
30
Comments
• Text between /* and */
• Used to “document” the code for the human
reader
• Ignored by compiler (not part of program)
• Have to be careful
– comments may cover multiple lines
– ends as soon as */ encountered (so no internal
comments - /* An /* internal */ comment */)
31
Comment Example
#include <stdio.h>

/* This comment covers


* multiple lines
* in the program.
*/

int main () /* The main header */ {


/* No local declarations */

printf(“Too many comments\n”);


} /* end of main */
32
Documentation
• Global - start of program, outlines overall
solution, may include structure chart
• Module - when using separate files,
indication of what each file solves
• Function - inputs, return values, and logic
used in defining function
• Add documentation for key (tough to
understand) comments
• Names of variables - should be chosen to be
meaningful, make program readable
33
Syntax of C
• Rules that define C language
– Specify which tokens are valid
– Also indicate the expected order of tokens
• Some types of tokens:
– reserved words: include printf int ...
– identifiers: x y ...
– literal constants: 5 ‘a’ 5.0 ...
– punctuation: { } ; < > # /* */
34
Identifier
• Names used for objects in C
• Rules for identifiers in C:
– first char alphabetic [a-z,A-Z] or underscore (_)
– has only alphabetic, digit, underscore chars
– first 31 characters are significant
– cannot duplicate a reserved word
– case (upper/lower) matters

35
Reserved Words

• Identifiers that already have meaning in C


• Examples:
– include, main, printf, scanf, if, else, …
– more as we cover C language

36
Valid/Invalid Identifiers
Valid Invalid
sum 7of9
c4_5 x-name
A_NUMBER name with spaces
longnamewithmanychars 1234a
TRUE int
_split_name AXYZ&

38

You might also like