KEMBAR78
Lec2 - Introduction To C Language | PDF | C (Programming Language) | Computer Program
0% found this document useful (0 votes)
4 views17 pages

Lec2 - Introduction To C Language

The document provides an introduction to computer programming, explaining key concepts such as programming, computer languages, and the differences between compilers and interpreters. It emphasizes problem-solving through algorithms and illustrates how to write algorithms and flowcharts for various problems. Additionally, it introduces the C programming language, its structure, and examples of simple C programs.

Uploaded by

alimuzaffar085
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)
4 views17 pages

Lec2 - Introduction To C Language

The document provides an introduction to computer programming, explaining key concepts such as programming, computer languages, and the differences between compilers and interpreters. It emphasizes problem-solving through algorithms and illustrates how to write algorithms and flowcharts for various problems. Additionally, it introduces the C programming language, its structure, and examples of simple C programs.

Uploaded by

alimuzaffar085
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/ 17

Introduction to Computer Programming

(Programming Fundamentals)
L EC T URE 2 (I N T RODUC TION TO C L A N GUAGE)
BY
P ROF. D R. M . FA RHA N SHA FI Q U E

1
What is Programming
Programming is the act of instructing computers to carry out tasks
◦ It is often referred to as coding

Computer Program
◦ a sequence of instructions that the computer executes

Computer in this definition is any device


◦ that is capable of processing code
◦ This could be smartphones, ATMs, the Raspberry Pi, Servers to name a few.

Programming analogy
◦ Taking instructions from superiors at work, to complete a task
◦ Following a recipe to cook a dish

2
Computer Language
The natural language of the computers is machine language
◦ That is a binary code 10101011100
◦ Difficult for the humans to understand

To communicate with machines who speak binary, we used a language


◦ That’s closer to our own natural language
◦ As well as structed enough to be translated to machine code
◦ This could be a high-level language, a translator (language processor) is needed
◦ Or, a low-level language, only an assembler is needed

Translators have the responsibility of converting your source code to the


◦ Machine language (also known as binary)
◦ We may refer to the binaries as Object Code
◦ the Program or a common word today: App.

Translators can be an Assembler, Compilers, Interpreter


◦ Or Compiler + Interpreter

3
Compiler vs. Interpreter
Compilers convert the source code in its entirety
◦ Via a compilation process to binary -> The binary is then executed
◦ If there were errors in the source code,
◦ they are detected during the compilation time and flagged.
◦ This interrupts the compilation process, and no binary is generated.

Interpreters translate line by line and


◦ execute the line before going on to the next line
◦ Reminder: computer program is a sequence of instructions that is executed by a computer.

An executing program is usually called a process


◦ Such programs use certain resources on the computer system or smartphone
◦ such as memory, disk space and the file system
◦ An executing program can also said to be running.

We use the word ‘run’ when we execute a computer program


◦ The time it takes to run such programs is known as the run-time of the program.

4
Problem Solving and Algorithms
Can you think of a day in your life which goes without problem solving?
◦ Of course, NO!!! All of our daily activities involve problem solving

Example 1: You want to switch TV to a sports channel


◦ Get the remote -> Point it towards the TV -> Enter the channel number

Example 2: Eating out with friends and then dividing the cost
◦ Find the total cost -> Divide equally/relatively -> each friend pays his share

If you can solve a problem, you can write an algorithm for it


◦ An algorithm is the set of steps to be taken/performed, to solve a problem
◦ “A sequence of activities to be processed, for getting desired output from a given input”

There may be more than one way to solve a


Programming is Problem Solving!!! problem, so, there may be more than one algorithm
for a problem.

5
Writing Algorithms and Flow Chart (1/3)
Before writing an algorithm for a problem, one should find out:
◦ the inputs to the algorithm, and the expected output

Problem 1: Find the area , of a Circle with radius .


◦ Inputs: Radius , expected output: Area Start
◦ Step 1: Read radius of the circle as input
◦ Step 2: Calculate area as pi *r*r Input
◦ Step 3: Write/Display area
= ⋅
A flowchart is a diagram which visually presents
◦ the flow of data through processing systems
Output
◦ A graphical representation of an algorithm

For Problem 1: The flow chart is Stop

6
Writing Algorithms and Flow Chart (2/3)
Problem 2: Add two numbers A and B (to obtain sum S)
◦ Inputs: Numbers & , expected output: Sum
Start
◦ Step 1: Start
◦ Step 2: Read/Get number A Input
◦ Step 3: Read/Get number B
Input
◦ Step 4: Sum ← + //calculation of sum
◦ Step 5: Print Sum = +
◦ Step 6: End
Output

Stop

7
Writing Algorithms and Flow Chart (3/3)
Problem 3: Find , which is larger of the two numbers A and B
◦ Inputs: Numbers & ,
Start
◦ expected output: Larger value
◦ Step 1: Start Input

◦ Step 2: Read/Get number A Input


◦ Step 3: Read/Get number B
◦ Step 4: if A is greater than B then C = A No If >
◦ Step 5: else C = B Yes
=
◦ Step 6: Print the large value =

◦ Step 7: End Output


Can we develop an alternative algorithm for this???

Stop
Introduction to C Language
C is a procedural programming language
◦ Initially developed by Dennis Ritchie in the year 1972.
◦ Developed as a system programming language to write an operating system

The main features of C language include


◦ low-level access to memory, a simple set of keywords, and clean style,
◦ which make C language suitable for system programming
◦ like an operating system or compiler development

Many later languages have borrowed syntax/features


◦ directly or indirectly from C language.
◦ Like syntax of Java, PHP, JavaScript, and many other languages
◦ are mainly based on C language.
◦ C++ is nearly a superset of C language
◦ There are few programs that may compile in C, but not in C++

9
Structure of a C Program
Writing a C program in any other structure will lead to a Compilation Errors

Include other (header) files which contain #include <stdio.h>


Header
functions and macro definitions
The program execution starts here, from the Main method int main()
first line of this function definition {

Define type and name of the variable int a = 10;


Variable
Optionally assign value to it
declaration
Without declaration, variable cannot be
used
Operations performed by this function
Body printf("%d", a);
Manipulation, calculation and printing etc.
Last statement, end of function/program. return 0;
Return
returns a value to its parent }

10
Explanation
The components of the above structure are:
Header Files Inclusion: The first and foremost component is the inclusion of the
Header files in a C program.
A header file is a file with extension .h which contains C function declarations and
macro definitions to be shared between several source files. Some of C Header files:
◦ stddef.h – Defines several useful types and macros.
◦ stdint.h – Defines exact width integer types.
◦ stdio.h – Defines core input and output functions
◦ stdlib.h – Defines numeric conversion functions, pseudo-random network generator, memory
allocation
◦ string.h – Defines string handling functions
◦ math.h – Defines common mathematical functions

Syntax to include a header file in C:


#include <stdio.h> or #include “stdio.h”
11
Main Method and variable Declaration:
The next part of a C program is to declare the main() function. The syntax to declare the main
function is:
Syntax to Declare main method:
int main() {
// Write code inside these parenthesis
}
The next part of any C program is the variable declaration. It refers to the variables that are to be
used in the function. Please note that in the C program, no variable can be used without being
declared. Also in a C program, the variables are to be declared before any operation in the
function.
Example:
int main() { int a; char ch, float gg…

12
Body:
Body of a function in C program, refers to the operations that are performed in the functions. It
can be anything like manipulations, searching, sorting, printing, etc. Example:
int main() {
int a;
printf("%d", a);
//other code goes here
}
The code written within the { } is called body of the function (here function name is main)

13
Return Statement:
The last part in any C program is the return statement. The return statement refers to the
returning of the values from a function. This return statement and return value depend upon the
return type of the function. For example, if the return type is void, then there will be no return
statement. In any other case, there will be a return statement and the return value will be of the
type of the specified return type. Example:
int main()
{ int a;
printf("%d", a);
return 0;
}

14
Writing first program:
#include <stdio.h>

int main(void) {

printf(“Hello to the world of programming");

return 0;

15
Write a simple C program (1/2) Output
5 Entered value of a
6 Entered value of b
The sum is: 11

Remember Problem 2, for which we wrote an algorithm?


Start
◦ Add two numbers a and b (to obtain sum s)
#include <stdio.h>
scanf Input
◦ Prompts user for input int main()
{
printf int a, b; Variable Input
◦ Prints on screen int s; declaration
scanf("%d", &a);
%d means scanf("%d", &b); = +
◦ Decimal/integer value s = a + b;
printf(“Sum S = %d", s); Output
&a means
◦ Address of variable a return 0;
} Stop

stdio.h library defines functions scanf & printf, for input and output, respectively

16
Write a simple C program (2/2)
Output
5
Problem 3: Find c, which is larger of the two numbers a and b 6
Large is: 6
Start
#include <stdio.h>

int main() Input


{
int a, b, c;
Input
scanf("%d", &a);
scanf("%d", &b);
No
If >
if (a>b)
c = a; Yes
else = =
c = b;

printf(“Large is: %d", c); Output

return 0;
} Stop

17

You might also like