You are on page 1of 28

Introduction to C Programming

Lexical Elements ,Data types and


the C system
Topic & Structure of the lesson

In this chapter you will learn about:

•C program structure
•Standard library and function
•Types of errors

CT018-3-1 Introduction To C Programming Program Structure and Construction


Learning Outcomes

If you have mastered this topic, you should be able


to use the following terms correctly in your
assignments:
• Preprocessor directive
• Stdio.h
• Main function
• Comments
• Constants
• Expression
• Syntax, logical and runtime errors

CT018-3-1 Introduction To C Programming Program Structure and Construction


Constants

• Constants are entities that appear in the


program code as fixed values.
• For Example:-
const double taxrate=0.0175;
• Character Constant:
 Is a character enclosed in single quotation
marks.
 Some examples of character constants are ‘1’,’a’
and ‘A’.

CT018-3-1 Introduction To C Programming Program Structure and Construction


Constants

• Example 1:-
The output statement,
printf(“%c”,’T’);
will print the letter T on the screen.
• Example 2:-
char S=‘a’;
printf(“%c”,S);
will print the letter S on the screen.

CT018-3-1 Introduction To C Programming Program Structure and Construction


String Constants

• String Constant:
 A string literal or string constant is a
sequence of any number of characters
surrounded by double quotation marks.
• Following are examples of String Constants.
“This is a String Constant”
“ 125”
“Is this John’s car?”

CT018-3-1 Introduction To C Programming Program Structure and Construction


Statements

• A statement is a Specification of an action


to be taken by the computer as the program
executes.
• Each C statement must end with the
punctuator/ separator semicolon (;).
• For Example:-
c= a+b;
• But, some of the statements should not end
with semicolon.
• For Example, while ( I <=10)
CT018-3-1 Introduction To C Programming Program Structure and Construction
Expressions

• An Expression is a combination of both


Operators and Operands.
• For Example:-
 city_tax = taxrate * gross_income;
 c= a + b;
 area = 2 * pi * r;

CT018-3-1 Introduction To C Programming Program Structure and Construction


Standard Output function printf()

• The Syntax for the printf() function is,


printf(Format Control String);
printf(Format Control String, print list);
• For Example :-
 printf(“Enter your age:”);
 printf(“%d”, age);
 printf(“Your age of birth is %d”, yob );

CT018-3-1 Introduction To C Programming Program Structure and Construction


Standard Input function scanf()

• The Syntax for the scanf() function is,


scanf (Format Control String, print list);
• Example 1:-
 printf(“Enter your age:”);
scanf(“%d”,&age);
printf(“Type your weight in inches:”);
scanf(“%d”,&weight);

CT018-3-1 Introduction To C Programming Program Structure and Construction


Standard Input function scanf()

• Example 2:-
Using Characters:
char a;
scanf(“%c”,&a);
printf(“The character is …..%c”,a);
 Input of String variables:
char name[10];
scanf(“%s”, name); Gets the characters
until the occurrence of
the white space/blank

CT018-3-1 Introduction To C Programming Program Structure and Construction


Structure of a C Program

• C program consists of the following


components:
 Program Comments
 Preprocessor directives
 Type declarations
 Named Constants
 Statements
 Function definitions
 Function Calls

CT018-3-1 Introduction To C Programming Program Structure and Construction


Example for a C Program

/* Program to find area of a circumference*/


#include<stdio.h>
Program
main() Preprocessor comments
{ Directives
int radius;
Type
double circ; Declarations
const double pi=3.14; Named
Constants
printf(“\nEnter the radius value:”);
scanf(“%d”,&radius);
circ=2*pi*radius; Statements

CT018-3-1 Introduction To C Programming Program Structure and Construction


Example for a C Program

printf(“\n Circumference of a Circle


= %lf”,circ);
}

OUTPUT:
Enter the radius value: 2

Circumference of a Circle = 12.56

CT018-3-1 Introduction To C Programming Program Structure and Construction


Program Comments

• Comments are explanations or annotations


that are included in a program for
documentation and clarification purposes.
• They are completely ignored by the
compiler during compilation, and they
have no effect on program execution.
• There are two types of comments:
 Multiple line comment is represented
by /*………*/

CT018-3-1 Introduction To C Programming Program Structure and Construction


Program Comments

 Single line comment is represented by //


• For Example :-
1. /* This Program is used to calculate
the Factorial of a number */
2. // Program to add two numbers
3. // Program to find area of a Circle
Comments are meant to aid,
never to hamper, readability.

CT018-3-1 Introduction To C Programming Program Structure and Construction


Preprocessor Directives

• preprocessor modifies the text of a C


program before compilation.
• A preprocessor directive starts with #
• #include<stdio.h> instructs the preprocessor
to include the contents of the standard
input.output(stdio.h), in the program, in
order to use certain functions “printf” and
”scanf”.
• Constant double pi=3.14 associates the
constant macro “pi” with the meaning “3.14”.

CT018-3-1 Introduction To C Programming Program Structure and Construction


main() Function

• Every C program has a ‘main’ function, where


program execution begins.
• A function definition consists of header and a
body.
• header: consists of the name of the function
and the parameter list enclosed within the
parenthesis().
• body: consists of declarations and executable
statements both enclosed in braces {},known
as block.
CT018-3-1 Introduction To C Programming Program Structure and Construction
main() Function

main ()
{
float inches,cm;
/* variable declaration */
printf(”Enter a length in inches:
”);
/* ... rest of statements follow ...
*/
}

CT018-3-1 Introduction To C Programming Program Structure and Construction


Types of Errors

Errors are classified into three types:

 Syntax Errors
 Logical Errors
 Runtime Errors

CT018-3-1 Introduction To C Programming Program Structure and Construction


Syntax Errors

Syntax errors are those errors


that will be pointed out by the
compiler, usually easy to fix.
Examples are mismatch of
parenthesis, missing semicolons,
commas and miss-spelt words.

CT018-3-1 Introduction To C Programming Program Structure and Construction


Logical Errors

Logic errors go unnoticed by the


compiler, but they cause fatal
errors in the outcome of a
program. Example is not
producing the required or
expected output.

CT018-3-1 Introduction To C Programming Program Structure and Construction


Runtime Errors

Runtime errors attempt to


perform some illegal operations
when it executes. Example is
dividing by zero.

CT018-3-1 Introduction To C Programming Program Structure and Construction


A Sample Program

Problem: Conversion from inches to centimeters

Recall the first step in problem solving:


Understand the problem
What is the Input?
Length in inches.Let’s call it inches.
What is the Output?
Length in centimeters.Let’s call it cm.

CT018-3-1 Introduction To C Programming Program Structure and Construction


A Sample Program

Step 2: Devise a plan

What is the relationship between the input and


the output?
one inch = 2.54 centimeters
So the algorithm is:
get the length in inches
compute length in centimeters
report length in centimeters
CT018-3-1 Introduction To C Programming Program Structure and Construction
A Sample Program

Step 3:Carry out the plan


Do I know how to compute the length in
centimeters?
Yes.”compute length in centimeters”
length in centimeters is 2.54 *length in inches

Is the plan(algorithm) correct?


Work out on some examples.
Now move from algorithm to writing program.

CT018-3-1 Introduction To C Programming Program Structure and Construction


A Sample Program
/* Program to convert from inches into centimeters*/
#include<stdio.h>
#define CM_PER_INCH 2.54
main()
{
float inches, cm;
printf(“Enter the length in inches:”);
scanf(“%f”,&inches);
cm= CM_PER_INCH * inches;
printf(“That equals %.2f centimeters \n”,cm);
}

CT018-3-1 Introduction To C Programming Program Structure and Construction


Output

Enter the length in inches: 2

That equals 5.08 centimeters

CT018-3-1 Introduction To C Programming Program Structure and Construction

You might also like