Chapter 2
Basic C Programming I
C Structures
Language Basics
▪
Source Files
▪
Case Sensitive
▪
Token
▪
Lexical & Syntax Grammar
▪
Comments
▪
/* This is my comment */
▪
// This is my comment
▪
ASCII
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
ASCII
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
Example: reserved words in vc
In most recent compilers,
reserved words are shown in a different colour.
Separators
▪
Parentheses ( )
▪
Braces { }
▪
Bracket [ ]
▪
Semicolon ;
▪
Comma ,
▪
Full Stop .
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 */
}
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.
Example of a standard identifier
#include<stdio.h>
void main() {
printf(“Hi”);
}
Example (the most) Standard Identifier
printf()
#include <stdio.h>
Syntax
printf(const char *format[,argument,…]);
Example
printf(“Hello World!”);
Library Identifiers
Usage
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
Semicolon(;)
⚫ Semicolon is required in all command(identifiers).
⚫ A few place that do not require semicolons
1. Preprocessor Directive
2. After the ‘{‘ sign
Escape Sequence
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
.
Example III
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
Variables
▪ Variables are used for storing data
▪ Different types of variable use different amount of storage
Declaration of Variables
Syntax
variable_type variable name,[variable_name];
Example
char c;
float x,y,z;
int a = 0;
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;
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.
Formatted Output & Flags (Cont.)
Expressions & Operators
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.
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.
Using scanf() for getting input data
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
Example of scanf()
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.
Preprocessor Directive
Directive Define
#define constant_name value Example
#define KMS_PER_MILE 1.609
Directive Include
#include <standard_lib>
#include “header_file”
Example
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
Integer
v.sFloat /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
Printing integer and float
⚫
int a=200;
⚫
float b=10.5;
⚫
printf(“%d %f”,a,b);
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
More Formatted Output & Flags
String
Example
char sex=‘F’;
char name[]=“Mingmanas”;
printf(“Name: %s\nsex: %c”,name,sex);
Name: Mingmanas sex: F
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);
printf() Example
Expressions & Operators
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
Hint
⚫ Do not display the word directly, use %s to align all the words together.