• No results found

Chapter 2

N/A
N/A
Protected

Academic year: 2022

Share "Chapter 2"

Copied!
42
0
0

Loading.... (view fulltext now)

Full text

(1)

Chapter 2

Basic C Programming I

(2)

C Structures

(3)

Language Basics

Source Files

Case Sensitive

Token

Lexical & Syntax Grammar

Comments

/* This is my comment */

// This is my comment

ASCII

(4)

Character Sets

Letter (52)

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

a b c d e f g h I j k l m n o p q r s t u v w x y z

Decimal Digits (10)

0 1 2 3 4 5 6 7 8 9

Punctuation Marks (29)

! “ # % & ‘ ( ) * + , - . / : ; < = > ? [ \ ] ^ _ { | } ~

White Space Characters (5)

space, horizontal tab, vertical tab, new line, and form feed

(5)

ASCII

(6)

Reserve Word

Reserve Word is prohibited to use in the program.

auto, break, case, char, const, continue, default, do, double, else, enum, extern, float, for, goto, if, int, long, register, return, short, signed, sizeof, static, struct, switch, typedef, union, unsigned, void, volatile, while

(7)

Example: reserved words in vc

In most recent compilers,

reserved words are shown in a different colour.

(8)

Separators

Parentheses ( )

Braces { }

Bracket [ ]

Semicolon ;

Comma ,

Full Stop .

(9)

Main Function

main part of all C programs.

indicates the starting point of the program.

main () is a compulsory part for all c program

It may be seen as void main() or int main()

main() {

/* Your Codes */

}

(10)

Identifier

The assigned name of any variables or function

which can be given by either programmer or can be standardly assigned by the compilers.

▪ Standard Identifier

is a standard identifier

which can be obtained by calling the library name. It can be different depending on the company of the

compilers.

(11)

Example of a standard identifier

#include<stdio.h>

void main() {

printf(“Hi”);

}

(12)

Example (the most) Standard Identifier

printf()

#include <stdio.h>

Syntax

printf(const char *format[,argument,…]);

Example

printf(“Hello World!”);

Library Identifiers

Usage

(13)

Example

#include<stdio.h> //1. library

void main(){ //2. start of the program

printf(“Welcome to EGCI111”); /*3. command

printf for displaying on screen */

}

Welcome to EGCI111

Example 1: Your first program

The program result

Source code

(14)

Semicolon(;)

Semicolon is required in all command(identifiers).

A few place that do not require semicolons

1. Preprocessor Directive

2. After the ‘{‘ sign

(15)

Escape Sequence

(16)

Example II

#include<stdio.h>

main(){

printf(“Welcome to EGCI111\n”);

printf(“Programming\t”);

printf(“with \”C\” language.”);

}

Welcome to EGCI111

Programming with “C” language

.

(17)

Example III

(18)

Variables

All data must be stored in a memory for operating

Number of memory used must be specified in advance.

Different data types use different of amount of memory

(19)

Variables

Variables are used for storing data

Different types of variable use different amount of storage

(20)

Declaration of Variables

Syntax

variable_type variable name,[variable_name];

Example

char c;

float x,y,z;

int a = 0;

(21)

Declaration of Variables (Cont.)

Variable name restrictions

Do not use Reserve Word or Standard Identifier

Do not begin with a number e.g. int 1X;

Do not use white space e.g. int X 1;

Do not use any punctuations except _ e.g.

⚫int x+1;

(22)

Printing variables

printf(“……”); will print whatever inside the “ ”

Printing data that are stored in variables needs a trick!!

Specifies are used to locate where to print from the variables

“%” is a symbol to indicate specifies e.g. printf(“I want %d”,x);

%d is the location where the value in a variable ‘x’ will be shown.

(23)

Formatted Output & Flags (Cont.)

(24)

Expressions & Operators

(25)

Mini lab1

1. Write a program to receive w and h of a rectangle.

Calculate the area of the rectangle.

Make w and h be variables.

Make a as another variable for area. (a=w*h)

Display the output

Make your program easy to understand.

(26)

Mini lab2

1. Modify the previous minilab to calculate the area of a triangle.

2. Number will be rounded, so do not worry.

Make your program easy to understand.

(27)

Using scanf() for getting input data

(28)

Using scanf (common mistakes)

Using scanf

Always use ‘&’ before your variable names.

(It refers to the location of the value to be inputted in)

The format of input SHOULD be exactly the same as format declare in scanf

e.g. scanf(“%d %d”,&a, &b);

// the input should be 2 integers separated by a space

(29)

Example of scanf()

(30)

LAB1

1. Write a program to receive w and h of a rectangle.

Calculate the perimeter of the rectangle.

2. Input 5 numbers, find an average of all 5 numbers. The

program always rounds the number down, so can make it round up if the decimal is more than 0.5

Make your program easy to use and easily interpreted.

(31)

Preprocessor Directive

Directive Define

#define constant_name value Example

#define KMS_PER_MILE 1.609

Directive Include

#include <standard_lib>

#include “header_file”

Example

(32)

Constants

Two methods of declaring a constant

Through Preprocessor Directive

#define KMS_PER_MILE 1.609

Through a reserve word: const

const float KMS_PER_MILE = 1.609

(33)

Integer

v.s

Float /double

Type Integer Float Double

Argument type int float double

Specifier %d, %i , etc. %f %f ,%lf

Specifier for scanf %d, %i , etc. %f %lf

Description Natural numbers No decimal digits are allowed

Number with decimal digits

Long float

Bits used 2/4 bytes 4 bytes 8 bytes

(34)

Printing integer and float

int a=200;

float b=10.5;

printf(“%d %f”,a,b);

(35)

Justified Printf

int a=200;

float b=10.5;

printf(“%d%f”,a,b);

printf(“%4d%5.2f”,a,b);

20010.500000

200 10.50

printf(“%d %.2f”,a,b); 200 10.50

printf(“%8d%9.2f”,a,b); 200 10.50

(36)

More Formatted Output & Flags

String

(37)

Example

char sex=‘F’;

char name[]=“Mingmanas”;

printf(“Name: %s\nsex: %c”,name,sex);

Name: Mingmanas sex: F

(38)

Justified printf v.s. tab(\t )

int x=5,y=7;

Using justified

printf("%13s%9d%8d\n","Hello",x,y);

printf("%13s%9d%8d\n","Hello Kitty",x+10,y+6);

Using tab

printf("\t%s\t%d\t%d\n","Hello",x,y);

printf("\t%s\t%d\t%d\n","Hello Kitty",x+10,y+6);

(39)

printf() Example

(40)

Expressions & Operators

(41)

LAB

Qty Price

====================

Orange 8 214.0 Banana 150 1444.5

Apple 29 372.4

====================

Sum 187 2030.9 Average 62.33 676.97

#define O_Price 25

#define B_Price 9

#define A_Price 12

(42)

Hint

Do not display the word directly, use %s to align all the words together.

References

Related documents

The proposed Intrusion Detection System (IDS) provides a network administrator with a comprehensive visualization of the network traffic.. Thus, the network manager can supervise

„ Wang AIDS 2005, Wines Drug Alcohol Depend 2007 Sporer 2006,. http://www.whitehousedrugpolicy.gov/news/fentnyl%5Fheroin%5Fforum ,

short signed 16-bit integer int signed 32-bit integer long signed 64-bit integer. float 32-bit floating point (IEEE 754-1985) double 64-bit floating point

Use a function are still apply to query parameter object are now is a variable inside curly brackets can save it than array declaration for const javascript tips in which

Variable declaration int i,j,k; long p,q; short s; unsigned u; float r; double dr; long double lr; char c, text[10];. MA511:

if ((connect(sockfd, (struct sockaddr *) &amp;their_addr, sizeof their_addr)) == -1) { perror(&#34;connect&#34;);.

Calculate the density of the concrete used to prepare the BALL, considering the mass and the volume of the BALL (the volume is the one calculated in Step 1).. The value

Another example of a group therapy that is based on connecting the client with the emotional decisions made in childhood that effect present-day behavior is Redecision Therapy