Sunday, 28 October 2018

Structure of C Program and Format Specifiers



Structure of a C program


<#include directive>

<#define directive>

main()

{

<variable declaration section>

Statements

}


  1. #include directive – “contains information needed by the program to ensure the correct operation of Turbo C’s standard library functions."

Example: #include <stdio.h>


  1. #define directive – “used to shorten the keywords in the programs.”

Example: #define g gotoxy


  1. Variable declaration section – “it is the place where you declare your variable.”


  1. Body of the program – “ start by typing main() and the { and } (open and close braces). All statements should be written inside the { and } braces.”


NOTE: Turbo C is a case-sensitive program, use lowercase letters only.


Commonly used include files in C language

  1. alloc.h – declares memory management functions

  2. conio.h – declares various functions used in calling IBM-PC ROM BIOS

  3. ctype.h – contains information used by the classification and character conversion macros.

  4. math.h – declares prototype for the math functions.

  5. stdio.h – defines types and macros needed for standard I/O.

  6. string.h – declares several string manipulation and memory manipulation routines.


Basic Input/ Output Statements of C


Usually, our program involves three main operations. The first one is the input operation that uses input functions such as scanf, getch(), gets(), getche(), getchar() and others. The second is the process operation. In this part of our program, we can see some equations that are calculated, conditions that are evaluated, and tasks being performed. The third part is the output operation. Here, we use output statements such as printf() function, puts(), putch(), putchar functions and other output statement functions. To learn this technique, we can jump right in and type our first program that involves these three main operations.



FORMAT SPECIFIERS 

All format specifiers start with a percent sign(%) and are followed by a single letter indicating the type of data and how data are to be formatted.

Format Specifier

Use

Sample Usage

%c

Used for single character

scanf(“%c”, &ch);

printf(“%c”,ch);

%d

Decimal number(whole number)

scanf(“%d”, &num);

printf(“%d”,num);

%e

Scientific notation/exponential form

scanf(“%e”, &result);

printf(“%e”,result);

%f

Number with floating or decimal point

scanf(“%f”, &pesos);

printf(“%f”,pesos);

%o

Octal number

scanf(“%o”, &value);

printf(“%o”,value);

%s

String of characters

scanf(“%s”, &str);

printf(“%s”,str);

%u

Unsigned number

scanf(“%c”, &nos);

printf(“%c”,nos);

%x

Hexadecimal number

scanf(“%x”, &value);

printf(“%x”,value);

%X

Capital number for hexadecimal number

scanf(“%X”, &nos);

printf(“%X”,nos);

%%

Print a percent sign

scanf(“%%”, &value);

printf(“%%”,value);

0 comments:

Post a Comment

If possible, leave a positive comment. No hate speech or elicit comments. Thank you.