Lecture 01 Introduction To Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Computer

Programming with C

ICT 012

Instructor: Mr. Katwale, S


Moshi Co-operative University (MoCU)
About the Course –Materials/Tools

• In this course we will discuss Computer Programming using C


programming language.

• IDE:Dev-C++

• Internet Resources:
https://www.w3schools.com/c/index.php
About the Course -Assessment

Assessment

• Class Test 1 - 15%

• Class Test 2 - 10%

• Assignment - 15%

• Final Examination - 60%


What is Computer Program?
• Computer program is a sequence of instructions written
using a Computer Programming Language to perform a
specified task by the computer.
• The two important terms that we have used in the
above definition are −
① Sequence of instructions
② Computer Programming Language
What is Computer Programming?
• Computer programming:
Is the process of writing code to facilitate specific actions in a
computer, application or software program, and instructs them on
how to perform.
• Computer programmers:
Are professionals that create instructions for a computer to execute
by writing and testing code that enables applications and software
programs to operate successfully.
Computer Programming...
• A computer program is also called a computer software, which can
range from two lines to millions of lines of instructions.

• Computer program instructions are also called program source code


and computer programming is also called program coding.

• A computer without a computer program is just a dump box; it is


programs that make computers active.
Levels of Programming
Language
• Programming languages are said to be "lower" or
"higher," depending on how close they are to the
language the computer itself uses ("Os and 1s" = low),

• Or close to the language people use (more English-like-


high). We will consider three levels of language. They
are numbered 1 through 3 to correspond to levels, or
generations.
Levels of Programming Language…

• In terms of ease of use and capabilities, each generation is an improvement over its
predecessors.

• The three generations of languages are:-

① Machine language

② Assembly language

③ High-level language
Machine language

• Humans do not like to deal in numbers alone-they prefer letters and


words. But, strictly speaking, numbers are what machine language is.

• This lowest level of language, machine language, represents data and


program instructions as 1s and Os-binary digits corresponding to the on
and off electrical states in the computer.
Machine language…

• Each type of computer has its own machine language. In the early days of
computing, programmers had rudimentary systems for combining numbers
to represent instructions such as add and compare.

• Primitive by today's standards, the programs were not convenient for


people to read and use.

• The computer industry quickly moved to develop assembly languages.


Assembly language
• Assembly Language is a low-level programming language.
• It helps in understanding the programming language to machine code.
• In computers, there is an assembler that helps in converting the assembly code into machine
code (executable).
• Assembly language is designed to understand the instruction and provide it to machine
language for further processing.

Example of Assembly
language codes

mnemonic
Assembly language…

• It mainly depends on the architecture of the system, whether it is the operating


system or computer architecture.

• Assembly Language mainly consists of mnemonic processor instructions or data and


other statements or instructions.

• Assemblers

The assemblers are used to translate the assembly language into machine language.
High-level language

• The first widespread use of high-level languages in the early 1960s transformed
programming into something quite different from what it had been.

• Programs were written in an English-like manner, thus making them more


convenient to use.

• As a result, a programmer could accomplish more with less effort, and programs
could now direct much more complex tasks.
High-level language…

• Of course, a translator is needed to translate the symbolic statements of a


high-level language into computer-executable machine language;

• This translator is usually a compiler. There are many compilers for each
language and one for each type of computer.
High-level language…

• Compiler is a special program that translates a programming language's source


code into machine code, bytecode or another programming language. The source
code is typically written in a high-level, human-readable language such as C, Java
or C++.

• Examples of high-level languages include BASIC, C, C++, Java, Pascal, PHP,


Python, Ruby, and Visual Basic.
C – Programming language

• The C Language is developed by Dennis Ritchie for creating system applications


that directly interact with the hardware devices such as drivers, kernels, etc.

• C programming is considered as the base for other programming languages, that


is why it is known as mother language.
Why to Learn C Programming?

key advantages of learning C Programming:

• Easy to learn

• Structured language

• It produces efficient programs

• It can handle low-level activities

• It can be compiled on a variety of computer platforms


Applications of C Programming

• C was initially used for system development work, particularly the


programs that make-up the operating system.

• C was adopted as a system development language because it produces


code that runs nearly as fast as the code written in assembly language.
Applications of C Programming…

• Some examples of the use of C are :-

• Operating Systems

• Language Compilers

• Assemblers

• Text Editors

• Print Spoolers

• Network Drivers

• Modern Programs

• Databases
C Programs

• A C program can vary from 3 lines to millions of lines and it should be written
into one or more text files with extension ".c";

• For example, hello.c. You can use "Notepad++”, or any other text editor to
write your C program into a file.

• But for this course we are going to use an Integrated Development Environment
(IDE) called Dev-C++.
What Is An IDE?

• Integrated development environments (IDE) are applications that


facilitates the development of other applications.

• Designed to encompass all programming tasks in one application, one


of the main benefits of an IDE is that they offer a central interface with
all the tools a developer needs, including:
What Is An IDE?...

• Code editor: Designed for writing and editing source code, these editors are distinguished from text
editors because work to either simplify or enhance the process of writing and editing of code for
developers

• Compiler: Compilers transform source code that is written in a human readable/writable language in a
form that computers can execute.

• Debugger: Debuggers are used during testing and can help developers debug their application programs.

• Build automation tools: These can help automate developer tasks that are more common to save time.
C - Program Structure

A C program basically consists of the following parts :−

• Preprocessor Commands

• Functions

• Variables

• Statements & Expressions

• Comments
Hello World
Program • Let us look at a simple code that would print the words
"Hello World“

#include <stdio.h>

int main() {
Output:
/* my first program in C */
Hello, World!
printf("Hello, World! \n");

return 0;

}
Parts of the • #include <stdio.h> is a preprocessor
command, which tells a C compiler to include
hello world <stdio.h> file before going to actual
program compilation.

• int main() is the main function where the


program execution begins.

• /*...*/ will be ignored by the compiler and it


has been put to add additional comments in
the program. So such lines are called
comments in the program.
Parts of the • printf(...) is another function available in

hello world C which causes the message "Hello,


World!" to be displayed on the screen.
program…
• return 0; terminates the main()
function and returns the value 0.
Compile and • Let us see how to save the source code in
Execute C a file, and how to compile and run it.
Program when Following are the simple steps −
using text editor
• Open a text editor and add the above-
mentioned code.

• Save the file as hello.c

• Open a command prompt and go to the


directory where you have saved the file.
Compile and Execute C Program when using
text editor…

• Type gcc hello.c and press enter to compile your code.

• If there are no errors in your code, the command prompt will take you to the
next line and would generate a.out executable file.

• Now, type a.out to execute your program.

• You will see the output "Hello World" printed on the screen.
How to create C Program Using Dev-C++ (IDE)

Step 1: Install Dev-C++

If you haven't already installed Dev-C++, you can download it from


the official website. Make sure to download the Dev-C++ with
Mingw included version as it comes with the GCC compiler.
How to create C Program Using Dev-C++ (IDE)…

Step 2: Create a New Project

• Launch Dev-C++.

• Click on "File" in the top menu.

• Select "New" and then "Project..." or use the keyboard shortcut "Ctrl+Shift+N."

• In the "New Project" dialog, choose "Console Application" and click "OK."

• Enter the project title and choose a location to save your project. Click "Save."
How to create C Program Using Dev-C++ (IDE)

Step 3: Create a New Source File

After creating the project, Dev-C++ will open a new source file. You can start writing your C code
here. By default, it creates a simple "Hello, World!" program.
How to create C Program Using Dev-C++ (IDE)

Step 4: Save Your Source File

Save your source file by clicking "File" and then "Save" or using the
keyboard shortcut "Ctrl+S." Choose the location where you want to save it
and provide a meaningful filename with the ".c" extension (e.g.,
myprogram.c).
How to create C Program Using Dev-C++ (IDE)

Step 5: Build and Compile

• Click on the "Execute" menu.

• Select "Compile."

• Dev-C++ will compile your code. If there are no errors, you'll see "0
errors" in the Output window at the bottom.
How to create C Program Using Dev-C++ (IDE)

Step 6: Run Your Program

• After successfully compiling, go to the "Execute" menu again.

• Select "Run."

• You'll see the output of your program in the console window that opens.
Thank You

You might also like