Introduction to C Programming

43
Ms. Azenith Rollan-Mojica INTRODUCTION TO C PROGRAMMING

description

ics

Transcript of Introduction to C Programming

Introduction to C Programming

Ms. Azenith Rollan-MojicaIntroduction to C Programming

1Format of a Turbo C Program

main(){

}

CommentStarts with /* and ends with */

Programmers insert comments to document programs and improve program readability.

Can be placed anywhere in the program.

Comments are ignored by the C compiler.

Include DirectiveIs the preprocessing directive to the C preprocessor.

Lines beginning with # are processed by the preprocessor before the program is compiled.

This tells the compiler what additional files are needed before processing the actual program.

Contains the information needed by the program to ensure correct operation of Turbo Cs standard library functions.

Include DirectiveExample:

#include

This specific line tells the preprocessor to include the contents of the standard input/output header file (stdio.h) in the program.

Variable DeclarationThis is the place where you declare the variables that will be used in the program.

Types of variable declaration:

a) Global variable declaration b) Local variable declaration

Variables definedVariables are identifiers that can store changeable value.

Variables are important since they contain the values needed for manipulation and evaluation of data.

Variable names are stored in the computers memory.

Rules for naming VariablesIt must consist only of letters, digits and underscore.It should not begin with a digit.An identifier defined in the C standard library should not be redefined.It is case sensitive (uppercase is not equal to lowercase letters).Do not include embedded blanks.Do not use any of the C language keywords as identifiers/variables.Do not call your variable/identifier by the same name as other functions.

Keywords Keywords are reserved words that have a special meaning in a programming language.

doubleelseenumexternfloatforgotoifintlongregisterreturnshortsignedsizeofstaticstructswitchtypedefunionunsignedvoidvolatilewhile

autobreakcasecharconstcontinuedefaultdo9Global VS Local VariablesGlobal VariablesVariables that are declared outside a function. They are known throughout the entire program.

Local VariablesVariables that are declared inside a function. They can only be referenced inside that function. They are also called automatic variables.

Global VS Local VariablesGlobal Variables

#include int a,b,c, x,y,z;main(){

}

function(){

}Local Variables

#include main(){int a,b,c;}

function(){int x,y,z;}Variable DeclarationAll variables must be declared before they may be used.

Syntax:type variable_list;Where:

type is any valid data type variable_list is 1 or more variable names with comma separatorData TypesThere are 5 basic data types in Turbo C:

a) Character (char) use to hold ASCII characters.

b) Integer (int) use to hold whole number values

c) Floating Point (float) use to hold real numbers

d) Double Floating Point (double) - use to hold real numbers

e) Void (void) use to declare a function with no return values, functions with no parameters and to create generic pointers.

13Data TypesTYPEBITWIDTHRANGE

char80 to 255 (ASCII)

int16-32768 to 32767

float323.4E-32 to 3.4 E+38

double641.7E-308 to 1.7 E+308

void0 valuelessData TypesTYPEEXAMPLES

charAb$9

int12504500

float3.542.56345.6789

double3.5647290 486.145875...

voidvaluelessVariable DeclarationExamples:char name;

int x, y,z;

float number;

float n1, n2, sum;

double counter;

Variable InitializationGiving a value to a variable during the variables declaration is called variable initialization.

Syntax:

type variable_name = value;

Example:

int count = 100;float grade = 3.75;char status = S;

main()Is a part of every C program.

The parenthesis after main indicate that main is program building block called a function.

C programs contain one or more functions, one of which must be main.

Every program in C begins executing at the function main.

{ and }The left brace, must begin the body of every function.

The right brace, ends the body of every function.

The pair of braces and the portion of the program between the braces is also called a block.

All statements that will be executed by the program should be placed inside the { and }.

Turbo C StatementsValid Turbo C statements that will be executed by your program.

Every statement in Turbo ends with a semicolon (;) which is the statement terminator in Turbo C.Clear Screen Statementclrscr();

This is the clear screen function in Turbo C.

This is used to clear the command prompt every time the program is executed.

Getch Statementgetch();

Last statement that will be placed before the closing brace }

Used to input a single character from the keyboard without echoing the character on the monitor.

Causes delay in displaying the output once the program is executed

The printf() statementThis is used to display argument list on the monitor.

This an output function in Turbo C

Syntax 1:printf (argument);

Examples:printf (UST Manila);printf (Computer Programming is Easy!\n);Note:\n is an escape sequence for NEW LINE\t is an escape sequence for TAB.

The printf() statementSyntax 2:

printf (control string code, argument list);

Example:a=100;printf (%d, a);printf (The value is %d, a);

The control string code contains the format command that tells printf() how to display arguments and how many arguments are on the list.

The printf() statementControl String CodeData Type

%ccharacter%dinteger%ffloat/doubleNotes:

1. Control string code and argument list should agree in type.2. They should have a one-to-one correspondence.

The printf() statementExample:

int x = 10;int y = 25;float z = 123.1234;printf( x = %d and y = %d\n, x, y);printf ( z = %f\n, z);printf ( z = %.2f\n, z);Output:x = 10 and y = 25z = 123.1234z = 123.12

The scanf StatementThis is used to input a single character or sequence of characters from the keyboard.

It needs the control string codes to be recognized.

Spaces are not accepted upon inputting.

Terminated by pressing spacebar.

This is an input function in Turbo C.

The scanf StatementSyntax:scanf (format control string, &input list);

Examples:

int x,y;float grade;char letter;scanf (%d, &x);scanf (%f, &grade);scanf (%c, &letter);scanf (%d %d, &x, &y);

The scanf StatementThe format control string indicates the type of data that should be input by the user. format control stringData Type

%ccharacter%dinteger%ffloat%lfdouble

The ampersand (&) is called the address operator in C. This is placed before the variable name in a scanf() statement.

The address operator in C tells scanf() the location in memory in which the variable will be stored. The computer then stores the value of the variable at that location.

The Assignment StatementThis is used to assign a value to a variable.

This is used for computations needed in the program.

Equal sign (=) is the assignment operator in Turbo C.

Syntax:variable_to_hold_value = expession;

Example:

sum = integer1 + integer2;

Turbo Cs Shorthand x = x + 5; x+=5;

x = x - 7; x-=7;

x = x * 12; x*=12;

x = x / y; x/=y;

Sample C program: Adding 2 numbers1/* Addition Program */23#include < stdio.h >45main( )6{7 int integer1, integer2, sum;8 clrscr();9 printf ( Enter first integer : );10 scanf ( %d, &integer1 );11 printf ( Enter second integer: );12 scanf ( %d, &integer2 );13 sum = integer1 + integer2;14 printf ( Sum is %d \n, sum );15 getch();16 }

ConstantsConstants are identifiers that can store a value that cannot be changed during program execution.

Like variables, constants are declared before they are used in the program.

Syntax:consttype variable_name = value;Where:constis a Turbo C keywordtype is any valid data type variable_name is the name given to a constantvaluefixed value given to a constant

Example:const int count = 100;

OperatorsOperator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

There are 3 classes of operators in Turbo C:

a) Arithmetic Operators b) Relational & Logical Operators c) Bitwise Operators

Arithmetic OperatorsArithmetic operators are used to perform arithmetic computations.

+Addition-Subtraction*Multiplication/Division%Modulus Division (MOD)--Decrement++Increment

Arithmetic OperatorsWhen division (/) is applied to an integer, any remainder is truncated.

% is only used on integer data type.

Any number preceded by - sign switches its sign.

The increment (++) operator adds one to its operand and the decrement operator (--) subtracts one.

Both ++ and -- may either precede or follow the operand (e.g. ++x or x++).

When an increment or decrement operator precedes it operand, C performs the increment or decrement operation prior to using the operands value. If it follows its operands, C uses the operands value before incrementing or decrementing it.

Relational & Logical OperatorsRelational operators show the relationship values have with one another.

Logical operators show the ways these relationships can be connected together using rules of formal logic.

The key to the concepts of true relationship between relational and logical operators is the idea of TRUE and FALSE. In turbo C, TRUE is any value other than zero (0). FALSE is zero (0).

Relational & Logical OperatorsA. Relational Operators>greater than>=greater than or equalShift Right> (Shift Right). Move all bits in a variable to the right.

6)