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

2 Structureofcprogramming

Uploaded by

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

2 Structureofcprogramming

Uploaded by

muhdfaridmfd0
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

C language

- Structure programming language


- Programmer able to focus on problem rather
than machine that program will be execute
- Able to run at various hardware platform
C program consists of….
Structure Example code Explanation
Header #include<stdio.h> To call library code
main() int main() Entry point of program
{ (standard before start to write code)
Variable int a=5; Define variable a with value of 5
declaration
Body printf(“I love c programming”); To display massage
Return return 0; 0 is the standard for the “successful
} execution of the program”.
Example greeting program

• Task: Display “I love c programming!!!”


Structure C programming

Preprocessor directive (e.g. call library)

Global declaration

Local declaration, value still can change

Body
function
statement
Software and basic execution

• File>new>source file
• Save the file (any
name)
• Write the code
• Execute>complie
and run (F11)
Write Comments in your code
• Character that are not executed as part of the program.
• Use for remark or debug the code
• Syntax:
/* …. Comments…..
Comment……………..*/ block comments
//…. Comment  line comments (for 1 line only)
Example: write your own notes
/*…*/
Exercise:

• Write a program to display the following statement:

I love c programming……..my name is syafiq


My matric no is CD123456
Preprocessor directives
Directive Function
#include To include files/code (e.g. #include math.h>
#define To define global/macro variable (e.g. #define PI 3.142)
#undef To undefine global/macro variable (e.g. #undef PI )
#if Check a compile time condition
• Not the partSpecifies
#else of the compiler but it#ifisTest
alternatives when directive
fails to preprocessor
#ifdef Test for a macro definition
#ifndef Check whether a macro is defined or not
Header file..

• #include – instruction to compiler to make it do something, it


tells to compile to include contents of file.
• #include<stdio.h>- standard library definition file for all standard
input/output.
• #include<conio.h>- name for all console input/output function’s, e.g.
printf(); scanf()
• #include<math.h> - used to all mathematical expression like sin cos,
log…etc
Identifiers (define variable)
One feature present in all computer languages is the
identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address.

Examples:
Y = m*x+c , here we have 4 variables
Fx = a + b, here we have 3 variables
Total = 12 + ab, here we have 3 variables
Rules for Identifiers
1. First character must be alphabetic character or underscore

2. Allowed to consist only alphabetic characters, digits or underscores

3. Commas (,) or blank spaces are not allowed within an identifier.

4. First 63 characters of an identifier are significant (a-z; A-Z, 0-9)

5. Cannot duplicate a keyword (reserved word).

6. Identifiers are also case sensitive in C (e.g name and Name are two different
identifiers in C).

7. No special characters, such as semicolon, period, whitespaces, slash or comma


are permitted to be used in or as Identifier (e.g. ~, @, %, *)
Reserved word
EXERCISE 1.0

Determine whether the following identifiers is valid or invalid.


Give reason for invalid cases.

1) int Parit Raja

2) int 20thCentury

3) int char

4) long float INTEGER

5) int _BMW2003

6) int Reservedword

7) float BDA24202

8) long int markah_pelajar

9) char jam*kredit

10) double printf


EXERCISE 2.0

Invalid identifier Invalid reason(s)


1Letter
double
Two*four
Joe’s
void
Variables
Variables are named memory locations that have a type,
such as integer or character, which is inherited from their
type. The type determines the values that a variable may
contain and the operations that may be used with its
values.

Topics discussed in this section:


Variable Declaration
Variable Initialization
Examples of Variable Declarations and Definitions
• int new;
• float maxItems; //word separator: capital
• double max_items; // word separator: underscore
• int new, old, great; //combination of same type variables
• int loadA=30, loadB=15; //initialize the variables
Note
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.

Initialise variable with data:


Integer  int x =32;
Decimal  float y = 3.142;
Character  char abc=‘C’;

20
How to insert variable data into printf(“ “);
Variable type Define variable (example) Data symbol (general)
Integer int x; %d
int sum;
Decimal float number; %f
float no;
character char room; %c
char sample;
Example: e.g. to display massage with the data type

printf(“ %d or %f or %c”, defined variable)  for 1 data

printf(“ %d %d”, defined variable, defined variable)  for 2 data


Constant/ Escape Character

Assign between statement


Assign anywhere in a statement
Assign anywhere in a statement

Assign anywhere in a statement


Assign anywhere in a statement
Assign anywhere in a statement

Assign anywhere in a statement


Assign anywhere in a statement
Assign quote symbol
Assign double quote symbol
Assign after a statement (write note)
22
PROGRAM 2-4 A Program That Prints “Nothing!”

23
PROGRAM 2-2 Print Sum of Three Numbers

24
PROGRAM 2-2 Print Sum of Three Numbers (continued)

25
PROGRAM 2-2 Print Sum of Three Numbers (continued)

26
EXERCISE:
using C Programming software, develop a complete code to display below output.
Constants
Constants are data values that cannot be changed during
the execution of a program. Like variables, constants have
a type. In this section, we discuss Boolean, character,
integer, real, complex, and string constants.

Topics discussed in this section:


Constant Representation
Coding Constants
28
Memory Constants

29
PROGRAM 2-3 Memory Constants (continued)

30
Topics discussed in next section:
Streams
Formatting Input/Output (integer %d, float %f,
character %c)

31
FIGURE 2-15 Stream Physical Devices
32
General Type and Size of Data Type
Data type size Range Format
specifier
Character 1 byte -128 to 127 %c
Integer 2 byte -32768 to %d
32767
Float 4 byte 3.4E(-38) to %f
3.4E(+38)
Double 8 byte 1.7E(-308) to %f
1.7E(+308)

Long double 10 byte 3.4E(-4932) to %lf


3.4E(+4932)
DATA TYPES- SET OF VALUES THAT VARIABLE CAN STORE ALONG A
SET OF OPERATION THAT CAN BE PERFORMED ON A VARIABLE

FIGURE 2-18 Conversion Specification


34
FIGURE 2-17 Output Stream Formatting Example
35
scanf

FIGURE 2-20 Input Stream Formatting Example


36
ACSII CODE IN COMPUTER
PROGRAMMING
The full form of ASCII is the American Standard Code for information interchange.
It is a character encoding scheme used for electronics communication. Each
character or a special character is represented by some ASCII code, and each ascii
code occupies 7 bits in memory.

e.g. value 65 = ‘A’ in


integer character/symbol
ASCII

e.g. character ‘B’ =


character/symbol integer
66 in ASCII
American Standard Code for
Information Interchange, is
a character encoding standard
for electronic communication.

A character variable
holds ASCII value (an integer
number between 0 and 127)
rather than that character itself
in C programming. That value is
known as ASCII value. For
example, ASCII value of 'A' is 65.
PROGRAM 2-6 Print Value of Selected Characters

Computer Science: A Structured Programming Approach Using C 40


PROGRAM 2-6 Print Value of Selected Characters (continued)

41
PROGRAM 2-6 Print Value of Selected Characters (continued)

42
PROGRAM 2-6 Print Value of Selected Characters (continued)

Computer Science: A Structured Programming Approach Using C 43


#include <stdio.h>

int main()
{
char first=‘T’;
char second=63;
printf(“the first example as letter is %c\n”, first);
printf(“the first example as number is %d\n”, first);
printf(“the second example as letter is %c\n”, second);
printf(“the second example as number is %d\n”, second);

return 0;
}

Output
the first example as letter is T
the first example as number is 84
the second example as letter is ?
the second example as number is 63
Character input and character output

Write a program to read a single character from the keyboard and store it in
a variable of type char using the scanf( ) function with the format specifier
%c.

#include <stdio.h>

int main()

{
char ch=0;
scanf("%c",&ch);
printf("The character is %c and the code value is %d",ch, ch);

return 0;
}
Display Integer In Different Styles (- is space)
Note
Use single quotes for character constants. (%c)
Use double quotes for string constants. (%s)

Example:
printf(“I love %c %s\n",‘C', "programming");

I love C programming

Next section will detailing on how to use %c


Display float (%f) In Different Styles (- is space)

float j=12.345;
PROGRAM 2-7 Calculate a Circle’s Area and Circumference

51
PROGRAM 2-7 Calculate a Circle’s Area and Circumference (continued)

Computer Science: A Structured Programming Approach Using C 52


OUTPUT

Computer Science: A Structured Programming Approach Using C 53


FIGURE 2-22 Output Specifications for Inventory Report
Computer Science: A Structured Programming Approach Using C 54
PROGRAM 2-8 A Sample Inventory Report

Computer Science: A Structured Programming Approach Using C 55


PROGRAM 2-8 A Sample Inventory Report (continued)

56
THANK YOU

You might also like