Programming Fundamentals lecture 5

Preview:

Citation preview

C Program Structure

REHAN IJAZ

By

Programm

ing Fundamentals

Main Function The main function serves as the starting point for program execution.

It usually executes statements and controls program execution by

directing the calls to other functions in the program.

A program usually stops executing at the end of main, although it can

terminate at other points in the program for a variety of reasons.

At times, perhaps when a certain error is detected, you may want to

force the termination of a program. To do so, use the exit function.

Main Function All C language programs must have a main() function.

It's the core of every program. It's required.

The main() function doesn't really have to do anything other than be

present inside your C source code.

Eventually, it contains instructions that tell the computer to carry out

whatever task your program is designed to do. But it's not officially

required to do anything.

Main Function When the operating system runs a program in C, it passes control of the

computer over to that program.

In the case of a C language program, it's the main() function that the

operating system is looking for to pass the control.

At a minimum, the main() function looks like this:main() {

}

Main Function Like all C language functions, first comes the function's name, main, then comes a set of parentheses, and finally comes a set of braces, also called curly braces.

Main Function Command Explanation1 #include <stdio.h> This is a preprocessor command that includes standard

input output header file(stdio.h) from the C library before compiling a C program

2 int main() This is the main function from where execution of any C program begins.

3 { This indicates the beginning of the main function.4 printf(“Hello_World! “); printf command prints the output onto the screen.5 getch(); This command waits for any character input from

keyboard.6 return 0; This command terminates C program (main function) and

returns 0.7 } This indicates the end of the main function.

Statement A statement is a command given to the computer that instructs the

computer to take a specific action, such as display to the screen, or

collect input.

A computer program is made up of a series of statements.

An assignment statement assigns a value to a variable. A printf()

statement writes desired text e.g. printf(“Hello_World! “); Int Stdregno, age;

Compound Statement Compound Statement also called a "block“is the way C groups multiple

statements into a single statement with the help of braces (i.e. { and }).

The body of a function is also a compound statement by rule.

#include <stdio.h>void main(){ printf("Hello, world!\n"); getch();}

Comment A comment is a note to yourself (or others) that you put into your source

code.

Comments provide clarity to the C source code allowing yourself and

others to better understand what the code was intended to accomplish

and greatly helping in debugging the code.

All comments are ignored by the compiler they are not executed as part

of the program.

Comment Comments are especially important in large projects containing hundreds

or thousands of lines of source code or in projects in which many

contributors are working on the source code.

Comments are typically added directly above the related C source code.

Adding source code comments to your C source code is a highly

recommended practice.

Comment Types

(i) Single line Comment

// comment goes here

(ii) Block Comment

/* comment goes heremore comment goes here */

Input-Output When we say Input, it means to feed some data into a program. An input

can be given in the form of a file or from the command line. C

programming provides a set of built-in functions to read the given input

and feed it to the program as per requirement.

When we say Output, it means to display some data on screen, printer,

or in any file. C programming provides a set of built-in functions to

output the data on the computer screen as well as to save it in text or

binary files.

Input-Output INPUT: When we say Input, it means to feed some data into a program.

An input can be given in the form of a file or from the command line. C

programming provides a set of built-in functions to read the given input

and feed it to the program as per requirement.

OUTPUT: When we say Output, it means to display some data on screen,

printer, or in any file. C programming provides a set of built-in functions

to output the data on the computer screen as well as to save it in text or

binary files.

Standard Input printf()

function writes the output to the standard output stream and produces

the output according to the format provided.

The format can be a simple constant string, but you can specify %s, %d,

%c, %f, etc., to print or read strings, integer, character or float

respectively.

Standard Outputscanf()

function reads the input from the standard input stream and scans that

input according to the format provided.

Standard Input-OutputExample

#include <stdio.h>int main( ) { char str[100]; int i; printf( "Enter values :"); scanf("%s %d", str, &i); printf( "\nYou entered: %s %d ", str, i); return 0;}

Enter a value : seven 7You entered: seven 7

C Preprocessor C pre processor refers to a separate program which will be invoked by the

compiler as first part of translation.

Before a C program is compiled in a compiler, source code is processed by a

program called preprocessor. This process is called preprocessing.

Commands used in preprocessor are called preprocessor directives and

they begin with “#” symbol.

Source Code Preprocessor Compiler Object Code

C Preprocessor Directives The preprocessor provides the ability for the inclusion of header files.

Preprocessor directives are lines included in the code of programs preceded

by a hash sign (#).

These lines are not program statements but directives for the preprocessor.

The preprocessor examines the code before actual compilation of code

begins and resolves all these directives before any code is actually

generated by regular statements.

C Preprocessor Directives

No semicolon (;) is expected at the end of a preprocessor directive.

Preprocessor directive can be extended by a backslash (\).

C Preprocessor DirectivesFew examples of preprocessor directives

#include<stdio.h>

#include<conio.h>

#include<math.h>

etc.

Types of C Preprocessor DirectivesS.no Preprocessor Syntax Description

1 Header file inclusion

#include <file_name>

The source code of the file “file_name” is included in the main program at the specified place

2 Macro #define This macro defines constant value and can be any of the basic data types.

3 Conditional compilation

#ifdef, #endif, #if, #else, #ifndef

Set of commands are included or excluded in source program before compilation with respect to the condition

4 Other directives

#undef, #pragma

#undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program

Types of C Preprocessor Directives

#include pre processor directive is used to include header files in the

program.

If header file name is enclosed within angle brackets (), then compiler

search this file only within its standard include directory.

Similarly, if header file name is enclosed within double quotes (“ ”) then

compiler will search this file within and out side of its standard path.

1. Header file inclusion (#include)

#include <stdio.h>void main(){ printf("Hello, world!\n"); getch();}

1. Header file inclusion (#include)

Example

Types of C Preprocessor Directives

Types of C Preprocessor Directives

In C/C++ programming language, Define directive is used for multi

functions such as to define a symbol, to define a name constant or to

define macros (short and fast processed code)

(a) To define a symbol,

(b)To define name and constants

(c) To define macros

2. Macro - Define Directive (#define)

Types of C Preprocessor Directives

(a) To define a symbol,

In the first statement, NULL is a symbol which would be replaced

with 0 in a program. Similarly, the symbol PLUS can be replaced with

+. So following both statements will be work in the same way.

2. Macro - Define Directive (#define)

C=A PLUS B; it will work same as C=A+B;

#define NUL 0#define PLUS +

Types of C Preprocessor Directives

(b) To define name and constants

#define preprocessor can help to define a named constant. Such as

2. Macro - Define Directive (#define)

#define INTEGER 5#define CHARACTER A

Types of C Preprocessor Directives(c) To define macro In C/C++ programming language, a macro is defined as a segment of text

and it is very useful due to its readability or compact factors. Some examples of macros are

2. Macro - Define Directive (#define)

#define pi 22.0/7#define Area(r) 3.14*r*r

The first macro is very simple and its name can be used in any expression such as A= pi* 5. Similarly, second macro has a parameter r and it calculate the area.

If a macro is defined at more than one line then you need to place \ at the end of each line except last line. \ Symbol is used for continuation from next line.

Types of C Preprocessor Directives2. Macro - Define Directive (#define) (c) To define macro

value of height : 100value of number : 3.140000value of letter : Avalue of letter_sequence : ABCvalue of backslash_char : ?

#include <stdio.h>#define height 100#define number 3.14#define letter 'A'#define letter_sequence "ABC"#define backslash_char '?'void main(){ printf("value of height : %d ", height ); printf("value of number : %f ", number ); printf("value of letter : %c ", letter ); printf("value of letter_sequence : %s ", letter_sequence); printf("value of backslash_char : %c ", backslash_char);}Output:

Types of C Preprocessor Directives

In this type of preprocessor, #ifdef directive is used to check the

existence of defined macros, constant name or symbol. For example,

when we write following statement

The work of #ifndef directive is same as of #ifdef but there is slight

difference. For #ifdef directive, if a symbol or named constant or

macro has been undefined by using #undef then #ifdef will return

false valu, but #ifndef will return true value for such case.

3. Conditional Compilation(#ifdef, #ifndef, #endif)

Types of C Preprocessor Directives

3. Conditional Compilation(#ifdef, #ifndef, #endif)

Khalid is defined. So, this line will be added in this C file

#include <stdio.h>#define Khalid 10int main(){ #ifdef Khalid printf("Khalid is defined. So, this line will be added in this

C file"); #else printf("Khalid is not defined"); #endif return 0;}

Output:

Types of C Preprocessor Directives

#undef preprocessor directive is used to undefined a macro, named

constant or any symbol which has been defined earlier.

Unlike #include and # define directives, #undef directive can be used

everywhere in a program but after the #define preprocessor.

#undef pi

#undef PLUS

4. Undefine Directives (#undef)

Thank you

Recommended