0% found this document useful (0 votes)
52 views

C Tutorial

The document provides an outline of topics for a C/C++ tutorial, including "Hello World" programs, data types, variables, functions, pointers, conditionals, loops, arrays, strings and more. Sample code is given to demonstrate many of the concepts, such as using printf() to output text, performing arithmetic operations, declaring variables, basic syntax of if/else statements and for/while loops, and defining and calling functions. The document serves as an introduction to many fundamental aspects of C/C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

C Tutorial

The document provides an outline of topics for a C/C++ tutorial, including "Hello World" programs, data types, variables, functions, pointers, conditionals, loops, arrays, strings and more. Sample code is given to demonstrate many of the concepts, such as using printf() to output text, performing arithmetic operations, declaring variables, basic syntax of if/else statements and for/while loops, and defining and calling functions. The document serves as an introduction to many fundamental aspects of C/C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

C/C++ Tutorial

Outline
 “Hello World" Program  Pointers
 Data Types & Variables  Functions
 printf()  Command-Line Argument
 Arithmetic & Logical  Data Structure
Operations  Memory Allocation
 Conditionals  Programming Tips
 Loops  C vs. C++
 Arrays & Strings  Books recommended
Hello World Program

 Code program
#include <stdio.h>
Void main() //int main()
{
printf("Hello World\n");
//return(0);
}
Struktur Program Bahasa C/C++
 Include <file.h>
 Fungsi Main / Void main()
 Kurung Kurawal buka ({)
 Definisi / Deklarasi variable
 Statment
 Komentar
 Kurung Kurawal tutup (})
#include<stdio.h> // include

void main() // fungsi main


{ // kurung buka
int jumlah; // deklarasi variable
float harga_per_satuan,harga_total;

jumlah=50; // statement (perintah)


harga_per_satuan=15.7;
harga_total=jumlah*harga_per_satuan;
printf("harga total = %6.2f\n",harga_total);
} // kurung tutup
Data types

Name Description Size* Range*


char Character or small 1 byte signed: -128 to 127
integer unsigned: 0 to 255
short int Short integer 2 bytes signed: -32768 to 32767
(short) unsigned: 0 to 65535
int Integer 4 bytes signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
long int Long integer 4 bytes signed: -2147483648 to
(long) 2147483647
unsigned: 0 to 4294967295
float Floating point 4 bytes 3.4e +/- 38 (7 digits)
number
double Double precision 8 bytes 1.7e +/- 308 (15 digits)
floating point number
long Long double 8 bytes 1.7e +/- 308 (15 digits)
double precision floating
point number
 Deklarasi Variable
int length = 100;
char num = ‘9’; //The actual value is 57
float deposit = 240.5;
unsigned short ID = 0x5544;

Try the following statements, and see what happens


unsigned char value = -1;
printf(“The value is %d \n”, value);

unsigned char value = 300;


printf(“The value is %d \n”, value);
Result

Definition Memory layout Display comment

unsigned char 11111111 255


value = -1
unsigned char 00101100 44 overflow
value = 300
Variable types
 Local variable
Local variables are declared within the body of a function, and can
only be used within that function.

 Static variable
Another class of local variable is the static type. It is specified by the
keyword static in the variable declaration.
The most striking difference from a non-static local variable is, a static
variable is not destroyed on exit from the function.

 Global variable
A global variable declaration looks normal, but is located outside any
of the program's functions. So it is accessible to all functions.
 An example

int global = 10; //global variable

int func (int x)


{
static int stat_var; //static local variable
int temp; //(normal) local variable
int name[50]; //(normal) local variable
……
}
printf()
The printf() function can be instructed to print
integers, floats and string properly.
 The general syntax is
printf( “format”, variables);

 An example
int stud_id = 5200;
char * name = “Mike”;
printf(“%s ‘s ID is %d \n”, name, stud_id);
 Format Identifiers
%d decimal integers
%x hex integer
%c character
%f float and double number
%s string
%p pointer

 How to specify display space for a variable?


printf(“The student id is %5d \n”, stud_id);
The value of stud_id will occupy 5 characters space in the
print-out.
 Why “\n”
It introduces a new line on the terminal screen.

escape sequence

\a alert (bell) character \\ backslash


\b backspace \? question mark
\f formfeed \’ single quote
\n newline \” double quote
\r carriage return \000 octal number
\t horizontal tab \xhh hexadecimal number
\v vertical tab
Arithmetic Operations
Arithmetic Assignment Operators
Increment and Decrement
Operators
awkward easy easiest

x = x+1; x += 1 x++

x = x-1; x -= 1 x--
Example
 Arithmetic operators
int i = 10;
int j = 15;
int add = i + j; //25
int diff = j – i; //5
int product = i * j; // 150
int quotient = j / i; // 1
int residual = j % i; // 5
i++; //Increase by 1
i--; //Decrease by 1
 Comparing them
int i = 10;
int j = 15;
float k = 15.0;

j/i=?
j%i=?
k/i=?
k%i=?
 The Answer
j / i = 1;
j % i = 5;
k / i = 1.5;
k % i It is illegal.
Note: For %, the operands can only be integers.
Logical Operations
 What is “true” and “false” in C
In C, there is no specific data type to represent “true” and “false”. C
uses value “0” to represent “false”, and uses non-zero value to stand
for “true”.

 Logical Operators
A && B => A and B
A || B => A or B
A == B => Is A equal to B?
A != B => Is A not equal to B?
A >B => Is A greater than B?
A >= B => Is A greater than or equal to B?
A <B => Is A less than B?
A <= B => Is A less than or equal to B?

 Don’t be confused
&& and || have different meanings from & and |.
& and | are bitwise operators.
Short circuiting
 Short circuiting means that we don't
evaluate the second part of an AND or OR
unless we really need to.

Some practices
Please compute the value of the following
logical expressions?
int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) =>
if( i != j || k < j) =>
if( j<= k || i > k) =>
if( j == k && m) =>
if(i) =>
if(m || j && i ) =>
int i = 10; int j = 15; int k = 15; int m = 0;
if( i < j && j < k) => false
if( i != j || k < j) => true
if( j<= k || i > k) => true
if( j == k && m) => false
if(i) => true
if(m || j && i ) => true

Did you get the correct answers?


Conditionals
 if statement
Three basic formats,
if (expression){
statement …
}
if (expression) {
statement …
}else{
statement …
}
if (expression) {
statement…
} else if (expression) {
statement…
} else{
statement…
}
 An example
if(score >= 90){
a_cnt ++;
}else if(score >= 80){
b_cnt++;
}else if(score >= 70){
c_cnt++;
}else if (score>= 60){
d_cnt++
}else{
f_cnt++
}
 The switch statement
switch (expression) 
{
case item1:
statement;
break;
case item2:
statement;
break;
default:
statement;
break;
}
Loops
 for statement
for (expression1; expression2; expression3){
statement…
}
expression1 initializes;
expression2 is the terminate test;
expression3 is the modifier;
 An example
int x;
for (x=0; x<3; x++)
{
printf("x=%d\n",x);
}

First time: x = 0;
Second time: x = 1;
Third time: x = 2;
Fourth time: x = 3; (don’t execute the body)
 The while statement
while (expression) {
statement …
}
while loop exits only when the expression is false.

 An example
int x = 3;
while (x>0) {
printf("x=%d n",x);
x--;
}
for <==> while
for (expression1; expression2; expression1;
expression3){ while (expression2)
statement… {
} equals statement…;
expression3;
}
Arrays & Strings
 Arrays
int ids[50];
char name[100];
int table_of_num[3][4];
 Accessing an array
ids[0] = 40;
i = ids[1] + j;
table_of_num[2][3] = 100;
Note: In C Array subscripts start at 0 and end one less than
the array size. [0 .. n-1]
 Strings
Strings are defined as arrays of characters.
The only difference from a character array is, a symbol “\0”
is used to indicate the end of a string.

For example, suppose we have a character array, char


name[8], and we store into it a string “Dave”.
Note: the length of this string 4, but it occupies 5 bytes.

D a v e \0
Functions
Functions are easy to use; they allow complicated programs to be broken
into small blocks, each of which is easier to write, read, and maintain.
This is called modulation.

 How does a function look like?


returntype function_name(parameters…)    

local variables declaration;  
function code;
return result;  
}
 Sample function
int addition(int x, int y)
{
int add;
add = x + y;
return add;
}
 How to call a function?
int result;
int i = 5, j = 6;
result = addition(i, j);
Thanks

You might also like