0% found this document useful (0 votes)
3 views43 pages

C programming

The document provides an overview of the structure and components of C programming, including sections such as documentation, definitions, links, global declarations, and functions. It also covers the C character set, keywords, data types, input/output functions like scanf and printf, and the phases of C program development. Additionally, it explains constants and symbolic constants, emphasizing their importance in programming for clarity and ease of maintenance.

Uploaded by

manjuparvin8
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)
3 views43 pages

C programming

The document provides an overview of the structure and components of C programming, including sections such as documentation, definitions, links, global declarations, and functions. It also covers the C character set, keywords, data types, input/output functions like scanf and printf, and the phases of C program development. Additionally, it explains constants and symbolic constants, emphasizing their importance in programming for clarity and ease of maintenance.

Uploaded by

manjuparvin8
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/ 43

C Programming

Structures of ‘C’ Programming


• ‘C’ Program Structure
Structure of a Program
Documentation Section / Comment section
Definition section
Link Section / Include Section
Global Declaration
main( ) function section
{
Local declaration part
Executable part
}
1. The documentation section consists of only comments. This section
is also called the comment section, it is not compiled by the
compiler, it is a user section.
The Single line comment can be written by // (double slash) & Multi
line comment can be written by /*------- ------------*/
Comments are helpful in identifying the program’s principal
features or in explaining the underlying logic of various program
features.
2. Definition Section: All the symbolic constants are written in
definition section. Macros are known as symbolic constants.
3. The Link section/ Pre-processor Directive(s) : All ‘C’ compilers use as
their first phase of compilation a pre-processor, which performs
various manipulations on the source file before it is actually compiled.
Pre-processor directives are not actually a part of the ‘C’ language,
but rather instructions from us to the compiler. The #include directive
tells the pre-processor to read in another file and include it with our
program. E.g. To use the input & output functions we use #include,
this is a link file, please note that the character (#) should be in the
first column and there is no semicolon at the end of this line.
4. Global Declaration section : There are two places where variables
are declared.
1) inside a function or
2) outside all functions.
Variables declared outside all functions are called global variables
and they may be accessed by any other function in the program.
Global variables exist the entire time the program is executing. The
variable used in more than one function can be declared in Global
section.
5. Function(s) : A function is a set of instructions aimed at achieving a
pre-defined goal or objective. Every C program consists of one or
more function, one of which must be called the main. The program
will always begin by executing the main function, which may access
other functions. Any other function definitions must be defined
separately, either ahead of or after main.
• The Function’s body comprises of two parts.
1. Variable declarations
2. Executable statements.
• All variables to be used in the function must be combined and put
together before the first of executable line.
• There is at least one executable statement in a ‘C’ function.
• The two parts of a function must appear between the opening
‘{‘and closing braces’}’.
F.Y.B.Sc.I.T SEM-1
THE C CHARACTER SET
• C uses the uppercase letters A to Z, the lowercase letters a to z, the
digits 0 to 9, and certain special characters as building blocks to form
basic program elements (e.g., constants, variables, operators,
expressions, etc.).
THE C CHARACTER SET ….contd
+ - * / = % & #

! ? ^ " ' ~ \ |

< > ( ) [ ] { }

: ; . , _ (blank space)
Special Characters
Character Name Description

// Double slash Marks the beginning of a comment


# Pound sign Marks the beginning of a preprocessor
directive
<> Opening and closing Encloses a filename when used with the #
brackets include directive
() Opening and closing Used in naming a function , as in
parentheses int main()

{} Opening and closing Encloses a group of statements, such as the


braces contents of a function
"" Opening and closing Encloses a string of characters, such as a
quotation marks message that is to be printed on the screen

; Semicolon Marks the end of a complete programming


statement
F.Y.B.Sc.I.T SEM-1
The C Character Set …contd
• C uses certain combinations of these characters, such as \b, \n and \t,
to represent special conditions such as backspace, newline and
horizontal tab, respectively.
• These character combinations are known as escape sequences.
Keywords
• Keywords are the words whose meaning has already been explained
to the C compiler (or in a broad sense to the computer).
• These keywords can be used only for their intended purpose; they
cannot be used as programmer-defined identifiers.
• The keywords are also called ‘Reserved words’.
• There are only 32 keywords available in C.
Standard keywords
auto extern sizeof
break float static
case for struct
char goto switch
const if typedef
continue int union
default long unsigned
do register void
double return volatile
else short while
enum signed
Sample program 1
/* This program will print a welcome message on the screen*/
#include <stdio.h>
void main ( ) //main function starts
{
printf (“welcome to your first program in c”);
} // end of main function end of the program
• The output of the program will be

welcome to your first program in c


/* This program will print a welcome message on the screen*/
#include <stdio.h>
#include <conio.h>
void main ( ) //main function starts
{
clrscr();
printf (“welcome to your first program in c”);
getch();
}
Phases of C Program
C program development goes through six steps:
• Step1: Edit (using text editor to type, correct and save the program file).
• Step2: preprocessor, automatically before compile, executes to include
other text files in the file to be compiled.
• Step3: Compile, the compiler translates the C++ code into machine
language (also called object-code).
• Step4: Linker, it is linking any used functions that are defined elsewhere
such as standard library functions, or private library functions. For a group
of programmers, linker links the object code with the code for the missing
functions to provide full code.
• Step5: Load, a program before executing , must be loaded into the main
memory, loader does this task, loading code from disk.
• Step6: Execute, the computer under the control of its CPU executes the
program instruction by instruction.

F.Y.B.Sc.I.T SEM-1
Basics of a Typical C Program Program is created in
Development Environment Editor the editor and stored
Disk
on disk.
• Phases of C Preprocessor Disk
Preprocessor program
processes the code.
Programs: Compiler creates
Compiler Disk object code and stores
1. Edit it on disk.
2. Preprocess Linker Disk Linker links the object
code with the libraries
3. Compile Primary Memory

4. Link Loader
Loader puts program
5. Load Disk ..
in memory.

6. Execute
..
..

Primary Memory
CPU takes each
CPU instruction and
executes it, possibly
storing new data
..
..
values as the
..
program executes.

F.Y.B.Sc.I.T SEM-1
FILES used in C PROGRAM
• Source code of the program
Source File
• (file extension of any C source code file is .c)

• Standard header files


• Extension .h
Header File
• Ex: stdio.h
• Programmers can design their own header files

• Generated by the compiler


Object File
• Have the extension .obj such as in windows OS

• Generated by the linker


• Linker links the various object files to produce a binary file
Executable File
• Directly executed
• Has the extension .exe
DATA TYPES
Range
signed int
32,768 to +32,767
unsigned int
0 to 65,535
Unsigned char 0 to 255
Signed char -128 to +127

3.4 x 10-38 to 3.4 x 1038

1.7 x 10-308 to 1.7 x 10+308

F.Y.B.Sc.I.T SEM-1
Data type Keyword equivalent

Character char

Unsigned character unsigned char

Signed character signed char or char

Signed integer signed int or int

Signed short integer signed short int or short int or short

Signed long integer signed long int, long int or long

Unsigned integer unsigned int or unsigned

Unsigned short integer Unsigned short int or unsigned short

Unsigned long integer Unsigned long int or unsigned long

Floating point float

Double-precision floating point double

Extended Double-precision floating point long double


ENTERING INPUT DATA - THE scanf FUNCTION
• Input data can be entered into the computer from a standard input
device by means of the C library function scanf.
• This function can be used to enter any combination of numerical
values, single characters and strings.
• The function returns the number of data items that have been
entered successfully.
• Within the control string, multiple character groups can be
contiguous, or they can be separated by whitespace characters (i.e.,
blank spaces, tabs or newline characters).
• If whitespace characters are used to separate multiple character
groups in the control string, then all consecutive whitespace
characters in the input data will be read but ignored.
• The use of blank spaces as character-group separators is very
common.
Commonly Used Conversion Characters for Data
Input
• Commonly Used Conversion Characters for Data Input
• The arguments are written as variables or arrays, whose types match
the corresponding character groups in the control string.
• Each variable name must be preceded by an ampersand (&).
Example
#include <stdio.h>
main( )
{
char item;
int partno;
float cost;
.....
scanf("%c %d %f", &item, &partno, &cost);
.....
}
#include <stdio.h>
void main()
{
char ch = 'B';
printf("%c\n", ch); //printing character data

//print decimal or integer data with d and i


int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", y);

float f = 12.67;
printf("%f\n", f); //print float value
printf("%e\n", f); //print in scientific notation
int a = 67;
printf("%o\n", a); //print in octal format
printf("%x\n", a); //print in hex format

char str[] = "Hello World";


printf("%s\n", str);

printf("%20s\n", str); //shift to the right 20 characters including the string


printf("%-20s\n", str); //left align
/*shift to the right 20 chars including the string, and print string up to 5
character*/
printf("%20.5s\n",str);
printf("%-20.5s\n", str); //left align and print string up to 5 character
}
Output
B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello
Hello
Output
B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello
Hello
The printf Function
• The printf function is written as
printf ( control string, ar1, arg2, . . . , argn)
• where control string refers to a string that contains formatting
information, and arg1, arg2, . . . , argn are arguments that represent
the individual output data items.
• These arguments can be constants, single variable or array names, or
more complex expressions.
• The arguments in a printf function do not represent memory
addresses and therefore are not preceded by ampersands.
The printf Function
• The control string consists of individual groups of characters, with one
character group for each output data item with each character group
preceding with a percent sign (%).
• Multiple character groups can be contiguous, or they can be
separated by other characters, including whitespace characters.
The printf Function
Example
#include <stdio.h>
#include <math.h>
void main()
{
/*print several floating-point numbers */
float i= 2.0, j = 3.0;
printf ( " %f %f %f %f", i, j, i + j , sqrt (i+j)) ;
}
getch function

getch() • getch is used to hold the screen

Don’ write • the screen will just flash and go away....


this
getch • prompts the user to press a character
and that character is not printed on
function screen

F.Y.B.Sc.I.T SEM-1
Constants And Its Types
• A constant is a value or an identifier whose value cannot be changed
in a program.
• Eg. 1, 2, 5, “It is easy to learn C” etc.
• Two ways to define constant
1. Using const keyword
2. Using #define preprocessor (Symbolic constant)
SYMBOLIC CONSTANTS
• Symbolic constants are usually defined at the beginning of a program.
• A symbolic constant is defined by writing
#define name value
• where name represents a symbolic name, typically written in uppercase
letters, and
• text represents the sequence of characters associated with the symbolic
name.

F.Y.B.Sc.I.T SEM-1
SYMBOLIC CONSTANTS
• Symbolic constant definitions.
#define TAXRATE 0.23
#define PI 3.141593
#define TRUE 1
#define FALSE 0
#define FRIEND "Susan "
• Now suppose that the program contains the statement
area = PI * radius * radius;

F.Y.B.Sc.I.T SEM-1
SYMBOLIC CONSTANTS
• During the compilation process the above statement will become
area = 3.141593 * radius * radius;
• Note : any text enclosed by (double) quotation marks will be
unaffected by this substitution process.

F.Y.B.Sc.I.T SEM-1
SYMBOLIC CONSTANTS
• Advantages:
• They contribute to the development of clear, orderly programs.
• It is much easier to change the value of a single symbolic constant than to
change every occurrence of some numerical constant that may appear in
several places within the program.

F.Y.B.Sc.I.T SEM-1
End

You might also like