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

Introduction-to-C-Language (1)

Uploaded by

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

Introduction-to-C-Language (1)

Uploaded by

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

CHAPTER II: Turbo C Environment

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 computer 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.

Turbo C Environment
Main Menu - let Turbo C to do something as indicated in the list of menu.
1. File – this is used to load, save files. Handles directories, invokes DOS
and exits Turbo C.
2. Run – used to run and test the program for errors.
3. Compile – used to compile the program currently in Turbo C
environment.

Editor Status Line and Edit Window – this is the part where you type
your codes. It also involves the status in what line location where you typing.

Message Window – it is located below the edit window. It shows the


compiler message like errors and linker messages.

Hot Keys – located at the bottom of the environment and let you type
shortcut keys for choosing a menu.
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 SIZE (Bits) Range


Char or Signed Char 8 -128 to 127
Unsigned Char 8 0 to 255
Int or Signed int 16 -32768 to 32767
Unsigned int 16 0 to 65535
Short int or Signed short int 8 -128 to 127
Unsigned short int 8 0 to 255
Long int or signed long int 32 -2147483648 to
2147483647
Unsigned long int 32 0 to 4294967295
Float 32 3.4 e-38 to 3.4 e+38
Double 64 1.7e-308 to 1.7e+308
Long Double 80 3.4 e-4932 to
3.4 e+4932
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
1. Arithmetic – an expression that contains arithmetic operators and
operands which can be reduced to a single numeric value.
2. Relational – an expression constructed by connecting constants,
variables and/or other expressions by a relational operator.
3. Logical – an expression constructed by combining individual conditional
variables or expressions into a more complex statements by combining them
with a logical operator.

Arithmetic operator – a symbol used to represent a mathematical


operation.

Operator Operation Action Example


++ Increment Increments operand by 1 a++
-- Decrement Decrements operand by 1 --z
+ Addition Adds its 2 operands a+b
- Subtraction Subtracts the 2nd operand Gross-
from the 1st operand deduction
* Multiplication Multiplies its 2 operands Grade * units
/ Division Divides the 1st operand Total / 100
by the 2 operand
nd

% Modulus Gives the remainder Minutes % 60


when the 1 operand is
st

divided by the 2nd


operand

Order of Precedence

Operators Precedence
++ - - 1
* / % 2
+ - 3
Example 2.1. Evaluate the expression:
Num1 = 5 % 5 * 5 + 5 / 5;
= 0 * 5 + 5 / 5;
= 0 + 5 / 5;
= 0 + 1;
= 1;

Example 2. Find the Result of the Expression:


Result = 8 * (4 % (3 + 2)) – 6 * 7;
= 8 * (4 % 5) – 6 * 7;
= 8 * 4 – 6 * 7;
= 32 – 6 * 7;
= 32 – 42;
= -10;

Example 3. What will be the value of x:


a = 45;
b = a / 3;
c = b - 7;

x = a + b * c;

//Solution:

b = a / 3; //a = 45
b = 15;
c = b – 7; //b = 15
c = 8;

x = a + b + c;
x = 45 + 15 + 8;
x = 68;

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 Baon != 100

Logical operator – a symbol that defines the logical connection between


two or more conditions.
Operator Meaning
! NOT (reverse the truth value of a
condition)
&& AND (both conditions must be true)
|| OR (at least one condition must be
true)

! OPERATION

Condition Result
True False
False True

&& OPERATOR

Condition 1 Condition 2 Result


True True True
True False False
False True False
False False False

|| OPERATOR

Condition 1 Condition 2 Result


True True True
True False True
False True True
False False False

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


1. Expense is higher than profit.
Answer: Expense > profit

2. Grade must not lower than 75.


Answer: Grade >= 75

3. Num is a positive number


Answer: Num > 0
4. Age must be greater than 18 but not greater than 30
Answer: (Age > 18) && (Age <= 30)

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

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

Example 5. Construct a correct programming expression of the following:


1. a = Vf - Vi
t

Answer: a = (Vf - Vi) / t

2. Rtotal = 1 _
1 + 1 + 1
R1 R2 R3

Answer: Rtotal = 1 / (1/R1 + 1/R2 + 1/R3)

Components of C Program

1. 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
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>

LIBRARY NAME DESCRIPTION

Ctype.h Function for case conversion and for testing characters (for
example, checking for uppercase or lowercase letters or for
special characters or blanks.
Float.h 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).
Limits.h 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).
Math.h Mathematics functions such as square root, trigonometric, etc.
Stdlib.h Function for number conversion, memory allocation, sorting,
searching, random-number generation, and program
termination.
Stdio.h Function containing the printf() and scanf() function.
String.h Sting manipulating functions for comparing, concatenating, and
copying, string, and for testing for the presence of specific
characters or substrings.
Time.h Functions for manipulating date and time.

2. main() Function

The only component that is require 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, that the braces are statements that makes upon the main
body of the program.

3. {}

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

4. Comment

a. // - when used, all characters at the right of the symbols will be treated
comment.
EXAMPLE:
int a; // variable a will hold the input value

b. /* */ - comments must enclose by this fair of symbols


EXAMPLE:
/* A sample comment line which can be placed anywhere in the program */

5. printf() Function

A library function that is used to display information on the screen.

Syntax:
printf(“ control string “, argument list);
where:
1. control string - contains characters to be displayed or format codes that
tell how to display the rest of the argument.
2. argument list – list of values or/and variables.

Format Code Meaning


%d or %i Display an integer
%ld Display a long integer
%f Display a float
%lf Display a double
%c Display a character
%s Display a string
%o Display an octal number
%x Display a hexadecimal number

Example 6.

Given: x = 99 my_name = “Joan”

a. printf (“C Programming Lecture” );


OUTPUT: C Programming Lecture

b. printf(“%d”, );
OUTPUT: 99

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


OUTPUT: Total no. of female students = 99

d. printf(“Hi! I am %s and I am %d years old.”, my_name, 20)


OUTPUT: Hi! I am Joan and I am 20 years old.

Backslash Characters

Code Meaning
\n Newline
\t Horizontal tab
\a 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:
gets(string variable);

EXAMPLES:
gets(name);
gets(my_address);

9. gotoxy() function – go to xy location

A function that used to locate the cursor to specified column and row.

Syntax:
gotoxy (row, column);
Example:
gotoxy (5,3);
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\n\n\n\n\t\t\t\tHELLO WORLD!");
getch();
}

Example 2. Build a C program that will input three integers and will display
the sum and the average.

#include<stdio.h>
#include<conio.h>
int num1, num2, num3, sum;
float ave;
main()
{
printf("Enter any integer num1: ");
scanf("%d",&num1);
printf("Enter any integer num2: ");
scanf("%d",&num2);
printf("Enter any integer num3: ");
scanf("%d",&num3);
sum=num1+num2+num3;
ave=sum/3.0;
printf("Sum is = %d\n",sum);
printf("Average is = %.2f",ave);
getch();
}

You might also like