1.introductionn Part1
1.introductionn Part1
1
C Programming | CCIT
Table of Contents
Introduction ................................................................................... 3
C language..................................................................................... 3
Compiler ....................................................................................... 3
General Format of C++ ................................................................. 4
Library Functions .......................................................................... 5
Escape sequences ........................................................................ 7
Data Types ..................................................................................... 8
Variables ....................................................................................... 8
Data types .................................................................................... 8
Primitive Data Types ..................................................................... 8
Format specifiers ....................................................................... 13
Operators ..................................................................................... 14
Arithmetic Operators ................................................................. 14
Assignment Operators ............................................................... 14
Relational Operators .................................................................. 15
Logical Operators ....................................................................... 15
Comments .................................................................................. 15
Operators Precedence ............................................................... 16
Operators Associvity .................................................................. 16
Mixed expression ....................................................................... 17
2
C Programming | CCIT
Introduction
C language
• C is a powerful general-purpose programming language.
• C is a middle level language which can be used to perform High Level
Operations as well as low level operations.
• C Language is used for system Programming i.e. to design programs to control
the system.
• It can be used to develop software like operating systems, databases,
compilers, and so on.
History
• C programming language was developed in 1972 by
Dennis Ritchie at bell laboratories of AT&T
(American Telephone & Telegraph), located in the
U.S.A.
• It was developed to overcome the problems of
previous languages such as B, BCPL, etc.
• Initially, C language was developed to be used in
UNIX operating system. It inherits many features of previous languages such
as B and BCPL.
Compiler
• Compiler is a program which converts high/middle level language source code
into m/c code (i.e. low level instructions).
3
C Programming | CCIT
Preprocessor command
• These are the instructions which we want to give to our compiler (Its
Preprocessor).
• They indicate how the program is to be compiled.
• A Preprocessor command starts with symbol ‘#’.
• for e.g.:
1. #include
2. #define
3. #ifdef..
Global Declaration
• In this section we can define different type of programming elements such as
Variables, Constants etc.
• Programming elements defined in this section can be accessible throughout
the program.
4
C Programming | CCIT
Function Main
• It is the entry point of our program.
• Execution of our program begins with function main.
Local Declaration
• In this section we can define different type of programming elements such as
Variable, Constants etc.
• Programming elements defined in this section are accessible only within the
function in which they are defined.
Statements
• These are set of instructions which we want to give to our system to perform
some task.
return statement
• Return statement within function main is use to return the control to
operating system.
• return value indicate how the program is terminated i.e. normally or
abnormally
o return 0 - normal termination
o return 1 - abnormal termination
• Note: return statement is optional
Library Functions
• To perform common task C language provide us many library functions.
• These functions are declared in different header files such as math.h,
graphics.h, stdio.h, string.h etc
• If we want to use these functions in our program. Then we have to include the
header file in our program by using the preprocessor command.
#include < headerfilename >
5
C Programming | CCIT
printf( )
• This function is used to print formatted string on standard output device.
• The function prints the string inside quotations.
• To use printf() in our program, we need to include stdio.h header file using
the #include<stdio.h> statement.
printf("formatted string",args1,arg2, . . . );
clrscr( )
• It is use to clear the screen.
• It is a predefined function in "conio.h" (console input output header file) used
to clear the console screen.
clrscr( )
getch()
• It is use to wait until a key is pressed.
• It is a predefined function in "conio.h" (console input output header file) will
tell to the console wait for some time until a key is hit given after running of
program.
getch( )
6
C Programming | CCIT
Escape sequences
• These are the special control characters which we can sent to our standard
output device to perform different action.
\n New line
\t tab
\b backspace
\r Carriage return
\' To Print Single quote
\" To Print Double quote
\\ To Print backslash
\f Form feed
C first program
#include<stdio.h> Welcome to C programming
main() Amravati
{ CCIT
printf("Welcome to C Programming");
printf("\nAmravati");
printf("\nCCIT");
}
7
C Programming | CCIT
Data Types
Memory Variables
• It is a location in memory where we can store some data.
• A memory variable can be defined by specifying its data type , variable name
and optional value.
Data types
• To store different type of data in memory c language provides us different
data types.
• According to these data types it is decided how much memory space will be
used to store the data and how data will be represented in memory.
• We can group these data type in different category
o Primitive data type (int ,float,double,char)
o Modified data type (long int, unsigned int…)
o Derived data type (arrays)
o User defined data type (struct, union…)
8
C Programming | CCIT
char
• It is used to store characters.
• Size in memory is 1 byte.
• Range is ±27 .( -127 to 128 )
• For ex: ‘A’,’z’, ‘$’, ‘7’ etc.
float
• It is used to store floating point values with single precision.
• Size in memory is 4 bytes
• float has 7 decimal digits of precision.
• Range (1.2e-38 to 3.4e38 )
• For ex: 3.14f , -0.0124f , 2.015fe+003 etc
double
• It is used to store floating point values with double precision.
• Size in memory is 8 bytes
• double has 15 decimal digits of precision.
• Range (2.2e-308 to 1.8e308 )
• For ex: 3.140163 , -0.01253424 , 2.015657e+003 etc
Format specifiers
• To print formatted output by using printf function C provide us different type
specifiers.
Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and
data.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
++ Increment
-- Decrement
Assignment Operators
Assignment operators are used to assign values to variables.
Operator Operation Equivalent to
= 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
10
C Programming | CCIT
Relational Operators
A relational operator is used to check the relationship between two operands.
Operator Meaning
< Less Than
== is Equal To
!= Not Equal To
Logical Operators
Logical operators are used to check whether an expression is true or false.
Operator Meaning
&& Logical AND.
True only if all the operands are true.
|| Logical OR.
True if at least one of the operands is true.
! Logical NOT.
True only if the operand is false.
Comments
C++ comments are hints that a programmer can add to make their code easier to
read and understand. They are completely ignored by C++ compilers.
There are two ways to add comments to code:
• // - Single Line Comments
• /* */ -Multi-line Comments
11
C Programming | CCIT
Operators Precedence
If an expression contains different operators then they are evaluated according to
their precedence
Precedence Operator Description Associativity
1 a++ Suffix/postfix increment Left to Right
a-- Suffix/postfix decrement
2 ++a Prefix increment Right to Left
--a Prefix decrement
3 a*b Multiplication Left to Right
a/b Division
a%b Modulus
4 a+b Addition Left to Right
a-b Subtraction
5 < Less than Left to Right
<= Less than or equal to
> Greater than
>= Greater than or equal to
6 == Equal to Left to Right
!= Not equal to
7 && Logical AND Left to Right
8 || Logical OR Left to Right
Operators Associvity
• If an expression contains different operators having same precedence then
they are evaluated according to their Associvity.
• For basic arithmetic operators it from left to right.
12
C Programming | CCIT
Mixed expression
• If an expression contains different type of values then result is calculated
according to higher data type of value present in expression.
scanf()
This function is use to read user input.
Syntax:
scanf(“format string”,addr1,addr2,…) ;
• It scan the user input according to format string and stores it at specified
memory addresses.
• ‘&’ operator is use to find memory address of variable.
Syntax:
&variableName
13
C Programming | CCIT
{ Total is 290
int a,b,c,d,e,t;
printf("Enter marks for 5 subjects ");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
t=a+b+c+d+e;
printf("Total is %d",t);
}
WAP to read length & breadth of Rectangle and find its area and perimeter
• A=L*B
• P=2*(L+B)
#include<stdio.h> . Enter Length and Breadth 5.2 3.1
main() Area is 16.12
{ Perimeter is 16.6
float L, B, A, P ;
printf("Enter Length and Breadth");
scanf("%f %f",&L,&B);
A=L*B;
P=2*(L+B);
printf("Area is %f",A);
printf("\nPerimeter is %f",P);
}
14
C Programming | CCIT
WAP to read radius of circle and find its area and circumference.
A=3.14* r*r
C=2*3.14*r
#include<stdio.h> Enter
. Radius 2.5
main() Area is 19.62
{ Circumference is 15.7
float r, a, c ;
printf("Enter Radius ");
scanf("%f",&r);
a=3.14*r*r;
c=2*3.14*r;
printf("Area is %f", a );
printf("\nCircumference is %f“ , c );
}
15
C Programming | CCIT
16
C Programming | CCIT
{ Temperature in celsius is
48.8
int f;
float c;
printf("Enter temperature in fahrenheit");
scanf("%d",&f);
c=5/9.0*(f-32);
printf("Temperature in celsius is %f",c);
}
17
C Programming | CCIT
WAP to read 2 values into variables A and B and exchange their values.
WAP to read 2 values into variables A and B and exchange their values without
using 3rd variable.
#include<stdio.h> Enter 2 Numbers 7 5
main() Value of a = 5 b = 7
{ Value of a = 7 b = 5
int a , b;
printf("Enter 2 Numbers ");
scanf( "%d %d“ , &a , &b );
printf("\nValue of a=%d b=%d",a,b);
a = a +b ;
b = a - b;
a = a - b;
printf("\nValue of a=%d b=%d",a,b);
}
18
C Programming | CCIT
19