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

C-Week1

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C-Week1

Uploaded by

info.bptrades
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Short Course

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

printf("value integer variable x is %d \n", x);


printf("value float variable y is %f \n", y);
printf("value char variable c is %c \n", c);
printf("value float with 2 decimal %.2f", y);
Print content – Character Array, Pointer
char* sch;
char sch[100];
sch = “Spring”.
printf (”value %s \n ", sch);

%p for Pointer Address


%h for hexadecimal
Input from screen - scanf
int ivar;
float fvar;
char cvar;

printf(“enter integer variable x value \n”);


scanf(“%d”, &ivar);
printf(“enter float variable y value \n”);
scanf(“%f”, &fvar);
printf(“enter char variable z value \n”);
scanf(“%c”, &cvar);
Data Type Format Specifier

int %d

char %c

float %f

double %lf

short int %hd

unsigned int %u

long int %li

long long int %lli

unsigned long int %lu

unsigned long long int %llu

signed char %c

unsigned char %c

long double %Lf


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";

Reading and Printing


0xff
Initialize
char myString[5] = {'\0’};
J a m e s \0
printf("Enter your name: ");
Data Retrieve
scanf(” %s", myString);
Condition
int x = 10;

- if..else switch (x){


- Nested if..else case 1: break;
- switch
case 2: break;

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

You might also like