2 Introduction To C Programming Language
2 Introduction To C Programming Language
2 Introduction To C Programming Language
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.
3 4
5
7
8
9
10 11
Starting The Dev C++ Application
}
// 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:
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);
• 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
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