Unit 2 C Programming
Unit 2 C Programming
_________________________________________________________
Contents
• Introduction to C
• History of C
• Characteristics of C
• Application of C
• Building blocks of C
• Data Type
• Program Structure
• First C Program
Computer is an electronic device. It can perform arithmetic and logical
operations. Computer consist of two types of elements. Hardware and Software.
It acts as an interface between the user and the hardware. A user cannot
run an application program on computer without OS.
Introduction to Software.
Software.
No Name Description
The software that helps the system to run
1 System Software. and manage all its task are called system
software.
These are the software developed as per
the user need of user. User can install these
2 Application software. software when required or can use it if
already available.
Ex- MS Office, Tally, Calculator.
The computer languages are called
programming languages. With the help of
3 Programming Languages. these languages user/ developer can
develop system or application software,
Ex- C, C++, Java.
The tools in which user can develop system
or application software are called
development tools. It is of two types.
Advanced web Front End Development tool- Tools to create
4
development tools. screens and reports or User Interface. Ex-
TurboC++, Netbeans.
Back end tools- Tools to store data entered.
Ex- Oracle , MySQL
These tool allows to create web sites and
5 Web based tools. web pages.
Ex- HTML, PHP.
C Programming
C is a computer programming language. It is a middle level language
developed by Dennis Ritchie in 1970.
History of C
C was invented and first implemented by Dennis Ritchie in 1970. For many
years the version used was supplied by the UNIX operating system. In the summer
of 1983 a committee was established to create an ANSI (American National
Standards Institute) standard that would define the C language. The
standardization process took 6 years. The ANSI (American National Standard
Institute) C was finally adopted in 1938 with the first copies became available in
early 1990.
What is C? / C characteristics.
• C is computer programming language.
• It is a high level language sometimes called middle level language.
• It is a block structured programming language.
• It is a Procedure Oriented Programming Language.
• It follows top down approach.
Applications developed in C
Operating System: Linux OS and its variants, Unix and it's variants like
BSD, Sun OS e.t.c.
TIOBE stands for The Importance of Being Earnest. The software quality
company. The TIOBE Programming Community index is an indicator of the
popularity of programming languages
2 2 C 16.33% +3.03%
5 6 C# 5.32% +2.05%
C as a procedural language
A procedural language breaks the program into functions, data structures, etc.
In the C language, we break the program into parts using functions. It makes
the program easier to understand and modify.
• The basic concept and programming skills are used in C++, Java, and
C#. SO without learning C it is very difficult to learn these languages.
• Major parts of OS like windows, UNIX, are written in C language.
• Device drivers are also written in C language.
• OS used in electronic devices are written in C.
Tokens
Tokens are the smallest individual unit in a C program. Token include
Identifier, Variables, special Symbol, etc.
Keywords
Identifie
rs
Special
Tokens Symbol
Constants
Strings
Operators
A character denotes any alphabet, digit or special symbol used to represent data
or information.
Digits 0–9
Alphabets A to Z and a to z
Special Symbol / * - + # @ ! & * ^, etc.
Variables
Constants-
C Constants are also like normal variables. But, only difference is, their
values cannot be modified by the program once they are defined. Constant in C
means the content whose value does not change at the time of execution of a
program.
a = 5 // 5 is constant
Types of constants
• Integer constants
• Real or Floating point constants
• String constants
INTEGER CONSTANTS IN C:
REAL CONSTANTS IN C:
Character Constant in C
Data Types-
Data types are used to specify which kind of value we are going to use in
our programs. Consider the example of an Employee which may have data like his
ID, salary and name. Here name of employee is set of alphabets, whereas ID is
whole number and salary may be in decimal point. To use data we require different
kind of data types to be matched with the information we are going to use.
• Primary/Fundamental
• Derived
• User defined
C data types
Array Structure
Function Union
Pointer
Data Keyword
Description Size(Bytes) Range
Type Used
Use to represent a single
Character char character. Alphabet, 1 -128 to +127
Digit, etc.
Use to indicate whole -32767 to
Integer int 2
number. +32767
3.4E-38 to
Float float Use for decimal value 4
3.4E+38
Use for precise decimal 1.7E-308 to
Double double 8
value. 1.7E+308
Syntax-
datatype variablename;
Example-
char name;
int age;
Type Qualifiers
C – Type qualifiers: The keywords which are used to modify the properties
of a variable are called type qualifiers. These qualifiers are used with data type
to modify their range.
Long
Short
Signed
Unsigned
Long and unsigned are used to increase the range of values at the time of
declaration.
If sign is not specified for the integer, float & double then by default sign is
‘+’ i.e. positive. If sign is required i.e., negative sign and the short values then
signed and short qualifiers are used data types with their extended data types.
Variables
Variables are the name given to a memory location. Variables are defined
as the entities which can be changed at different times. One variable stores one
value at a time. To store the value in a variable data type and variable name is
required. The variable name is also called identifier. Real values can be stored
using float or double data type.
Variable declaration
datatype variable_name;
Variable Initialization
For example:
Header file
#include Instruction
#include statement is used to include the standard library functions. The
definition of these library functions are written in the header files. To carry out the
input and output operations we need to include <stdio.h> header file. Header file
is the first line in any C program. Including a header file can be stated in other
meaning as including the contents of that file in our program.
Executing a C program
Main function-
Program execution always start from main function. The main function is
called by the operating system. It is necessary to write main function in the
program. Main function can be written in many ways.
void main()
int main()
int main(void)
void is used when the main is not returning any value. Main function can
return value or not. If main is written as int main() then it is necessary to write
return 0; statement at the end of main function.
printf() function
The printf() function is used for output. It prints the given statement to
the console.
printf("format string",argument_list);
scanf() function
The scanf() function is used for input. It reads the input data from the console.
scanf("format string",argument_list);
Example
printf("enter a number:");
scanf("%d",&number);
Structure of C Program-
Example-
Based on the above structure the sample program in given below. This
program do not include any user defined functions or any symbolic constant.
My First C Program
Program to display hello World message.
#include<stdio.h>
void main()
{
// addition
int a,b,c;
a=10,b=20;
c=a+b;
printf("Addition is %d",c);
}
Comments
We can add comments in our program to describe what we are doing in the
program. These comments are ignored by the compiler and are not executed.
#include<stdio.h>
int main(){
printf("You\nare\nlearning\n\'c\' language\n\"Do you know C language\
"");
return 0;
}
C Preprocessor Directives
The C preprocessor is a micro processor that is used by compiler to
transform your code before compilation. It is called micro preprocessor
because it allows us to add macros.
Formatted Unformatted
These functions allow to supply input These functions are the most basic
or display output in user desired form of input and output and they do
format. not allow to supply input or display
output in user desired format.
printf() and scanf() are examples for getch(), getche(), getchar(), gets(),
formatted input and output functions. puts(), putchar() etc. are examples of
unformatted input output functions.
Functions Description
getch()
Reads a single character from the user at the console,
without echoing it.
getche()
Reads a single character from the user at the console,
and echoing it.
getchar()
Reads a single character from the user at the console,
and echoing it, but needs an Enter key to be pressed at
the end.
gets()
Reads a single string entered by the user at the
console.
puts()
Displays a single string's value at the console.
putch()
Displays a single character value at the console.
putchar()
Displays a single character value at the console.
// program for getche and putchar // program for gets and puts
#include<stdio.h> #include<stdio.h>
#include<conio.h> void main()
{
void main() char str[20];
{ printf("Enter your name \n");
char ch ; gets(str);
clrscr(); puts(str);
printf(“Press any character:”); }
ch = getche();
printf(“\nYou pressed :”
putchar(ch);
getch();
}
// getchar and putchar demo //Program to take string and print using scanf
program and gets.
#include <stdio.h>
void main( ) #include<stdio.h>
{ void main()
int c; {
printf("Enter a character\n"); char name[20];
printf("Enter your name\n");
c = getchar(); scanf("%s",name); //only accept first word
ex- welcome
putchar(c);
} gets(name); //it will accept whole string with
blank space. //ex- Welcome java
printf("Hello %s Welcome to C language
\n",name);
puts(name);
}
Declaring Constants.
e.g. variable pi can be assigned a value [Link] Value does not change
throughout the program. So these types of Variable can be declare using ‘const’
keyword or by using #define preprocessor
#include <stdio.h>
void main()
{
const float pi = 3.14;
printf("value of PI :%f \n", pi );
}
#include<stdio.h>
#define val 10
#define floatVal 4.5
#define charVal 'G'
int main()
{
printf("Integer Constant: %d\n",val);
printf("Floating point Constant: %.1f\n",floatVal);
printf("Character Constant: %c\n",charVal);
return 0;
}
Operators
Operators are defined as the symbols used to indicate the operations on
the operands (values). Where operand are the entities on which operation is
performed.
For example:
Operand
100 + 80
Operator
c= a+b;
Here a and b are the operands and + is the operator. The addition of a
and b is assigned to c
Arithmetic Operator-
Operator Description
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Remainder after division
Program-
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c=a/b;
printf("a/b = %d \n",c);
return 0;
}
Note – The difference between division / and remainder % is that division gives
the quotient and % modulus operator % gives the remainder.
Operator Description
++ Increases the value by 1
-- Decreases the value by 1
• pre-increment
• post-increment
In pre-increment first increment the value of variable and then used inside
the expression (initialize into another variable).
Syntax
++ variable;
Syntax
variable ++;
#include<stdio.h>
void main()
{
int a,b,result;
a=10,b=5;
printf("value of a is %d\t\n",a);
result=++a;
printf("value of a is %d\t\n",result);
printf("value of b is %d\t\n",b);
result=b++;
printf("value of b is %d\t\n",result);
}
3- Assignment Operator
= 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 = 22, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("Addition is c = %d \n", c);
c -= a; // c = c-a
printf("Substraction is c = %d \n", c);
c *= a; // c = c*a
printf("Multipliaction is c = %d \n", c);
c /= a; // c = c/a
printf("Division c = %d \n", c);
c %= a; // c = c%a
printf("Remainder is c = %d \n", c);
return 0;
}
4- Relational Operator
Operator Description
== Equal to
> Greater than
< Less than
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
5- Logical Operator
An expression containing logical operator returns either 0 or 1 depending
upon whether expression results true or false. Logical operators are commonly
used in decision making in C programming.
C language supports following 3 logical operators. Suppose a=1 and b=0,
|| Logical OR (a || b) is true
6- Bitwise Operator
Operator Description
| Bitwise OR
^ Bitwise exclusive OR
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0
Bitwise operators perform manipulations of data at bit level. These
operators also perform shifting of bits from right to left. Bitwise operators are
not applied to float or double.
7- Conditional Operator
#include<stdio.h>
void main()
{
int n1,n2,max;
printf("Enter any two number \n");
scanf("%d%d",&n1,&n2);
max=(n1>n2)?n1:n2;
printf("Greater number is %d",max);
}
8 – Special Operator
Program-
#include<stdio.h>
void main()
{
printf("Size of char is %d ",sizeof(char));
printf("\nSize of int is %d ",sizeof(short int));
printf("\nSize of float is %d ",sizeof(float));
printf("\nSize of double is %d ",sizeof(double));
}
Precedence
Associativity
#include <stdio.h>
main() {
int a = 20;
int b = 10;
int c = 15;
int d = 5;
int e;
e = (a + b) * c / d; // ( 30 * 15 ) / 5
printf("Value of (a + b) * c / d is : %d\n", e );
e = ((a + b) * c) / d; // (30 * 15 ) / 5
printf("Value of ((a + b) * c) / d is : %d\n" , e );
e = (a + b) * (c / d); // (30) * (15/5)
printf("Value of (a + b) * (c / d) is : %d\n", e );
e = a + (b * c) / d; // 20 + (150/5)
printf("Value of a + (b * c) / d is : %d\n" , e );
return 0;
}
The type conversion performed by the programmer by posing the data type
of the expression of specific type is known as explicit type conversion.