Module1 C Tokens & Input Output Stataments
Module1 C Tokens & Input Output Stataments
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.
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)
~ | [
Tilde Vertical bar Left bracket
^ + Plus sign ;
Caret Semicolon
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:
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
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
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
\\ Backslash Backslash
\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”
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
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.
%c Character
%o octal number
%s String
%lf double
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