Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views45 pages

2 Introduction To C Programming Language

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 45

Introduction to

Programming Language C
Learning Outcome :
• Introduction in C
• C Language Overview
• Why Using C
• Starting C Programs
• Dev C++ Installation
• Starting the Dev C++ Application
• C Structure
• Programming C using online compiler
• Create an Example Program of hello world
• Character
• Identifier
• Keywords
• Data Types
• Variable
Introduction in C
• Preparation to COMP6047 – Algorithm and Programming
course at 1st semester.
• Textbook:
C : How to program :with
an introduction to C++
Paul Dietel & Harvey Dietel . 2016.
Pearson Education.
ISBN: 9780133976892
C Language Overview
C was originally first implemented on the DEC PDP-11 computer in 1972.
In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly
available description of C, now known as the K&R standard.

The C has now become a widely used professional language for various
reasons.
• 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
Why Using C
• Flexibility
Close to low level machine language yet easy to understand.

• Portability
Used form microcontroller, micro computer to super computer.

• A Well Known Programming Language


It is used in many forms of implementations such as O/S, scientific
application, business application, etc.

• Supported With a Large Number of Libraries


Starting C Programs
To start the C program can use IDE based on desktop apps or online
apps.
To programs the C based on desktop apps can use Dev C ++ or Code
Block application.

DEV C++ Code Blocks


Or another solution to programs the C based on online apps can use a C
online compiler https://www.onlinegdb.com/online_c_compiler
Dev C ++ Installation
• This study will use a Dev C++ app as main compiler to practice the
programming of C.
• Download the Dev C++  https://www.bloodshed.net/

1 Double click this file

3 4
5

7
8
9

10 11
Starting The Dev C++ Application

Double click this application

• Until this step, the user can directly open


the programs and start the first program.
C Structure
• C language is a structural programming language
• It consists of functions
• There is no separation between function and procedure (if you are
from Pascal language background)
• Each C program has one main function called main
• Program will be started from the first line of the main function
• C language is case sensitive
• Every statement should be ended with a semi-colon (;)
C Structure

main() main() The four structure at the


{ {
statements; statements; left is several ways to start
} return(0); programming C.
}

void main() int main()


{
The program will error
{
statements; statements; when compiled without
} return(0); the main()'s body.
}
C Structure
• Directive #include generally written at the beginning of a program
• The Coding Style (depending on the programmer) can be written as
an example displayed in the below left or right section.
#include<stdio.h> #include<stdio.h>
int main() int main(){
{ printf (“Welcome to BINUS\n”);
printf (“Welcome to BINUS\n”); Or return (0);
return (0); }
}
Programming C Using Online Compiler
• By using the internet's browser, we can also practice compiling the
programming C using an online compiler without installing the
application on your computer necessary.
• Please access the website through this link as follow 
https://www.onlinegdb.com/online_c_compiler
Create an Example Program of hello world
Create an Example Program of hello world
• Used for readability of the program
• Not accounted as a command/statement by the compiler
• Using /* and */
• Using // at the beginning of line for one line comment
• Example:
/*--------------------------
My First Program
--------------------------*/
#include<stdio.h>
void main(){
printf (“Hello, BINUSIAN\n”);

}
// This program will simply print out a message
Character
• C program is written using ASCII character subset:
- Capital letters A…Z
- Lower Case a…z
- Digit 0…9
- Special characters ‘!’, ‘&’, ‘+’, ‘\’, ‘_’, etc.

• ASCII
American Standards Committee for Information Interchange
http://www.asciitable.com/
Identifier
• The naming mechanism for various element in a program such as: variable,
function, constant, etc.
• Started with a letter or underscore_
• It is case sensitive
• Maximum length is vary for every compiler
Example: Turbo 2.0 (DOS), max 32 characters
• Never use reserved word/keyword
(such as: for, while, if, main)
• Example:
name, x1, _total, cubic()
wrong: 1time, int
Keywords
• Some compilers will highlight keywords with distinct color, as seen
from the figure below;

Keywords in Visual
C++ use blue
color
Data Types
• In the C programming language, data types refer to an
extensive system used for declaring variables or functions of
different types. The type of a variable determines how much
space it occupies in storage and how the bit pattern stored is
interpreted.
• The types in C can be classified as follows:
Variable
• A variable is nothing but a name given to a storage area that
our programs can manipulate. Each variable in C has a
specific type, which determines the size and layout of the
variable's memory;
Variable
• Identifier for storing data/information
• Each variable has its name, address (L-value), type, size and data (R-value)
• Data or variable value can be modified at run time
• Declaration format:
<data type> <variable name>;
<data type> <variable name> = <initial value>;
• Example:
int a, b, c, total;
float salary, bonus;
int num_students = 20;
Variable
• Variable Declaration:
– Variable can be declared at every statement block
– Block statement or compound statement is statement exists
between { and } sign
– Example:
int x;
int y;
int z;

or:
int x, y, z;

or:

int x; int y; int z;


Variable
• Example:

char ch=65 address


Memory

ch 65 123456

Range of value:
name -128 – 127
value
Variable Examples
Data Type & Variable Example
include<stdio.h>
int main(){
int a = 50;
float b = 10.53f;
double c = 40.7;
char d = 'A’;
printf("%d | %.2f | %.3f | %c\n",a,b,c,d);
getchar();
return 0;
}
Character Constants
Input Operation: scanf() function
• scanf() function returns an integer that stated how many fields are successfully
assigned

• Example :
int x,y,z,w;
x = scanf("%d %d %d",&y,&z,&w);

– Input three values of integer 6 7 8, then x = 3;


– Input four values 6 7 8 9 then x = 3 (successfully assign 3 variables y z w)
Input Operation: scanf() function
• Format Type:

Type Used to scan


d integer
u unsigned integer
x hexadecimal
e, f, g floating point
C single character
s string ended with whit space
O data unsigned octal
[…] string ended with non of the value inside [...]
[^..] string ended with the value inside [...]
Input Operation: scanf() function
/* Program Calculating rectangle area v1*/
#include <stdio.h>
int main(){
int width, height, area;
scanf(”%d”,&width); scanf()
scanf(”%d”,&height); function
area = width * height; can use
return(0);
more than
}
one
argument

/* Program Calculating rectangle area v2*/


#include <stdio.h>
int main(){
int width, height, area;
scanf(“%d %d”,&width, &height);
area = width * height;
return(0);
}
Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical
Manipulations. C language is rich in built-in operators and provides the
following types of operators:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Misc Operators
Assignment Operators
• A binary operator

• Used in assigning value to an operand


Syntax:
Operand1 = Operand2;

• Left hand side operand (Operand1) should have (L-Value) such as variable

• Right hand side operand (Operand2) can be constant, another variable, expression
or function
Assignment Operators
• Example:
x = 2; // constant
x = y; // other variable
x = 2 * y; // expression
x = sin (y); // function

• Type of the result will follow the left hand side operand
int x = 7/2; /*x value is 3 not 3.5*/
float y = 3; /*y value is 3.000 */
Arithmetic Operators
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C language is rich in built-in
operators and provides the following types of operators:
Arithmetic Operators
• Modulo
– Symbol : %
– Binary operator
– To find reminder of a division
– N % 2, can be used to find an odd or even number
 N % 2 = 0  N is even
 N % 2 = 1  N is odd

• Increment and Decrement


– Symbol : ++(increment), --(decrement)
– Unary operator
– Increase (++) and decrease (--) the value of a variable by 1.
– Its position can be in the front (pre) or after (post) a variable.
Operators
Examples
Repetition: DO-WHILE
Example :
• Syntax : int counter=0;
do{ do {
printf( "%d ", counter );
< statements >; counter++;
} while(condition); } while (counter <= 10);

• Keep executing the statements while condition is true


• condition evaluation done after executing the statement(s)
Repetition: WHILE
• Syntax : #include<stdio.h>
void main() {
while(condition){ int x = 1;
while (x<=10)
< statements >; {
} printf( "%d ", x );
x++;
}
• Keep executing the statements while condition is true }

• condition evaluation done before executing the statement(s)


Exercise
1. Create a program to decide, is the input number is an even or
odd number.

2. Create a program to print number sequence from 5 to 15


(multiples of 3) with repetition
3. Create a program to print table for 8 in one column.

4. Create a program from user input an integer and print out that
integer’s times table.
• 5. Create a program that will ask the user to give three integers.
Call these integers start, step_by and stop.
Another Implementation of C Programs

You might also like