C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int...
-
Upload
antonia-annice-morton -
Category
Documents
-
view
221 -
download
5
Transcript of C Programming Laboratory I. Introduction to C Language /* the first program for user */ #include int...
C Programming
Laboratory I
Introduction to C Language
/* the first program for user */#include <stdio.h>int a=0;int main(void){
printf(“Hello World\n”); return a;
}
CommentHeader fileDecaration
Program Body
Comment
Do not want to compile this section There are 2 forms
/* comment part */
// comment part used only borland C compiler
/* This is comment *//* For produce the good program, should Write comment every 10-15 line of program *///Or use line comment for Borland C
Preprocessing Directives (#)
Use for inform compiler to do something before compiling
#define #endif #error#ifndef #elif #if#include #else#ifdef#pragma #undef
#define Directive
Define word expression or command
#define <name> <value|expression>
Example #define TEN 10#define PI 3.141592654#define R 2*PI#define MUCH 3*6+5 \
-(7*3)
#include Directive
Read the according files to co-compile
#include <filename>#include “filename”
Example #include <dos.h>#include “sample.c”
Structure of module
Program, written by C consisted of modules
Main() is the principle module One program have to have one main()
Module-type module-name(argument){
a sequence of commands;}
Identifiers
Definition of all of name such as variable
1. Keywords
2. User-defined word
Auto default float register struct volatileBreak do for return switch whileCase double goto short typedefChar else if signed unionConst enum int sizeof unsignedContinue extern long static void
Rule of User-defined word
Case sensitive Character “_” Number First with Character or “_” Have to define before use Define in any command or expression Don’t same as KeyWord
Type of Identifiers
Global Identifiers : define external a set of command
Local Identifiers : define internal a set of command
A
B
DATA TYPE
DATA TYPE Size
char -128 to 127 1 Byte
unsigned char 0 to 255 1 Byte
int -32768 to 32767 2 Byte
unsigned int 0 to 65535 2 Byte
long -2147483648 to 2147483647 4 Byte
unsigned long 0 to 4 Byte
float -3.4E+38 to 3.4E+38 4 Byte
double 1.7E+308 8 Byte
void No-specified / No-Value return -
constant
Integer constant
Floating constant
Character constant
Interger : 5 10 -12 75Long int : 100000LHex Form : 0x7a 0xfe 0x100
0.0e0 : 3.2e50.0E0 : 3.4E-8
‘A’ ‘b’ ‘#’
Variables
Name for calling the value in memory Format for define variables
<DATA-TYPE> <VAR-NAME>[,<VAR-NAME>];
Examplechar a,b,c;unsigned int d;
Expressions
Expressions in form
operand1 operator operand2
Operator Meaning Expression Result
+ Add 7 + 3 10
- Substract 7 – 3 4
* Multiple 7 * 3 21
/ Divide 7 / 3 2.333
% Modulate 7 % 3 1
Statements
The complete command to instruct computer to do something
Enclose with ; (semi-colon) Blank space is not important
Assignment Statements
following Form
It mean calculate expression in right side and assign in variable in left side
<Variable> = Expression;
Input & Output
Character Code String Output Statement Input Statement
Character Code
Character represent to computer in number,called ASCII Code ( American Standard Code for Information Interchange) 128 characters,7 Bits
Extended ASCII Code 128 characters for special character such as Greek-Latin character, including Thai font
Big program occurred when using more than 2 language or the language require more character such Chinese Language.
Represent enclose with ‘ and ’ (qoute symbol)
‘a’ ‘4’ ‘\0’ ‘\n’
STRING
A sequence of characters Represent enclose with “ and ” (double qoute)
“Hello world!”
Output Statement
command present string on monitor
printf(charformat[,argument]);
Exampleprintf(“Hello World!”);a = 12;
printf(“Hello! Number%d”,a);
Output Statement
Type this program
#include<stdio.h> /* Call module stdio.h */void main(void) /* Main function*/{
char *msg = “ชื่��อของนั�กศึ�กษา \n”;int id = รหั�สนั�กศึ�กษา 4 ตั�วท้�าย ;printf(”%s student-code = %d \n”,msg,id);
}
Input Statement
Command get value from keyboard
scanf(char-format[,address]);
#include<stdio.h>void main(void){ int date,month,year; printf(“Please enter date in dd/mm/yy :”); printf(“date :”); scanf(“%d”,&date); printf(“month:”); scanf(“%d”,&month); printf(“year :”); scanf(“%d”,&year); printf(“Date: %.2d/%.2d/%.2d\n”,date,month,year);}
งานัปฏิ�บั�ตั�การ 5
จงเขี�ยนโปรแกรมร�บค่�าค่วามยาวฐานขีองสามเหลี่��ยมแลี่ะค่วามส�งขีองสามเหลี่��ยมแลี่�วน�ามาประมวลี่ผลี่หาค่�าพื้ !นที่��ขีองสามเหลี่��ยมInput Base Length : 10Input Height : 5Output Area of Triangle = 25