Вы находитесь на странице: 1из 27

C PROGRAMMING

MODULE 1
C PROGRAM
 Program
to display the message “Welcome to computer
programming!”

/*display a message on the monitor*/


#include<studio.h>
void main()
{
printf(“Welcome to computer programming!/n”);
}
C PROGRAM
 Every C program contains a function called ‘main’
 This is the starting point of executable program
 A C program may contain one or more functions (Modules) one of
which should be a main function which invokes all other functions
 Each function is associated with one data type it returns.
Eg: void(null), int, float(decimal point number) etc.
 Each function statements should be enclosed in {…………………..}
 Comments are inserted in between /* ………………..*/ , which are
statements not compiled or translated to object (Machine) code
C PROGRAM
 ‘printf’ is a library function, provided by compiler ready for use
 Arguments of function are written inside (…………..)
 C uses a semicolon as statement terminator
 All program statements should be written in lower case letter
 #include<studio.h> is a preprocessor directive, written just before
main ( ) in the C program, not terminated by semicolon
 stdio.h - Header file which contains printf( ) function
 Header file is added at the beginning of the program before
compilation
 \n - Escape sequence to print new line character, to output a special
character
Escape sequences

Code Meaning
\t Horizontal tab
\v Vertical tab
\\ Backslash
\” Double quote
\’ Single quote
\n New line
Program -2

 A)Display your Name & Address in separate lines


 B) Display Address in online line with one tab space in between.
Identifier

 Identifier or name is a sequence of character used by programmer to


identify or name a specific object
 In C variables, arrays, functions and labels are named
 Variable is a data object in storage
 Array is a collection of objects of same data type
 Function is an executable module, also called sub routine
C Identifier - Rules

 Case significant : Number is not same as number or NUMBER


 Under score should not be first character, can be character in
between
 Numeric digit should not be first character, can be character in
between or at end
 Upto 31 characters can be used, Eight to ten sufficient
 Should not be key word
 Key word eg:
Data Types

Derived
Primitive/Basic User-defined Valueless
Data Type
Data Type Data Type • void
• array
• char • structure
• function
• int • union
• pointer
• float • enumeration
• double

Data Type Size(in bits) Range


char 8 -128 to 127
int 16 -32768 to 32767
float 32 3.4E-38 to 3.4E+38
double 64 1.7E-308 to 1.7E+308
void 0 Null
Operators

Arithmetic Assignment Equality Relational Logical Bit-wise


Type of operator Operator symbols with meaning Type of Operator symbols with
operator meaning

Arithmetic ++ Increment Relational >,<.>=,<=


_ _ Decrement
Binary Equality ==(Equal to)
+ Addition !=(Not equal to)
- Subtraction
* Multiplication Logical &&(Logical AND)
/ Division || (Logical OR)
% Modulus ! (Logical NOT)
Assignment Simple assignment = Bitwise & (Bitwise AND)
Compound assignment | (Bitwise OR)
+=,-+,*=,/=,%=,&=,^=,|= ^ (Exclusive OR)
Expression assignment ~ (Complement)
A=5+(b=8+(c=2))-4 >> (Right shift)
Program Statement

 Comprise an expression or a combination of keywords or


expression with or without operators.
 Terminated by semi-colon(;)
Declaration
Expression
Compound
Statement Labeled Selection
Controlled Iteration
Jump
Declaration of variables
 Declaration introduces one or more variable
 Directs compiler to allocate memory for the variable
 Declaration gives the variable a symbolic name
 Format:
data_type variable_name;
Eg:
int a;
int b;
float c;
char d;
int a,b;
Constants

 It is an explicit data value written by programmer.


 It is a value known by compiler at compiling time.
 Constants:
 Integer constant
 Floating point constant
 Character constant
 String constant
Assignment Operator And Initialisation

 variable_name=expression
 Eg:
 i=6;

 i=i+1

 y=x+2-z
Formatted Input & Output Functions
 printf(“control_string”,variable1,variable2,variable3…….);
 Eg:
 printf(“hello”);

 printf(“Total=%d”,total);

 Format specifiers for printf() Eg : printf (“Print : %d \t%f \t%c%t%s”, x,y,a,b)


Conversion code Usual variable type Display
%c char Single character
%d(%i) int Signed integer
%f float Signed decimal
%p pointer Address stored in
%s array of char pointer
%u int String
Unsigned integer
Input Function scanf()

 Scanf(“control_string”,variable1_address,variable2-address,…………..);
 Eg:
 int x
scanf(“Number=%d”,&x);
printf (“value of x is %d\n”,x);
01. Program to print Welcome

/* 01 Print Welcome*/ #include<stdio.h>


main()
{
printf("Welcome\n");
}
/*End of Program*/
/* OUTPUT
Welcome
*/
02. Program to calculate and print the area
of a triangle(b=4,h=6)
/* 02 Program to calculate the area of a triangle*/
#include<stdio.h>
main()
{
int b,h,area; b=4;
h=6;
area=(b*h)/2;
printf("Area =%d\n",area);
}
/*End of Program*/
03. Program to find and display the perimeter and
area of a given rectangle(l=4,b=6)
/*03 Program to find and display the perimeter and area of a given
rectangle */
#include<stdio.h>
void main()
{
int l,b,peri,area;
l=4;
b=6;
peri=(l+b)*2;
area=l*b;
printf("Perimeter=%d\n",peri);
printf("Area=%d\n",area); } /* End of main */
/*
04. Program to find area and perimeter
of a rectangle
/* 04 Program to find area and perimeter of a rectangle*/
#include<stdio.h>
void main()
{
int p,q,area,perimeter;
printf("Enter the length of rectangle:"); scanf("%d",&p);
printf("Enter the breadth of rectangle:"); scanf("%d",&q);
area=p*q;
perimeter=2*(p+q); printf("Area =%d\n",area);
printf("perimeter =%d\n",perimeter);
}
/*End of Program*/
05. Program to calculate the area of a
triangle

/* 05 Program to calculate the area of a triangle*/


#include<stdio.h>
main()
{
int b,h,area;
printf("Enter the height of Triangle:"); scanf("%d",&b);
printf("Enter the base of Triangle:"); scanf("%d",&h);
area=(b*h)/2;
printf("Area =%d\n",area);
}
/*End of Program*/
06. Program to find average height

/*07 Program to find average height*/


#include<stdio.h>
main()
{
float a,b,c,d; float av;
printf("\nEnter the heights of the four boys\n");
scanf("%f%f%f%f",&a,&b,&c,&d);
av=(a+b+c+d)/4;
printf("\nAverage height of the boys is %f",av);
}
/*End of program*/
07. Program to calculate area of a circle

/*08 Program to calculate area of a circle*/


#include<stdio.h>
main()
{
float area,r;
printf("\nEnter the radius\n"); scanf("%f",&r);
area=3.14*r*r;
printf("\nArea of the circle is %f\n",area);
}
/*End of program*/
08. Program to convert temperature

/*09Program to convert temperature*/


#include<stdio.h>
main()
{
float f,c;
printf("Enter the temperature in fahrenheit\n"); scanf("%f",&f);
c=(f-32)/1.8;
printf("\nThe temperature in celcius is %f",c);
}
/*End of program*/
09. Program to convert from celcius to
fahrenheit
/*10 Program to convert from celcius to fahrenheit*/
#include<stdio.h>
main()
{
float f,c;
printf("\nEnter the temperature in celcius\n"); scanf("%f",&c);
f=1.8*c+32;
printf("\nThe temperature in fahrenheit is %f",f);
}
/*End of program*/
10. Program to calculate the price of any
number of mangoes from the price of a dozen
/* 11 Program to calculate the price of any number of mangoes from the
price of a dozen*/
#include<stdio.h>
main()
{
float dz,pr; int no;
printf("Enter the price of a dozen mangoes\n"); scanf("%f",&dz);
printf("Enter the number of mangoes\n"); scanf("%d",&no);
pr=(dz/12)*no;
printf("Price of %d mangoes is %f\n",no,pr);
}

Вам также может понравиться