Prog101 Prelim

Download as pdf or txt
Download as pdf or txt
You are on page 52of 54
 
 
Learning Module on PROG101
 
Fundamentals of Computer Programming
PROG101
 
 
 
Learning Module on PROG101
STUDENT
Name: Student Number: Program: Section: Home Address: Email Address: Contact Number:
PROFESSOR
Name:  Academic Department: Consultation Schedule: Email Address: Contact Number:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

5
 
 
Learning Module on PROG101
LEARNING MODULE GUIDELINES
 At CCC-DCE you learn through self-instructional modules, otherwise known as DCE Self-Learning modules of DCEModL such as the one that you are now reading. It is significant that  you observe the following instructions: 1.
 
Every student or course has a total of three (3) modules, each one might contain two or more lessons. You must complete and satisfy the work prescribed by each module before  you can sit for the written examination that will determine if you pass or fail the course. 2.
 
Make an effort to do some extra reading on any reference book on the subject (online or offline) will do. 3.
 
Test your own progress by performing the enrichment activities/application provided by each lesson. Read the instructions very carefully and understand them well. 4.
 
Consultation will be entertained by your professors through Facebook Messenger from Monday to Friday (8-3pm) or as advised by your professors. Strictly follow the schedule of consultations. 5.
 
 At the end of each module, perform and accomplish the Assessment Test. 6.
 
Enrichment Activities/Application and Assessment Test with answers will be collected periodically (sealed in long brown envelope signed across the flap and properly label it with your name and year & section) and submit to the Office of the Director of the Department of Computer Education (DCE), Baretto St., Brgy VII, Calamba City, Laguna, for correction and grading. Corrected tests will be returned to you with the corresponding grade or further instruction, if needed. While waiting, go on to the next module and observe the same study procedure. 7.
 
When you have accomplished all the modules of the course you are enrolled, you have to come to CCC to take the written examination in person.
************************************ DISCLAIMER **********************************
No reproduction of any part of the module may be used, sold or distributed for commercial purposes or be changed or included in any other Colleges and Universities, work or publication, whether in print or electronic, including sending, publishing or incorporating them to another module unless prior permission has been granted. Modules are reviewed, evaluated and checked by:
Dr. Regina G. Almonte Ms. Jesse Ann Salvador Content Editor Technical Editor Faculty Member, DCE
 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

4
 
 
Learning Module on PROG101
LEARNING MODULE INFORMATION
I. Module Number
1
II. Module Title
Basics of C Programming
III. Scope of the Module
This module consists of 9 lessons, namely:
 
Lesson 1: Computer Programming Basics
 
Lesson 2: Overview of C Language
 
Lesson 3: The C Program Structure
 
Lesson 4: The Elements of C Programming
 
L
es
s
o
n
 
5
:
 
T
h
e
 
I
n
p
u
t
/
Ou
t
pu
t
 
Elem
en
t
 
Lesson 6: The Data Types
 
Lesson 7: The Variables, Constants and Literals
 
Lesson 8: The Operators
 
Lesson 9: The Expressions
IV. Overview of the Module
This module identifies the terminologies and important concepts of computer programming. Using C, the students will be guided to the basics of C programming language. Towards the end of this module, the use of C in solving a particular problem is clearly explained.
 VI. Module Outcomes
 After studying this module on the basics of C Programming Language, the students shall be able to:
 
Determine the basic terminologies used in computer programming.
 
Narrate the important key points in the history of C Language
 
Identify the important elements of C programming.
 
Create a simple C program to solve a particular problem.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
1 Learning Module on PROG101
 
LESSON 1. COMPUTER PROGRAMMING BASICS
Objectives:
 At the end of this lesson, you will be able to:
 
Install Turbo C/C++software.
 
Define and understand the basic programming terminologies.
Turbo C/C++ Installation
Several compilers are available for C, and you just have to download. To install the Turbo C software on your desktop, you need to follow following steps. For Desktop Personal Computers 1.
 
Download Turbo C or C++software. You can download Turbo C or C++from many sites (e.g. https://developerinsider.co/download-turbo-c-for- windows-7-8-8-1-and-windows-10-32-64-bit-full-screen/). 2.
 
Extract downloaded “Turbo C++ 3.2.zip” file.
 3.
 
Run “Setup.exe” file.
 4.
 
Follow the setup instructions. Below is the Window Mode of Turbo C/C++ Integrated Development Environment (IDE).

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
2 Learning Module on PROG101
 
For Tablet and Smartphones
 You can also use your tablets or smartphones to download from Playstore for android users or Appstore for iOS users. Download either of the following: 1.
 
Cfordroid 2.
 
Mobile C 3.
 
TurboCdroid
Compile and Run C program
Using Turbo C IDE, follow the steps: 1.
 
Open turbo C IDE, click on File and then click on New. 2.
 
Write the example below as it is.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
3 Learning Module on PROG101
 
3.
 
Click on compile or press Alt+f9 to compile the code. 4.
 
Click on Run or press Ctrl+f9 to run the code.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
4 Learning Module on PROG101
 
5.
 
Output of the program is below:
Basic Programming Terminologies
The following are the terminologies in computer programming:
 
Program. It is a set of commands or what we call as instructions.
 
Software/System/Application. It is a set of interrelated programs.
 
Programmer. He/she is the one who writes the program.
 
User. He/she is the one who uses the program written by the programmer
Programming Language
In writing a program, you must have the language that will serve as the medium between the human and the machine (computer) and this is what we call as Programming Language. A programming language is a set of grammatical rules for instructing a computer or any computing device to perform specific tasks. There are categories of programming language and these are:
 
Machine language where instructions are written in 0’s and 1’s.
 This language is used by the primitive machines.
 
Low-level language or assembly language where instructions are written in mnemonic codes/symbols.
 
High-level language are those instructions that have resemblance to our English language statements. This language has classifications:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
5 Learning Module on PROG101
 
o
 
Procedural language is a programming language that follows, in order, a set of commands. Examples of this language are BASIC, C, FORTRAN, Java, and Pascal.
o
 
Object-Oriented programming language is a way of writing a computer program which is using the idea of
“objects” to represent data and methods. Examples of this language are C++, Java, VB.NET, etc.
 
Translator Programs
Translator programs are programs that are utilized by a particular programming language to translate instructions written either in low-level language or high-level language. Machine language does not need any of these translator programs since its instructions are understood by the computers. There are three translator programs available for programming languages:
 
 Assembler. It translates program written in assembly language or low-level language into machine language. This serves as the fastest translator programs that we have so far.
 
Interpreter. It translates program written in high-level language into machine language one statement/command at a time. Programming languages that utilize interpreter are Python, Ruby, etc.
 
Compiler. It translates program written in high-level language into machine language. It scans the entire program and translates it as a whole into a machine code. Programming languages that utilize compiler are C, C++, Java,  VB.Net, etc.
Summary of the Lesson:
1.
 
Turbo C/C++program can be installed either on your desktop personal computers or smartphones.
2.
 
Program is a set of instructions and a software/system/application is a set of interrelated programs.
3.
 
Programming language serves as the communication between the programmer and the computing devices.
4.
 
Since the language that the computing devices can understand is machine language, other categories of programming language is utilizing the translator program to translate those instructions into machine language.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
6 Learning Module on PROG101
 
M1:L1 - Enrichment Activity
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________
Identify the following:
_______________1. A category of programming language where
instructions are written in 0’s and 1’s.
 _______________2. It is a set of grammatical rules for instructing a computer or any computing device to perform specific tasks. _______________3. These are programs that are utilized by a particular programming language to translate instructions written either in low-level language or high-level language. _______________4. It is a set of interrelated programs. _______________5. It translates program written in high-level language into machine language one statement/command at a time. _______________6. A category of programming language that is used by primitive machines. _______________7. It is a programming language that follows, in order, a set of commands. _______________8. It is the fastest translator programs. _______________9. It is a category of programming language where instructions are written in mnemonic codes/symbols. _______________10. Examples of this programming language are C++, Java, VB.NET, etc.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
7 Learning Module on PROG101
 
LESSON 2. OVERVIEW OF C LANGUAGE
Objectives:
 At the end of this lesson, you will be able to:
 
Define C programming.
 
Identify the reason of learning C programming.
 
Enumerate the important points in the history of C language.
 
Distinguish the applications of C programming.
 
Understand the phases of program execution in C. C is a powerful general-purpose programming language. C programming is an excellent language to learn to program for beginners like you.
 About C Programming
 
Procedural Language
 – 
 Instructions in a C program is executed step by step.
 
Portable
 – 
 You can move C programs from one platform to another, and run it without any or minimal changes.
 
Speed
 – 
 C programming is faster than most programming languages like Java, Python, etc.
 
Translator Program
 – 
 C uses compiler as its translator program.
 
General Purpose
 – 
 C programming can be used to develop operating systems, embedded systems, databases, and so on.
The need to learn C programming
It is important for you to understand the rationale of learning C programming. As a matter of fact, C helps you to understand the internal architecture of a computer, how computer stores and retrieves information. After learning C, it will be much easier to learn other programming languages like Java, Python, etc. Moreover, learning C is an opportunity to work on open source projects. Some of the largest open-source projects such as Linux kernel, Python interpreter, SQLite database, etc. are written in C programming.
History of C Language
The history of C programming language is quite interesting. C was originally designed for and implemented on the UNIX operating system on the DEC PDP-ll, by Dennis Ritchie. C is the result of a development process that started with an older language called BCPL. BCPL was developed by Martin Richards, and it influenced a language called B, which was invented by Ken Thompson. B led to the development of C in the 1970s. For many years, the de facto standard for C was the version supplied with the UNIX operating system. In the summer of 1983, a committee was established to create an ANSI (American National Standards Institute) standard that would define the C language. The standardization process took six years (much longer than anyone reasonably expected).

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
8 Learning Module on PROG101
 
The ANSI C standard was finally adopted in December 1989, with the first copies becoming available in early 1990. The standard was also adopted by ISO (International Standards Organization), and the resulting standard was typically referred to as ANSI/ISO Standard C. In 1995, Amendment 1 to the C standard was adopted, which, among other things, added several new library functions. The 1989 standard for C, along with Amendment 1, became a base document for Standard C++, defining the C subset of C++. The version of C defined by the 1989 standard is commonly referred to as C89. During the 1990s, a new standard for C was being developed. It was the 1999 standard for C, usually referred to as C99. In general, C99 retained nearly all of the features of C89. The C99 standardization committee focused on two main areas: the addition of several numeric libraries and the development of some special-use, but highly innovative, new features, such as  variable-length arrays and the restrict pointer qualifier. These innovations have once again put C at the forefront of computer language development. C89 is the version of C in widest use, it is currently accepted by all C compilers, and it forms the basis for C++.
 Applications of C Programming
C was initially used for system development work, particularly the programs that make-up the operating system. C was adopted as a system development language because it produces code that runs nearly as fast as the code written in assembly language. Some examples of the use of C are -
 
Operating Systems
 
Language Compilers
 
 Assemblers
 
Text Editors
 
Print Spoolers
 
Network Drivers
 
Modern Programs
 
Databases
 
Language Interpreters
 
Utilities
C Program Execution Phases
First of all, execution or executing a program refers that we are now checking program for errors and then for output. C program goes under six (6) phases for execution: 1.
 
Edit. In this C program execution phase a programmer edit(type) a program with an editor program. 2.
 
Preprocess. In this C preprocessor executes before a program is compiled. It performs some actions like a) inclusion of other files in the file being compiled. b) symbolic constants and micros c) conditional compilation of program codes. d) conditional execution of preprocessor directories.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
9 Learning Module on PROG101
 
3.
 
Compiling. In Phase 3 compiler translate the C program into machine language code. It issues an error message to locate and fix the incorrect statements that means compiler issues and error when it does not recognize the syntax of code because when a statement is not as per according to the rules of the language. Syntax error are also called compile errors or compile time errors. 4.
 
Linking. C program basically contains reference to function defined elsewhere such as in the standard libraries or private libraries of group of programmers working on a particular project. Object code produced by c compiler typically contains "HOLES" due to this missing parts. Therefore, Linker links the object code with standard libraries or missing codes to produce an executable image. 5.
 
Loading. Before a program can be executed, the program has to be placed in memory. This is done by 'loader' which takes the executable image from disc and transfer it to memory.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
10 Learning Module on PROG101
 
6.
 
Execution. After all phases the execution phase comes in this phase CPU that is central Processing Unit execute the program one instruction at a time and the output of that code is printed on the monitor screen of the computer.
Summary of the Lesson:
1.
 
C is a powerful procedural language that uses compiler as a translator program. 2.
 
C enables you to understand the internal architecture of the computer system. 3.
 
C was written, designed and implemented by Dennis Ritchie for the purpose of UNIX operating system development. 4.
 
Through C, you will be able to develop an operating system, translator programs, text editors, etc. 5.
 
There are six (6) program execution in C: edit, preprocesses, compiling, linking, loading, and execution.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
11 Learning Module on PROG101
 
M1:L2 - Enrichment Activity
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________
Matching Type.
 Match Column A with the correct answer on Column B, write only the letter of answer on the blank provided.
Column A
Column B
 
1. It is the result of the development process that started with an older
language called BCPL.
 a. Dennis Richie 2. He developed BCPL. b. Loader 3. This program execution phase is where an object code is produce by compiler that contains holes. c. Compiling 4. He developed C language. d. Martin Richards 5.
This
program execution phase executes before a program
is
compiled.
e. C
 6. The first Operating System attempted to developed in C. f. Ken Thompson 7. He developed B language. g. Edit 8.
This
program execution phase translates the C program into machine language code. h. Preprocess 9. It takes the executable image from disc and transfer it to memory. i. Compiling 10. This program execution phase allows the programmer to type the program with an editor program.
 j. Linking
 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
12 Learning Module on PROG101
 
LESSON 3: THE C PROGRAM STRUCTURE
Objectives:
 At the end of this lesson, you will be able to:
 
 
Explain the C program structure
 
Identify the parts of the basic C program.
 
Practice the basic program in C.
The C Program Structure
A C program basically consists of the following parts −
 
 
Preprocessor Commands/Directives
 
main() Function
 
Statements & Expressions
 
Comments Take a look
at a simple code that will display the word “Hello World” on your 
 screen/monitor. Each line of the program is explained below:
 
The first line of the program #include <stdio.h>is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation. C commands are organized into libraries and .h is the extension name of the C libraries. stdio.h is the library that covers the input and output commands of C.
 
The next line int main () is the main function where the program execution begins.
 
The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So, such lines are called comments in the program. You can use comments to remind the programmer of what the program/statement does.
 
The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.
 
The next line is return 0; terminates the program execution. Therefore, the basic C program structure is shown below: #preprocessor directives
#include <stdio.h> main() {  /* my first program in C */
printf ("Hello, World! \n");
 return 0; 
}
 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
13 Learning Module on PROG101
 
main() { /* statements */ } Between the curly braces { } are the codes or statements that will be included in your program. The succeeding lessons covers the elements of programming that is needed in solving a problem with C language.
Summary of the Lesson:
1.
 
 A C program structure composes of preprocessor directives, main() function, statements, and comments. 2.
 
stdio.h is the standard input and output library of C. 3.
 
Comments are ignored by the compiler. 4.
 
{} are the indication of the begin and end of the program.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
14 Learning Module on PROG101
 
M1:L3 - Enrichment Activity
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________  Applying the structure and example provided in this lesson. Write a simple
 program that displays your “REGINA” on your
screen.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
15 Learning Module on PROG101
 
LESSON 4. THE ELEMENTS OF C PROGRAMMING
Objectives:
 At the end of this lesson, you will be able to:
 
 
Identify the elements of C Programming.
 
Differentiate the elements of C programming.
Elements of Programming
There are elements of programming that you can use in solving a problem with programming language. These elements can be your guide in learning not only C language but also other programming languages whether it be procedural or object-oriented programming language. These are:
 
Input/Output. These elements of computer programming allow interaction of the program with the external entities. Example of input/output element are printing something out to the screen, capturing some text that user input on the keyboard and can be include reading and writing files.
 
 Variables. Variables in programming tells how the data is represented which can be range from very simple value to complex one. The value they contain can be change depending on condition. As we know program consist of instructions that tell the computer to do things and data the program use when it is running. Data is constant with the fixed values or variable. They can hold a very simplex value like an age of the person to something very complex like a student track record of his performance of whole year.
 
Conditional. Conditionals specify the execution of the statements depending on the whether condition is satisfied or not. Basically, it refers to an action that only fulfil when the applied condition on instructions satisfied. They are one of the most important components of the programming language because they give freedom to program to act differently every time when it executes that depends on input to the instructions.
 
Iterations. We can define loop as a sequence of instructions that are repeated continuously till a certain condition is not satisfied. How a loop starts understand this first a certain process is done, to get any data and changing it after that applied condition on the loop is checked whether counter reached to prescribed number or not. Basically, a loop carry out execution of a group of instruction of commands a certain number of times. There is also a concept of infinite loop which is also termed as endless loop is a piece of code that lack from functional exit and goes to repeat indefinitely.
 
Subroutines. This element of the programming allows a programmer to use snippet of code into one location which can be used over and over again. The primary purpose of the functions is to take arguments in numbers of values and do some calculation on them after that return a single result. Functions are required where you need to do complicated calculations and the result of that may or may not be used subsequently used in an expression. If we talk about subroutines that return several results. Where calls to subroutines cannot be placed in an expression whether it is in the main program where subroutine is activated by using CALL statement which include the list of inputs and outputs that enclosed in the open and closed parenthesis and they are called the arguments of the subroutines. There are some of the rules follow by both to define name like less than six letters and start with the letters. The name should be different that used for variables and functions. In succeeding lessons of this course, each element will be discussed with the application of C language statements and its syntax.
Summary of the Lesson:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
16 Learning Module on PROG101
 
1.
 
Elements of programming helps you to solve to solve problems using programming language. 2.
 
The basic elements of programming are: output, input, variables, conditional, iterations, and subroutines. 3.
 
Output is the most important element of programming. 4.
 
Input allows you to interact with computer using input devices. 5.
 
 Variables enables you to represent data in programs. 6.
 
Conditional enables you to specify the execution of the statements whether it be true or false. 7.
 
Iterations is where set of instructions are repeated continuously. 8.
 
Subroutines allows you to use a group of codes and use it over and over again.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
17 Learning Module on PROG101
 
M1:L4 - Enrichment Activity:
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________ Discuss in your own words the basic elements of C program.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
18 Learning Module on PROG101
 
LESSON 5. THE INPUT/OUTPUT ELEMENT
Objectives:
 At the end of this lesson, you will be able to:
 
 
Identify the different input element of programming using C language.
 
Determine the different output element of programming using C language.
 
Create a program that utilize the input/output elements in C. In C Language input and output function are available as C compiler function or C library provided with each C compiler implementation. These all functions are collectively known as Standard I/O Library function. Here I/O stands for Input and Output used for different inputting and outputting statements. These I/O functions are categorized into three processing functions. Console input/output function (deals with keyboard and monitor), disk input/output function (deals with floppy or hard disk) and port input/output function (deals with serial or parallel port). As all the input/output statements deals with the console, so these are also Console Input/Output functions. Console Input/Output function access the three major files before the execution of a C Program. These are as:
 
stdin: This file is used to receive the input (usually is keyboard file, but can also take input from the disk file).
 
stdout: This file is used to send or direct the output (usually is a monitor file, but can also send the output to a disk file or any other device).
 
stderr: This file is used to display or store error messages.
Input/Output Statement
Input and Output statement are used to read and write the data in C programming. These are embedded in stdio.h (standard Input/Output header file). There are mainly two of Input/Output functions are used for this purpose. These are discussed as:
1.
 
Unformatted I/O functions There are mainly six unformatted I/O functions and these are getchar(), purchar(), gets(), puts(), getch(), and getche(). 1.
 
getchar(). This function is an Input function. It is used for reading a single character from the keyboard. It is buffered function. Buffered functions get the input from the keyboard and store it in the memory buffer temporally until you press the Enter key. The general syntax is as:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
19 Learning Module on PROG101
 
v = getchar();
where v is the variable of character type.
 
For example: char n; n =getchar(); To apply getchar() function, simple C-program to read a single character from the keyboard is as: 2.
 
putchar(). This function is an output function. It is used to display a single character on the screen. The general syntax is as:
 putchar(v);
where v is the variable of character type.
 
For example: char n; putchar(n);  A simple program is written as below, which will read a single character using getchar() function and display inputted data using putchar() function:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
20 Learning Module on PROG101
 
3.
 
gets(). This function is an input function. It is used to read a string from the keyboard. It is also buffered function. It will read a string, when you type the string from the keyboard and press the Enter key from the keyboard. It will mark null character ('\0') in the memory at the end of the string, when you press the enter key. The general syntax is as:
gets(v);
where v is the variable of character type. For example: char n[20]; gets(n);  A simple C program to illustrate the use of gets() function:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
21 Learning Module on PROG101
 
4.
 
puts(). This is an output function. It is used to display a string inputted by gets() function. It is also used to display an text (message) on the screen for program simplicity. This function appends a newline ("\n") character to the output. The general syntax is as:
 puts(v);
or
 puts("text line");
where v is the variable of character type. For example:
 puts(“Name is”);
 puts(n);  A simple C program to illustrate the use of puts() function:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
22 Learning Module on PROG101
 
Output of this program would be: 5.
 
getch(). This is also an input function. This is used to read a single character from the keyboard like getchar() function. But getchar() function is a buffered is function, getchar() function is a non-buffered function. The character data read by this function is directly assign to a variable rather it goes to the memory buffer, the character data directly assign to a variable without the need to press the Enter key. Another use of this function is to maintain the output on the screen till you have not press the Enter Key. The general syntax is as:
v = getch();
where v is the variable of character type.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
23 Learning Module on PROG101
 
 A simple C program to illustrate the use of getch() function: Output of this program would be: 6.
 
getche(). All are same as getch() function except it is an echoed function. It means when you type the character data from the keyboard it will visible on the screen. The general syntax is as:
v = getche();
where v is the variable of character type.  A simple C program to illustrate the use of getche() function:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
24 Learning Module on PROG101
 
Output of this program would be:
2.
 
Formatted I/O functions Formatted I/O functions which refers to an Input or Output data that has been arranged in a particular format. There are mainly two formatted I/O functions discussed as follows: 7.
 
scanf(). The scanf() function is an input function. It used to read the mixed type of data from keyboard. You can read integer, float and character data by using its control codes or format codes. The general syntax is as:
scanf("control strings",arg1,arg2, argN);
or

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
25 Learning Module on PROG101
 
scanf("control strings",&v1,&v2,&v3, &vN);
where arg1, arg2, argN are the arguments for reading and v1, v2, v3, vN all are the variables. The scanf() format code (specifier) is as shown in the below table:
Format Code Meaning
%c To read a single character
%d
 To read a signed decimal integer (short) %ld To read a signed long decimal integer
%e
 To read a float value exponential
%
 To read a float (short) or a single precision value
%lf 
 To read a double precision float value
%g
 To read double float value %h
 
To read short integer
%i
 To read an integer (decimal, octal, hexadecimal) %o
 
To read an octal integer only
%x
 To read a hexadecimal integer only %u
 
To read unsigned decimal integer (used in pointer)
%s
 To read a string
%[..]
To read a string of words from the defined range
%[^]
To read string of words which are not from the defined range Try the codes below:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
26 Learning Module on PROG101
 
8.
 
printf(). This is an output function. It is used to display a text message and to display the mixed type (int, float, char) of data on screen. The general syntax is as:
 printf("control strings",&v1,&v2,&v3, &vN);
or
 printf("Message line or text line");
where v1,v2,v3, vn all are the variables. The control strings use some printf() format codes or format specifiers or conversion characters. These all are discussed in the below table as:
Format Code Meaning
%c To read a single character
%s
 To read a string
%d
 To read a signed decimal integer (short) %ld To read a signed long decimal integer
%
 To read a float (short0 or a single precision value
%lf 
 To read a double precision float value
%e
 To read a float value exponential
%g
 To read double float value
%o
 To read an octal integer only
%x
 To read a hexadecimal integer only
%u
 To read unsigned decimal integer (used in pointer) Try the codes below:

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
27 Learning Module on PROG101
 
Summary of the Lesson:
1.
 
Input and output (I/O) element of programming is the use of the input and output devices in developing a program. 2.
 
There are two classifications of I/O elements and these are unformatted I/O and formatted I/O functions. 3.
 
Unformatted I/O functions are gets(), getch(), getche(), getchar(), puts(), putchar(). Functions gets() and puts() is specifically for string data type while the rest of the unformatted I/O works for character data type. 4.
 
Formatted I/O functions are scanf() and printf() which uses the format specification or what we called as format string. These functions can be used for various data types.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
28 Learning Module on PROG101
 
M1:L5 - Enrichment Activity
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________
Identify what is being required by the following task:
1.
 
Write an input statements that will allow the user to store integer value to num variable. a.
 
_______________ b.
 
_______________ c.
 
_______________ d.
 
_______________ e.
 
_______________ 2.
 
Write different input statements that will allow the user to store character value to ch variable. a.
 
_______________ b.
 
_______________ c.
 
_______________ d.
 
_______________ e.
 
_______________ 3.
 
Write an output statement for the number 1 task. _______________________________________ 4.
 
Write different output statements for the number 2 task. a.
 
_______________ b.
 
_______________ c.
 
_______________ d.
 
_______________ e.
 
_______________

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
29 Learning Module on PROG101
 
LESSON 6. THE DATA TYPES
Objectives:
 At the end of this lesson, you will be able to:
 
 
Explain the use of data types in programming.
 
Identify the different data types in C programming.
 
Integrate data types in C program through solving a problem. Data types specify how we enter data into our programs and what type of data we enter. C Data Types are used to:
 
Identify the type of a variable when it declared.
 
Identify the type of the return value of a function.
 
Identify the type of a parameter expected by a function. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities. C language supports different type of data types: 1.
 
Primary data types.
 These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.
 
char:
 The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers.
 
int:
 As the name suggests, an int variable is used to store an integer.
 
float:
 It is used to store decimal numbers (numbers with floating point value) with single precision.
 
double:
 It is used to store decimal numbers (numbers with floating point value) with double precision. 2.
 
Derived data types.
 Derived data types are nothing but primary data types but a little twisted or grouped together like array, references and pointer. These are discussed in details later on this course.
 
 Arrays. Arrays are sequences of data items having homogeneous values. They have adjacent memory locations to store values.
 
References. Function pointers allow referencing functions with a particular signature.
 
Pointer. These are powerful C features which are used to access the memory and deal with their addresses. 3.
 
User Defined Data Types:
 
Structure
,
Union
, and
 Enumeration
.
 
Structure. It is a package of variables of different types under a single name. This is done to handle data efficiently. "struct" keyword is used to define a structure.
 
Union. These allow storing various data types in the same memory location. Programmers can define a union with different members, but only a single member can contain a value at a given time.
 
Enum. Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. "enum" keyword is used to define the enumerated data type. Different data types also have different ranges up to which they can store numbers. These ranges may vary from compiler to compiler. Below is list of ranges along with the memory requirement and format specifiers on 32 bit gcc compiler.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
30 Learning Module on PROG101
 
Date Type
Memory (Bytes)
Range
Format Specifier
short int
2
 
-32,768 to 32,767
 %hd unsigned short int
2
 
0 to 65,535
 %hu unsigned int
4
 
0 to 4,294,967,295
 
%u
 int
4
 -2,147,483,648 to
2,147,483,647
 
%d
 long int
8
 -2,147,483,648 to
2,147,483,647
 %ld unsigned long int
8
 
0 to 4,294,967,295
 
%lu
 long long int
8
 
-(2^63) to (2^63)-1
 
%lld
 unsigned long long int
8
 0 to
18,446,744,073,709,551,615
 
%llu
 signed char
1
 
-128 to 127
 %c unsigned char
1
 0 to 255 %c float
4
 
%
 
double
 
8
%lf 
 
long double
 
16
 
%Lf
 Additional Data Types
1.
 
 void.
 void is an incomplete type. It means "nothing" or "no type". You can think of void as absent. For example, if a function is not returning anything, its return type should be void. 2.
 
short and long.
 If you need to use a large number, you can use a type specifier long. Here's how:
 
long a;
 
long long b;
 
long double c; Here variables a and b can store integer values. And, c can store a floating-point number. If you are sure, only a
small integer ([−32,767, +32,767] range) will be used, you can use
short.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
31 Learning Module on PROG101
 
short d;
 You can always check the size of a variable using the sizeof() operator. 3.
 
signed and unsigned.
 In C, signed and unsigned are type modifiers. You can alter the data storage of a data type by using them. For example,
unsigned int x;
Here, the variable x can hold only zero and positive values because we have used the unsigned modifier. Considering the size of int is 4 bytes, variable y can hold values from -231 to 231-1, whereas variable x can hold values from 0 to 232-1.
Summary of the Lesson:
1.
 
Data types identifies the type of variable when it is declared. 2.
 
There are various data types to use and these are: primary data types, derived data types, and user-defined data types. 3.
 
 Among the primary data types are int for integer, char for character, and float for numbers with fractional part. 4.
 
The derived data types are similar to primary data types but they are treated as a group and these are: array, pointers, and references. 5.
 
The user-defined data types are structures, union, and enum. 6.
 
Other data types include short, long, signed, unsigned, and void.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
32 Learning Module on PROG101
 
M1:L6 - Enrichment Activity
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________ I. Provide on the space provided the format specifiers or string of the following data type: a.
 
Integer - _______________ b.
 
Signed integer - _______________ c.
 
Character - _______________ d.
 
Float - _______________ e.
 
Double - _______________ II. Enumerate the following: 1.
 
What are the classification of data types: a.
 
_______________ b.
 
_______________ c.
 
_______________ 2.
 
What are the keywords for primitive data types: d.
 
_______________ e.
 
_______________ f.
 
_______________ g.
 
_______________

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
33 Learning Module on PROG101
 
LESSON 7. VARIABLES, CONSTANTS AND LITERALS
Objectives:
 At the end of this lesson, you will be able to:
 
 
Understand the variables and rules for naming a variable.
 
Identify different literals in C programming and how to create constants.
 Variables
In programming, a variable is a container or storage area to hold data. To indicate a storage area, each variable should be given a unique name, we call it as identifier. Variable names are just the symbolic representation of a memory location. For example:
int number = 7;
With the example given, number is a variable of int data type which is assigned an integer value 7. Another example:
char ch;
In the given example, ch is a variable of character data type. There are rules in naming a variable or identifier: 1.
 
 A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. 2.
 
The first letter of a variable should be either a letter or an underscore. 3.
 
There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters. C is a strongly typed language. This means that the variable type cannot be changed once it is declared. For example:
int number = 5;
// error
 integer variable number = 5.5;
// error
 double number;
// error
 
Here, the type of number variable is int. You cannot assign a floating-point (decimal) value 5.5 to this variable. Also, you cannot redefine the data type of the variable to double. By the way, to store the decimal values in C, you need to declare its type to either double or float.
Constants
If you want to define a variable whose value cannot be changed, you can use the const keyword. This will create a constant. For example,
const double PI = 3.14;

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
34 Learning Module on PROG101
 
Notice, we have added keyword const.  You can also place a symbolic constant which its value cannot be changed.
const double PI = 3.14;
 You can also define a constant using the #define preprocessor directive.
Literals
Literals are data used for representing fixed values. They can be used directly in the code. For example: 1, 2.5, 'c' etc. They are literals because you cannot assign different values to these terms. There are different literals in C language. 1.
 
Integers.
 These are numeric literals associated with numbers without any fractional or exponential part. There are three types of integer literals in C programming:
 
decimal (base 10)
 
octal (base 8)
 
hexadecimal (base 16) For example:
 
Decimal: 0, -9, 22 etc
 
Octal: 021, 077, 033 etc
 
Hexadecimal: 0x7f, 0x2a, 0x521 etc In C
programming
, octal starts with a 0, and hexadecimal starts with a 0x. 2.
 
Floating-point Literals.
 These are numeric literal that have either a fractional form or an exponent form. For example:
 
-2.0
 
0.0000234
 
-0.22E-5
 
Note: E-5 =10-5 3.
 
Characters.
 These are created by enclosing a single character inside single quotation marks. For example: 'a', 'm', 'F', '2', '}' etc. 4.
 
Escape Sequences.
 Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C programming. For example: newline(enter), tab, question mark etc. In order to use these characters, escape sequences are used.
Escape Sequences Character
\b
 
Backspace
 
\f 
 Form feed

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
35 Learning Module on PROG101
 
\n
 Newline
\r
 Return
\t
 Horizontal tab
\v
  Vertical tab
\\
 Backslash
\'
 Single quotation mark \" Double quotation mark
\?
 Question mark
\0
 Null character For example: \n is used for a newline. The backslash \ causes escape from the normal way the characters are handled by the compiler. 5.
 
String Literals.
 A string literal is a sequence of characters enclosed in double- quote marks. For example:
 
"good" /*string constant*/
 
"" /*null string constant*/
 
" " /*string constant of six white space*/
 
"x" /*string constant having a single character. */
 
"Earth is round\n" /*prints string with a newline*/
Summary of the Lesson:
1.
 
 Variables serves as container of the data, constants are the values that cannot be changed, while literals are data representing fixed data. 2.
 
There are rules in naming variables or what we call as identifiers.
 
It must start with a letter.
 
The rest of the identifier could be a letter, digit, or underscore.
 
No space is allowed.
 
No keyword is allowed. Keyword has a special meaning to the compiler such as printf, scanf, int, char, float, if, etc.
 
No special symbol is allowed such as %, @, $, *, +, -, etc.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
36 Learning Module on PROG101
 
M1:L7 - Enrichment Activity
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________
Identify if it is V- valid or I
 – 
 invalid name of variables.
________1. MyName ________2. First number ________3. void ________4. num*1 ________5. 2ndnumber ________6. sum; ________7. HaveAGreatDay ________8. NAME ________9. _sample ________10. Number-1

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
37 Learning Module on PROG101
 
LESSON 8. C PROGRAMMING OPERATORS
Objectives:
 At the end of this lesson, you will be able to:
 
 
Identify the different operators in C programming with the help of examples.  An operator is a symbol that operates on a value or a variable. For example: +is an operator to perform addition. C has a wide range of operators to perform various operations.
Types of Operators
1.
 
 Arithmetic/mathematical operator.
 An arithmetic/mathematical operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).
Operato
 
Meaning of Operator 
 
+
 addition or unary plus
-
 subtraction or unary minus
*
 multiplication
/
 division
%
 remainder after division (modulo division)

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
38 Learning Module on PROG101
 
Output would be: The operators +, - and * computes addition, subtraction, and multiplication respectively as you might have expected. In normal calculation, 9/4 =2.25. However, the output is 2 in the program. It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 2 instead of 2.25. The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1. The % operator can only be used with integers. Suppose a =5.0, b =2.0, c =5 and d =2. Then in C programming,
// Working of arithmetic operators
#include <stdio.h> 
int main()  int a = 9,b = 4, c;  c = a+b; 
c = a-b; 
c = a*b; c = a/b; c = a%b; wh return 0;  a+b = 13 
a-b = 5
 a*b = 36 a/b = 2 Remainder when a divided by b=1 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
39 Learning Module on PROG101
 
2.
 
C Increment and Decrement Operators.
C programming has two operators increment ++and decrement -- to change the value of an operand (constant or variable) by 1. Increment ++increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.
// Either one of the operands is a floating-point number a/b = 2.5 a/d = 2.5  c/b = 2.5  // Both operands are integers // Working of increment and decrement operators #include <stdio.h> int main()  int a = 10, b = 100;  float c = 10.5, d = 100.5;  printf ("--b = %d \n", --b); printf ("--d = %f \n", --d);  return 0;

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
40 Learning Module on PROG101
 
Output: Here, the operators ++and -- are used as prefixes. These two operators can also be used as postfixes like a++and a--. 3.
 
C Assignment Operators.
 An assignment operator is used for assigning a value to a variable. The most common assignment operator is =.
Operator
 
Example
 
Same as
 
=
 
a = b
 
a = b
 
+=
 
a +=b
 
a = a+b
 
-=
 
a -= b
 
a = a-b
 
*=
 
a *= b
 
a = a*b
 
/=
 
a /= b a = a/b
%=
 
a %= b a = a%b
Try the codes below:
++a = 11
 
--b = 99
 
++c = 11.500000
 
++d = 99.500000
 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
41 Learning Module on PROG101
 
Output: 5
// Working of assignment operators #include <stdio.h> int main()  int a = 5, c;  c = a; // c is 5 c += a;
 // c is 10
c -= a; // c is 5 c *= a; // c is 25 c /= a; // c is 5
c %=
a; // c = 0 return 0;  c = 5 c = 10 c = 5 c = 25 c = 5 c = 0 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
43 Learning Module on PROG101
 
Output: 5.
 
C Logical Operators.
 An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming.
Operato
 
Meaning
 
Example
 
&&
 Logical AND. True only if all operands are true If c =5 and d =2 then,
expression ((c==5) && (d>5))
equals to 0.
||
 Logical OR. True only if either one operand is true If
c
=5 and d =2 then,
expression ((c==5) || (d>5))
equals to 1.
!
 Logical NOT. True only if the operand is 0 If c =5 then, expression !(c==5) equals to 0. Example: Logical Operators
5 == 5 is 1
 
5 == 10 is 0
 
5 > 5 is 0
 
5 > 10 is 0
 
5 < 5 is 0
 
5 < 10 is 1
 
5 != 5 is 0
 
5 != 10 is 1
 
5 >= 5 is 1
 
5 >= 10 is 0
 
5 <= 5 is 1
 
5 <= 10 is 1
 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
44 Learning Module on PROG101
 
Output: Explanation of logical operator program
o
 
(a ==b) && (c >5) evaluates to 1 because both operands (a ==b) and (c>b) is 1 (true).
o
 
(a ==b) && (c <b) evaluates to 0 because operand (c <b) is 0 (false).
o
 
(a ==b) || (c <b) evaluates to 1 because (a =b) is 1 (true).
o
 
(a !=b) || (c <b) evaluates to 0 because both operand (a !=b) and (c<b) are 0 (false).
o
 
!(a !=b) evaluates to 1 because operand (a !=b) is 0 (false). Hence, !(a !=b) is 1 (true).
// Working of logical operators 
#include <stdio.h>
int main() 
{
 int a = 5, b = 5, c = 10, result;  result = (a == b) && (c > b); 
printf ("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b); 
printf ("(a == b) && (c < b) is %d \n", result);
result = (a == b)
||
(c < b); printf ("(a == b) || (c < b) is %d \n", result); result = (a != b)
||
(c < b); printf ("(a != b)
||
(c < b) is %d \n", result); result = !(a != b); 
printf ("!(a == b) is %d \n", result);
result = !(a == b);
 
printf ("!(a == b) is %d \n", result);
 return 0; 
}
 (a
==
b) && (c
>
b)
is
1 (a
==
b) && (c
<
b)
is
0 (a
==
b)
||
(c
<
b)
is
1 (a
!=
b)
||
(c
<
b)
is
0 
!(a != b) is 1
 
!(a == b) is 0
 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

9
 
 
45 Learning Module on PROG101
 
o
 
!(a ==b) evaluates to 0 because (a ==b) is 1 (true). Hence, !(a ==b) is 0 (false). 6.
 
C Bitwise Operators.
 During computation, mathematical operations like: addition, subtraction, multiplication, division, etc. are converted to bit-level which makes processing faster and saves power. Bitwise operators are used in C programming to perform bit-level operations.
Operators
 
Meaning of operators
 
&
 
Bitwise AND
 
|
 
Bitwise OR
 
^
Bitwise exclusive OR
 
~
 
Bitwise complement
 
<<
 
Shift left
 
>>
Shift right
 
7.
 
Comma Operator.
 Comma operators are used to link related expressions together. For example:
int a, c = 5, d;
8.
 
The sizeof operator.
 The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).
#include <stdio.h>
int main() 
{
 int a; float b; double c; char d; 
pr
i
n
tf 
(
"
Si
z
e
 of
i
n
t=%
l
u
 byte
s
\
n"
,s
i
z
eo
(
a
))
;
pr
i
n
tf 
(
"
Si
z
e
 of
l
o
a
t=%
l
u
 byte
s
\
n"
,s
i
z
eo
(
b)
)
;
pr
i
n
tf 
(
"
Si
z
e
 of
d
o
u
bl
e
=%l
u
 byte
s
\
n"
,s
i
z
eo
(
c
))
;
pr
i
n
tf 
(
"
Si
z
e
 of
c
h
a
r=%l
u
 byte
\
n"
,s
i
z
eo
(
d
))
;
 return 0; 
}
 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

5
 
 
46 Learning Module on PROG101
 
Output:
Summary of the Lesson:
1.
 
 An operator is a symbol that operates on a value or a variable. 2.
 
 Among the types of operators are arithmetic/mathematical operators, increment and decrement operators, assignment operators, relational operators, logical operators, bitwise operators, comma and sizeof operators.
Size of int = 4 bytes Size of float = 4 bytes  Size of double = 8 bytes Size of char = 1 byte 

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

6
 
 
47 Learning Module on PROG101
 
M1:L8 - Enrichment Activity
Provide examples of the use of different operators:
1.
 
arithmetic/mathematical operators 2.
 
increment and decrement operators 3.
 
assignment operators 4.
 
relational operators 5.
 
logical operators 6.
 
bitwise operators 7.
 
comma 8.
 
sizeof operators.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

6
 
 
48 Learning Module on PROG101
 
Lesson 9. C Programming Expressions
Objectives:
 At the end of this lesson, you will be able to:
 
 
Identify the valid C programming expressions.
 
Use expressions in C program. In programming, an expression is any legal combination of symbols that represents a value. C Programming provides its own rules of expression, whether it is legal expression or illegal expression. For example, in the C language x+5 is a legal expression. Every expression consists of at least one operand and can have one or more operators. Operands are values and Operators are symbols that represent particular actions.
 Valid C Programming Expression:
C Programming code gets compiled firstly before execution. In the different phases of compiler, c programming expression is checked for its validity.
Expressions
 
 Validit
 a +b Expression is valid since it contains +operator which is binary operator
++
a
+
b
In
 v
a
lid
 
E
x
p
r
e
ss
ion
 
Priority and Expression
In order to solve any expression, we should have knowledge of C Programming Operators and their priorities of what we call as order of precedence. Operator precedence & associativity are listed in the following table and this table is summarized in decreasing Order of priority i.e topmost operator has highest priority and bottommost operator has Lowest Priority.
Operator
 
Description
 
Associativity
 
( ) [ ] . ->++
 –
 Parentheses (function call) Brackets (array
subscript)
Member selection via object name Member selection via pointer Postfix increment/decrement left-to-right
 
++
 –
 +
 –
 ! ~(type) * & sizeof Prefix increment/decrement Unary plus/minus Logical negation/bitwise complement Cast (convert value to temporary value of type) Dereference Address (of operand) Determine size in bytes on this implementation right-to-left
 
* / % Multiplication/division/modulus
 
left-to-right
 
+
 –
 Addition/subtraction
 
left-to-right
 
<<>>
Bitwise shift left, Bitwise shift right
 
left-to-right
 
<<=>>=Relational less than/less than or equal to Relational greater than/greater than or equal to
 
left-to-right ==!=Relational is equal to/is not equal to left-to-right

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

10
 
 
49 Learning Module on PROG101
 
& Bitwise AND
left-to-right
 ^Bitwise exclusive OR
left-to-right
 | Bitwise inclusive OR
left-to-right
 && Logical AND
left-to-right
 | | Logical OR
left-to-right
 ? : Ternary conditional
right-to-left
 =+=-=*=/= %=&= ^=|= <<=>>=
Assignment Addition/subtraction assignment Multiplication/division assignment Modulus/bitwise AND assignment Bitwise exclusive/inclusive OR assignment Bitwise shift left/right assignment
right-to-left
 , Comma (separate expressions)
left-to-right
 
Summary of the Lesson:
1.
 
Comma Operator Has Lowest Precedence . 2.
 
Unary Operators are Operators having Highest Precedence. 3.
 
Sizeof is Operator not Function . 4.
 
Operators sharing Common Block in the Above Table have Equal Priority or Precedence . 5.
 
While Solving Expression , Equal Priority Operators are handled on the basis of FIFO [ First in First Out ] i.e Operator Coming First is handled First.

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

7
 
 
50 Learning Module on PROG101
 
M1:L9 - Enrichment Activity
Name: ___________________________________________ Score: __________________________  Year and Section: __________________________________ Professor: _______________________
I.
Solve the following expressions:
1)
 
10*2+3-(6%2+9) 2)
 
6%3%4+9+3/10/5 3)
 
6>=5 && 10==10 4)
 
!(!(3<4 || 5>=2) && 8>1)
II.
Programming.
 Write a program that will input two integers and determine the product of the two integers. Sample output: Enter 2 integers: 3 6 Product =18

Unlock this document

Upload a document to download this document or subscribe to read and download.

or

Unlock this page after an ad

7

Reward Your Curiosity

Everything you want to read.
Anytime. Anywhere. Any device.
No Commitment. Cancel anytime.
576648e32a3d8b82ca71961b7a986505