Prog101 Prelim
Prog101 Prelim
PROFESSOR
Name:
Academic Department:
Consultation Schedule:
Email Address:
Contact Number:
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 clearlyexplained.
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.
Below is the Window Mode of Turbo C/C++ Integrated Development Environment (IDE).
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
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:
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.
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.
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.
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).
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
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.
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.
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.
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
a. DennisRichie
older language called BCPL.
2. He developed BCPL. b. Loader
3. This program execution phase is where an object code is produced c. Compiling
by compiler that contains holes.
4. He developed Clanguage. d. MartinRichards
5. This program execution phase executes before a program is e. C
compiled.
6. The first Operating System attempted to developed in C. f. KenThompson
7. He developed B language. g. Edit
8. This program execution phase translates the C program into h. Preprocess
machine language code.
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 j. Linking
program with an editor program.
Take a look at a simple code that will display the word “Hello World” on your screen/monitor.
#include <stdio.h>
main() {
/* my first program in C */
printf("Hello, World! \n");
return 0;
}
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.
#preprocessor directives
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.
Applying the structure and example provided in this lesson. Write a simple program that displays your “REGINA” on your
screen.
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.
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.
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:
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:
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);
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);
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.
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.
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
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:
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. _______________
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.
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.
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.
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 - _______________
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;
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;
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
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.
\b Backspace
\f Form feed
\n Newline
\r Return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\? 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*/
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
r
+ addition or unaryplus
/ division
% remainder after division (modulo division)
//Workingofarithmeticoperators
#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
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,
a/b = 2.5
a/d=2.5
c/b=2.5
integers
c/d = 2
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.
#include <stdio.h>
int main()
printf("--b=
%d\n",--b);
printf("--d=%f\n",--d);
return 0;
Output:
++a = 11
--b = 99
++c = 11.500000
++d = 99.500000
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
//Workingofassignmentoperators
#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;
Output:
c=5
c = 10
c=5
c = 25
c=5
c=0
5
4. C Relational Operators. A relational operator checks the relationship between two operands. If the relation is true,
it returns 1; if the relation is false, it returns value 0. Relational operators are used in decision making and
loops.
//Workingofrelationaloperators
#include <stdio.h>
int main()
int = 5, = 5, = 10;
a, printf("%d>%dis%d\n",a,
b,a>b); a,
printf("%d<%dis%d\n",a,b,a<b);
a, printf("%d!=%dis%d\n",a,b,a
!=b);
a,
a,
a,
return 0;
Output:
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
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
r
Logical AND. True only if all operands are If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals
&&
true to 0.
Logical OR. True only if either one operand is If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to
||
true 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0.
{
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);
return 0;
Output:
(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
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.
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).
{
int a; float
b; double c;
char d;
return 0;
Output:
3. assignment operators
4. relational operators
5. logical operators
6. bitwise operators
7. comma
8. sizeof operators.
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.
C Programming code gets compiled firstly before execution. In the different phases of compiler, c programming expression
is checked for its validity.
Expressions Validity
a+b Expression is valid since it contains + operator which is binary operator
++a+b Invalid 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.
2) 6%3%4+9+3/10/5
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