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

Chapter2

Uploaded by

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

Chapter2

Uploaded by

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

Course: Programming In C

Title of Chapter: Basics of C Programming

Instructor
Prof. S.S Pathare
K. K. Wagh Polytechnic,
Nashik
Course Outcome
After completing this course a learner will able to,

CO1: Identify building blocks of C program


CO2: List out types of tokens
CO3: Write C programs using arithmetic expression and data type
CO4: Use input-output statements for given data
History of C language
 C programming language was
developed in 1972 by Dennis
Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph),
located in the U.S.A.
 Dennis Ritchie is known as
the founder of the c language
 Initially, C language was developed
to be used in UNIX operating
system. It inherits many features of
previous languages such as B and
Features of C language
 C is a procedure-based programming language. Various functions
modules or code blocks are thus, written to solve this problem.
 C functions can accept parameters and return values and perform
variety of tasks like input from the user, displaying the
information, etc.
 C is simple and easy to learn and use.
 In C, errors are checked only at compile time.
 C works best for small projects where performance is important.
 C is highly portable. C programs written on one computer can run
on other computer without making any changes in the program.
Structure of C program
 Documentation Section - It is the part of the program where the
programmer gives the details associated with the program. He usually gives
the name of the program, the details of the author and other details like the
time of coding and description
 Link Section -This part of the code is used to declare all the header files that
will be used in the program
 Definition Section - In this section, we define different constants. The
keyword define is used in this part.
 Global Declaration Section - This part of the code is the part where the
global variables are declared.
 Main function Section - Every C-programs needs to have the main function.
Each main function contains 2 parts. A declaration part and an Execution part.
The declaration part is the part where all the variables are declared.
Structure of C program
stdio.h header file
The header file stdio.h stands for Standard Input Output. It has
the information related to input/output functions.

Functions :
printf() - This function is used to print the character, string, float,
integer, octal and hexadecimal values onto the output screen
scanf() - This function is used to read a character, string, numeric
data from keyboard.
getc() - It reads character from file
gets() - It reads line from keyboard
conio.h header file
The conio.h header file used in C programming language contains
functions for console input/output.

Functions :
clrscr() - This function is used to clear the output screen.
getch() - It reads character from keyboard
getche() -It reads character from keyboard and echoes to o/p screen
textcolor() -This function is used to change the text color
textbackground() -This function is used to change text background
Character set
 every language contains a set of characters used to construct words, statements, etc.,C language also has a
set of characters which include alphabets, digits, and special symbols.
 C language support total 256 characters
 C language character set contains the following set of characters.
 Alphabets
 C language supports all the alphabets from the English language. Lower and upper case letters together
support 52 alphabets.
 lower case letters - a to z UPPER CASE LETTERS - A to Z
 Digits
 C language supports 10 digits which are used to construct numerical values in C language.
Digits - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
 Special Symbols
C language supports a rich set of special symbols that include symbols to perform mathematical operations, to
check conditions, white spaces, backspaces, and other special symbols. Special Symbols - ~ @ # $ % ^ & * ( ) _
- + = { } [ ] ; : ' " / ? . > , < \ etc.
Token
 TOKEN is the smallest unit in a 'C' program. It is each and every word and punctuation that you come
across in your C program.
 The compiler breaks a program into the smallest possible units (tokens) and proceeds to the various stages
of the compilation.
 A token is divided into six different types, viz, Keywords, Operators, Strings, Constants, Special Characters,
and Identifiers
Keywords
 Keywords have fixed meanings, and the meaning cannot be changed.
 They act as a building block of a 'C' program.
 There are a total of 32 keywords in 'C'.
 Keywords are written in lowercase letters.
Identifier
 An identifier is nothing but a name assigned to an element in a program.
 Example, name of a variable, function, etc. Identifiers are the user-
defined names consisting of 'C' standard character set.
 As the name says, identifiers are used to identify a particular element in
a program. Each identifier must have a unique name.
 Uppercase and lowercase characters are treated differently
 Following rules must be followed for identifiers:
 The first character must always be an alphabet or an underscore.
 It should be formed using only letters, numbers, or underscore.
 A keyword cannot be used as an identifier.

Constant
constants, as the name itself suggests, are fixed values i.e. they cannot
change their value during program run once they are defined.
Syntax of Constant in C Programming-
const data_type variable_name = value;
Using #define pre-processor #define pi=3.14
Various Types of Constants in C Language-
Integer constants: For example, const int value = 400;
Floating constants: For example, const float pi = 3.14;
Character constants: For example, const char gender = ‘f’;
String constants: For example, const char name[] = ‘‘DataFlair’’;
Octal constants: The number system which consists only 8 digits, from 0 to 7 is called the
octal number system. The constant octal values can be declared as, const int oct = 040;
Hexadecimal constants: The number system which consists of 16 digits, from 0 to 9 and
alphabets ‘a’ to ‘f’ is called hexadecimal number system. The constant hexadecimal values can
be declared as, const int hex = 0x40;
Data Type
A data type specifies the type of data that a variable can store
Each data type requires different amounts of memory and has some
specific operations which can be performed over it.
Following are the examples of some very common data types used in C:
char: The most basic data type in C. It stores a single character and
requires a single byte of memory in almost all compilers.
int: As the name suggests, an int variable is used to store an integer.
float: It is used to store decimal numbers (numbers with floating point
value) with single precision.
double: It is used to store decimal numbers (numbers with floating
point value) with double precision.
Data Type
Declaring variables
 C variable is a named location in a memory where a program can
manipulate the data. This location is used to hold the value of the
variable.
 The value of the C variable may get change in the program.
 C variable might be belonging to any of the data type like int,
float, char etc.
Syntax:
datatype variable_list;

The example of declaring the variable is given below:


int a;
float b;
char c;
Here, a, b, c are variables. The int, float, char are the data types.

We can also provide values while declaring the variables as given below:
int a=10,b=20;//declaring 2 variable of integer type
float f=20.8;
char c='A';
Operators
 Operators as symbols that help us to perform specific
mathematical and logical computations on operands. In other
words, we can say that an operator operates the operands.
 Main types of operator
 A unary operator is an operator that operates on a single
operand. An operand can be a value or an expression.
 a binary operator operates on two operands
 a ternary operator operates on three operands
Arithmetic operators
These are the operators used to perform arithmetic/mathematical
operations on operands.
Increment / Decrement operator
(++ / --)
Types :
Pre-increment- Value of input variable increased by one and incremented value
assigned to output variable
Let a=5 b=++a then a=6 and b=6

Post-increment-Value of input variable is assigned to output variable and then


value of input variable increased by one
Let a=5 b=a++ then b=5 and a=6

Pre-decrement- Value of input variable decreased by one and incremented


value assigned to output variable
Let a=5 b=--a then a=4 and b=4

Post-decrement-Value of input variable is assigned to output variable and then


value of input variable decreased by one
Let a=5 b=a-- then b=5 and a=4
Output statement in C
Output means to display data on screen or write the data to a printer or
a
Infile.
C programming, printf () is one of the main output function. The
function sends formatted output to the screen
Syntax:
printf (“ Message”);
printf (“Good Day”);

printf (“format specifier / string”, arg1,arg2…argn);


Format string is a way to tell the compiler what type of data is in a
variable
Format Meaning
String
%d Scan or print an integer as signed decimal number
%f Scan or print a floating point number
%c To scan or print a character
Escape characters in C
 An escape sequence in C language is a sequence of characters
that doesn't represent itself when used inside string literal or
character.
 It is composed of two or more characters starting with
backslash \
Input statement in C
scanf() is one of the commonly used function to take input from the
user. The scanf() function reads formatted input from the standard
input such as keyboards.

Syntax:
scanf (“format string”, &arg1, &arg2, …..);

Format string is a way to tell the compiler what type of data is in a


variable
and
& is the address operator in C, which tells the compiler to change the
real value of this variable, stored at this address in the memory.
Comments in C
Comments in C language are used to provide information about lines of
code
Comments are statements that are not executed by the compiler and
interpreter Multi-Line comments are represented by slash
Single Line Comments asterisk \* ... *\. It can occupy many lines of
Single line comments are code, but it can't be nested.
Syntax:
represented by double slash \\. /*
Let's see an example of a single code
line comment in C. to be commented
#include<stdio.h> */
Let's see an example of a multi-Line comment in
int main(){ C.
//printing information #include<stdio.h>
printf("Hello C"); int main(){
/*printing information
return 0; Multi-Line Comment*/
} printf("Hello C");
return 0;

You might also like