0% found this document useful (0 votes)
25 views29 pages

Basic Structure of C Language

eratuma

Uploaded by

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

Basic Structure of C Language

eratuma

Uploaded by

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

Computer Programming

Fundamental
DCS 1.1
By Mr. Godson Samwel
BASIC STRUCTURE OF C LANGUAGE:
• The program written in C language follows this basic structure. The sequence of
sections should be as they are in the basic structure.
• A C program should have one or more sections but the sequence of sections is to
be followed.
1. Documentation section
2. Linking section
3. Definition section
4. Global declaration section
BASIC STRUCTURE OF C LANGUAGE:
5. Main function section
{
Declaration section
Executable section
}
6. Sub program or function section
1.Documentation Section
• comes first and is used to document the use of logic or reasons in your
program. It can be used to write the program's objective, developer and
logic details.
• The documentation is done in C language with /* and */ . Whatever is
written between these two are called comments.
2.LINKING SECTION :
• This section tells the compiler to link the certain occurrences of keywords
or functions in your program to the header files specified in this section.
• e.g. #include<stdio.h>
3.DEFINITION SECTION :
• It is used to declare some constants and assign them some value.
• Example #define Max 37
• Here #define is a compiler directive which tells the compiler whenever
MAX is found in the program replace it with 37.
4. GLOBAL DECLARATION SECTION
• Here the variables which are used through out the program (including
main and other functions) are declared so as to make them global(i.e
accessible to all parts of program.
• e.g. double i; (before main())
5.MAIN FUNCTION SECTION :
• It tells the compiler where to start the execution from
main()
{
point from execution starts
}
main function has two sections
declaration section : In this the variables and their data types are declared
Executable section : This has the part of program which actually performs
the task we need.
6.SUB PROGRAM OR FUNCTION
SECTION
• This has all the sub programs or the functions which our program needs.
• Demonstration
#include<stdio.h>
int main()
{
printf(“Goodbye Harry, cruel world!\n”);
return(0);
}
The Format of C

i. Statements are terminated with semicolons


ii. Indentation is ignored by the compiler.
iii. C is case sensitive - all keywords and Standard.
iv. Library functions are lowercase
v. Strings are placed in double quotes
vi. Newlines are handled via \n
vii. Programs are capable of flagging success or error, those forgetting to do so have
one or other chosen randomly!
Elements of C Language
C Tokens
Tokens are the smallest individual unit known as tokens. C recognizes six
types of tokens. C programs are written using these tokens and the syntax of
the language. Following are the C Tokens:
C Tokens
Keywords.
Identifiers.
Literals(constants).
 Operators.
Separators.
1. Keywords
• There are 32 keywords in C.
• Keywords are the reserved words whose meaning has already been
explained to the C compiler.
2. Identifiers
• Identifiers are the names given to variables.
• Using this identifier we can access that variable.
• All other names in a C program such as array name or a function name
are also known as Identifiers.
Variables
• Variable is the place inside the main memory that is basically used to store
some particular type of data that vary during the program execution.
• Variable names are names given to locations in memory.
• These locations can contain integer, real or character constants
• All the variables that are used in the program should be declared i.e. typed
at the top with their respective data types.
• Example int z, y ;or char x;
Variables
• . The variable declaration tells two things:
i. It tells the compiler the name of the variable.
ii. It also tells the type of value the variable will hold.
Examples
int a;
float b;
char c;
Rules of Naming Variables
• Variable name may be consist of letter, digits and under line(-) with
following the below rules
• They must begin with a letter.
• Upper case & lower case are significant mean total is differ. 3)
• Variable name should not be a keyword.
Variables Format
• The basic format for declaring variable is
[Data type name] var1, var2, -------;
int px1,px2;
3. Literals/Constants:
• Constants in C refer to fixed values that do not change during the
execution of a program. In C there are 3 types of constants:
i. Numeric constants
ii. Floating or Real constants
iii. Character constants
Numeric Constants
• A) Integer constants – They are a sequence of digits. There are three types
of integers: decimal, octal, hexadecimal.
• Decimal numbers contain set of digits 0 to 9 which can be positive (+) or
negative (-). E.g.: 6, 46, 398, 658736, -89, +598 are all integers Spaces,
comma or special symbols are not allowed. E.g.: 12 897, 364,897, $56,
78@56 are all invalid or wrong numbers.
Numeric Constants
• Octal numbers consist of any combination of digits from 0 to 7, with a
leading 0. E.g.:037, 0435, 0551.
• Hexadecimal numbers has 16 digits 0 to 9 and alphabets A to F (10-15),
the sequence of digits is preceded by 0x or 0X. E.g.:0X2, 0x9f, 0xbcd.
Floating or Real constants -
• Integer constants are not sufficient to represent all the quantities such as
price, distance, temperature, etc. These numbers have a decimal point(.)
i.e. fractional parts like 65.987, 0.000569, 89.36.
• A floating number can also be represented as exponential or scientific
notation for e.g. 0.0000123 can be written as 1.23*10 -5 or 1.23e-05.Thus
the general form is: Mantissa e exponent.
• So, 1.23 is called mantissa and -05 is called as exponent
Character constant
• Single character constants: A single character alphabets in
uppercase(‘A’ to ‘Z’) or lowercase (‘a’ to ‘z’) or digits(0 to 9) or a special
symbol(@, #, $, %, *, &, +, -, ., :, / etc.) which are enclosed in single
quotes (‘ ’). E.g.: ‘a’ or ’A’ or ‘7’or’+’ are all character constants.
• Note: All escape sequences are also considered characters. ii)
• String constants: A string constant is a single character or a group of
characters enclosed in double quotes (“ “). Like: “hello”, “4598”,
“hello235”, “5+3
Character constant
• ”. Note - The character constant ‘7’ is not the same as digit 7.
• Every character constants has an equivalent integer number. ‘A’ to ‘Z’ has
ASCII 65 to 90. i.e. ‘A’ has ASCII 65 ‘B’ has ASCII 66 and so on. ‘a’ to
‘z’ has ASCII 97 to 122 ‘0’ to ‘9’ has ASCII 48 to 57 ‘ ‘ (space) has ASCII
32. ☺ (smiling space) has ASCII 1.
Data Types
• C language is rich in data types. There are 4 classes of data types:
i. Primary data types (int, float, char)
ii. User defined data types (enumerator, typedef)
iii. Derived data types (array, function, pointers, structure, union)
iv. Empty data sets. (void)
Primary Data Types -
Formula to calculate range -
• 2exponet n-1 Where n is the number of bits occupied by the data type.
• Range of int: int occupies 2 byte i.e. 16 bits ( since 1 byte is equal to 8
bits) So, 216 -1 = 215= 32768. Thus range becomes: -32768 to +32767
• Note- This formula cannot be used to calculate the range of real data types
(float, double, long double)
Format specifiers:
• There are quite a number of format specifiers for printf and scanf. Here
are the basic ones

You might also like