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

1.introductionn Part1

Uploaded by

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

1.introductionn Part1

Uploaded by

Manglya Vasule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C Programming | CCIT

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

General Format of C++ Program

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.

datatype variablename [ =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…)

Primitive Data Types


int
• It is used to store whole numbers.
• Size in memory is 2 or 4 bytes depending on compiler and system
• Range 2 bytes ±215 (-32,768 to 32,767 )
• Range 4 bytes ±231 (-2,147,483,648 to 2,147,483,647 )
• For ex: 4117, -458, 0x13a8, 017 etc.

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.

datatype Type specifier


int %d %x %o
float %f %e
double %lf
char %c
9
C Programming | CCIT

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

> Greater Than

<= Greater Than or Equal To

>= Less Than or Equal To

== 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

Program to read 2 nos and find their sum.


#include<stdio.h> . Enter 2 numbers 45 55
main() Sum is 100
{
int a,b,c;
printf("Enter 2 numbers ");
scanf("%d %d",&a,&b);
c=a+b;
printf("Sum is %d",c);
}

13
C Programming | CCIT

Program to read marks for 5 subjects and find total marks.

#include<stdio.h> .Enter marks for 5 subjects


main() 50 60 55 60 65

{ 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 );
}

WAP to read radius of sphere and find its Volume


4
V= 𝜋𝑟 3 .
3

#include<stdio.h> Enter Radius 2.5


main() Volume of Sphere is
65.416666
{
float r, v ;
printf("Enter Radius ");
scanf( "%f“ , &r );
v = 4 / 3 * 3.14 * r * r * r;
printf("Volume of Sphere is %f",v);
}

15
C Programming | CCIT

WAP to read value of x and calculate value of y for given exp


3 4
Y= 𝑥 3 − 𝑥 2 + 100
7 5

#include<stdio.h> Enter value of x 2


main() Result is 100.22857
{
float x , y ;
printf("Enter value of x ");
scanf( "%f", &x );
y =3/7.0 * x * x * x – 4/5.0 * x * x+100 ;
printf("Result is %f",y);
}

WAP to read value of x& y and calculate result of given exp


(𝑥+𝑦)2
Z=
𝑥−𝑦

#include<stdio.h> Enter value of x and y 2 3


main() Result is 25
{
float x , y , z ;
printf("Enter value of x and y ");
scanf("%f %f“ , &x , &y ) ;
z = ( x + y )*( x + y )/( x – y ) ;
printf("Result is %f“ , z ) ;
}

16
C Programming | CCIT

WAP to read temp in Fahrenheit and convert it into degree Celsius


5
𝐶 = (𝐹 − 32)
9

#include <stdio.h> Enter temperature in


int main() fahrenheit 120

{ 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);
}

WAP to read 3 integer values and find their mean.

#include<stdio.h> Enter 3 Numbers 5 8 2


main() Mean is 15
{
int a , b , c ;
float m;
printf("Enter 3 numbers ");
scanf("%d %d %d",&a,&b,&c);
m=a+b+c/3;
printf("Mean is %f",m);
}

17
C Programming | CCIT

WAP to read 2 values into variables A and B and exchange their values.

#include<stdio.h> Enter 2 Numbers 7 5


main() Value of a = 5 b = 7
{ Value of a = 7 b = 5
int a , b;
int temp;
printf("Enter 2 Numbers ");
scanf( "%d %d“ , &a , &b );
printf("\nValue of a=%d b=%d",a,b);
temp = a;
a = b;
b = temp;
printf("\nValue of a=%d b=%d",a,b);
}

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

WAP to read a number and find its last digit.


#include<stdio.h> Enter a Number 28
main() Last digits is 8
{
int a;
int x;
printf("Enter a Number ");
scanf("%d",&a);
x = a%10 ;
printf("Last digit is %d",x);
}

WAP to read 2 nos and find sum of their last digits

#include<stdio.h> Enter 2 Numbers 78 62


main() Sum of last digits is 10
{
int a , b ;
int x , y , z ;
printf("Enter 2 Numbers ");
scanf("%d %d",&a,&b);
x = a%10;
y = b%10;
z = x+y;
printf("sum of Last digit is %d",z);
}

19

You might also like