Chapter 9 Introduction To C
Chapter 9 Introduction To C
Fundamentals and
Programming in C
2nd Edition
Reema Thareja
1
© Oxford University Press 2016. All rights reserved.
CHAPTER 9
INTRODUCTION TO C
USES OF C {
Statement 1;
Statement 2;
• C language is primarily used for system programming. The portability, ……………
……………
efficiency, the ability to access specific hardware addresses and low runtime Statement N;
}
demand on system resources makes it a good choice for implementing Function1()
{
operating systems and embedded system applications. Statement 1;
Statement 2;
• C has been so widely accepted by professionals that compilers, libraries, and ……………
……………
interpreters of other programming languages are often implemented in C. Statement N;
}
• For portability and convenience reasons, C is sometimes used as an Function2()
{
intermediate language by implementations of other languages. Example of Statement 1;
Statement 2;
compilers which use C this way are BitC, Gambit, the Glasgow Haskell ……………
……………
Compiler, Squeak, and Vala. Statement N;
}
• C is widely used to implement end-user applications ………………….
………………….
FunctionN()
{
Statement 1;
Statement 2;
……………
STRUCTURE OF A C PROGRAM ……………
Statement N;
}
A C program contains one or more functions
The statements in a C program are written in a logical sequence to perform a specific task.
Execution of a C program begins at the main() function
You can choose any name for the functions. Every program must contain one function that has its name
as main().
main()
{
Local Declarations;
Statement 1;
……………
Statement N;
}
Function1()
{
Local Declarations;
Statement 1;
……………
Statement N;
}
-----------------
-----------------
FunctionN()
{
Local Declarations;
Statement 1;
……………
Statement N;
}
5
© Oxford University Press 2016. All rights reserved.
FILES IN A C PROGRAM
Files in a C program
6
© Oxford University Press 2016. All rights reserved.
COMPILING AND EXECUTING C PROGRAMS
#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
return 0;
}
DATA TYPES IN C
Variables
To declare a variable specify data type of the variable followed
by its name.
Variable names should always be meaningful and must reflect
the purpose of their usage in the program.
Numeric Variable Character Variables
Variable declaration always ends with a semicolon. Example,
int emp_num;
float salary;
char grade;
double balance_amount;
unsigned short int acc_no;
© Oxford University Press 2016. All rights reserved.
CONSTANTS
• Constants are identifiers whose value does not change.
• Constants are used to define fixed values like PI or the charge on an electron so that their value does not
get changed in the program even by mistake.
• To declare a constant, precede the normal variable declaration with const keyword and assign it a value.
For example,
const float pi = 3.14;
• Another way to designate a constant is to use the pre-processor command define.
#define PI 3.14159
When the preprocessor reformats the program to be compiled by the compiler, it replaces each defined
name with its corresponding value wherever it is found in the source program. Hence, it just works like
the Find and Replace command available in a text editor.
Streams in a C
program
Monitor Data
Divide / a / b result = a / b 3
ARITHMETIC
Addition + a + b result = a + b 12
OPERATORS
Subtraction - a - b result = a – b 6
Modulus % a % b result = a % b 0
EQUALITY OPERATORS
• C language supports two kinds of equality operators to compare their operands for strict equality or
inequality. They are equal to (==) and not equal to (!=) operator.
• The equality operators have lower precedence than the relational operators.
OPERATOR MEANING
A B A &&B A B A || B A !A
0 0 0 0 0 0
0 1
0 1 0 0 1 1
1 0 0 1 0 1
1 0
1 1 1 1 1 1
UNARY OPERATORS
Unary operators act on single operands. C language supports three unary operators. They are
unary minus, increment and decrement operators.
When an operand is preceded by a minus sign, the unary operator negates its value.
The increment operator is a unary operator that increases the value of its operand by 1. Similarly,
the decrement operator decreases the value of its operand by 1. For example,
int x = 10, y;
y = x++;
is equivalent to writing
y = x;
x = x + 1; whereas, y = ++x;
is equivalent to writing
x = x + 1;
y = x;
© Oxford University Press 2016. All rights reserved.
CONDITIONAL OPERATOR
• The conditional operator operator (?:) is just like an if .. else statement that can be written within
expressions.
• The syntax of the conditional operator is
exp1 ? exp2 : exp3
Here, exp1 is evaluated first. If it is true then exp2 is evaluated and becomes the result of the expression,
otherwise exp3 is evaluated and becomes the result of the expression. For example,
large = ( a > b) ? a : b
• Conditional operators make the program code more compact, more readable, and safer to use as it is
easier both to check and guarantee that the arguments that are used for evaluation.
• Conditional operator is also known as ternary operator as it is neither a unary nor a binary operator; it
takes three operands.
• The assignment operator is responsible for assigning values to the variables. While the equal sign (=) is the
fundamental assignment operator, C also supports other assignment operators that provide shorthand ways to
represent common variable assignments. They are shown in the table.
SIZEOF OPERATOR
• sizeof is a unary operator used to calculate the sizes of data types.
• It can be applied to all data types.
• The operator returns the size of the variable, data type or expression in bytes.
• 'sizeof' operator is used to determine the amount of memory space that the
variable/expression/data type will take. For example,
• sizeof(char) returns 1, that is the size of a character data type. If we have,
int a = 10;
unsigned int result;
result = sizeof(a);
then result = 2,
float x;
int y = 3;
x = y;
Now, x = 3.0,
• Type casting is also known as forced conversion. It is done when the value of a higher data
type has to be converted in to the value of a lower data type. For example, we need to explicitly
type cast an integer variable into a floating point variable.
27
© Oxford University Press 2016. All rights reserved.
PROGRAMS
• Write a C program to swap two numbers using
a temp variable.
• Write a C program to swap two numbers
without using a temp variable.
• Write a C program that displays the size of
every data type.
• Write a C program to calculate the total
amount of money in the piggybank.
(denomination is coins)
28
© Oxford University Press 2016. All rights reserved.
PROGRAMS
• Write a C program to calculate the bill amount
for an item given its quantity sold, value,
discount and tax.
29
© Oxford University Press 2016. All rights reserved.