C Uptoweek3
C Uptoweek3
C Uptoweek3
C Programing
Tr.Pwint Phyu Shwe
Envioronment
• http://www.Cygwin.com
• http://cygwin.com/cygwin-ug-net/cygwin-ug-net.html
• PATH %PATH%;c:\cygwin\bin
Standard
• Preprocessor
• Library/header
#include <stdio.h>
• Entry
int main(){return 0;}
main(){}
Escape Sequence
Escape Sequence Purpose Example
\n Create new line
\t Create tab
\r Move cursor to the beginning of current
line
\\ Insert backslash C:\
\” Double quote ”This is sample”
\’ Single quote Susan’s house
Example:
printf("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
printf("\t\t\t\t1\t2\t3\n");
printf("4\t5\t6\t7\t8\t9\t10\n");
Compilation
compiler
• gcc <filename.c> => default a.out
• gcc <programName.c> –o <executableName.out> => executableName
• gcc <programName.c> –o <executableName> => executableName
Run Program
• ./a.out
• executableName
Primary Data Type
Data Types
Integers – int , short , long
Floating Point – float, double
Character – char
0x001
Declaration
<data types> < variable name> = <value>
int x;
Assignment
int x = 10;
Print content
int x;
float y; Placeholder
char c;
%d
%f
x = -44;
%c
y = 10.33;
c = ‘z’; %.2f
int %d
char %c
float %f
double %lf
unsigned int %u
signed char %c
unsigned char %c
memory address %p
fgets
fgets() is a library function in C. It reads a line from the specified stream and stores it into the string pointed to
by the string variable. It only terminates when either:
• end-of-file is reached
• n-1 characters are read
• the newline character is read
int x;
char str[100];
scanf("%d", &x);
fgets(str, 100, stdin);
printf("x = %d, str = %s", x, str);
Constant
• const
can’t change through out program
const float PI = 3.14
Arithmetic in C
X = 10, y=3, z = 2
Symbol Operation
+ x+y 13
- x-y 7
* x*y 30
/ x/y 3.333
% x%y 1
Precedence
1. */ %
2. +-
Char Array
• Characters array Dynamic array using Pointer
char myString[5] = {‘J', ‘a', ‘m', ’e’, ’s’, '\0'};
char myString[] = “James”;
Pointer Character (To explain details in next chapert)
char *myString = ”James";
case 10:
X*=10;
break;
default: printf(“there is no value relevant”);
}
Begin
yes
encrypt Do encrypt
No
yes
decrypt Do decrypt
No
yes
encode Do encode
No
yes
decode Do encode
No
Error
Week - 2
Function
• Function is used for code reusability
• Dedicated activities conduct in each function block
account
User design
Function - call
• Funcation can be call multiple times from calling program
• Function can have muliples
• Empty Function – display()
• Function with one or more arguments/parameters – deposit(double)
• Function with no return - void
• Function with return – datatype (int, bool, char, double)
Variable Scope
• Global Scope
• Share data between and across functions. Global variables are created and defined outside any functions
• Local Scope
• local scope variables are tied to their originating functions, you can’t reuse variable names in other functions without running the risk
of overwriting data. Local variables are created and defined inside function
Array
• Supports to build collections of same data type.
• Share same variable with different index
• Each value inside index is called element
• can be declared to various data type (int, float, short, char)
index
memory addr
COLUMN
[0][0] [0][1] [0][2]
int iTwoD[3][3] = { {10, 12, 18},
R 10 12 18 {21, 23, 22},
O {20, 30, 40} };
W [1][0] 21 23 22
*ptr = 20;
Week 3
Initialization Pointer
int *ptrSample = NULL;
int *ptrSample = 0;
int x = 10;
int* ptrSample = &x;
As practice: pointer must be initialized with another variable’s memory address or with 0 or NULL. Not initializing
may result invalid data or invalid expression.
Array with Pointer
Array variable name holds the address of first index.
For example:
variable num holds the address of first index and it is constant value.
num[0] 7 2290xF
Variable name
num[1] 20 229012
index
Example: memory addr
num[0] 7 2290xF
Variable name
num[1] 20 229012
index
memory addr
S u s a n \0
J o s e p h \0
P h i l i p \0
Convert String to Number
• Under stdlib.h
• string to floating point - atof
• string to number - atoi