Professional Documents
Culture Documents
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.
Page | 1
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.
Data Type
-
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
Expressions
Kind of Expressions
Arithmetic an expression that contains arithmetic operators and operands
which can be reduced to a single numeric value.
Arithmetic Expression
Arithmetic operator a symbol used to represent a mathematical operation.
Operator
Operation
Increment
Action
Increase the operand by 1.
a++
Decrement
z--
num1 + num2
Salary - Deduction
++
-+
-
Subtraction
Multiplication
Division
Modulus
Addition
Example
Grade * units
Total / 100
Minutes % 60
Order of Precedence
Operators
++
--
Precedence
1
Page | 3
+ -
= 0 * 5 + 5 / 5;
= 0 + 5 / 5;
= 0 + 1;
= 1;
//a = 45
//b = 15
x = a + b * c;
x = 45 + 15 * 8;
x = 165;
Relational Expression
Page | 4
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
>=
<=
!=
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
CONDITION 2
False
True
False
True
RESULT
False
False
False
True
Page | 5
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
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
Page | 6
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.
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
// (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
Page | 8
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
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
Page | 11