[go: up one dir, main page]

0% found this document useful (0 votes)
12 views70 pages

Introduction To Programming Language C/C++

The document provides an introduction to programming languages and C++. It discusses the characteristics, components, and evolution of programming languages. Tips are provided for learning new programming languages, including practicing daily, working on projects, and learning from others. The five generations of programming languages are also outlined.

Uploaded by

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

Introduction To Programming Language C/C++

The document provides an introduction to programming languages and C++. It discusses the characteristics, components, and evolution of programming languages. Tips are provided for learning new programming languages, including practicing daily, working on projects, and learning from others. The five generations of programming languages are also outlined.

Uploaded by

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

KIGALI IKINDEPENDENT UNIVERSITY ULK

School of Science and technology


Department of Computer Science

Module: COMPUTER
PROGRAMMING IN C++

Code: CSM108

Class: Year 1/CS/DAY/All majors

Lecturer : Kwizera Jean Pierre


E-mail : kwijpeter01@gmail.com
1 04/12/2024
Audience

This Module has been prepared for the


beginners to help them understand the basic to
advanced concepts related to C++.

2 INTRODUCTION TO C 04/12/2024
Introduction to C Language

3 INTRODUCTION TO C 04/12/2024
Introduction to Programming Languages

Introduction:

A programming language is a set of


instructions and syntax used to create
software programs.

 Some of the key features of programming


languages include:

4 INTRODUCTION TO C 04/12/2024
 Syntax: The specific rules and structure
used to write code in a programming
language.

 Data Types: The type of values that can be


stored in a program, such as numbers,
strings, and booleans.

 Variables: Named memory locations that


can store values.

 Operators: Symbols used to perform


operations on values, such as addition,
subtraction, and comparison.

5 INTRODUCTION TO C 04/12/2024
 Control Structures: Statements used to
control the flow of a program, such as if-else
statements, loops, and function calls.

 Libraries: Collections of pre-written code that


can be used to perform common tasks and
speed up development.

6 INTRODUCTION TO C 04/12/2024
 A programming language is a formal language that
specifies a set of instructions for a computer to
perform specific tasks.

 It’s used to write software programs and


applications, and to control and manipulate
computer systems.

 There are many different programming languages,


each with its own syntax, structure, and set of
commands.

 Some of the most commonly used programming


languages include Java, Python, C++, JavaScript,
7 andINTRODUCTION
C#. TO C 04/12/2024
Characteristics of a programming
Language –
 A programming language must be simple,
easy to learn and use, have good
readability, and be human recognizable.

 A portable programming language is always


preferred.

 Programming language’s efficiency must be


high so that it can be easily converted into
a machine code and its execution
consumes little space in memory.

8 INTRODUCTION TO C 04/12/2024
 A programming language should be well
structured and documented so that it is
suitable for application development.

 Necessary tools for the development,


debugging, testing, maintenance of a
program must be provided by a
programming language.

A programming language must be


consistent in terms of syntax and
semantics.

9 INTRODUCTION TO C 04/12/2024
 Basic Terminologies in Programming
Languages:

 Variable: A named storage location in memory


that holds a value or data.
 Data Type: A classification that specifies what
type of data a variable can hold, such as integer,
string, or Boolean.
 Function: A self-contained block of code that
performs a specific task and can be called from
other parts of the program.
 Control Flow: The order in which statements are
executed in a program, including loops and
conditional statements.
 Syntax: The set of rules that govern the structure
andINTRODUCTION
format ofTOaCprogramming language.
10 04/12/2024
 Operator: A symbol or keyword that
represents an action or operation to be
performed on one or more values or
variables, such as + (addition), –
(subtraction), * (multiplication), and /
(division).

 Statement: A single line or instruction in a


program that performs a specific action or
operation

11 INTRODUCTION TO C 04/12/2024
Tips for learning new programming language:

1. Start with the fundamentals: Begin by learning the basics


of the language, such as syntax, data types, variables, and
simple statements. This will give you a strong foundation to
build upon.

2. Code daily: Like any skill, the only way to get good at
programming is by practicing regularly. Try to write code
every day, even if it’s just a few lines.

3. Work on projects: One of the best ways to learn a new


language is to work on a project that interests you. It could
be a simple game, a web application, or anything that
allows you to apply what you’ve learned that is the most
12
important part. TO C
INTRODUCTION 04/12/2024
4. Read the documentation: Every
programming language has documentation that
explains its features, syntax, and best practices.
Make sure to read it thoroughly to get a better
understanding of the language.

5. Join online communities: There are many


online communities dedicated to programming
languages, where you can ask questions, share
your code, and get feedback. Joining these
communities can help you learn faster and make
connections with other developers.

13 INTRODUCTION TO C 04/12/2024
6. Learn from others: Find a mentor or
someone who is experienced in the language
you’re trying to learn. Ask them questions,
review their code, and try to understand how
they solve problems.

7. Practice debugging: Debugging is an


essential skill for any programmer, and you’ll
need to do a lot of it when learning a new
language. Make sure to practice identifying
and fixing errors in your code.

14 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES
There are five generations of Programming
languages. They are:

1) First-Generation Languages :
These are low-level languages like machine
language.

2) Second-Generation Languages :

These are low-level assembly languages used


in kernels and hardware drives.

3) Third-Generation Languages :
These are high-level languages like C, C++,
Java,
15 VisualTO Basic,
INTRODUCTION C and JavaScript. 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

4) Fourth Generation Languages :

 These are languages that consist of


statements that are similar to
statements in the human language.

 These are used mainly in database


programming.

 Examples of these languages include


Perl, Python, Ruby, SQL, and
16 MatLab(MatrixLaboratory).
INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

5) Fifth Generation Languages :

These are the programming languages


that have visual tools to develop a
program. Examples of fifth-generation
languages include Mercury, OPS5, and
Prolog.

17 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES
The first two generations are called
low-level languages.
The next three generations are called
high-level languages.

18 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

1. First-Generation Language :

The first-generation languages are also


called machine languages/ 1G language.

This language is machine-dependent.

The machine language statements are


written in binary code (0/1 form)
because the computer can understand only
binary language.

19 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

Advantages :

1. Fast & efficient as statements are


directly written in binary language.
2. No translator is required.

Disadvantages :

1.Difficult to learn binary codes.


2. Difficult to understand – both
programs & where the error occurred.

20 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES
2. Second Generation Language :

 The second-generation languages are also


called assembler languages/ 2G languages.
Assembly language contains human-readable
notations that can be further converted
to machine language using an assembler.

 Assembler – converts assembly level


instructions to machine-level
instructions.

 Programmers can write the code using


symbolic instruction codes that are
meaningful abbreviations of mnemonics. It
21 INTRODUCTION TO C 04/12/2024
is also known as low-level language.
EVOLUTION OF PROGRAMMING LANGUAGES
Advantages :

1. It is easier to understand if
compared to machine language.
2. Modifications are easy.
3. Correction & location of errors are
easy.

Disadvantages :

1. Assembler is required.

22 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

3. Third-Generation Language :

The third generation is also called


procedural language /3 GL.

It consists of the use of a series


of English-like words that humans
can understand easily, to write
instructions.

It’s also called High-Level


Programming Language.
23 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

For execution, a program in this


language needs to be translated into
machine language using a Compiler/
Interpreter.

Examples of this type of language


are C++, C, PASCAL, FORTRAN, COBOL,
etc.

24 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

Advantages :

1. Use of English-like words makes it a


human-understandable language.
2. Lesser number of lines of code as
compared to the above 2 languages.
3. Same code can be copied to another
machine & executed on that machine by
using compiler-specific to that machine.

Disadvantages :
1. Compiler/ interpreter is needed.
2. Different compilers are needed for
different machines.
INTRODUCTION TO C
25 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

4. Fourth Generation Language :

The fourth-generation language is


also called a non – procedural
language/ 4GL. It enables users to
access the database. Examples: SQL,
Foxpro, Focus, etc.

These languages are also human-


friendly to understand.

26 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

Advantages :

1. Easy to understand & learn.


2. Less time is required for
application creation.
3. It is less prone to errors.

Disadvantages :

1. Memory consumption is high.


2. Less flexible.
27 INTRODUCTION TO C 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

5. Fifth Generation Language :

 The fifth-generation languages are also


called 5GL.
 It is based on the concept of artificial
intelligence.
 It uses the concept that rather than
solving a problem algorithmically, an
application can be built to solve it
based on some constraints, i.e., we make
computers learn to solve any problem.

 Parallel Processing & superconductors


are used for this type of language to
28 make real TO
INTRODUCTION artificial
C intelligence. 04/12/2024
EVOLUTION OF PROGRAMMING LANGUAGES

 Examples: PROLOG, LISP, etc.

Advantages :

1. Machines can make decisions.


2. Programmer effort reduces to solve a
problem.
3. Easier than 3GL or 4GL to learn and use.

Disadvantages :

1. Complex and long code.


2. More resources are required & they are
expensive too.
29 INTRODUCTION TO C 04/12/2024
Historical development of C and C++

By 1960 many computer languages had come


into existence, each was meant for a
specific purpose. Example COBOL was used
for Commercial Applications, FORTRAN for
Engineering and Scientific Applications.

With this many languages in place


programmers wanted to come up with one
language which could be used to handle all
possible applications. An international
committee was set up and it came out with
a language called ALGOL 60.

30 INTRODUCTION TO C 04/12/2024
ALGOL 60 turned out to be too abstract

and too general.

A new language called Combined


Programming Language (CPL) was
developed at Cambridge to counter the
ALGOLS 60 problem but it also turned to
be too big, with so many features, which
made it hard to learn, an to implement.

31 INTRODUCTION TO C 04/12/2024
Martin Richards developed basic combined
Programming Language at Cambridge
(BCPL) University to overcome the
problem of CPL, but it turned to be less
powerful and too specific.

Around the same time Ken Thompson, wrote


a language called B at AT & T’s Bell
Labs, but the B language also turned to
be so specific.

32 INTRODUCTION TO C 04/12/2024
Dennis Ritchie inherited the features
of B and BCPL, added some of his own
featured and developed the C Language.

 C++ evolved from c.


Bjarne Stroustrup
took c and extended the
feature needed to
facilitate object oriented
programming.

 He created C++ in 1980


at the AT&T laboratories
located in U.S.A.
33 INTRODUCTION TO C 04/12/2024
What is C?
C is a general-purpose programming language
created by Dennis Ritchie at the Bell Laboratories
in 1972.

It is a very popular language, despite being old.


The main reason for its popularity is because it is
a fundamental language in the field of
computer science.

34 Introduction in C Language 04/12/2024


Difference between C and C++

C++ was developed as an extension of C, and


both languages have almost the same syntax

The main difference between C and C++ is that


C++ support classes and objects, while C does
not

35 Introduction in C Language 04/12/2024


Get started with C

 To start using C, you need two things:

A text editor, like Dev C++, CodeBlock, Turbo


C++ to write C code

There are many text editors and compilers to


choose from.

36 Introduction in C Language 04/12/2024


C Quickstart

Let's create our first C file.

Write the following C code and save the file


as myfirstprogram.c

#include <stdio.h>
int main()
{
printf("Hello World!");
return 0;
}37 Introduction in C Language 04/12/2024
Example explained

 Line 1: #include <stdio.h> is a header file library


that lets us work with input and output functions,
such as printf() (used in line 4). Header files add
functionality to C programs.

 Don't worry if you don't understand how #include


<stdio.h> works. Just think of it as something that
(almost) always appears in your program.

38 Introduction in C Language 04/12/2024


 Line 2: A blank line. C ignores white space. But we
use it to make the code more readable.

 Line 3: Another thing that always appear in a C


program is main(). This is called a function. Any
code inside its curly brackets {} will be executed.

 Line 4: printf() is a function used to output/print


text to the screen. In our example, it will output
"Hello World!".

Note that: Every C statement ends with a semicolon ;

39 Introduction in C Language 04/12/2024


Note: The body of int main() could also been written
as:

int main()
{
printf("Hello World!");
return 0;
}

Remember: The compiler ignores white spaces.


However, multiple lines makes the code more
readable.

40 Introduction in C Language 04/12/2024


 Line 5: return 0 ends the main() function.

 Line 6: Do not forget to add the closing curly


bracket } to actually end the main function.

41 Introduction in C Language 04/12/2024


Exercise:

 Insert the missing part of the code below to output "Hello


World!":

42 Introduction in C Language 04/12/2024


Statements

A computer program is a list of "instructions" to be


"executed" by a computer.

In a programming language, these programming


instructions are called statements.

The following statement "instructs" the compiler to print


the text "Hello World" to the screen:

Example: printf("Hello World!");

43 Introduction in C Language 04/12/2024


 It is important that you end the statement with
a semicolon ;

 If you forget the semicolon (;), an error will


occur and the program will not run:

44 Introduction in C Language 04/12/2024


Many Statements

Most C programs contain many statements.

The statements are executed, one by one, in


the same order as they are written:

Example:

printf("Hello World!");
printf("Have a good day!");
return 0;

45 Introduction in C Language 04/12/2024


Example explained

From the example above, we have three statements:

1. printf("Hello World!");
2. printf("Have a good day!");
3. return 0;

 The first statement is executed first (print "Hello World!"


to the screen).

 Then the second statement is executed (print "Have a


good day!" to the screen).

 And at last, the third statement is executed (end the C


program successfully).
46 Introduction in C Language 04/12/2024
C Output (Print Text)

To output values or print text in C, you can use


the printf() function:
Double Quotes:
Example:
 When you are working
with text, it must be
wrapped inside double
#include <stdio.h> quotations marks "".
 If you forget the double
int main() quotes, an error
occurs:
{
printf("Hello World!");
return 0;
} Introduction in C Language
47 04/12/2024
Example:

printf("This sentence will work!");

printf(This sentence will produce an error.);

48 Introduction in C Language 04/12/2024


Many printf Functions

You can use as many printf() functions as you


want. However, note that it does not insert a
new line at the end of the output:

Example:

#include <stdio.h>
int main()
{
printf("Hello World!");
printf("I am learning C.");
printf("And it is awesome!");
return 0;
}
49 Introduction in C Language 04/12/2024
New Lines

To insert a new line, you can use the \n character:

Example:

#include <stdio.h>
int main()
{
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}

50 Introduction in C Language 04/12/2024


You can also output multiple lines with a single
printf() function.

However, this could make the code harder to read:

Example:

#include <stdio.h>
int main()
{
printf("Hello World!\n I am learning C.\n And it is
awesome!");
return 0;
}

Tip: Two \n characters after each other will create


a blank
51
line:
Introduction in C Language 04/12/2024
Example
#include <stdio.h>

int main()
{
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}

52 Introduction in C Language 04/12/2024


C Comments

Single-line Comments

 Single-line comments start with two forward slashes


(//).

 Any text between // and the end of the line is


ignored by the compiler (will not be executed).

 This example uses a single-line comment before a


line of code:
Example:
// This is a comment
printf("Hello World!");
 Example

53printf("Hello
Introduction in CWorld!");
Language // This is a comment 04/12/2024
C Multi-line Comments

 Multi-line comments start with /* and ends with


*/.

 Any text between /* and */ will be ignored by the


compiler:

 Example:

/* The code below will print the words Hello World!


to the screen, and it is amazing */
printf("Hello World!");

 Single or multi-line comments?


It is up to you which you want to use. Normally, we
use //
54 for short comments, and /* */ for longer.
Introduction in C Language 04/12/2024
C Variables

 Variables are containers for storing data values,


like numbers and characters.

 In C, there are different types of variables


(defined with different keywords), for example:

 int - stores integers (whole numbers), without


decimals, such as 123 or -123

 float - stores floating point numbers, with


decimals, such as 19.99 or -19.99

 char - stores single characters, such as 'a' or 'B'.


Characters are surrounded by single quotes
55 Introduction in C Language 04/12/2024
 Declaring (Creating) Variables

 To create a variable, specify the type and assign it


a value:
Syntax:

type variableName;

So, to create a variable that should store a number,


look at the following example:

Example:
Create a variable called myNum of type int and
assign the value 15 to it:

int myNum = 15;


56 Introduction in C Language 04/12/2024
You can also declare a variable without
assigning the value, and assign the value later:

Example:

// Declare a variable

int myNum;

// Assign a value to the variable

myNum = 15;

57 Introduction in C Language 04/12/2024


 In many other programming languages (like
Python, Java, and C++), you would normally use
a print function to display the value of a
variable.

 However, this is not possible in C:

 Example:

int myNum = 15;


printf(myNum); // Nothing happens

58 Introduction in C Language 04/12/2024


 To output variables in C, you must get familiar
with something called "format specifiers",
which you will learn them.

59 Introduction in C Language 04/12/2024


Format Specifiers

 Format specifiers are used together with the


printf() function to tell the compiler what type of
data the variable is storing. It is basically a
placeholder for the variable value.

 A format specifier starts with a percentage sign


%, followed by a character.

 For example, to output the value of an int


variable, use the format specifier %d surrounded
by double quotes (""), inside the printf()
function:

60 Introduction in C Language 04/12/2024


 To print other types, use %c for char and %f for
float:
 Example:

// Create variables

int myNum = 15; // Integer (whole number)


float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character

// Print variables

printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);

61 Introduction in C Language 04/12/2024


Program example:

#include <stdio.h>

int main() {
// Create variables
int myNum = 15; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character

// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}

62 Introduction in C Language 04/12/2024


Example:

int myNum = 15;


printf("%d", myNum); // Outputs 15

63 Introduction in C Language 04/12/2024


To combine both text and a variable, separate
them with a comma inside the printf() function:

Example:

int myNum = 15;


printf("My favorite number is: %d", myNum);

64 Introduction in C Language 04/12/2024


#include <stdio.h>

int main() {
int myNum = 15;
printf("My favorite number is: %d", myNum);
return 0;
}

o print different types in a single printf() function,


you can use the following:

Example:

int myNum = 15;


char myLetter = 'D';
printf("My number is %d and my letter is %c",
myNum, myLetter);
65 Introduction in C Language 04/12/2024
#include <stdio.h>

int main() {
int myNum = 15;
char myLetter = 'D';
printf("My number is %d and my letter is %c",
myNum, myLetter);
return 0;
}

66 Introduction in C Language 04/12/2024


TIME FOR CLASS EXERCISES

67 INTRODUCTION TO C 04/12/2024
1. Write a Program to Print “Hello World!” on the
Console.
2. Write a c program that read age, sex, height of a
given student from the keyboard and display the
output on the screen.
3. Write a Program to find the Sum of two numbers
entered by the user.
4. Write a Program to Swap the values of two
variables
5. Write a program to calculate the area of a
rectangle

68 INTRODUCTION TO C 04/12/2024
6. Write a c program that calculate the area of a
circle using constants.
7. Write a program to convert from Celsius degree
(0C) to Fahrenheit degrees (0F)
Formular to use: fahrenheit = (1.8 * celsius) + 32;

8. Write a c program to read the name and marks


of a student for 3 subjects (Math, chemistry and
Computer) from keyboard.
The program should calculate the total and
average marks for the student.

69 INTRODUCTION TO C 04/12/2024
End

70 INTRODUCTION TO C 04/12/2024

You might also like