0% found this document useful (0 votes)
5 views11 pages

CSC 2312 Structured Programming 3

The document outlines the steps involved in compiling and executing a C program, including preprocessing, compilation, assembly, linking, and loading. It also covers fundamental elements of the C programming language, such as keywords, identifiers, data types, constants, and variable declaration. Additionally, it explains input-output operations in C, including the use of functions like printf() and scanf() for displaying and receiving data.

Uploaded by

muhammedgurara87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

CSC 2312 Structured Programming 3

The document outlines the steps involved in compiling and executing a C program, including preprocessing, compilation, assembly, linking, and loading. It also covers fundamental elements of the C programming language, such as keywords, identifiers, data types, constants, and variable declaration. Additionally, it explains input-output operations in C, including the use of functions like printf() and scanf() for displaying and receiving data.

Uploaded by

muhammedgurara87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

YOBE STATE UNIVERSITY, DAMATURU

FACULTY OF SCIENCE
DEPARTEMNT OF COMPUTER SCIENCE

CHAPTER SEVEN
Executing A C Program:
CSC2312
Structured Programming

7.1 Compilation and Execution of a C Program


The compilation and execution process of C can be divided into several steps:
➢ Preprocessing - Using a Preprocessor program to convert C source code in expanded
source code. "#includes" and "#defines" statements will be processed and replaced
source codes in this step.
➢ Compilation - Using a Compiler program to convert C expanded source to assembly
source code.
➢ Assembly - Using an Assembler program to convert assembly source code to object
code.
➢ Linking - Using a Linker program to convert object code to executable code. Multiple
units of object codes are linked to together in this step.
➢ Loading - Using a Loader program to load the executable code into CPU for execution.

Sample I/O steps


Here is a simple table showing input and output of each step in the
compilation and execution process:

Input Program Output


source code > Preprocessor > expanded source code
expanded source code > Compiler > assembly source code
assembly code > Assembler > object code
object code > Linker > executable code
executable code > Loader > execution

7.2 Pictorial Diagram of C Compilation and Execution


A pictorial diagram showing the compilation and execution of a C program is shown
following.
2
PART THREE
CHAPTER EIGHT
Introduction To C Programming Language

8.1 Elements of C
Every language has some basic elements and grammatical rules. Before starting with
programming, we should be acquainted with the basic elements that build the language.

Character Set
Communicating with a computer involves speaking the language the computer understands.
In C, various characters have been given to communicate.
Character set in C consists of:

8.2 Keywords
Keywords are the words whose meaning has already been explained to the C compiler. The
keywords cannot be used as variable names because if we do so we are trying to assign a
new meaning to the keyword, which is not allowed by the computer. There are 32 keywords
available in C as shown below.

Identifier:
In the programming language C, an identifier is a combination of alphanumeric characters,
the first being a letter of the alphabet or an underline, and the remaining being any letter of
the alphabet, any numeric digit, or the underline.
Two rules must be kept in mind when naming identifiers.

3
➢ The case of alphabetic characters is significant. Using "INDEX" for a variable is not
the same as using "index" and neither of them is the same as using "InDeX" for a
variable. All three refer to different variables.
➢ As C is defined, up to 32 significant characters can be used and will be considered
significant by most compilers. If more than 32 are used, they will be ignored by the
compiler.

CHAPTER NINE
DATA TYPES
9.1 Introduction
In C programming language, data types refer to a domain of allowed values and the
operations that can be performed on those values. The type of a variable determines how
much space it occupies in storage and how the bit pattern stored is interpreted. There are 4
fundamental data types in C, which are- char, int, float and, double. Char is used to store any
single character; int is used to store any integer value, float is used to store any single
precision floating point number and double is used to store any double precision floating
point number.

We can use 2 qualifiers with these basic types to get more types.

There are 2 types of qualifiers


▪ Sign qualifier- signed & unsigned
▪ Size qualifier- short & long
The data types in C can be classified as follows:

4
9.2 Constants
A constant is an entity that doesn’t change whereas a variable is an entity that may change.
C constants can be divided into two major categories:
✓ Primary Constants
✓ Secondary Constants

Here our only focus is on primary constant. For constructing these different types of
constants certain rules have been laid down.

9.3 Rules for Constructing Integer Constants


➢ An integer constant must have at least one digit.
➢ It must not have a decimal point.
➢ It can be either positive or negative.
➢ If no sign precedes an integer constant it is assumed to be positive.
➢ No commas or blanks are allowed within an integer constant.
➢ The allowable range for integer constants is -32768to 32767.
➢ Eg.: 426, +782,-8000, -7605

5
9.4 Rules for Constructing Real Constants
✓ Real constants are often called Floating Point constants.
✓ The real constants could be written in two forms—Fractional form and Exponential
form.
✓ Rules for constructing real constants expressed in fractional form:
o A real constant must have at least one digit.
o It must have a decimal point.
o It could be either positive or negative.
o Default sign is positive.
o No commas or blanks are allowed within a real constant.
o Ex. +325.34, 426.0, -32.76, -48.5792
✓ Rules for constructing real constants expressed in exponential form
o The mantissa part and the exponential part should be separated by a letter e. b) The
mantissa part may have a positive or negative sign.
o Default sign of mantissa part is positive.
o The exponent must have at least one digit, which must be a positive or negative integer
o Default sign is positive.
o Range of real constants expressed in exponential form is -3.4e38 to 3.4e38.
o Ex. +3.2e-5, 4.1e8, -0.2e+3, -3.2e-5

9.6 Rules for Constructing Character Constants


o A character constant is a single alphabet, a single digit or a single special symbol
enclosed within single inverted commas.
o The maximum length of a character constant can be 1 character.
o Ex.: ‘M’, ‘6’, ‘+’

CHAPTER TEN
VARIABLES AND VARIABLE DECLARATION

10.1 Variables and Variable Declaration


Variables are names that are used to store values. It can take different values but one at a
time. A data type is associated with each variable & it decides what values the variable can
take. When you decide your program needs another variable, you simply declare (or define)
a new variable and C makes sure you get it. You declare all C variables at the top of whatever
blocks of code need them. Variable declaration requires that you inform C of the variable's
name and data type.

Syntax: datatype variablename;


Eg:
int page_no;

6
char grade;
float salary;
long y;
There are two places where you can declare a variable:
a. After the opening brace of a block of code (usually at the top of a function)
b. Before a function name (such as before main() in the program)

Consider various examples:


Suppose you had to keep track of a person's first, middle, and last initials. Because an initial
is obviously a character, it would be prudent to declare three-character variables to hold the
three initials. In C, you could do that with the following statement:

1. main()
{
char first, middle, last;
// Rest of program follows
}
2. main()
{
char first;
char middle;
char last;
// Rest of program follows
}

10.2 Initialization of Variables


When a variable is declared, it contains undefined value commonly known as garbage value.
If we want, we can assign some initial value to the variables during the declaration itself.
This is called initialization of the variable.

Eg.,
int pageno=10;
char grade=’A’;
float salary= 20000.50;

3.3 Expressions
An expression consists of a combination of operators, operands, variables & function calls.
An expression can be arithmetic, logical or relational. Here are some expressions:

arithmetic operation ➔ a+b


relational operation ➔ a>b
logical operation ➔ a == b
function call ➔ func (a,b)
4+21
7
a*(b + c/d)/20
q = 5*2
x = ++q % 3
q>3

As you can see, the operands can be constants, variables, or combinations of the two. Some
expressions are combinations of smaller expressions, called subexpressions. For example,
c/d is a subexpression of the sixth example.
An important property of C is that every C expression has a value. To find the value, you
perform the operations in the order dictated by operator precedence.

10.4 Statements
Statements are the primary building blocks of a program. A program is a series of statements
with some necessary punctuation. A statement is a complete instruction to the computer. In
C, statements are indicated by a semicolon at the end. Therefore
legs = 4
is just an expression (which could be part of a larger expression), but,
legs = 4;
is a statement. What makes a complete instruction? First, C considers any expression to be
a statement if you append a semicolon. (These are called expression statements.) Therefore,
C won't object to lines such as the following:
8;
3 + 4;
However, these statements do nothing for your program and can't really be considered
sensible statements. More typically, statements change values and call functions:
x = 25;
++x;
y = sqrt(x);
Although a statement (or, at least, a sensible statement) is a complete instruction, not all
complete instructions are statements. Consider the following statement:
x = 6 + (y = 5);
In it, the subexpression y = 5 is a complete instruction, but it is only part of the statement.
Because a complete instruction is not necessarily a statement, a semicolon is needed to
identify instructions that truly are statements.

10.5 Compound Statements (Blocks)


A compound statement is two or more statements grouped together by enclosing them in
braces; it is also called a block. The following while statement contains an example:
while (years < 100)
{
wisdom = wisdom * 1.05;
printf("%d %d\n", years, wisdom);
years = years + 1;
}
8
If any variable is declared inside the block, then it can be declared only at the beginning of
the block. The variables that are declared inside a block can be used only within the block.

10.6 Input-Output in C
When we are saying Input that means we feed some data into program. This can be given in
the form of file or from command line. C programming language provides a set of built-in
functions to read given input and feed it to the program as per requirement.
When we are saying Output that means to display some data on screen, printer or in any file.
C programming language provides a set of built-in functions to output the data on the
computer screen. Functions printf() and scanf() are the most commonly used to display out
and take input respectively.

Let us consider an example:


#include <stdio.h> //This is needed to run printf() function.
int main()
{
printf("C Programming"); //displays the content inside
quotation
return 0;
}
Output:
C Programming

Explanation:
o Every program starts from main() function.
o printf() is a library function to display output which only works if #include<stdio.h> is
included at the beginning.
o Here, stdio.h is a header file (standard input output header file) and #include is command
to paste the code from the header file when necessary. When compiler encounters
printf()function and doesn't find stdio.h header file, compiler shows error.
o return 0; indicates the successful execution of the program.

10.7 Input-Output of integers in C


#include<stdio.h>
int main()
{
int c=5;
printf("Number=%d",c);
return 0;
}
Output:
Number=5
Inside quotation of printf() there, is a conversion format string "%d" (for integer). If this
conversion format string matches with remaining argument, i.e, c in this case, value of c is
displayed.
9
#include<stdio.h>
int main()
{
int c;
printf("Enter a number\n");
scanf("%d",&c);
printf("Number=%d",c);
return 0;
}
Output:
Enter a number
4
Number=4

The scanf() function is used to take input from user. In this program, the user is asked an
input and value is stored in variable c. Note the '&' sign before c. &c denotes the address of
c and value is stored in that address.

10.8 Input-Output of floats in C

#include <stdio.h>
int main()
{
float a;
printf("Enter value: ");
scanf("%f",&a);
printf("Value=%f",a); //%f is used for floats instead of %d
return 0;
}
Output
Enter value:
23.45
Value=23.450000
Conversion format string "%f" is used for floats to take input and to display floating value
of a variable
.
10.9 Input-Output of characters and ASCII code

#include <stdio.h>
int main()
{
char var1;
printf("Enter character: ");
scanf("%c",&var1);
printf("You entered %c.",var1);
10
return 0;
}
Output
Enter character:
g
You entered g.
Conversion format string "%c" is used in case of characters.

10.10 ASCII code


When character is typed in the above program, the character itself is not recorded a numeric
value (ASCII value) is stored. And when we displayed that value by using "%c", that
character is displayed.

#include <stdio.h>
int main()
{
char var1;
printf("Enter character: ");
scanf("%c",&var1);
printf("You entered %c.\n",var1);
/* \n prints the next line(performs work of enter). */
printf("ASCII value of %d",var1);
return 0;
}
Output:
Enter character:
g
103
When, 'g' is entered, ASCII value 103 is stored instead of g. You can display character if
you know ASCII code only. This is shown by following example.

#include <stdio.h>
int main()
{
int var1=69;
printf("Character of ASCII value 69: %c",var1);
return 0;
}
Output
Character of ASCII value 69:
E
The ASCII value of 'A' is 65, 'B' is 66 and so on to 'Z' is 90. Similarly,
ASCII value of 'a' is 97, 'b' is 98 and so on to 'z' is 122.

11

You might also like