You are on page 1of 11

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

INTRODUCTION TO C PROGRAMMING
History of C Language
C was created at the Bell Telephone Laboratories in 1972 by Dennis Ritchie. It
was developed into such a powerful and flexible language that its use quickly spread
beyond Bell Labs. The C language is so named because its predecessor was called B.
The B language was also developed at Bell Labs, by Ken Thompson.
There are several reasons why many computers professionals feel that C is on
top of the heap among the other high-level programming languages.

C is an efficient language It is a concise language that lets you say what you
mean in fewer words. The final code tends to the compact and to run quickly.

C has a lot desirable design features C programming language use sound


programming techniques such as top-down planning, structured programming
and modular design. The result is a more reliable, understandable program.

C is a powerful and flexible language C programming language is used for


projects as diverse as operating systems, word processors, graphics and even
compilers for other languages.

C is a popular language It is by far the preferred language among


professional programmers. As a result, there are a wide variety of C compilers
and helpful accessories available.

C is oriented toward programmer needs C allows the programmer to have


access to hardware. It lets you manipulate individual bits in memory. It has a rich
selection of operators that let you express yourself. It has a large library of useful
functions that are generally available on most C implementations.

C is a portable language Portable means that a C program written for one


computer system can be complied and run on another system with little or no
modification.

As these features indicate C is an excellent choice for your first programming


language, programming in C can be taxing, difficult and frustrating, but it can be
intriguing, exciting and satisfying.

Page | 1

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

Identifiers
-

One or more characters used to identify or name data elements such as


variables, constants, arrays, records, and subprograms.

An identifier is a combination of alphanumeric characters, the first being a letter


of the alphabet or an underscore, and the remaining being any letter of the
alphabet, any numeric digit, or the underscore.

Constant
- A data item that remains unchanged throughout the execution of the
program.
- A memory location whose constants stay the same during the execution of
the program.

Variable
- A named data item, the value of which may change during the execution
of the program.
- A memory location whose contents can be filled and changed during the
execution of the program.

Data Type
-

A definition or interpretation of a set of data specifying the possible range of


values of the set, the operation that can be performed on the rules and the way in
which values are stored in the memory.

TYPE
CHAR OR SIGNED CHAR
UNSIGNED CHAR
INT / SIGNED INT
UNSIGNED INT
SHORT INT / SIGNED SHORT INT
UNSIGNED SHORT INT
LONG INT / SIGNED LONG INT
UNSIGNED LONG INT
FLOAT
DOUBLE
LONG DOUBLE

SIZE (BITS)
8
8
16
16
8
8
32
32
32
64
80

RANGE
-128 to 127
0 to 255
-32768 to 32767
0 to 65535
-128 to 127
0 to 255
-2147483648 to 2147483647
0 to 4294967295
3.4 e-38 to 3.4 e+38
1.7e-308 to 1.7e+308
3.4 e-4932 to 3.4 e+4932

Page | 2

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

Expressions

It is a combination of values, variables, operators, and functions that are


interpreted (evaluated) according to the particular rules of precedence and of
association for a particular programming language, which computes and then
produces (returns, in a stateful environment) another value.

Kind of Expressions
Arithmetic an expression that contains arithmetic operators and operands
which can be reduced to a single numeric value.

Relational an expression constructed by connecting constants, variables


and/or other expressions by a relational operator.

Logical an expression constructed by combining individual conditional


variables or expressions into a more complex statements by combining them with
a logical operator.

Arithmetic Expression
Arithmetic operator a symbol used to represent a mathematical operation.
Operator

Operation
Increment

Action
Increase the operand by 1.

a++

Decrement

Decrease the operand by 1.

z--

Adds the 2 operands together.

num1 + num2

Subtracts the 2nd operand from


the 1st operand.
Multiplies the 2 operands
together.
Divides the 1st operand by the
2nd operand.
Gives the remainder when the
1st operand is divided by the 2 nd
operand.

Salary - Deduction

++
-+
-

Subtraction

Multiplication

Division

Modulus

Addition

Example

Grade * units
Total / 100
Minutes % 60

Order of Precedence
Operators
++

--

Precedence
1

Page | 3

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)


* / %

+ -

Example 2.1. Evaluate the expression:


Num1 = 5 % 5 * 5 + 5 / 5;
Num1
Num1
Num1
Num1

= 0 * 5 + 5 / 5;
= 0 + 5 / 5;
= 0 + 1;
= 1;

Example 2.2. Find the result of the expression:


Result = 8 * (4 % (3 + 2)) 6 * 7;
Result = 8 * (4 % 5) 6 * 7;
Result = 8 * 4 6 * 7;
Result = 32 6 * 7;
Result = 32 42;
Result = -10;
Example 2.3. What will be the value of x:
a = 45;
b = a / 3;
c = b - 7;
x = a + b * c;
//Solution:
b = a / 3;
b = 15;
c = b 7;
c = 8;

//a = 45
//b = 15

x = a + b * c;
x = 45 + 15 * 8;
x = 165;

Relational Expression

Page | 4

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

Relational operator a symbol used to compare operands and yields either true or
false.
Operator

Operation

Example

==

Equal

x == y

>

Greater than

x >y

<

Less than

y<x

>=

Greater than or equal

Income >= 25000

<=

Less than or equal

Grade <= 1.75

!=

Not equal

Score != 100

Logical Expression
Logical operator a symbol that defines the logical connection between two or more
conditions.
Operator
!
&&
||

NAME
NOT
AND
OR

NOT ( ! ) OPERATOR
- Reverses the truth value of a given condition.
CONDITION
False
True

RESULT
True
False

AND ( && ) OPERATOR


- If one of the condition is false then the result is false, otherwise true.
CONDITION 1
False
False
True
True

CONDITION 2
False
True
False
True

RESULT
False
False
False
True

Page | 5

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

OR ( || ) OPERATOR
- If one of the condition is true then the result is true, otherwise false.
CONDITION 1
False
False
True
True

CONDITION 2
False
True
False
True

RESULT
False
True
True
True

Example 2.4. Build a Boolean Expression for the following situation.


Expense is higher than profit.
Answer: Expense > profit

Grade must not lower than 75.


Answer: Grade >= 75

Num is a positive number


Answer: Num > 0

Age must be greater than 18 but not greater than 30


Answer: (Age > 18) && (Age <= 30)

N is either 5, 10 or 15.
Answer: (N==5) || (N==10) || (N==15)

N is an even number.
Answer: N % 2 == 0

Components of C Program

Compiler Directive #include (Header Files)


- The #include directive instructs the C compiler to read the contents of an
include file into your program during compilation. An include file is separate

Page | 6

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)


disk file that contains information needed by the compiler. Most C programs
require one or more include files.
EXAMPLES:
#include <stdio.h>
#include <math.h>
#include <conio.h>
LIBRARY NAME
ctype.h
float.h
limits.h
math.h
stdlib.h
stdio.h
string.h
time.h

DESCRIPTION
Function for case conversion and for testing characters (for
example, checking for uppercase or lowercase letters or for
special characters or blanks.
Definitions of various type float and double limits for your
computer system (for example, the maximum integer n such that
10n is representable in your computer).
Definitions of various type integer and character limits for your
computer system (for example, the largest and smallest integers
that can be stored and manipulated).
Mathematics functions such as square root, trigonometric, etc.
Function for number conversion, memory allocation, sorting,
searching, random-number generation, and program termination.
Function containing the printf() and scanf() function.
Sting manipulating functions for comparing, concatenating, and
copying, string, and for testing for the presence of specific
characters or substrings.
Functions for manipulating date and time.

main() Function
- One of the essential component required in every C program is the main()
function. It consists of the name main followed by a fair of empty parenthesis
and a fair of braces, the braces contains the statements that makes up the
main body of the program.

{ } (Opening and closing curly braces)


- Signifies the beginning and the end of the main() function or a group of more
than one statement called a block in the program.

Comment
- Used as an initial documentation for programmers, can be used to describe
contents of the program, to-do list in the program and guide to non-technical
readers of the program.

Page | 7

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

// (Double forward slash) - when used, all characters at the right of the
symbols will be treated by the compiler as comments, also known as a
single-line comment.
Example:
int a; // variable a will hold the input value

/*
*/ - when used, all characters inside the block will be treated by the
compiler as comments, also known as block comment.

Example:
/* A sample comment line which can be placed anywhere in the program */
printf() Function
- A function included inside the stdio.h library that is used to display information
on the screen.
Syntax:
printf(control string, argument list);
where:
control string - contains characters to be displayed or format codes
that tell how to display the rest of the argument.
argument list list of values or/and variables.

Example
2.6.
Format Code
Given:
%d or %i
%ld
%f
%lf

Meaning
Meaning
Display an integer
x =Display a long integer
n = Display a float
Display a double
Format Code
%c
Display a character
%s
Display a string
%o
Display an octal
number

99;
83.25;
my_letter = A;
my_name = James;
printf(Introduction to
Programming);
Output: Introduction to
Programming

printf(%f,n);
Output: 83.250000

printf(Total no. of female students = %d, x);


Output: Total no. of female students = 99

printf(I am %s, my favorite letter is %c and I am %d years old.,


my_name, my_letter, 16);
Output: I am James, my favorite letter is A and I am 16 years old.

Page | 8

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

Backslash Characters
Code
\n
\t
\a
\\

Meaning
Newline
Horizontal tab
Alert (beep)
Backslash

6. scanf() Function
A library function that reads data from the keyboard and assigns that data
to one or more program variable.
Syntax:
scanf(control string, argument list);
Where:
control string format codes of the values to input.
argument list list of variables where the values read from the keyboard will be stored.
Variable must be prefix by &.
EXAMPLES:
scanf(%d, &x);
scanf(%d %d, &no1,&no2);
scanf(%s %c %d,&name, &sex, &age);
7. puts() Function
Also used to display text messages on the screen. It cannot display numeric
values. It takes a single string as its argument and displays it, automatically adding a
new-line at the end.
Syntax:
puts(argument);
EXAMPLE:
puts(This is a sample using puts!);
8. gets() Function get string
A function that accepts a string from the standard input device, the keyboard. Its
method is to read characters until it reaches a newline (\n) character, which is
generated by pressing the Enter key.
Syntax:

Page | 9

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)


gets(string variable);
EXAMPLES:
gets(name);
gets(my_address);
Example 1. Construct a C program that will display Hello World in the
middle of the screen.
#include<stdio.h>
#include<conio.h>
main()
{
printf("\n\n\n\n\n\n\t\t\t");
printf("HELLO WORLD!");
getch();
}

Example 2. Build a C program that will input three numbers and display the
sum and the average.
#include<stdio.h>
#include<conio.h>
float num1, num2, num3, sum, ave;
main()
{
printf("Enter num1: ");
scanf("%f",&num1);
printf("Enter num2: ");
scanf("%f",&num2);
printf("Enter num3: ");
scanf("%f",&num3);
sum=num1+num2+num3;
ave=sum/3;
printf("Sum is %f\n",sum);
printf("Average is %.2f",ave);
getch();
}

Page | 10

NES 113 (COMPUTER SOFTWARE FUNDAMENTALS)

Machine Problems #1:


1. Make a C program that will display I will pass COMPUTER
PROGRAMMING on the center of the screen.
2. Design a program in C language that will display # at every corners of
the screen.
3. Construct a program that will display your name, address, and contact
number on the upper left corner of the screen.
4. Create a program in C language that will input a temperature in Celsius
and display the equivalent temperature in Fahrenheit. (F = 9C/5 + 32)
5. Write a program that will input a length in inches and output the
equivalent in meters. 1 inch = 2.54 cm, 1 m = 100 cm.
6. At the University of the East, every unit is worth P1450 in enrollment fee.
An additional 15% of this amount is paid in miscellaneous fees with a
blanket fee of P1500 regardless of the load. Design a C program that will
enter the number of UNITS a student is enrolled in and output the tuition fee
to be paid.
7. A car travels at a constant speed of 50 kilometers per hour. Make a
program that will input TIME in minutes that a trip took and output the
DISTANCE in kilometers that the car traveled.
8. A taxi charges a fixed rate of P1.25 for every 250 meters that it travels.
Create a program in C that will input the DISTANCE in kilometers that a taxi
traveled and output the COST of the trip.
9. Create a program that will input two integer variables, A and B and
exchange their values
10. A certain store sells soft drinks for P 8.25 and sandwiches for P16.85.
Construct a program that will input the number of drinks and sandwiches a
customer bought and output the BILL.

Page | 11

You might also like