SlideShare a Scribd company logo
1 of 104
Unit I BASICS OF C PROGRAMMING 9
Introduction to programming paradigms - Structure of C program – Compilation Process-
Executing a C program- Data Types – Storage classes - Constants – Enumeration Constants -
Keywords – Operators: Precedence and Associativity - Expressions – Input and output
statements, Assignment statements – Decision making statements - Switch statement -
Looping statements
Unit II ARRAYS AND STRINGS 9
Introduction to Arrays: Declaration, Initialization – One dimensional array – Operations on
Arrays-Two dimensional arrays –Operations-Applications -Strings: Defining a String – Null
Character– Initialization of a String- Character arrays – Reading String Input- String Library
functions- operations: length, compare, concatenate, copy – Arrays of Strings
Unit III FUNCTIONS AND POINTERS 9
Introduction to functions: Function prototype, function definition, function call, Return
values and their types- Nesting of functions-Passing arrays and strings to functions- Built-in
functions – Recursion : Pointers – Pointer operations –Null Pointer- Pointer arithmetic –
Pointer to a pointer – Pointer to an Array – Pointers and Strings -Parameter passing: Pass by
value, Pass by reference
Unit IV STRUCTURES AND UNIONS 9
Structure - Nested structures – typedef declaration- Operations on structures- Pointer and
Structures – Array of structures –– Functions and Structures -Self-referential structures – Dynamic
memory allocation :Union - Definition – Declaration- Operations on unions –Arrays of Union
variables – Union inside Structure- Enumerations – Bit Fields
Unit V PREPROCESSOR DIRECTIVES AND FILE PROCESSING 9
Preprocessor Directives – Types – Macros- File Inclusion – Conditional Compilation Directives-
Files – Streams - I/O using streams –File types – File Operations - Command line arguments
BASICS OF C PROGRAMMING
Introduction to programming paradigms - Structure of C program –
Compilation Process- Executing a C program- Data Types – Storage
classes - Constants – Enumeration Constants - Keywords – Operators:
Precedence and Associativity - Expressions – Input and output statements,
Assignment statements – Decision making statements - Switch statement -
Looping statements.
Introduction
 C was developed in the early 1970s by Dennis Ritchie at Bell Laboratories.
 C was initially developed for writing system software.
 It was named as ‘C’ because many of its features were derived from an
earlier language called ‘B’.
 Today, C has become a popular language and various software programs
are written using this language.
 Many other commonly used programming languages such as C++ and Java
are also based on C.
Programming Languages
 High Level
 Middle Level
 Low Level
High Level Programming Languages:
 These level languages provide almost everything that the programmer
might need to do as already build into the language.
 Example: Java, Python
Middle level languages:
 These languages don’t provide all the built-in functions found in high level
languages, but provide all building blocks that we need to produce the
result we want.
 Example: C, C++
Low level languages:
 These languages provide nothing other than access to the machine’s basic
instruction set.
 Example: Assembly language.
 C is a robust language whose rich set of built in functions and operators
can be used to write complex programs.
 The C Compiler combines the features of assembly language and High
level language, which makes it best suited for writing system software as
well as business packages.
 A high level programming language
 Small size. C has only 32 keywords. This makes it relatively easy to learn.
 Makes extensive use of function calls.
 C is well suited for structured programming. In this programming
approach enables the users to think of problem in terms of functions
/modules where the collection of all the modules makes up a complete
program.
 Stable language.
 Quick language – Very fast and Efficient.
 Facilitates low level (bitwise) programming
 Supports pointers to refer computer memory, array, structures and
functions.
 C is a core language
 C is a portable language – C program written for one computer can be run
on another computer with little or no modification.
 C is an extensible language as it enables the user to add his own functions
to the C library.
 C language is primarily used for system programming.
 The portability, efficiency, the ability to access specific hardware
addresses and low runtime demand on system resources makes it a good
choice for implementing operating systems and embedded system
applications.
 C has been so widely accepted by professionals that compilers, libraries,
and interpreters of other programming languages are often implemented in
C.
 For portability and convenience reasons, C is sometimes used as an
intermediate language by implementations of other languages.
 Example of compilers which use C this way are BitC, Gambit, the
Glasgow Haskell Compiler, Squeak, and Vala.
 C is widely used to implement end-user applications.
 Pre-processor Directives: The preprocessor directives contain special
instructions that indicate how to prepare the program for compilation. One
of the most important and commonly used preprocessor commands is
include which tells the compiler that to execute the program, some
information is needed from the specified header file.
 Global Declaration Sections: Used for declaring/Initializing global
variables.
 main() function : It is the most important function and is a part of every C
program. The execution of C program begins at this function.
 All functions including main () are divided into two parts – Declaration
section and the statements section.
 The Declaration section precedes the statement section and is used to
describe the data that will be used in the function.
 Note that the data declared within function are known as local declaration
as that the data will be visible only till the function ends.
 The statement section in a function contains the code that manipulates the
data to perform a specified task.
WRITING THE C PROGRAM
// This is my first program in C
#include<stdio.h>
main()
{
printf(" Welcome to the world of C ");
}
 #include<stdio.h> - This is a preprocessor command that comes as the
first statement in our code.
 All preprocessor commands start with symbol hash (#).
 The #include statement tells the compiler to include the standard
input/output library or header file (stdio.h) in the program.
 This file has some in built in functions like printf, scanf etc., By simply
including this file in our code, we can use this functions directly.
 The standard input/output header file contains functions for input and
output of data like reading values from the keyboard and printing the
results on the screen.
int main()
 Every C program contains a main function which is the starting point of the
C program.int is the return value of the main() function.
 {} , the two curly braces are used to group all the related statements of the
main function. All the statements between the braces form the function
body. The function body contains a set of instructions to perform the given
task.
printf("Welcome to the world of C ");
 The printf function is defined in the stdio.h file and is used to print text on
the screen.
return 0;
 This is a return command that is used to return value 0 to the operating
system to give an indication that there were no errors during the execution
of the program.
 Every statement in the main function ends with semicolon;
FILES USED IN C PROGRAM
 Source file
 Header file
 Object file
 Executable file
1. SOURCE FILE
 The source file contains the source code of the program. The file extension
of any C source code is “.c”.
2. HEADER FILE
 A header file is a file with extension .h which contains C function
declarations and macro definitions to be shared between several
source files.
 Examples
 string.h : for string handling functions
 stdio.h : For standard input and output functions
 math.h : for mathematical functions
 conio.h : for clearing the screen
3. OBJECT FILES
 Object files are generated by the compiler as a result of processing the
source code file. Object file contains the binary code of the function
definitions. Linker uses these object files to produce an executable file by
combining the object files together. Object files have ‘.o’ extension.
4. BINARY EXECUTABLE FILES
 The binary executable file is generated by the linker. The linker links the
various object files to produce a binary file that can be directly executed.
The executable file has an “.exe” extension
The entire C compilation is broken to four stages.
 Pre-processing
 Compilation
 Assembling and
 Linking
 Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the
preprocessor converts the source code into expanded source code. The
extension of the expanded source code would be hello.i.
 The expanded source code is passed to the compiler, and the compiler
converts this expanded source code into assembly code. The extension of
the assembly code would be hello.s.
 This assembly code is then sent to the assembler, which converts the
assembly code into object code.
 After the creation of an object code, the linker creates the executable file.
The loader will then load the executable file for the execution.
 Every word in a C program is either an identifier or a keyword.
 Identifiers are basically names given to program elements such as
variables, arrays, and functions.
 They are formed by using a sequence of letters (both uppercase and
lowercase), numerals, and underscores.
 Tokens – 6 types
◦ Keywords, variables, constants, strings, special characters and operators
 Identifiers cannot include any special characters or punctuation marks (like
#, $, ^, ?, ., etc.) except the underscore “_”.
 There cannot be two successive underscores.
 Keywords cannot be used as identifiers.
 The case of alphabetic characters that form the identifier name is
significant. For example, ‘FIRST’ is different from ‘first’ and ‘First’.
 Identifiers must begin with a letter or an underscore. However, use of
underscore as the first character must be avoided because several complier-
defined identifiers in the standard C library have underscore as their first
character. So, inadvertently duplicated names may cause definition
conflicts.
Identifiers can be of any reasonable length. They should not contain more
than 31 characters. (They can actually be longer than 31, but the compiler
looks at only the first 31 characters of the name.)
 Like every computer language, C has a set of reserved words often known
as keywords that cannot be used as an identifier.
 All keywords are basically a sequence of characters that have a fixed
meaning.
 By convention, all keywords must be written in lower case letters.
 The following table contains the list of keywords in C.
 Data type determines the set of values that a data
item can take and the operations that can be
performed on the item.
 C language provides four basic data types.
 The following table lists the data types, their size,
range, and usage for a C programmer.
 The char data type is of one byte and is used to store
single characters.
 Note that C does not provide any data type for
storing text.
 This is because text is made up of individual
characters. You might have been surprised to see that
the range of char is given as –128 to 127.
 In addition, C also supports four modifiers—
two sign specifiers (signed and unsigned) and
two size specifiers (short and long).
 The following table shows the variants of
basic data types.
 A variable is defined as a meaningful name
given to a data storage location in the
computer memory.
 When using a variable, we actually refer to the
address of the memory where the data is
stored.
 C language supports two basic kinds of
variables.
 Numeric Variables
 Character Variables
Numeric Variables
 Numeric variables can be used to store either
integer values or floating point values.
 Modifiers like short, long, signed, and unsigned
can also be used with numeric variables.
 The difference between signed and unsigned
numeric variables is that signed variables can be
either negative or positive but unsigned variables
can only be positive.
 Therefore, by using an unsigned variable we can
increase the maximum positive range.
 When we omit the signed/unsigned modifier, C
language automatically makes it a signed
variable.
 To declare an unsigned variable, the unsigned
modifier must be explicitly added during the
declaration of the variable.
Character Variables
 Character variables are just single characters
enclosed within single quotes.
 These characters could be any character from the
ASCII character set—letters (‘a’, ‘A’), numerals
(‘2’), or special characters (‘&’).
Declaring Variables
 To declare a variable, specify the data type of the
variable followed by its name.
 The data type indicates the kind of values that
the variable can store.
 Variable names should always be meaningful and
must reflect the purpose of their usage in the
program.
 In C, variable declaration always ends with a
semi-colon. For example,
 int emp_num;
 float salary;
Initializing Variables
 While declaring the variables, we can also
initialize them with some value. For example,
 int emp_num = 7;
 float salary = 9800.99
 char grade = ‘A’;
 double balance_amount = 100000000;
 Constants are identifiers whose values do not
change.
 While values of variables can be changed at
any time, values of constants can never be
changed.
 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.
Integer type Constant
 A constant of integer type consists of a sequence of digits.
 Example:
1,34,546,8909 etc. are valid integer constants.
Floating point type constant
 Integer numbers are inadequate to express numbers that
have a fractional point. A floating point constant therefore
consistent of an integer part, a decimal point, a fractional
part, and an exponent field containing an e or E (e means
exponents) followed by an integer where the fraction part and
integer part are a sequence of digits.
 Example:
Floating point numbers are 0.02, -0.23, 123.345, +0.34 etc.
Character Constant
 A character constant consists of a single character enclosed
in single quotes. For example, ‘a’, ‘@’ are character
constants. In computers, characters are stored using machine
character set using ASCII codes.
String Constant
 A string constant is a sequence of characters enclosed in
double quotes. So “a” is not the same as ‘a’. The characters
comprising the string constant are stored in successive
memory locations. When a string constant is encountered in a
C program, the compiler records the address of the first
character and appends a null character (‘0’) to the string to
mark the end of the string.
Declaring Constants
 To declare a constant, precede the normal
variable declaration with const keyword and
assign it a value.
 const float pi = 3.14;
 or by using #define preprocessor directives
 #define PI 3.14
Rules for declaring constant
 Rule 1: Constant names are usually written in
capital letters to visually distinguish them from
other variable names which are normally written in
lower case characters
 Rule 2: No blank spaces are permitted in between
the # symbol and define keyword
 Rule 3: Blank space must be used between #define
and constant name and constant value
 Rule 4: #define is a pre-processor compiler
directive and not a statement. Therefore, it does
not end with a semi-colon.
 Comments are a way of explaining what a
program does. C supports two types of
comments.
 // is used to comment a single statement.
 /* is used to comment multiple statements.
 A /* is ended with */ and all statements that
lie between these characters are commented.
 Note that comment statements are not
executed by the compiler.
 Rather, they are ignored by the compiler as
they are simply added in programs to make
the code understandable by programmers as
well as other users.
 The most fundamental operation in a C
program is to accept input values from a
standard input device and output the data
produced by the program to a standard
output device. As discussed, we can assign
values to variables using the assignment
operator ‘=’.
 For example,
 int a = 3;
 What if we want to assign value to variable a is
inputted from the user at run-time? This is done
by using the scanf function that reads data from
the keyboard.
 Similarly, for outputting results of the program,
printf function is used that sends results to a
terminal.
 Like printf and scanf, there are different
functions in C that can carry out the input–output
operations.
 These functions are collectively known as
Standard Input/Output Library.
 A program that uses standard input/output
functions must contain the following statement
at the beginning of the program:
 #include <stdio.h>
 SCANF()
 The scanf()function is used to read formatted
data from the keyboard. The syntax of the
scanf()
 function can be given as,
 scanf ("control string", arg1, arg2, arg3
...argn);
 The control string specifies the type and
format of the data that has to be obtained
from the keyboard and stored in the memory
locations pointed by the arguments, arg1,
arg2, ...,argn.
The scanf function ignores any blank spaces, tabs, and newlines
entered by the user.
The function simply returns the number of input fields successfully
scanned and stored.
 The address of the variable is denoted by an
& sign followed by the name of the variable.
 Look at the following code that shows how
we can input value in a variable of int data
type:
 int num;
 scanf(" %4d ", &num);
 The scanf function reads first four digits into
the address or the memory location pointed
by num.
 In case of reading strings, we do not use the
& sign in the scanf function.
 The printf function is used to display
information required by the user and also prints
the values of the variables.
 Its syntax can be given as:
 printf ("control string", arg1,arg2,arg3,...,argn);
 Consider three variables declared as,
 int a=9, b=3, result;
 We will use these variables to explain
arithmetic operators.
 The following table shows the arithmetic
operators, their syntax, and usage in C
language.
 A relational operator, also known as a
comparison operator, is an operator that
compares two values or expressions.
 Relational operators return true or false value,
depending on whether the conditional
relationship between the two operands holds or
not.
 C language supports different types of operators,
which can be used with variables and constants
to form expressions. These operators can be
categorized into the following major groups:
 1. Arithmetic operators
 2. Relational operators
 3. Equality operators
 4.Logical operators
 5. Unary operators
 6.Conditional operator
 7. Bitwise operators
 8.Assignment operators
 9. Comma operator
 10.Sizeof operator
 C language also supports two equality
operators to compare operands for strict
equality or inequality.
 They are equal to (==) and not equal to (!=)
operators.
 The equality operators have lower precedence
than the relational operators.
 Following table shows all the logical
operators supported by C language. Assume
variable A holds 1 and variable B holds 0,
then −
 Bitwise operator works on bits and perform
bit-by-bit operation. The truth tables for &, |,
and ^ is as follows −
 The & (bitwise AND) in C or C++ takes two numbers as
operands and does AND on every bit of two numbers. The
result of AND is 1 only if both bits are 1.
 The | (bitwise OR) in C or C++ takes two numbers as
operands and does OR on every bit of two numbers. The
result of OR is 1 if any of the two bits is 1.
 The ^ (bitwise XOR) in C or C++ takes two numbers as
operands and does XOR on every bit of two numbers. The
result of XOR is 1 if the two bits are different.
 The << (left shift) in C or C++ takes two numbers, left
shifts the bits of the first operand, the second operand
decides the number of places to shift.
 The >> (right shift) in C or C++ takes two numbers, right
shifts the bits of the first operand, the second operand
decides the number of places to shift.
 The ~ (bitwise NOT) in C or C++ takes one number and
inverts all bits of it
// C Program to demonstrate use of bitwise operators
#include <stdio.h>
int main()
{
// a = 5(00000101), b = 9(00001001)
unsigned char a = 5, b = 9;
// The result is 00000001
printf("a = %d, b = %dn", a, b);
printf("a&b = %dn", a & b);
// The result is 00001101
printf("a|b = %dn", a | b);
// The result is 00001100
printf("a^b = %dn", a ^ b);
// The result is 11111010
printf("~a = %dn", a = ~a);
// The result is 00010010
printf("b<<1 = %dn", b << 1);
// The result is 00000100
printf("b>>1 = %dn", b >> 1);
return 0;
}
Output:
a = 5,
b = 9
a&b = 1
a|b = 13
a^b = 12
~a = 250
b<<1 = 18
b>>1 = 4
 unary minus(-)
 increment(++)
 decrement(- -)
 NOT(!)
 Address of operator(&)
 sizeof()
 The minus operator changes the sign of its
argument.
 A positive number becomes negative, and a
negative number becomes positive.
 int a = 10;
 int b = -a; // b = -10
 unary minus is different from subtraction
operator, as subtraction requires two
operands.
 It is used to increment the value of the
variable by 1.
 The increment can be done in two ways:
 prefix increment
In this method, the operator preceeds the
operand (e.g., ++a).
 The value of operand will be altered before it
is used. int a = 1; int b = ++a; // b = 2
 postfix increment
In this method, the operator follows the
operand (e.g., a++). The value operand will
be altered after it is used. int a = 1; int b =
a++; // b = 1 int c = a; // c = 2
 It is used to decrement the value of the
variable by 1. The decrement can be done in
two ways:
 prefix decrement
In this method, the operator preceeds the
operand (e.g., – -a). The value of operand will
be altered before it is used. int a = 1; int b =
--a; // b = 0
 posfix decrement
In this method, the operator follows the
operand (e.g., a- -). The value of operand will
be altered after it is used. int a = 1; int b =
a--; // b = 1 int c = a; // c = 0
 It is used to reverse the logical state of its
operand.
 If a condition is true, then Logical NOT
operator will make it false.
 If x is true, then !x is false.
 If x is false, then !x is true.
 It gives an address of a variable. It is used to
return the memory address of a variable.
 These addresses returned by the address-of
operator are known as pointers because they
“point” to the variable in memory.
 & gives an address on variable n
 int a;
 int *ptr;
 ptr = &a; // address of a is copied to the
location ptr.
 The sizeof operator is the most common
operator in C.
 It is a compile-time unary operator and used
to compute the size of its operand.
 It returns the size of a variable.
 It can be applied to any data type, float type,
pointer type variables.
 #include <stdio.h>
 int main()
 { int a = 16;
 printf("Size of variable a : %dn",sizeof(a));
 printf("Size of int data type : %dn",sizeof(int));
 printf("Size of char data type : %dn",sizeof(char));
 printf("Size of float data type : %dn",sizeof(float));
 printf("Size of double data type : %dn",sizeof(double));
 return 0; }
 The conditional operator is also known as
a ternary operator. The conditional statements
are the decision-making statements which
depends upon the output of the expression.
 It is represented by two symbols, i.e., '?' and ':'.
 As conditional operator works on three operands,
so it is also known as the ternary operator.
 The behavior of the conditional operator is
similar to the 'if-else' statement as 'if-else'
statement is also a decision-making statement.
The conditional operator is of the form
variable = Expression1 ? Expression2 : Expression3
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ? printf("m is greater than n that is %d > %d",
m, n)
: printf("n is greater than m that is %d > %d",
n, m);
return 0;
}
Output:
m is greater than n that is
5>4
 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
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %dn", c);
c += a; // c is 10
printf("c = %dn", c);
c -= a; // c is 5
printf("c = %dn", c);
c *= a; // c is 25
printf("c = %dn", c);
c /= a; // c is 5
printf("c = %dn", c);
c %= a; // c = 0
printf("c = %dn", c);
return 0;
}
Output:
c = 5
c = 10
c = 5
c = 25
c = 5
c = 0
The comma operator (represented by the token, ) is
a binary operator that evaluates its first operand
and discards the result, it then evaluates the second
operand and returns this value (and type). The
comma operator has the lowest precedence of any
C operator, and acts as a sequence point.
int i = (5, 10); /* 10 is assigned to i*/
int j = (f1(), f2()); /* f1() is called
(evaluated) first followed by f2().
The returned value of
f2() is assigned to j */
 Size of variable a : 4
 Size of int data type : 4
 Size of char data type : 1
 Size of float data type : 4
 Size of double data type : 8
 d=a+=b*=c-=10 a=2,b=5,c=1
 c=c-10 c=1-10=-9 c=-9
 a+=b*=-9
 b=b*-9 b=5*-9 b=-45
 a+=-45 a=2+(-45)=2-45=-43
 Unformatted I/O Functions
 Formatted I/O Functions
 There are mainly six unformatted I/O
functions discussed as follows:
a) getchar()
b) putchar()
c) gets()
d) puts()
e) getch()
f) getche()
 This function is an Input function. It is used for reading a single
character from the keyborad. It is buffered function. Buffered functions
get the input from the keyboard and store it in the memory buffer
temporally until you press the Enter key.
 The general syntax is as:
v = getchar();
where v is the variable of character type. For example:
char n;
n = getchar();
A simple C-program to read a single character from the keyboard is as:
/*To read a single character from the keyboard using the getchar()
function*/
#include
void main()
{
char n;
n = getchar();
}
This function is an output function. It is used to display a single
character on the screen. The general syntax is as:
 putchar(v);
where v is the variable of character type. For example:
char n;
putchar(n);
A simple program is written as below, which will read a single character
using getchar() function and display inputted data using putchar()
function:
/*Program illustrate the use of getchar() and putchar() functions*/
#include
main()
{
char n;
n = getchar();
putchar(n);
}
This function is an input function. It is used to read a string from the
keyboard. It is also buffered function. It will read a string, when you type
the string from the keyboard and press the Enter key from the keyboard.
It will mark nulll character ('0') in the memory at the end of the string,
when you press the enter key. The general syntax is as:
 gets(v);
where v is the variable of character type. For example:
char n[20];
gets(n);
A simple C program to illustrate the use of gets() function:
/*Program to explain the use of gets() function*/
#include
main()
{
char n[20];
gets(n);
}
This is an output function. It is used to display a string inputted by
gets() function. It is also used to display an text (message) on the screen
for program simplicity. This function appends a newline ("n") character
to the output.

The general syntax is as:
puts(v);
or
puts("text line");
where v is the variable of character type.
A simple C program to illustrate the use of puts() function:
/*Program to illustrate the concept of puts() with gets() functions*/
#include
main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}
 This is also an input function. This is used to read a single character
from the keyboard like getchar() function. But getchar() function is a
buffered is function, getchar() function is a non-buffered function. The
character data read by this function is directly assign to a variable rather
it goes to the memory buffer, the character data directly assign to a
variable without the need to press the Enter key.
Another use of this function is to maintain the output on the screen till
you have not press the Enter Key. The general syntax is as:
 v = getch();
where v is the variable of character type.
A simple C program to illustrate the use of getch() function:
/*Program to explain the use of getch() function*/
#include
main()
{
char n;
puts("Enter the Char");
n = getch();
puts("Char is :");
putchar(n);
getch();
}
 All are same as getch() function execpt it is an echoed
function. It means when you type the character data from
the keyboard it will visible on the screen. The general
syntax is as:
 v = getche();
where v is the variable of character type.
A simple C program to illustrate the use of getch()
function:
/*Program to explain the use of getch() function*/
#include
main()
{
char n;
puts("Enter the Char");
n = getche();
puts("Char is :");
putchar(n);
getche();
}
 Formatted I/O functions which refers to an
Input or Ouput data that has been arranged
in a particular format. There are mainly two
formatted I/O functions discussed as follows:
 a) scanf()
b) printf()
 The scanf() function is an input function. It used
to read the mixed type of data from keyboard.
You can read integer, float and character data by
using its control codes or format codes. The
general syntax is as:
 scanf("control strings",arg1,arg2,..............argn);
or
scanf("control
strings",&v1,&v2,&v3,................&vn);
Where arg1,arg2,..........argn are the arguments
for reading and v1,v2,v3,........vn all are the
variables.
 The scanf() format code (spedifier) is as
shown in the below table:
 This ia an output function. It is used to display a
text message and to display the mixed type (int,
float, char) of data on screen. The general syntax
is as:
 printf("control
strings",&v1,&v2,&v3,................&vn);
or
printf("Message line or text line");
Where v1,v2,v3,........vn all are the variables.
The control strings use some printf() format
codes or format specifiers or conversion
characters.

 These all are discussed in the below table as:
 /*Below the program which show the use of
printf() function*/ #include
main()
{
int a;
float b;
char c;
printf("Enter the mixed type of data");
scanf("%d",%f,%c",&a,&b,&c);
getch();
}
An assignment statement gives a value to a variable. For example,
 x = 5;
 gives x the value 5.
 The value of a variable may be changed. For example, if x has the value
5, then the assignment statement
 x = x + 1;
 will give x the value 6.
 The general syntax of an assignment statement is
 variable = expression;
where:
 the variable must be declared;
 the variable may be a simple name, or an indexed location in an array, or
a field (instance variable) of an object, or a static field of a class; and
 the expression must result in a value that is compatible with the type of
the variable.
 In other words, it must be possible to cast the expression to the type of
the variable.
 Syntax:
 if(condition) {
 // Statements to execute if
 // condition is true
 }
 if(condition)
 statement1;
 statement2;
Example
#include<stdio.h>
int main()
{
int number=0;
printf("Enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
return 0;
}
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers?");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest",a);
}
if(b>a && b > c)
{
printf("%d is largest",b);
}
if(c>a && c>b)
{
printf("%d is largest",c);
}
if(a == b && a == c)
{
printf("All are equal");
}
}
Syntax:
 if (condition) {
 // Executes this block if
 // condition is true
 }
 Else
 {
 // Executes this block if
 // condition is false
 }
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number",number);
}
return 0;
}
Syntax:
 if (condition)
 statement;
 else if (condition)
 statement;
 . . else statement;
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks?");
scanf("%d",&marks);
if(marks > 85 && marks <= 100)
{
printf("Congrats ! you scored grade A ...");
}
else if (marks > 60 && marks <= 85)
{
printf("You scored grade B + ...");
}
else if (marks > 40 && marks <= 60)
{
printf("You scored grade B ...");
}
else if (marks > 30 && marks <= 40)
{
printf("You scored grade C ...");
}
else
{
printf("Sorry you are fail ...");
}
}
 Syntax:
 if (condition1)
 {
 // Executes when condition1 is true
if (condition2) {
 // Executes when condition2 is true
 }
 }
switch (expression) ​
{
case constant1:
// statements break;
case constant2:
// statements break;
. . . default:
// default statements
}
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equals to 10");
break;
case 50:
printf("number is equal to 50");
break;
case 100:
printf("number is equal to 100");
break;
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
 A Loop executes the sequence of statements
many times until the stated condition
becomes false.
 A loop consists of two parts, a body of a loop
and a control statement.
 The control statement is a combination of
some conditions that direct the body of the
loop to execute until the specified condition
becomes false.
 The purpose of the loop is to repeat the same
code a number of times.
 1. Entry controlled loop
 2. Exit controlled loop
 In an entry controlled loop, a condition is
checked before executing the body of a loop.
 It is also called as a pre-checking loop.
 In an exit controlled loop, a condition is
checked after executing the body of a loop.
 It is also called as a post-checking loop.
 'C' programming language provides us with
three types of loop constructs:
 1. The while loop
 2. The do-while loop
 3. The for loop
 A while loop is the most straightforward
looping structure.
 Syntax of while loop in C programming
language is as follows:
while (condition)
{
statements;
}
#include<stdio.h>
int main()
{
int num=1; //initializing the variable
while(num<=10) //while loop with condition
{
printf("%dn",num);
num++; /incrementing operation
}
return 0;
}
do {
statements
} while (expression);
 #include<stdio.h>
 int main()
 { int num=1; //initializing the variable
 do //do-while loop
 {
 printf("%dn",2*num);
 num++; //incrementing operation
}while(num<=10);
 return 0;
 }
 for (initial value; condition; incrementation or
decrementation )
 {
 statements;
 }
 #include<stdio.h>
 int main()
 {
 int number;
for(number=1;number<=10;number++)
//for loop to print 1-10 numbers
 {
 printf("%dn",number);
 }
 return 0;}
 The break statement is used mainly in in the switch
statement. It is also useful for immediately stopping a
loop.
 We consider the following program which introduces
a break to exit a while loop:
#include <stdio.h>
int main()
{
int num = 5;
while (num > 0)
{ if (num == 3)
break;
printf("%dn", num); num--;
}
}
 When you want to skip to the next iteration but remain in
the loop, you should use the continue statement.
 For example:
#include <stdio.h>
int main()
{
int nb = 7;
while (nb > 0)
{
nb--;
if (nb == 5)
continue;
printf("%dn", nb);
}
}

More Related Content

Similar to C Programming UNIT 1.pptx

Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic concepts
ssuserf86fba
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
AashutoshChhedavi
 

Similar to C Programming UNIT 1.pptx (20)

C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTTC programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
C programming notes BATRACOMPUTER CENTRE IN Ambala CANTT
 
C programming
C programming C programming
C programming
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
Unit-2.pptx
Unit-2.pptxUnit-2.pptx
Unit-2.pptx
 
Prog1-L1.pdf
Prog1-L1.pdfProg1-L1.pdf
Prog1-L1.pdf
 
Introduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic conceptsIntroduction To C++ programming and its basic concepts
Introduction To C++ programming and its basic concepts
 
C programming presentation(final)
C programming presentation(final)C programming presentation(final)
C programming presentation(final)
 
Introduction to c language
Introduction to c language Introduction to c language
Introduction to c language
 
C language introduction geeksfor geeks
C language introduction   geeksfor geeksC language introduction   geeksfor geeks
C language introduction geeksfor geeks
 
C lecture notes new
C lecture notes newC lecture notes new
C lecture notes new
 
Basics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptxBasics of C Lecture 2[16097].pptx
Basics of C Lecture 2[16097].pptx
 
C programming slide day 01 uploadd by md abdullah al shakil
C programming slide day 01 uploadd by md abdullah al shakilC programming slide day 01 uploadd by md abdullah al shakil
C programming slide day 01 uploadd by md abdullah al shakil
 
Unit 2 ppt
Unit 2 pptUnit 2 ppt
Unit 2 ppt
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
C programming course material
C programming course materialC programming course material
C programming course material
 
Unit 2 l1
Unit 2 l1Unit 2 l1
Unit 2 l1
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 

Recently uploaded

1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Recently uploaded (20)

NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
457503602-5-Gas-Well-Testing-and-Analysis-pptx.pptx
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 

C Programming UNIT 1.pptx

  • 1.
  • 2. Unit I BASICS OF C PROGRAMMING 9 Introduction to programming paradigms - Structure of C program – Compilation Process- Executing a C program- Data Types – Storage classes - Constants – Enumeration Constants - Keywords – Operators: Precedence and Associativity - Expressions – Input and output statements, Assignment statements – Decision making statements - Switch statement - Looping statements Unit II ARRAYS AND STRINGS 9 Introduction to Arrays: Declaration, Initialization – One dimensional array – Operations on Arrays-Two dimensional arrays –Operations-Applications -Strings: Defining a String – Null Character– Initialization of a String- Character arrays – Reading String Input- String Library functions- operations: length, compare, concatenate, copy – Arrays of Strings Unit III FUNCTIONS AND POINTERS 9 Introduction to functions: Function prototype, function definition, function call, Return values and their types- Nesting of functions-Passing arrays and strings to functions- Built-in functions – Recursion : Pointers – Pointer operations –Null Pointer- Pointer arithmetic – Pointer to a pointer – Pointer to an Array – Pointers and Strings -Parameter passing: Pass by value, Pass by reference
  • 3. Unit IV STRUCTURES AND UNIONS 9 Structure - Nested structures – typedef declaration- Operations on structures- Pointer and Structures – Array of structures –– Functions and Structures -Self-referential structures – Dynamic memory allocation :Union - Definition – Declaration- Operations on unions –Arrays of Union variables – Union inside Structure- Enumerations – Bit Fields Unit V PREPROCESSOR DIRECTIVES AND FILE PROCESSING 9 Preprocessor Directives – Types – Macros- File Inclusion – Conditional Compilation Directives- Files – Streams - I/O using streams –File types – File Operations - Command line arguments
  • 4. BASICS OF C PROGRAMMING Introduction to programming paradigms - Structure of C program – Compilation Process- Executing a C program- Data Types – Storage classes - Constants – Enumeration Constants - Keywords – Operators: Precedence and Associativity - Expressions – Input and output statements, Assignment statements – Decision making statements - Switch statement - Looping statements.
  • 5. Introduction  C was developed in the early 1970s by Dennis Ritchie at Bell Laboratories.  C was initially developed for writing system software.  It was named as ‘C’ because many of its features were derived from an earlier language called ‘B’.  Today, C has become a popular language and various software programs are written using this language.  Many other commonly used programming languages such as C++ and Java are also based on C. Programming Languages  High Level  Middle Level  Low Level
  • 6. High Level Programming Languages:  These level languages provide almost everything that the programmer might need to do as already build into the language.  Example: Java, Python Middle level languages:  These languages don’t provide all the built-in functions found in high level languages, but provide all building blocks that we need to produce the result we want.  Example: C, C++ Low level languages:  These languages provide nothing other than access to the machine’s basic instruction set.  Example: Assembly language.
  • 7.  C is a robust language whose rich set of built in functions and operators can be used to write complex programs.  The C Compiler combines the features of assembly language and High level language, which makes it best suited for writing system software as well as business packages.  A high level programming language  Small size. C has only 32 keywords. This makes it relatively easy to learn.  Makes extensive use of function calls.  C is well suited for structured programming. In this programming approach enables the users to think of problem in terms of functions /modules where the collection of all the modules makes up a complete program.
  • 8.  Stable language.  Quick language – Very fast and Efficient.  Facilitates low level (bitwise) programming  Supports pointers to refer computer memory, array, structures and functions.  C is a core language  C is a portable language – C program written for one computer can be run on another computer with little or no modification.  C is an extensible language as it enables the user to add his own functions to the C library.
  • 9.  C language is primarily used for system programming.  The portability, efficiency, the ability to access specific hardware addresses and low runtime demand on system resources makes it a good choice for implementing operating systems and embedded system applications.  C has been so widely accepted by professionals that compilers, libraries, and interpreters of other programming languages are often implemented in C.  For portability and convenience reasons, C is sometimes used as an intermediate language by implementations of other languages.  Example of compilers which use C this way are BitC, Gambit, the Glasgow Haskell Compiler, Squeak, and Vala.  C is widely used to implement end-user applications.
  • 10.
  • 11.  Pre-processor Directives: The preprocessor directives contain special instructions that indicate how to prepare the program for compilation. One of the most important and commonly used preprocessor commands is include which tells the compiler that to execute the program, some information is needed from the specified header file.  Global Declaration Sections: Used for declaring/Initializing global variables.  main() function : It is the most important function and is a part of every C program. The execution of C program begins at this function.  All functions including main () are divided into two parts – Declaration section and the statements section.  The Declaration section precedes the statement section and is used to describe the data that will be used in the function.
  • 12.  Note that the data declared within function are known as local declaration as that the data will be visible only till the function ends.  The statement section in a function contains the code that manipulates the data to perform a specified task. WRITING THE C PROGRAM // This is my first program in C #include<stdio.h> main() { printf(" Welcome to the world of C "); }
  • 13.  #include<stdio.h> - This is a preprocessor command that comes as the first statement in our code.  All preprocessor commands start with symbol hash (#).  The #include statement tells the compiler to include the standard input/output library or header file (stdio.h) in the program.  This file has some in built in functions like printf, scanf etc., By simply including this file in our code, we can use this functions directly.  The standard input/output header file contains functions for input and output of data like reading values from the keyboard and printing the results on the screen.
  • 14. int main()  Every C program contains a main function which is the starting point of the C program.int is the return value of the main() function.  {} , the two curly braces are used to group all the related statements of the main function. All the statements between the braces form the function body. The function body contains a set of instructions to perform the given task. printf("Welcome to the world of C ");  The printf function is defined in the stdio.h file and is used to print text on the screen. return 0;  This is a return command that is used to return value 0 to the operating system to give an indication that there were no errors during the execution of the program.  Every statement in the main function ends with semicolon;
  • 15. FILES USED IN C PROGRAM  Source file  Header file  Object file  Executable file 1. SOURCE FILE  The source file contains the source code of the program. The file extension of any C source code is “.c”. 2. HEADER FILE  A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source files.  Examples  string.h : for string handling functions  stdio.h : For standard input and output functions  math.h : for mathematical functions  conio.h : for clearing the screen
  • 16. 3. OBJECT FILES  Object files are generated by the compiler as a result of processing the source code file. Object file contains the binary code of the function definitions. Linker uses these object files to produce an executable file by combining the object files together. Object files have ‘.o’ extension. 4. BINARY EXECUTABLE FILES  The binary executable file is generated by the linker. The linker links the various object files to produce a binary file that can be directly executed. The executable file has an “.exe” extension
  • 17. The entire C compilation is broken to four stages.  Pre-processing  Compilation  Assembling and  Linking
  • 18.  Firstly, the input file, i.e., hello.c, is passed to the preprocessor, and the preprocessor converts the source code into expanded source code. The extension of the expanded source code would be hello.i.  The expanded source code is passed to the compiler, and the compiler converts this expanded source code into assembly code. The extension of the assembly code would be hello.s.  This assembly code is then sent to the assembler, which converts the assembly code into object code.  After the creation of an object code, the linker creates the executable file. The loader will then load the executable file for the execution.
  • 19.  Every word in a C program is either an identifier or a keyword.  Identifiers are basically names given to program elements such as variables, arrays, and functions.  They are formed by using a sequence of letters (both uppercase and lowercase), numerals, and underscores.  Tokens – 6 types ◦ Keywords, variables, constants, strings, special characters and operators
  • 20.  Identifiers cannot include any special characters or punctuation marks (like #, $, ^, ?, ., etc.) except the underscore “_”.  There cannot be two successive underscores.  Keywords cannot be used as identifiers.  The case of alphabetic characters that form the identifier name is significant. For example, ‘FIRST’ is different from ‘first’ and ‘First’.  Identifiers must begin with a letter or an underscore. However, use of underscore as the first character must be avoided because several complier- defined identifiers in the standard C library have underscore as their first character. So, inadvertently duplicated names may cause definition conflicts. Identifiers can be of any reasonable length. They should not contain more than 31 characters. (They can actually be longer than 31, but the compiler looks at only the first 31 characters of the name.)
  • 21.  Like every computer language, C has a set of reserved words often known as keywords that cannot be used as an identifier.  All keywords are basically a sequence of characters that have a fixed meaning.  By convention, all keywords must be written in lower case letters.  The following table contains the list of keywords in C.
  • 22.  Data type determines the set of values that a data item can take and the operations that can be performed on the item.  C language provides four basic data types.  The following table lists the data types, their size, range, and usage for a C programmer.  The char data type is of one byte and is used to store single characters.  Note that C does not provide any data type for storing text.  This is because text is made up of individual characters. You might have been surprised to see that the range of char is given as –128 to 127.
  • 23.  In addition, C also supports four modifiers— two sign specifiers (signed and unsigned) and two size specifiers (short and long).
  • 24.  The following table shows the variants of basic data types.
  • 25.  A variable is defined as a meaningful name given to a data storage location in the computer memory.  When using a variable, we actually refer to the address of the memory where the data is stored.  C language supports two basic kinds of variables.  Numeric Variables  Character Variables
  • 26. Numeric Variables  Numeric variables can be used to store either integer values or floating point values.  Modifiers like short, long, signed, and unsigned can also be used with numeric variables.  The difference between signed and unsigned numeric variables is that signed variables can be either negative or positive but unsigned variables can only be positive.  Therefore, by using an unsigned variable we can increase the maximum positive range.  When we omit the signed/unsigned modifier, C language automatically makes it a signed variable.  To declare an unsigned variable, the unsigned modifier must be explicitly added during the declaration of the variable.
  • 27. Character Variables  Character variables are just single characters enclosed within single quotes.  These characters could be any character from the ASCII character set—letters (‘a’, ‘A’), numerals (‘2’), or special characters (‘&’). Declaring Variables  To declare a variable, specify the data type of the variable followed by its name.  The data type indicates the kind of values that the variable can store.  Variable names should always be meaningful and must reflect the purpose of their usage in the program.  In C, variable declaration always ends with a semi-colon. For example,  int emp_num;  float salary;
  • 28. Initializing Variables  While declaring the variables, we can also initialize them with some value. For example,  int emp_num = 7;  float salary = 9800.99  char grade = ‘A’;  double balance_amount = 100000000;
  • 29.  Constants are identifiers whose values do not change.  While values of variables can be changed at any time, values of constants can never be changed.  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.
  • 30.
  • 31. Integer type Constant  A constant of integer type consists of a sequence of digits.  Example: 1,34,546,8909 etc. are valid integer constants. Floating point type constant  Integer numbers are inadequate to express numbers that have a fractional point. A floating point constant therefore consistent of an integer part, a decimal point, a fractional part, and an exponent field containing an e or E (e means exponents) followed by an integer where the fraction part and integer part are a sequence of digits.  Example: Floating point numbers are 0.02, -0.23, 123.345, +0.34 etc.
  • 32. Character Constant  A character constant consists of a single character enclosed in single quotes. For example, ‘a’, ‘@’ are character constants. In computers, characters are stored using machine character set using ASCII codes. String Constant  A string constant is a sequence of characters enclosed in double quotes. So “a” is not the same as ‘a’. The characters comprising the string constant are stored in successive memory locations. When a string constant is encountered in a C program, the compiler records the address of the first character and appends a null character (‘0’) to the string to mark the end of the string.
  • 33. Declaring Constants  To declare a constant, precede the normal variable declaration with const keyword and assign it a value.  const float pi = 3.14;  or by using #define preprocessor directives  #define PI 3.14
  • 34. Rules for declaring constant  Rule 1: Constant names are usually written in capital letters to visually distinguish them from other variable names which are normally written in lower case characters  Rule 2: No blank spaces are permitted in between the # symbol and define keyword  Rule 3: Blank space must be used between #define and constant name and constant value  Rule 4: #define is a pre-processor compiler directive and not a statement. Therefore, it does not end with a semi-colon.
  • 35.  Comments are a way of explaining what a program does. C supports two types of comments.  // is used to comment a single statement.  /* is used to comment multiple statements.  A /* is ended with */ and all statements that lie between these characters are commented.  Note that comment statements are not executed by the compiler.  Rather, they are ignored by the compiler as they are simply added in programs to make the code understandable by programmers as well as other users.
  • 36.  The most fundamental operation in a C program is to accept input values from a standard input device and output the data produced by the program to a standard output device. As discussed, we can assign values to variables using the assignment operator ‘=’.  For example,  int a = 3;
  • 37.  What if we want to assign value to variable a is inputted from the user at run-time? This is done by using the scanf function that reads data from the keyboard.  Similarly, for outputting results of the program, printf function is used that sends results to a terminal.  Like printf and scanf, there are different functions in C that can carry out the input–output operations.  These functions are collectively known as Standard Input/Output Library.  A program that uses standard input/output functions must contain the following statement at the beginning of the program:  #include <stdio.h>
  • 38.  SCANF()  The scanf()function is used to read formatted data from the keyboard. The syntax of the scanf()  function can be given as,  scanf ("control string", arg1, arg2, arg3 ...argn);  The control string specifies the type and format of the data that has to be obtained from the keyboard and stored in the memory locations pointed by the arguments, arg1, arg2, ...,argn.
  • 39. The scanf function ignores any blank spaces, tabs, and newlines entered by the user. The function simply returns the number of input fields successfully scanned and stored.
  • 40.  The address of the variable is denoted by an & sign followed by the name of the variable.  Look at the following code that shows how we can input value in a variable of int data type:  int num;  scanf(" %4d ", &num);  The scanf function reads first four digits into the address or the memory location pointed by num.  In case of reading strings, we do not use the & sign in the scanf function.
  • 41.  The printf function is used to display information required by the user and also prints the values of the variables.  Its syntax can be given as:  printf ("control string", arg1,arg2,arg3,...,argn);
  • 42.  Consider three variables declared as,  int a=9, b=3, result;  We will use these variables to explain arithmetic operators.  The following table shows the arithmetic operators, their syntax, and usage in C language.
  • 43.  A relational operator, also known as a comparison operator, is an operator that compares two values or expressions.  Relational operators return true or false value, depending on whether the conditional relationship between the two operands holds or not.
  • 44.  C language supports different types of operators, which can be used with variables and constants to form expressions. These operators can be categorized into the following major groups:  1. Arithmetic operators  2. Relational operators  3. Equality operators  4.Logical operators  5. Unary operators  6.Conditional operator  7. Bitwise operators  8.Assignment operators  9. Comma operator  10.Sizeof operator
  • 45.  C language also supports two equality operators to compare operands for strict equality or inequality.  They are equal to (==) and not equal to (!=) operators.  The equality operators have lower precedence than the relational operators.
  • 46.  Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then −
  • 47.  Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ is as follows −
  • 48.
  • 49.  The & (bitwise AND) in C or C++ takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.  The | (bitwise OR) in C or C++ takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.  The ^ (bitwise XOR) in C or C++ takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.  The << (left shift) in C or C++ takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.  The >> (right shift) in C or C++ takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.  The ~ (bitwise NOT) in C or C++ takes one number and inverts all bits of it
  • 50. // C Program to demonstrate use of bitwise operators #include <stdio.h> int main() { // a = 5(00000101), b = 9(00001001) unsigned char a = 5, b = 9; // The result is 00000001 printf("a = %d, b = %dn", a, b); printf("a&b = %dn", a & b); // The result is 00001101 printf("a|b = %dn", a | b); // The result is 00001100 printf("a^b = %dn", a ^ b); // The result is 11111010 printf("~a = %dn", a = ~a); // The result is 00010010 printf("b<<1 = %dn", b << 1); // The result is 00000100 printf("b>>1 = %dn", b >> 1); return 0; } Output: a = 5, b = 9 a&b = 1 a|b = 13 a^b = 12 ~a = 250 b<<1 = 18 b>>1 = 4
  • 51.  unary minus(-)  increment(++)  decrement(- -)  NOT(!)  Address of operator(&)  sizeof()
  • 52.  The minus operator changes the sign of its argument.  A positive number becomes negative, and a negative number becomes positive.  int a = 10;  int b = -a; // b = -10  unary minus is different from subtraction operator, as subtraction requires two operands.
  • 53.  It is used to increment the value of the variable by 1.  The increment can be done in two ways:  prefix increment In this method, the operator preceeds the operand (e.g., ++a).  The value of operand will be altered before it is used. int a = 1; int b = ++a; // b = 2  postfix increment In this method, the operator follows the operand (e.g., a++). The value operand will be altered after it is used. int a = 1; int b = a++; // b = 1 int c = a; // c = 2
  • 54.  It is used to decrement the value of the variable by 1. The decrement can be done in two ways:  prefix decrement In this method, the operator preceeds the operand (e.g., – -a). The value of operand will be altered before it is used. int a = 1; int b = --a; // b = 0  posfix decrement In this method, the operator follows the operand (e.g., a- -). The value of operand will be altered after it is used. int a = 1; int b = a--; // b = 1 int c = a; // c = 0
  • 55.  It is used to reverse the logical state of its operand.  If a condition is true, then Logical NOT operator will make it false.  If x is true, then !x is false.  If x is false, then !x is true.
  • 56.  It gives an address of a variable. It is used to return the memory address of a variable.  These addresses returned by the address-of operator are known as pointers because they “point” to the variable in memory.  & gives an address on variable n  int a;  int *ptr;  ptr = &a; // address of a is copied to the location ptr.
  • 57.  The sizeof operator is the most common operator in C.  It is a compile-time unary operator and used to compute the size of its operand.  It returns the size of a variable.  It can be applied to any data type, float type, pointer type variables.
  • 58.  #include <stdio.h>  int main()  { int a = 16;  printf("Size of variable a : %dn",sizeof(a));  printf("Size of int data type : %dn",sizeof(int));  printf("Size of char data type : %dn",sizeof(char));  printf("Size of float data type : %dn",sizeof(float));  printf("Size of double data type : %dn",sizeof(double));  return 0; }
  • 59.  The conditional operator is also known as a ternary operator. The conditional statements are the decision-making statements which depends upon the output of the expression.  It is represented by two symbols, i.e., '?' and ':'.  As conditional operator works on three operands, so it is also known as the ternary operator.  The behavior of the conditional operator is similar to the 'if-else' statement as 'if-else' statement is also a decision-making statement.
  • 60. The conditional operator is of the form variable = Expression1 ? Expression2 : Expression3 if(Expression1) { variable = Expression2; } else { variable = Expression3; }
  • 61. #include <stdio.h> int main() { int m = 5, n = 4; (m > n) ? printf("m is greater than n that is %d > %d", m, n) : printf("n is greater than m that is %d > %d", n, m); return 0; } Output: m is greater than n that is 5>4
  • 62.  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
  • 63. #include <stdio.h> int main() { int a = 5, c; c = a; // c is 5 printf("c = %dn", c); c += a; // c is 10 printf("c = %dn", c); c -= a; // c is 5 printf("c = %dn", c); c *= a; // c is 25 printf("c = %dn", c); c /= a; // c is 5 printf("c = %dn", c); c %= a; // c = 0 printf("c = %dn", c); return 0; } Output: c = 5 c = 10 c = 5 c = 25 c = 5 c = 0
  • 64. The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator, and acts as a sequence point. int i = (5, 10); /* 10 is assigned to i*/ int j = (f1(), f2()); /* f1() is called (evaluated) first followed by f2(). The returned value of f2() is assigned to j */
  • 65.  Size of variable a : 4  Size of int data type : 4  Size of char data type : 1  Size of float data type : 4  Size of double data type : 8  d=a+=b*=c-=10 a=2,b=5,c=1  c=c-10 c=1-10=-9 c=-9  a+=b*=-9  b=b*-9 b=5*-9 b=-45  a+=-45 a=2+(-45)=2-45=-43
  • 66.
  • 67.  Unformatted I/O Functions  Formatted I/O Functions
  • 68.  There are mainly six unformatted I/O functions discussed as follows: a) getchar() b) putchar() c) gets() d) puts() e) getch() f) getche()
  • 69.  This function is an Input function. It is used for reading a single character from the keyborad. It is buffered function. Buffered functions get the input from the keyboard and store it in the memory buffer temporally until you press the Enter key.  The general syntax is as: v = getchar(); where v is the variable of character type. For example: char n; n = getchar(); A simple C-program to read a single character from the keyboard is as: /*To read a single character from the keyboard using the getchar() function*/ #include void main() { char n; n = getchar(); }
  • 70. This function is an output function. It is used to display a single character on the screen. The general syntax is as:  putchar(v); where v is the variable of character type. For example: char n; putchar(n); A simple program is written as below, which will read a single character using getchar() function and display inputted data using putchar() function: /*Program illustrate the use of getchar() and putchar() functions*/ #include main() { char n; n = getchar(); putchar(n); }
  • 71. This function is an input function. It is used to read a string from the keyboard. It is also buffered function. It will read a string, when you type the string from the keyboard and press the Enter key from the keyboard. It will mark nulll character ('0') in the memory at the end of the string, when you press the enter key. The general syntax is as:  gets(v); where v is the variable of character type. For example: char n[20]; gets(n); A simple C program to illustrate the use of gets() function: /*Program to explain the use of gets() function*/ #include main() { char n[20]; gets(n); }
  • 72. This is an output function. It is used to display a string inputted by gets() function. It is also used to display an text (message) on the screen for program simplicity. This function appends a newline ("n") character to the output.  The general syntax is as: puts(v); or puts("text line"); where v is the variable of character type. A simple C program to illustrate the use of puts() function: /*Program to illustrate the concept of puts() with gets() functions*/ #include main() { char name[20]; puts("Enter the Name"); gets(name); puts("Name is :"); puts(name); }
  • 73.  This is also an input function. This is used to read a single character from the keyboard like getchar() function. But getchar() function is a buffered is function, getchar() function is a non-buffered function. The character data read by this function is directly assign to a variable rather it goes to the memory buffer, the character data directly assign to a variable without the need to press the Enter key. Another use of this function is to maintain the output on the screen till you have not press the Enter Key. The general syntax is as:  v = getch(); where v is the variable of character type. A simple C program to illustrate the use of getch() function: /*Program to explain the use of getch() function*/ #include main() { char n; puts("Enter the Char"); n = getch(); puts("Char is :"); putchar(n); getch(); }
  • 74.  All are same as getch() function execpt it is an echoed function. It means when you type the character data from the keyboard it will visible on the screen. The general syntax is as:  v = getche(); where v is the variable of character type. A simple C program to illustrate the use of getch() function: /*Program to explain the use of getch() function*/ #include main() { char n; puts("Enter the Char"); n = getche(); puts("Char is :"); putchar(n); getche(); }
  • 75.  Formatted I/O functions which refers to an Input or Ouput data that has been arranged in a particular format. There are mainly two formatted I/O functions discussed as follows:  a) scanf() b) printf()
  • 76.  The scanf() function is an input function. It used to read the mixed type of data from keyboard. You can read integer, float and character data by using its control codes or format codes. The general syntax is as:  scanf("control strings",arg1,arg2,..............argn); or scanf("control strings",&v1,&v2,&v3,................&vn); Where arg1,arg2,..........argn are the arguments for reading and v1,v2,v3,........vn all are the variables.
  • 77.  The scanf() format code (spedifier) is as shown in the below table:
  • 78.  This ia an output function. It is used to display a text message and to display the mixed type (int, float, char) of data on screen. The general syntax is as:  printf("control strings",&v1,&v2,&v3,................&vn); or printf("Message line or text line"); Where v1,v2,v3,........vn all are the variables. The control strings use some printf() format codes or format specifiers or conversion characters. 
  • 79.  These all are discussed in the below table as:
  • 80.  /*Below the program which show the use of printf() function*/ #include main() { int a; float b; char c; printf("Enter the mixed type of data"); scanf("%d",%f,%c",&a,&b,&c); getch(); }
  • 81. An assignment statement gives a value to a variable. For example,  x = 5;  gives x the value 5.  The value of a variable may be changed. For example, if x has the value 5, then the assignment statement  x = x + 1;  will give x the value 6.  The general syntax of an assignment statement is  variable = expression; where:  the variable must be declared;  the variable may be a simple name, or an indexed location in an array, or a field (instance variable) of an object, or a static field of a class; and  the expression must result in a value that is compatible with the type of the variable.  In other words, it must be possible to cast the expression to the type of the variable.
  • 82.
  • 83.  Syntax:  if(condition) {  // Statements to execute if  // condition is true  }  if(condition)  statement1;  statement2;
  • 84. Example #include<stdio.h> int main() { int number=0; printf("Enter a number:"); scanf("%d",&number); if(number%2==0) { printf("%d is even number",number); } return 0; }
  • 85. #include <stdio.h> int main() { int a, b, c; printf("Enter three numbers?"); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) { printf("%d is largest",a); } if(b>a && b > c) { printf("%d is largest",b); } if(c>a && c>b) { printf("%d is largest",c); } if(a == b && a == c) { printf("All are equal"); } }
  • 86. Syntax:  if (condition) {  // Executes this block if  // condition is true  }  Else  {  // Executes this block if  // condition is false  }
  • 87. #include<stdio.h> int main(){ int number=0; printf("enter a number:"); scanf("%d",&number); if(number%2==0){ printf("%d is even number",number); } else{ printf("%d is odd number",number); } return 0; }
  • 88. Syntax:  if (condition)  statement;  else if (condition)  statement;  . . else statement;
  • 89. #include<stdio.h> int main(){ int number=0; printf("enter a number:"); scanf("%d",&number); if(number==10){ printf("number is equals to 10"); } else if(number==50){ printf("number is equal to 50"); } else if(number==100){ printf("number is equal to 100"); } else{ printf("number is not equal to 10, 50 or 100"); } return 0; }
  • 90. #include <stdio.h> int main() { int marks; printf("Enter your marks?"); scanf("%d",&marks); if(marks > 85 && marks <= 100) { printf("Congrats ! you scored grade A ..."); } else if (marks > 60 && marks <= 85) { printf("You scored grade B + ..."); } else if (marks > 40 && marks <= 60) { printf("You scored grade B ..."); } else if (marks > 30 && marks <= 40) { printf("You scored grade C ..."); } else { printf("Sorry you are fail ..."); } }
  • 91.  Syntax:  if (condition1)  {  // Executes when condition1 is true if (condition2) {  // Executes when condition2 is true  }  }
  • 92. switch (expression) ​ { case constant1: // statements break; case constant2: // statements break; . . . default: // default statements }
  • 93. #include<stdio.h> int main(){ int number=0; printf("enter a number:"); scanf("%d",&number); switch(number){ case 10: printf("number is equals to 10"); break; case 50: printf("number is equal to 50"); break; case 100: printf("number is equal to 100"); break; default: printf("number is not equal to 10, 50 or 100"); } return 0; }
  • 94.  A Loop executes the sequence of statements many times until the stated condition becomes false.  A loop consists of two parts, a body of a loop and a control statement.  The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false.  The purpose of the loop is to repeat the same code a number of times.
  • 95.  1. Entry controlled loop  2. Exit controlled loop  In an entry controlled loop, a condition is checked before executing the body of a loop.  It is also called as a pre-checking loop.  In an exit controlled loop, a condition is checked after executing the body of a loop.  It is also called as a post-checking loop.
  • 96.  'C' programming language provides us with three types of loop constructs:  1. The while loop  2. The do-while loop  3. The for loop
  • 97.  A while loop is the most straightforward looping structure.  Syntax of while loop in C programming language is as follows: while (condition) { statements; }
  • 98. #include<stdio.h> int main() { int num=1; //initializing the variable while(num<=10) //while loop with condition { printf("%dn",num); num++; /incrementing operation } return 0; }
  • 99. do { statements } while (expression);
  • 100.  #include<stdio.h>  int main()  { int num=1; //initializing the variable  do //do-while loop  {  printf("%dn",2*num);  num++; //incrementing operation }while(num<=10);  return 0;  }
  • 101.  for (initial value; condition; incrementation or decrementation )  {  statements;  }
  • 102.  #include<stdio.h>  int main()  {  int number; for(number=1;number<=10;number++) //for loop to print 1-10 numbers  {  printf("%dn",number);  }  return 0;}
  • 103.  The break statement is used mainly in in the switch statement. It is also useful for immediately stopping a loop.  We consider the following program which introduces a break to exit a while loop: #include <stdio.h> int main() { int num = 5; while (num > 0) { if (num == 3) break; printf("%dn", num); num--; } }
  • 104.  When you want to skip to the next iteration but remain in the loop, you should use the continue statement.  For example: #include <stdio.h> int main() { int nb = 7; while (nb > 0) { nb--; if (nb == 5) continue; printf("%dn", nb); } }