Programming Introduction - C Language
Programming Introduction - C Language
(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
• Portability
Used form micro computer to super computer
OR
Online
Compiler
https://www.onlinegdb.co
DEV C++ m/online_c_compiler
main() main()
1. { 3. {
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
• 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.
int x;
int y;
int z;
or:
int x, y, z;
or:
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';
• Example :
int x,y,z,w;
x = scanf("%d %d %d",&y,&z,&w);
• 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
• 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
• Syntax :
Example :
do{ int counter=0;
< statements >; do {
printf( "%d ", counter );
} while(condition); counter++;
} while (counter <= 10);
• Syntax :
#include<stdio.h>
while(condition){ void main() {
int x = 1;
< statements >; while (x<=10) {
printf( "%d ", x );
} }
x++;