2 C Program Structure

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

C Program Structure

Prepared By:
G. J. Kamani
C Program Structure

Documentation Section set of comment lines giving program information.

Link Section instruction to the compiler to link functions from


the system library
Definition Section
defines all symbolic constants.

There are some variables that are used in more


Global Declaration Section
than one function. Such variables are called
main( ) Function Section global variables and are declared in the global
{ declaration section
Every C program must have one main( ) function
}
section
Subprogram Section

contains all the user defined functions that are


Function 1 (User Define called in the main function
Function 2 Functions)
Simple Program

/* Simple Program to Display Welcome To Documentation Section


CAET-Godhra on the screen. */

Link Section
#include <stdio.h>

void main()
{ main( ) Function Section
printf(“Welcome To CAET-Godhra”);
}
C Character Set

C language character set denotes any valid character in C programming


language. This character can be alphabet, digit, special symbols and white
spaces. Following table shows the valid alphabets, numbers, special symbols
and white spaces allowed in C language.

Alphabets : A, a, B, b, C, c etc….
Numbers : 1,2,3etc…
Special Symbols : , (Comma), ;(Semi colon), # (Hash), =(equal) etc…
White Space : Blank Space, Horizontal Tab, New Line etc…
C Constant

Constants in C refer to fixed values that do not change during the execution of a
program. C language constants are also known as Literals. C supports several
types of constants as under.

NUMERIC CONSTANT:
Integer Constant=> 11, 22, -400, +323, 0, 99 etc…
Real Constant => 12.32, -23.465, 0.50 etc…

CHARACTER CONSTANT:
Single Character Constant => ‘a’, ‘A’, ‘5’, ‘$’ etc…
String Constant => “Hello”, “5+3” etc…
C Keywords

In C language, keywords are special words with fixed meaning. These


meanings cannot be changed. 32 keywords available in C. those are given
below:

auto break case char


const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while
C Data Types

PRIMARY DATA TYPES

Integral Data Types Character

Signed Unsigned

int unsigned int char

short int unsigned short int signed char

long unsigned long unsigned char

Floating point Type


void
float double long double
Data type value range

Type Length Range

char 8 bits -128 to 127


signed char 8 bits -128 to 127
unsigned char 8 bits 0 to 255

int 16 bits -32768 to 32767


signed int 16 bits -32768 to 32767
unsigned int 16 bits 0 to 65535
signed short int 8 bits -128 to 127
unsigned short int 8 bits 0 to 255
long 32 bits -2147483648 to 2147483647
signed long 32 bits -2147483648 to 2147483647
unsigned long 32 bits 0 to 4294967295

float 32 bits 3.4 * 10–38 to 3.4 * 10+38


double 64 bits 1.7 * 10–308 to 1.7 * 10+308
long double 80 bits 3.4 * 10–4932 to 3.4 * 10+4932
C Variables

A variable is a data name that may be used to store a data value. A variable may take
different values at different times during execution.
A variable name can be chosen by the programmer in a meaningful way.

A variable name must follow following rules.


1. The first character in the variable name must be Letter.
2. Variable name length is maximum 31 characters.
3. Keywords are not allowed as variable name.
4. No commas or blanks are allowed within a variable name.
5. No special symbol other than underscore( as in gross_sal ) can be used in a
variable name.
Valid Variable Name: name, marks, tot_marks, sub1, sub_10
Invalid Variable Name: 1sub, 123, (name), tot*marks
printf() Function

The printf function is just a useful function from the standard library(stdio.h) of
functions that are accessible by C programs.

printf() Format Specifiers printf() escape sequences


There are many format specifiers defined in C.
%i or %d int
\n (newline)
%c char
\t (tab)
%f float
\v (vertical tab)
%lf double
\f (new page)
%s string
%x Hexadecimal \b (backspace)
%o Octal \r (carriage return)
%u Unsigned value
Note: %lf stands for long float.
printf() function Program 1

#include <stdio.h>
void main()
{
int a;
float b;
a=100;
b=10.5628;
Output
printf("%d \n",a); 1 0 0
printf("%5d \n",a); 1 0 0
printf("%-5d \n",a); 1 0 0
1 0 . 5 6 2 8
printf("%f \n",b);
printf("%.2f \n",b); 1 0 . 5 6
printf("%10.3f \n",b); 1 0 . 5 6 3
printf("%-10.3f \n",b); 1 0 . 5 6 3
printf(“%c \n”,’A’); A
printf(“%c \n”,65);
A
}
printf() function Program 2

#include <stdio.h>
void main()
Output
{
F F
printf("Hexadecimal: %x\n", 255);
3 7 7
printf("Octal: %o\n", 255);
1 5 0
printf("Unsigned value: %u\n", 150);
6 5 5 2 6
printf("Unsigned value: %u\n", -10);
H E L L 0
printf("%s\n", "HELLO");
H E L L O
printf(“%10s\n", " HELLO");
H E L L O
printf(“%-10s\n", " HELLO");
H E L
printf(“%.3s\n", " HELLO");
H E L
printf(“%10.3s\n", " HELLO");
}
Thank You

You might also like