Ent189 Week2
Ent189 Week2
Ent189 Week2
Who developed C?
C language was developed by Dennis Ritchie and Ken Thomson at
At&T Bell Laboratories.
A Simple program in C
Source Program
Keyboard
Word Processor
Loader
Input
Memory
Output
CPU
The original program written in a high level language is called the
source program.
GLOBAL DECLARATIONS
This section defines the variables, functions that are visible or
accessible to the whole program.
MAIN FUNCTION
Alphabets A,B,…,Z,
a,b,…,z
Digits 0,1,2,3,4,5,6,7,8,9
Special Symbols
Special Symbols
EXAMPLE:
12 -12 2341 -12321 -98709L
76543LU
Type Bytes Minimum value Maximum value
Example:
0.0 7.231 3.1416 Double
-2.6f 3.232f 7.23123f float
3.121314123L 1.234234L long double
Largest Possible floating point number is 3.40282e+38
Largest Possible double floating point is 1.79769e+308
Largest Possible long double floating point is 1.189732e+4932
EXAMPLE:
“”
“A”
“UniMAP”
“GOOD DAY”
In ‘C’ zero represents false and any non zero value
represents true.
VARIABLES
Variable are named memory locations, which stores values that
are going to change during the execution of a program.
8 Byte size
Four byte size
88.777 cost
NOTE A variable can not be of type void.
Variable initialization:
A variable can be assigned a value while declaring them
EXAMPLE
int i = 50; 50 i
char c = ‘a’;
float x = 20.1;
65 c
20.1 x
Type declaration:
EXAMPLE
short int i,j,k;
int m,n;
long int p,q;
unsigned short int u1,u2;
unsigned int m1,m2;
unsigned long int v1,v2,v3;
char letter1, letter2;
float sum,cost,rate;
double phi,charge;
long double ratio,mass,speed;
Structure of a C Program
Local Declarations
Statements
Function end }
A SIMPLE PROGRAM
#include<stdio.h>
/*preprocessor*/
/* main function declaration */
void main (void)
{
/* beginning of main */
printf(“Welcome to UniMAP\n”);
}
/* end of main */
main function
Every C program has a primary function that must be assigned
the name ‘main’.
Text that are written between the starting symbol pair /* and the
ending symbol pair */ forms a comment and it will be ignored by the
compiler.
There should not be any blank space between / and *. The /* and */
form a couple, but they need not be on the same line.
A comment statement can start at any column and may continue to
another line also.
/* This is a comment */
/ *This is not a comment /
void /*comment*/ main(void)
printf /* comment */ (“UMS”);
printf(/*can not place comment like this */);
#include</*not a valid comment*/stdlib.h>
FORMATED INPUT/OUTPUT FUNCTIONS
KEYBOARD
scanf()
BUFFER
BUFFER
printf()
MONITOR MEMORY
printf()
The printf() function is used to convert the binary and text data
stored in the memory into user readable formatted data.
function denoted by ( )
function name
%<flag><minimum width><precision><size>conversion_code
CONVERSION CODE
Char – c
float – f
float scientific – e
integer – d
string – s
unsigned – u
octal – o
hexadecimal – x or X
None c Char %c
None d int %d
None f float %f
None f double %f
12 12 BB12
When both width and precision are specified, the width must be
large enough to contain the integral value of the number, the decimal
point and the number of digits in the decimal position.
EXAMPLE
Example 2:
printf(“%d %f %c”,123,4.56,’a’);
123 4.560000 a
Example 3:
printf(“%5d%7.2f”,123,4.56);
bb123bbb4.56
Example 4:
printf(“Transformers”);
Transformers
Example 5:
printf(“My age is %d”,21);
My age is 21
Example 6:
printf(“The cost of pen is %4.2f”,3.20);
The cost of pen is 3.20
Example 7:
printf(“Good Morning Class”);
Good Morning Class
Example 8:
printf(“Good\n\Morning\nClass”);
Good
Morning
Class
Example 9:
Printf(“Good\t\Morning\nClass”);
Good Morning
Class
Example 10:
Printf(“%06d\t%2hd”,22,22);
000022 22
scanf() function
scanf() is used to read the data supplied by the user and to transfer
them to the variable names.
The general syntax of scanf function is:
scanf(format string, address list);
scanf() requires that the variables in the address list be represented
by their addresses. To specify an address, you prefix the variable
name with the address operator, the ampersand (&).
Example:
To read the following data: 214 156 14z
scanf(“%d%d%d%c”,&a,&b,&c,&d);
The ‘scanf’ function allows the user to enter the data through the
standard input (keyboard), formats the data entered and assign
them to variables.
Buffer
Address Data
Keyboard 1 2 3 . 4 H
1500 12 a
1501
b
1502
scanf(“%d %f %c”, &a, &b &c )
1503 3.4
1504
1505 H c
1506
The general form a ‘scanf’ function is
The format string contains format specifiers that must match in order
with the arguments type. The number of format specifers must be
equal to the number of addresses in the address list. The ‘scanf’
functions returns the number of fields assigned values. If an error
occurs before any assignments are made, EOF (end of file) is
returned.
A field specifier must begin with a percentage sign. A field speciifer
must have a conversion code. With a few exceptions, the conversion
code used in a ‘printf’ function is used in the ‘scanf’ function also.
The general form of a format specifier is shown below
The conversion codes are used to interpret the data value keyed in by
the users and store them in the variable names. For example, a ‘%c’
field specifier specifies that the input data has to be interpreted as a
character.
Data type Conversion code
Char c
short int
int d
long int
unsigned int
unsigned short int u
unsigned long int
short-octal
int-octal o
long-octal
short-hexadecimal
int-hexadecimal x, X
long-hexadecimal
float
double f
long double
float-scientific
double-scientific e, E, g, G
long double-scientific
String s
Pointer p
When a user enters data, the data values are buffered in the input
stream.
The ‘scanf’ function reads the sequence of characters stored in the
buffer and interprets them using the format specifier.
Except for a character field specifier ‘%c’, the scanf function skips
all the leading white spaces, tabs or new-lines from the input
stream.
If the conversion code is of character type, then the scanf function
reads one character. This may be any character including white
space. While reading a character data and to skip the leading white
space a blank space is always included before the character field
specification (example “ %c”).
We have already seen that each and every memory location has an
address and using a type declaration statement we have also attached a
name to it. The name is referred as a variable name. Address operator
‘&’ is used to get the address associated to a variable name. For
example if ‘count’ is a variable name and prefixing a ‘&’ symbol to
the variable name, &count is the address.
The following section contains several examples on ‘scanf’
functions.
1. To read integer values
int a,b,c;
scanf(“%d%d%d”,&a,&b&c);
then ‘a’ will be assigned with the value 1234, the variable ‘b’ will
be assigned with 56 and c will be assigned with the value 7.
The same result will occur if the user enters the three data on three
different lines as
1234
56
7
2. A leading white space is required to read a character. Consider
the following example.
int a;
char c;
scanf(“%d%c”,&a,&c);
Then the variable ‘a’ will be assigned with 1234 and a white space
character is assigned to c.
The variable ‘a’ is assigned with 1234 and the variable c will be
assigned with ‘x’.
The ‘scanf’ function shown above is modified as
scanf(“%d %c”,&a,&c);
c. If the user enters the data as
1234bx
The variable ‘a’ is assigned with 1234 and the variable c will be
assigned with ‘x’.
3. To read an integer, float and a character type data
int a;
float f;
char c;
scanf(“%d %f %c”,&a,&f,&c);
printf(“%d %f %c:,a,f,c);
12 34.435 A
12b34.435000bA
4. To read data using a format specifier that has a maximum width
specifier
int a;
float f1,f2;
scanf(“%3d%5f%5f”,&a,&f1,&f2);
printf(“%d %f %f\n”,a,f1,f2);
12312.3412.345
12b12.340000b12.345000
5. If the number of field specifiers is more than the number of
addresses in the address list, the ‘scanf’ forces the user to input as
many data values as the number of field specifiers. After reading
the data, the system will indicate the null pointer assignment.
int a,b;
scanf(“%d%d%d”,&a,&b);
The ‘scanf’ will force the user to input three values and as it is not
possible to assign all the three values, it will give an error
message as a “Null pointer assignment”.
Write a program that prompts the user to enter four integer
numbers and print them horizontally and vertically.
#include<stdio.h>
int main(void)
{
int a, b, c, d;
printf(“Please enter four numbers “);
scanf(“%d %d %d %d”,&a,&b,&c,&d);
printf(“\nYou have entered \n”);
printf(“%d %d %d %d\n”,a,b,c,d);
printf(“\nThe numbers are printed vertically\n”);
printf(“%d\n%d\n%d\n%d\n”,a,b,c,d);
return 0;
}
A sample run of the above program
is shown below.
int main(void)
{
float length, breadth, height;
printf(“Enter the length of the room “);
scanf(“%f”, &length);
printf(“Enter the breadth of the room “);
scanf(“%f”,& breadth);
printf(“Enter the height of the room “);
scanf(“%f”, &height);
printf(“\nRoom Dimensions \n”);
printf(“Length = %.2f\n”, length);
printf(“Breadth = %.2f\n”, breadth);
printf(“Height = %.2f\n”, height);
return 0;
}
A sample run of the above program is shown below.
Room Dimensions
Length = 10.23
Breadth = 6.00
Height = 5.12
Thank You