Introduction-to-C-Language (1)
Introduction-to-C-Language (1)
History of C Language
There are several reasons why many computer professionals feel that
C is on top of the heap among the other high-level programming languages.
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.
Hot Keys – located at the bottom of the environment and let you type
shortcut keys for choosing a menu.
Identifiers
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.
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.
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;
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;
! OPERATION
Condition Result
True False
False True
&& OPERATOR
|| OPERATOR
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
2. Rtotal = 1 _
1 + 1 + 1
R1 R2 R3
Components of C Program
EXAMPLES:
# include <stdio.h>
# include <math.h>
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
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
5. printf() Function
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.
Example 6.
b. printf(“%d”, );
OUTPUT: 99
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
Syntax:
puts(“argument”);
EXAMPLE:
puts(“This is a sample using puts!”);
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);
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();
}