Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 51

Chapter 2

Introduction to the C Language


Objectives
❏ To understand the structure of a C-language program.
❏ To write your first C program.
❏ To introduce the include preprocessor command.
❏ To be able to create good identifiers for objects in a program.
❏ To be able to list, describe, and use the C basic data types.
❏ To be able to create and use variables and constants.
❏ To understand input and output concepts.
❏ To be able to use simple input and output statements.

Computer Science: A Structured Programming Approach Using


C
1
2-1 Background

C is a structured programming language. It is


considered a high-level language because it allows the
programmer to concentrate on the problem at hand
and not worry about the machine that the program will
be using. That is another reason why it is used by
software developers whose applications have to run on
many different hardware platforms.

Computer Science: A Structured Programming Approach Using


C
2
General Aspect of C
C was originally developed in the 1970s, by Dennis
Ritchie at Bell Telephone Laboratories, Inc.
C is a High level , general –purpose structured
programming language. Instructions of C consists of
terms that are very closely same to algebraic
expressions, consisting of certain English keywords
such as if, else, for ,do and while
C contains certain additional features that allows it to
be used at a lower level , acting as bridge between
machine language and the high level languages.
This allows C to be used for system programming as
well as for applications programming
Computer Science: A Structured Programming Approach Using
C
3
2-2 C Programs

It's time to write your first C program.

Topics discussed in this section:


Structure of a C Program
Your First C Program
Comments
The Greeting Program

Computer Science: A Structured Programming Approach Using


C
4
FIGURE 2-2 Structure of a C Program
Computer Science: A Structured Programming Approach Using
C
5
2-3 Basic term used in C program
Variable:- It is the address of memory location where
values can store.eg a = 10; Value 10 is store into
Variable a _
Datatype :- Datatype tells the compiler which types of
value an variable can store. Eg char a= “nitin”; here
variable a store an integer value 5
There are basically 2 types of Datatypes 1 Primary 2
Secondary
Keyword:- keyword are the word whose meaning
already known by compiler eg int a=5; here int mean
integer no. whose meaning already known by compiler.
There are 32 keyword in c
Computer Science: A Structured Programming Approach Using
C
6
2-3 Basic term used in C program

Header file:- Header file are the file which contain some
predefined function eg printf(), scanf() function defined
in Stdio.h file, clrscr(),getch() function defined in conio.h
file,pow(),sqrt() function defined in math.h header file.

Syntax for writing header file

#include<stdio.h> or #include<math.h>
#include ”stdio.h” or #include “math.h”

Computer Science: A Structured Programming Approach Using


C
7
2-3 Basic term used in C program
Input Function:- Scanf is the input Function which is
used to take input from user through keyboard.
Scanf(“%d”,&a);
Output Function:- printf () function is the output
function which is used to display output on Monitor
screen. Eg printf(“value of a =%d”,a);

Format Specifier:- Symbol to represent datatype:


Eg:- %d used for Integer
%f used for Float
%c used for character
%s used for string
Computer Science: A Structured Programming Approach Using
C
8
FIGURE 2-3 The Greeting Program
Computer Science: A Structured Programming Approach Using
C
9
PROGRAM 2-1 The Greeting Program

Computer Science: A Structured Programming Approach Using


C
10
FIGURE 2-4 Examples of Block Comments
Computer Science: A Structured Programming Approach Using
C
11
FIGURE 2-5 Examples of Line Comments
Computer Science: A Structured Programming Approach Using
C
12
2-3 Identifiers

One feature present in all computer languages is the


identifier. Identifiers allow us to name data and other
objects in the program. Each identified object in the
computer is stored at a unique address.

Computer Science: A Structured Programming Approach Using


C
13
Table 2-1 Rules for Identifiers

Computer Science: A Structured Programming Approach Using


C
14
Note
An identifier must start with a letter or underscore:
it may not have a space or a hyphen.

Computer Science: A Structured Programming Approach Using


C
15
Note
C is a case-sensitive language.

Computer Science: A Structured Programming Approach Using


C
16
Table 2-2 Examples of Valid and Invalid Names

Computer Science: A Structured Programming Approach Using


C
17
Keywords
• Keywords are nothing but auto double int struct
system defined identifiers.
break else long switch
• Keywords are reserved words
of the language.
case enum register typedef
• They have specific meaning in
the language and cannot be char extern return union
used by the programmer as
variable or constant names const float short unsigned
• C is case senitive, it means these
must be used as it is continue for signed void
• 32 Keywords in C Programming
default goto sizeof volatile

do if static while
2-4 Types

A type defines a set of values and a set of operations


that can be applied on those values.

Topics discussed in this section:


Void Type
Integral Type
Floating-Point Types
Computer Science: A Structured Programming Approach Using
C
19
FIGURE 2-7 Data Types
Computer Science: A Structured Programming Approach Using
C
20
FIGURE 2-8 Character Types
Computer Science: A Structured Programming Approach Using
C
21
FIGURE 2-9 Integer Types
Computer Science: A Structured Programming Approach Using
C
22
Note
sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)

Computer Science: A Structured Programming Approach Using


C
23
Table 2-3 Typical Integer Sizes and Values for Signed Integers

Computer Science: A Structured Programming Approach Using


C
24
FIGURE 2-10 Floating-point Types
Computer Science: A Structured Programming Approach Using
C
25
Note
sizeof (float) ≤ sizeof (double) ≤ sizeof (long double)

Computer Science: A Structured Programming Approach Using


C
26
Table 2-4 Type Summary

Computer Science: A Structured Programming Approach Using


C
27
2-5 Variables

Variables are named memory locations that have a type,


such as integer or character, which is inherited from their
type. The type determines the values that a variable may
contain and the operations that may be used with its
values.

Topics discussed in this section:


Variable Declaration
Variable Initialization

Computer Science: A Structured Programming Approach Using


C
28
FIGURE 2-11 Variables
Computer Science: A Structured Programming Approach Using
C
29
Table 2-5 Examples of Variable Declarations and Definitions

Computer Science: A Structured Programming Approach Using


C
30
‘B’

FIGURE 2-12 Variable Initialization


Computer Science: A Structured Programming Approach Using
C
31
Global and Local Variables

• Global Variables /*
/* Compute
Compute Area
circle
circle */
*/
Area and
and Perimeter
Perimeter of
of aa
#include <stdio.h>
• These variables are #include <stdio.h>
float
declared outside all float pi
pi == 3.14159;
3.14159; /* /* Global
Global */
*/
functions. main()
main() {{
• Life time of a global float
float rad;
rad;/*
/* Local
Local */
*/
variable is the entire printf(
execution period of the printf( “Enter
“Enter the
the radius
radius ““ );
);
scanf(“%f” , &rad);
scanf(“%f” , &rad);
program.
• Can be accessed by any if
if (( rad
rad >> 0.0
0.0 )) {{
float
float area == pi
area pi ** rad
rad ** rad;
rad;
function defined below the float
declaration, in a file. float peri
peri == 22 ** pi
pi ** rad;
rad;
printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf(
printf( “Peri
“Peri == %f\n”
%f\n” ,, peri
peri );
);
}}
else
else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
}}

Lectures on Numerical Methods 32


Global and Local Variables

• Local Variables /*
/* Compute
Compute Area
circle
circle */
*/
Area and
and Perimeter
Perimeter of
of aa
#include <stdio.h>
• These variables are #include <stdio.h>
float
declared inside some float pi
pi == 3.14159;
3.14159; /* /* Global
Global */
*/
functions. main()
main() {{
• Life time of a local float
float rad;
rad;/*
/* Local
Local */
*/
variable is the entire printf(
execution period of the printf( “Enter
“Enter the
the radius
radius ““ );
);
scanf(“%f” , &rad);
scanf(“%f” , &rad);
function in which it is
defined. if
if (( rad
rad >> 0.0
0.0 )) {{
float
float area == pi
area pi ** rad
rad ** rad;
• Cannot be accessed by any float
rad;
other function. float peri
peri == 22 ** pi
pi ** rad;
rad;
• In general variables printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
printf(
printf( “Peri
“Peri == %f\n”
%f\n” ,, peri
peri );
declared inside a block }}
);
are accessible only in else
that block. else
printf(
printf( “Negative
“Negative radius\n”);
radius\n”);
printf(
printf( “Area
“Area == %f\n”
%f\n” ,, area
area );
);
}}

Lectures on Numerical Methods 33


Note
When a variable is defined, it is not initialized.
We must initialize any variable requiring
prescribed data when the function starts.

Computer Science: A Structured Programming Approach Using


C
34
PROGRAM 2-2 Print Sum of Three Numbers

Computer Science: A Structured Programming Approach Using


C
35
PROGRAM 2-2 Print Sum of Three Numbers (continued)

Computer Science: A Structured Programming Approach Using


C
36
PROGRAM 2-2 Print Sum of Three Numbers (continued)

Computer Science: A Structured Programming Approach Using


C
37
2-6 Constants

Constants are data values that cannot be changed during


the execution of a program. Like variables, constants
have a type. In this section, we discuss Boolean,
character, integer, real, complex, and string constants.

Topics discussed in this section:


Constant Representation
Coding Constants

Computer Science: A Structured Programming Approach Using


C
38
Note
A character constant is enclosed in single quotes.

Computer Science: A Structured Programming Approach Using


C
39
Table 2-6 Symbolic Names for Control Characters
Computer Science: A Structured Programming Approach Using
C
40
Table 2-7 Examples of Integer Constants

Computer Science: A Structured Programming Approach Using


C
41
Table 2-8 Examples of Real Constants

Computer Science: A Structured Programming Approach Using


C
42
FIGURE 2-13 Some Strings
Computer Science: A Structured Programming Approach Using
C
43
FIGURE 2-14 Null Characters and Null Strings
Computer Science: A Structured Programming Approach Using
C
44
Note
Use single quotes for character constants.
Use double quotes for string constants.

Computer Science: A Structured Programming Approach Using


C
45
Escape Sequences

Sometimes, it is necessary to use characters which cannot be typed or has special


meaning in C programming. For example: newline(enter), tab, question mark etc. In
order to use these characters, escape sequence is used.
• For example: \n is used for newline. The backslash ( \ ) causes "escape" from the
normal way the characters are interpreted by the compiler.Escape
Sequences Character
• \b Backspace
• \f Form feed
• \n Newline
• \r Return
• \t Horizontal tab
• \v Vertical tab
• \\ Backslash
• \' Single quotation mark
• \" Double quotation mark
• \? Question mark
• \0 Null character
Operators in C:An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.

C programming has wide range of operators to perform various


operations. For better understanding of operators, these
operators can be classified as:
• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional Operators
• Bitwise Operators
• Special Operators
Arithmetic Operator

• Operator Meaning of Operator


•+ addition or unary plus
•- subtraction or unary minus
•* multiplication
•/ division
•% remainder after division( modulo
division)
Increment and Decrement Operators

1. C programming has two operators increment ++ and


decrement -- to change the value of an operand (constant
or variable) by 1.
2. Increment ++ increases the value by 1 whereas
decrement -- decreases the value by 1.
3. These two operators are unary operators, meaning they
only operate on a single operand.
eg. int a=10, b=100
If(a--)
Printf(“a”);
--b = 99
C Assignment Operators

• An assignment operator is used for assigning a value


to a variable. The most common assignment operator
is =
• Operator Example Same as
•= a=b a=b
• += a += b a = a+b
• -= a -= b a = a-b
• *= a *= b a = a*b
• /= a /= b a = a/b
• %= a %= b a = a%b
C Relational Operators
• A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is
false, it returns value 0.
• Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
• == Equal to 3 == 3 returns T
•> Greater than 5 > 3 returns 1
•< Less than 5 < 3 returns 0
• != Not equal to 5 != 3 returns 1
• >= Greater than or equal to 5 >= 3 returns 1
• <= Less than or equal to 5 <= 3 return 0

You might also like