0% found this document useful (0 votes)
85 views43 pages

Programming Introduction - C Language

Uploaded by

Muhammad Rafif
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)
85 views43 pages

Programming Introduction - C Language

Uploaded by

Muhammad Rafif
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/ 43

Programming Introduction

(C Language)
First Year Program (FYP) Binusian 2024
Introduction
• Preparation to COMP6047 - Algorithm and
Programming course at 1st semester.

• Textbook:
C : How to program :with
an introduction to C++
Paul Deitel & Harvey Deitel . 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 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


How to Start with C

OR
Online
Compiler
https://www.onlinegdb.co
DEV C++ m/online_c_compiler

NOTE : Please use this platform to explore each of code


in this session. You need to try to run the code, and repair
it if needed.
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()
1. { 3. {
statements; statements;
} return(0);
}

void main() int main()


2. { 4. {
statements; statements;
} return(0);
}

Standard C
Compiler
C Structure
• Directive #include generally written at the beginning of a
program
• Coding Style (depends to the programmer)

#include<stdio.h>
int main()
{
printf (“Welcome to BINUS\n”);
return (0);
}

#include<stdio.h>
int main(){
printf (“Welcome to BINUS\n”);
return (0);
}
Hello World
Comments in C
• 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
Identifier
• Keywords/reserved words are words that have special
meaning to the C compiler.
• Example:
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
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:


Variables

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
Examples
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(”%d”,&height);
area = width*height;
return(0);
} scanf()
function
/* Program Calculating rectangle area v2*/ can use
#include <stdio.h>
int main(){ more than
int width, height, area; one
scanf(“%d %d”,&width, &height); argument
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.
Examples
Relational Operators
Example
Decision Making in C

Decision making structures require


that the programmer specify one or
more conditions to be evaluated or
tested by the program, along with a
statement or statements to be
executed if the condition is
determined to be true, and
optionally, other statements to be
executed if the condition is
determined to be false.
if statement

If the Boolean expression evaluates


to true, then the block of code
inside the if statement will be
executed.

If Boolean expression evaluates to


false, then the first set of code after
the end of the if statement (after
the closing curly brace) will be
executed
if...else statement

If the Boolean expression evaluates


to true, then the if block of code
will be executed, otherwise else
block of code will be executed.
Logical Operators
Repetition: DO-WHILE

• Syntax :
Example :
do{ int counter=0;
< statements >; do {
printf( "%d ", counter );
} while(condition); counter++;
} while (counter <= 10);

• Keep executing the statements while condition is


true
• Condition evaluation is done after executing the
statement(s)
Repetition: WHILE

• Syntax :
#include<stdio.h>
while(condition){ void main() {
int x = 1;
< statements >; while (x<=10) {
printf( "%d ", x );
} }
x++;

• Keep executing the statements while condition is true


• Condition evaluation is done before executing the
statement(s)
Exercise

1. Create a program to decide whether the inputted


number is even or odd.
2. Create a program to print number sequence from 5 to
15 (multiples of 3) with repetition
3. Create a program to print square shape with “*”
according to user input. Example :
user input : 4
****
****
****
****
Exercise

4. Create a program to print table for 8 in one


column.

5. Create a program that has input from user in


form of an integer and it will print multiple of the
integer.
Exercise

6. Create a program that will ask the user to input 3


integers. The inputted numbers will be the starting
point, distance between and stopping point
sequentially. 

You might also like