Chapter 9 Introduction To C@4th Nov
Chapter 9 Introduction To C@4th Nov
Fundamentals and
Programming in C
2nd Edition
Reema Thareja
1
© Oxford University Press 2016. All rights reserved.
CHAPTER 9
INTRODUCTION TO C
• Facilitates low level (bitwise) programming. C allows direct manipulation of hardware and memory
through bitwise operators, providing greater control and efficiency at a low level, closer to machine code.
• C is a portable language. C programs can be compiled and run on various computer architectures with minimal
changes, making it highly portable.
• C is an extensible language
© Oxford University Press 2016. All rights reserved.
main()
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 demand on system Statement N;
}
resources makes it a good choice for implementing operating systems and embedded Function1()
system applications. {
Statement 1;
• C has been so widely accepted by professionals that compilers, libraries, and Statement 2;
……………
interpreters of other programming languages are often implemented in C. ……………
Statement N;
• For portability and convenience reasons, C is sometimes used as an intermediate }
Function2()
language by implementations of other languages. Example of compilers which use C {
Statement 1;
this way are BitC, Gambit, the Glasgow Haskell Compiler, Squeak, and Vala. Statement 2;
……………
• C is widely used to implement end-user applications. ……………
Statement N;
}
………………….
………………….
FunctionN()
STRUCTURE OF A C PROGRAM {
Statement 1;
Statement 2;
……………
……………
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().
Global declarations
main( )
{
Local declarations
Statements
}
Function 1()
{
Local declarations
Statements
}
-------------------------------
Function N()
{
Local declarations
Statements
}
© Oxford University Press 2016. All rights reserved.
YOUR FIRST C PROGRAM
#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
return 0;
}
// user only defines the steps of the function and also defines the
4) User defined function name of the function
stdio.h : standard input & output ( functions like printf() ,scanf() are defined.)
math.h : Math functions like pow(), sin(), log() , sqrt() are defined.
These are called as formatted Input and output functions because they have special
format to read and write The text from streams and then converting them into binary
stream.
Syntax of printf() :
printf ( “ control string” , var1, var2 …..varn).
Printf (“Hello welcome to C program”)
Printf ( “ %d %d %f ” , a , b , c), where a and b are integer variables and c is float
variable.
%d and %f are known as format specifiers.
Syntax of scanf() : scanf ( “ Control string” , & var1, & var2,.. ,& var n). Format
specifiers : For integer variables ------ %d
For float variables --------%f, For character variables ------- %c
© Oxford University Press 2016. All rights reserved.
Input Statements ( reading input)
The preprocessor in programming is a tool that processes the source code before it is
passed to the compiler. It handles directives, typically starting with #, and performs
various transformations. In C, C++, and similar languages, the preprocessor plays an
essential role in preparing the code for compilation .
\r- carriage return: Moves the cursor to the beginning of the current
line without advancing to the next line.
\f – form feed: Advances the cursor to the start of the next page or
screen or clears the current display.
\n – new line: moves the cursor to the new line
-> Escape Sequence: They allow programmers to control formatting, insert
special characters, and handle things like newlines, tabs, and quotes within strings.
Variables
Declaring Variables:
To declare a variable specify data type of the variable followed by its name.
Variable declaration always ends with a semicolon.
int emp_num;
float salary;
char grade;
double balance_amount;
unsigned short int acc_no;
Variables declared inside the function called as local variables and outside all the
functions, called as global variables.
Initializing Variables:
We can also initialize variables with some value.
int emp_num=7;
float salary=2156.35;
char grade=‘A’
int count, flag=1; will initialize only one variable flag to 1.
int count=0, flag=1; will initialize count to 0 and flag to 1.
© Oxford University Press 2016. All rights reserved.
CONSTANTS
• Constants are identifiers whose value does not change.
• Constants are used to define fixed values like mathematical constant PI or the
charge on an electron so that their value does not get changed in the program even
by mistake.
How to Define Constants in C: 4 types – int, floating point, char, string
1.Using #define Directive:
•The #define directive is used to create named constants.
#define PI 3.14
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; int a, b=10;
is equivalent to writing a = -(b);
x = x + 1; The result is a=-10 bcz variable b has positive
value
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.
Example : Int a=5, b=3, c=7, small;
small = (a<b ? (a < c ? a : c) : (b < c ? b : c ));
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);
© Oxford University Press 2016. All rights reserved.
TYPE CONVERSION AND TYPE CASTING
• Type conversion and type casting of variables refers to changing a variable of one data
type into another.
• While type conversion is done implicitly, casting has to be done explicitly by the
programmer. We will discuss both of them here.
• Type conversion is done when the expression has variables of different data types. So to
evaluate the expression, the data type is promoted from lower to higher level where
the hierarchy of data types can be given as: double, float, long, int, short and char.
• For example, type conversion is automatically done when we assign an integer value to
a floating point variable. For ex,
float x; int y = 3; x = y;
Now, x = 3.0,
int a = 5;
float b = 25, c; ab c
c = a / b; printf(“%f” , c); 5 25.0 0.25
• 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.
float salary = 10000.00;
int sal;
sal = (int) salary;
• Typecasting can be done by placing the destination data type in
parentheses followed by the variable name that has to be converted.
int a = 7, c;
a b c
float b = 4.0;
b = a % (int) b; 7 4.0 3
printf(“%d” , c);
b -> Converted into int & evaluated
X=3*4+5*6 X = 3 * (4 + 5) * 6 X=3*4%5/2 X = 3 * (4 % 5) / 2
= 12 + 5 * 6 =3*9*6 = 12 % 5 / 2 =3*4/2
= 12 + 30 = 27 * 6 =2/2 =3*2
= 42 = 42 =1 =6