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 one 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 Data
Primitive/Basic User-defined Data Valueless
Type
Data Type 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)
<< (Left 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 Initialization
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 */
/*