0% found this document useful (0 votes)
11 views

Module1 C Tokens & Input Output Stataments

This document provides an introduction to the C programming language. It covers basic concepts like data types, operators, and expressions. The syllabus outlines pseudo code solutions, declaration statements, and programming examples. Key topics include character sets, tokens like keywords and identifiers, and constants like integers, reals, and enumerations. Programming in C provides structured programming and efficiency while being machine independent.

Uploaded by

karanphutane2254
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module1 C Tokens & Input Output Stataments

This document provides an introduction to the C programming language. It covers basic concepts like data types, operators, and expressions. The syllabus outlines pseudo code solutions, declaration statements, and programming examples. Key topics include character sets, tokens like keywords and identifiers, and constants like integers, reals, and enumerations. Programming in C provides structured programming and efficiency while being machine independent.

Uploaded by

karanphutane2254
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Programming in C and Data Structures Module 1

MODULE 1
INTRODUCTION TO C LANGUAGE
Syllabus
 Pseudo code Solution to Problem
 Basic Concepts of C Program
 Declaration, Assignment & Print Statements
 Data Types
 Operators and Expressions
 Programming Examples and Exercise.
1.1 Introduction to C
 C is a general-purpose programming language developed by Dennis Ritchie at AT&T
Bell laboratories in 1972.
Advantages/Features of C Language
C language is very popular language because of the following features:
1. C is structured Programming Language
2. It is considered a high-level language because it allows the programmer to solve a
problem without worrying about machine details.
3. It has wide variety of operators using which a program can be written easily to solve a
given problem.
4. C is more efficient which increases the speed of execution and management of
memory compared to low level languages.
5. C is machine independent. The program written on one machine will work on another
machine.
6. C can be executed on many different hardware platforms.
1.2 Pseudocode: A solution to Problem
 Definition:
 It is a series of steps to solve a given problem written using a mixture of English and
C language.
 It acts as a problem solving tool.
 It is the first step in writing a program.

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 1
Programming in C and Data Structures Module 1

 Purpose:
 Is to express solution to a given problem using mixture of English language
and c like code.
 Advantage:
 Easy to write and understand
 It is relatively easy to convert English description solution of small programs to C
program.
Ex 1: Addition of two numbers
1. Get the numbers[a,b]
2. Compute addition [Sum= a + b]
3. Print the results [Sum]
Ex 2: Area of Circle
1. Get the radius[r]
2. Compute area[Area = 3.141*r*r]
3. Print the results [Area]
Disadvantage:
 It is very difficult to translate the solution of lengthy and complex problem in English to
C

C Programming Concepts
 Program: A program is a group of instructions given by the programmer to perform a
specific task.
1.3 Character set of C language
 Definition: A symbol that is used while writing a program is called a character.
 A character can be: .
 Alphabets/Letters (Lowercase a-z, Uppercase A-Z)
 Digits (0-9)
 Special Symbols ( ~ ‘ ! @ # % & * () - + / $ = \ {
 } [ ] : ; “ “ ? etc)

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 2
Programming in C and Data Structures Module 1

Symbol Name Symbol Name Symbol Name

~ | [
Tilde Vertical bar Left bracket

# hash ( Left parenthesis ]


Right bracket

$ Dollar sign ) Right parenthesis


: Colon

% Percent sign _ Underscore ” Quotation


mark

^ + Plus sign ;
Caret Semicolon

& Ampersand { <


Left brace Less than

* Asterisk } Greater than


Right brace >

Assignment
‘ Single quote . dot =

Division
, comma \ backslash /

1.4 C Tokens
 Tokens are the smallest or basic units of C program.
 One or more characters are grouped in sequence to form meaningful words.these
meaningful words are called as tokens.
 A token is collection of characters.
 Tokens are classified in to 5 types as below:

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 3
Programming in C and Data Structures Module 1

1.4.1 Keywords
 The tokens which have predefined meaning in C language are called keywords.
 They are reserved for specific purpose in C language they are called as Reserved
Words.
 There are totally 32 keywords supported in C they are:
auto double if static
break else int struct
case enum long switch
char extern near typedef
const float register union
continue for return unsigned
default volatile short void
do goto signed while

Rules for keywords


1. Keywords should not be used as variables ,function names, array names etc.
2. All keywords should be written in lowercase letters.
3. Keywords meaning cannot be changed by the users.
1.4.2 Identifiers
Definition:
 Identifiers are the names given to program elements such as variables, constants ,function
names, array names etc
 It consists of one or more letters or digits or underscore.
Rules for identifiers
1. The First character should be an alphabet or an underscore _
Then First character is followed by any number of letters or digits.
Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT
Page 4
Programming in C and Data Structures Module 1

2. No extra symbols are allowed other than letters ,digits and Underscore
3. Keywords cannot be used as an identifier
4. The length can be 31 characters for external, 63 for internal.
5. Identifiers are case sensitive.
Example: Area, Sum_

Ex:-
Identifier Reasons for invalidity
india06 Valid
_india Valid
india_06 Valid
india_06_king Valid

__india valid
06india not valid as it starts from digits
int not valid since int is a keyword
india 06 not valid since there is space between india and
06
india@06 not valid since @ is not allowed

1.4.3 Constants
Definition:
 Constants refers to fixed values that do not change during the execution of a program
 The different types of constants are:
1. Integer constant
2. Real constant/Floating Pointing constant
3. Enumeration constant
4. Character constant
5. String constant
1. Integer constant
 Definition: An integer is whole number without any decimal point.no extra characters
are allowed other than + or _ .
Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT
Page 5
Programming in C and Data Structures Module 1

 Three types of integers


 Decimal integers: are constants with a combination of digits 0 to 9,optional + or -
Ex: 123 , -345, 0 , 5436 , +79
 Octal integers: are constants with a combination of Digits from 0 to 7 but it has a
prefix of 0
Ex: 027 , 0657 , 0777645
 Hexadecimal integers: Digits from 0 to 9 and characters from a to f, it has to start
with 0X or 0x
Ex: 0X2 0x56 0X5fd 0xbdae

2. Real constants/Floating point:


 Floating point constants are base 10 numbers that are represented by fractional
parts, such as 10.5.They can be positive or negative.
 Two forms are:
i. Fractional form:
 A floating point number represented using fractional form has an integer part
followed by dot and a fractional part.
 Ex: 0.0083
 215.5
 -71.
 +0.56 etc..
ii. Exponential form:
 A floating point number represented using Exponent form has 3 parts
 mantissa e exponent
 Mantissa is either real number expressed in decimal notation or integer.
 Exponent must be integer with optional + or –
 Ex 9.86 E 3 => 9.86*103
 Ex 9.86 E -3 => 9.86*10-3
3. Enumeration constant
 A set of named integer constants defined using the keyword enum are called
enumeration constants.
 Syntax:
enum identifier{enum list};

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 6
Programming in C and Data Structures Module 1

 enum Boolean{NO,YES};
NO is assigned with 0
YES is assigned with value 1
 enum days{ mon,tue.wed};
mon is assigned with 0
tue is assigned with 1
wed is assigned with 2
4.Character constant
 A symbol enclosed within pair of single quotes is called character constant.
 Each character is associated with unique value called ASCII value.
 Ex: ‘9’
 ‘$’
 Backslash constants(Escape sequence character)
 Definition: An escape sequence character begins with backslash and is followed
by one character.
 A backslash along with a character give rise to special print effects.
 It always start with backslash ,hence they are called as backslash constants.
character name Meaning

\a Bell Beep sound

\b Backspace Cursor moves towards left by one


position
\n Newline Cursor moves to next line

\t Horizontal tab Cursor moves towards right by 8


position
\r Carriage return Cursor moves towards beginning of the
same line
\” Double quotes Double quotes

\’ Single quotes Single quotes

\? Question mark Question mark

\\ Backslash Backslash

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 7
Programming in C and Data Structures Module 1

\0 NULL

5.String constant
 A sequence of characters enclosed within pair of double quotes is called string constant.
 The string always ends with a NULL character.
 Ex: “9”
 “SVIT”

1.4.4 Data types and sizes


 Definition: The data type defines the type of data stored in memory location or the type
of data the variable can hold.
 The classification of data types are:

 There are few basic data types supported in C.


1. int
2. float
3. double
4. char
5. void
1.Integer data type (int):
 It stores an integer value or integer constant.
 An int is a keyword using which the programmer can inform the compiler that the
data associated with this keyword should be treated as integer.
 C supports 3 different types of integer:
o short int,
o int,

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 8
Programming in C and Data Structures Module 1

o long int
n-bit Type size Range of unsigned Range of signed int
machine int be -2n to +2n -1
0 to 2n-1
16- bit short int 2 bytes 0 to 216-1 -215 to +215 -1
machine 0 to 65535 -32768 to +32767

int 2 bytes 0 to 216-1 -215 to +215 -1


0 to 65535 -32768 to +32767

long int 4 bytes 0 to 232-1 -231 to +231 -1


0 to 4294967295 -2147483648 to
+ 2147483647
2. Floating data type (float):
 An float is a keyword using which the programmer can inform the compiler that the
data associated with this keyword should be treated as floating point number.
 The size is 4 bytes.
 The range is -3.4e38 to +3.4e38.
 Float can hold the real constant accuracy up to 6 digits after the decimal point.

3.Double data type (double):


 An double is a keyword using which the programmer can inform the compiler that the
data associated with this keyword should be treated as long floating point number.
 The size is 8 bytes.
 The range is from 1.7e-308 to 1.7 e+308.
 Double can hold real constant up to 16 digits after the decimal point.
4.Character data type (char):
 char is a keyword which is used to define single character or a sequence of character
in c language capable of holding one character from the local character set.
 The size of the character variable is 1 byte.
 The range is from -128 to +127.
 Each character stored in the memory is associated with a unique value termed as
ASCII (American Standard Code for Information Interchange).
 It is used to represent character and strings.

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 9
Programming in C and Data Structures Module 1

n-bit Type size Range of unsigned Range of signed


machine char char
n
0 to 2 -1 be -2n to +2n -1
16- bit short int 2 bytes 0 to 28-1 -27 to +27 -1
machine 0 to 255 -128 to +127

5. void data type (void):


 It is an empty data type.
 No memory is allocated.
 Mainly used when there are no return values.
1.4.4 variable
 A variable is a name given to memory location where data can be stored.
 Using variable name data can be stored in a memory location and can be accessed or
manipulated very easily.
 Rules for variables
 The First character should be an alphabet or an underscore _
 Then First character is followed by any number of letters or digits.
 No extra symbols are allowed other than letters ,digits and Underscore
 Keywords cannot be used as an identifier
Example:
Sum – valid
For1- valid
for -invalid (it is a keyword)
1.4.4.1Declaration of variables:
 Giving a name to memory location is called declaring a variable.
 Reserving the required memory space to store the data is called defining a variable.
 General Syntax:
datatype variable; (or)
datatype variable1, variable2,……..variablen;
example: int a;
float x, y;
double sum;

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 10
Programming in C and Data Structures Module 1

 From the examples we will come to know that “a” is a variable of type integer and
allocates 2 bytes of memory. “x” and “y” are two variable of type float which will be
allocated 4 bytes of memory for each variable. “sum” is a double type variables
which will be allocated with 8 bytes of memory for each.
1.5 Variable Initialization
 Variables are not initialized when they are declared and defined, they contain garbage
values(meaningless values)
 The method of giving initial values for variables before they are processed is called
variable initialization.
 General Syntax:
Var_name = expr;
Where,
Var_name is the name of the variable ,
expr is the value of the expression.
 The “expr” on the right hand side is evaluated and stored in the variable name
(Var_name) on left hand side.
 The expression on the right hand side may be a constant, variable or a larger formula
built from simple expressions by arithmetic operators.
Examples:
 int a=10;
// assigns the value 10 to the integer variable a
 float x;
x=20;
// creates a variable y of float type and assigns value 20 to it.
 int a=10,b,b=a;
// creates two variables a and b. “a” is assigned with value 10, the value of “a” is
assigned to variable “b”. Now the value of b will be 10.
 price = cost*3;
//assigns the product of cost and 3 to price.
 Square = num*num;
// assigns the product of num with num to square.

1.6 (OUTPUT FUNCTION)


Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT
Page 11
Programming in C and Data Structures Module 1

Displaying Output using printf


 printf is an output statement in C used to display the content on the screen.
 print: Print the data stored in the specified memory location or variable.
 Format: The data present in memory location is formatted in to appropriate data type.
 There are various forms of printf statements.
Method 1: printf(“ format string”);
 Format string may be any character. The characters included within the double quotes
will be displayed on the output screen
 Example: printf(“Welcome to India”);
Output:
Welcome to India
Method 2:
printf(“ format string”, variable list);
 Format string also called as control string.
 Format string consist of format specifier of particular data type
 Format specifiers starts with % sign followed by conversion code.
 variable list are the variable names separated by comma.
 Example: int a=10;
float b=20;
printf(“ integer =%d, floating=%f”,a,b);
o output: integer=10, floating=20.00000

 Number of format specifiers must be equal to number of variables.


 While specifying the variables name make sure that it matches to the format
specifiers with in the double quotes.
Format Specifiers
 Format specifiers are the character string with % sign followed with a character.
 It specifies the type of data that is being processed.
 It is also called conversion specifier or conversion code.
 There are many format specifiers defined in C.
Symbols Meaning

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 12
Programming in C and Data Structures Module 1

%d Decimal signed integer number

%f float point number

%c Character
%o octal number

%x hexadecimal integer(Lower case letter x)

%X hexadecimal integer(Upper case letter X)

%e floating point value with exponent(Lower case letter e)

%E floating point value with exponent (Upper case letter E)

%ld long integer

%s String

%lf double

1.7 Input Function ( scanf)


Inputting Values Using scanf
 To enter the input through the input devices like keyboard we make use of scanf
statement.
General Syntax:
scanf(“format string”, list of address of variables);
 Where: Format string consists of the access specifiers/format specifiers.
 Format string also called as control string.
 Format string consist of format specifier of particular data type
 Format specifiers starts with % sign followed by conversion code.
 List of addresses of variables consist of the variable name preceded with &
symbol(address operator).
Example: int a;
float b;
scanf(“%d%f”,&a,&b);

Rules for scanf

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 13
Programming in C and Data Structures Module 1

 No escape sequences or additional blank spaces should be specified in the format


specifiers.
 Ex: scanf(“%d %f”,&a,&b); //invalid
scanf(“%d\n%f”,&a,&b); //invalid
 & symbol is must to read the values, if not the entered value will not be stored in the
variable specified. Ex: scanf(“%d%f”,a,b);//invalid.

A Simple C Program
Example 1:Let us discuss a simple program to print the Hello SVIT
/*program to print Hello svit*/
#include<stdio.h>
void main( )
{
printf(“ Hello SVIT”);
}
Output: Hello SVIT

Example 2:Consider another example program to find the simple interest


#include<stdio.h>
void main( )
{
float p,t,r;
float si;
printf(“enter the values of p,t,r\n”);
scanf(“%f%f%f”,&p,&t,&r);
si = (p*t*r)/100;
printf("Simple Interest=%f",si);
}
The above program illustrates the calculation of simple interest.
 Firstly we have declared the header files stdio.h for including standard input and
output which will be printf and scanf statements.
 Next we start with the main program indicated by void main( )

Prof.Chandrika C N, Prof.nagashree C, Department of CSE, SVIT


Page 14

You might also like