3.structure of C 9.1-9.14
3.structure of C 9.1-9.14
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 demand on system Statement N;
}
resources makes it a good choice for implementing operating systems and embedded Function1()
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 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().
int main () {
int y; Local Declaration
Function printf("Enter x and y: ");
scanf(&x,&y); Statements
printf("Sum is %d\n",x+y);
}
#include<stdio.h>
int main()
{
printf("\n Welcome to the world of C ");
return 0;
}
Library
Files
20
© Oxford University Press 2016. All rights reserved.
21
© Oxford University Press 2016. All rights reserved.
C Tokens
Tokens are the basic building blocks in C language. A C token can be thought
of a smallest
•individual unit in a C program. A C program is constructed using a
combination of these
•tokens. There are six main types of C tokens and are as follows:
a) Keywords
b) Variables
c) Constants
d) Strings
e) Special Characters
f) Operators
22
© Oxford University Press 2016. All rights reserved.
USING COMMENTS
• It is a good programming practice to place some comments in the code to help the reader understand
the code clearly.
• Comments are just a way of explaining what a program does. It is merely an internal program
documentation.
• The compiler ignores the comments when forming the object file. This means that the comments are
non-executable statements.
C supports two types of commenting.
• // is used to comment a single statement. This is known as a line comment. A line comment can be
placed anywhere on the line and it does not require to be specifically ended as the end of the line
automatically ends the line.
• /* is used to comment multiple statements. A /* is ended with */ and all statements that lie within these
characters are commented.
KEYWORDS
• C has a set of 32 reserved words often known as keywords. All keywords are basically a sequence of
characters that have a fixed meaning. By convention all keywords must be written in lowercase (small)
letters.
• Example: for, while, do-while, auto break, case, char, continue, do, double, else,
enum, extern, float, goto, if, int, long, register, return, short, signed, sizeof, static,
struct, switch, typedef, union, unsigned, void, volatile
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.
Character Variable
Variable declaration always ends with a semicolon. Example,Numeric Variable
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.
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.
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.
© Oxford University Press 2016. All rights reserved.