programming fundamentals lecture 4

12
BY REHAN IJAZ 04- Getting Started with C

Upload: rehan-ijaz

Post on 25-Jan-2017

97 views

Category:

Engineering


4 download

TRANSCRIPT

BY REHAN IJAZ

04- Getting Started with C

BY REHAN IJAZ

C's Character SetC does not use, nor requires the use of, every character found on a modern computer keyboard. The use of most of this set of characters will be discussed throughout the course. The only characters required by the C Programming Language are as follows:

character:- It denotes any alphabet, digit or special symbol used to represent information.

Use:- These characters can be combined to form variables. C uses variables, operators, keywords and expressions as building blocks to form a basic C program.

Character set:- The character set is the fundamental raw material of any language and they are used to represent information. Like natural languages, computer language will also have well defined character set, which is useful to build the programs.

The characters in C are grouped into the following two categories:

1. Source character seta. Alphabetsb. Digitsc. Special Charactersd. White Spaces

2. Execution character seta. Escape Sequence

1. Source character set

ALPHABETSUppercase letters A-ZLowercase letters a-z

DIGITS 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

SPECIAL CHARACTERS

WHITESPACE CHARACTERS

BY REHAN IJAZ

\b blank space \t horizontal tab \v vertical tab \r carriage return \f form feed \n new line

\\ Back slash \’ Single quote \" Double quote \? Question mark \0 Null \a Alarm (bell)

2. Execution Character Set

Certain ASCII characters are unprintable, which means they are not displayed on the screen or printer. Those characters perform other functions aside from displaying text. Examples are backspacing or moving to a newline .

They are used in output statements. Escape sequence usually consists of a backslash and a letter or a combination of digits. An escape sequence is considered as a single character but a valid character constant.

These are employed at the time of execution of the program. Execution characters set are always represented by a backslash (\) followed by a character. Note that each one of character constants represents one character, although they consist of two characters. These characters combinations are called as escape sequence.

Escape Sequence

Invalid Sequence

#include <stdio.h>int main() {

printf("Hello,world!");

}

Correct Sequence

#include <stdio.h>int main() {

printf("Hello,\nworld!");}

BY REHAN IJAZ

More Escape Sequences Null \0 Null Alarm (bell) \a Beep Sound Back space \b Moves previous position Horizontal tab \t Moves next horizontal tab New line \n Moves next Line Vertical tab \v Moves next vertical tab Form feed \f Moves initial position of next page Carriage return \r Moves beginning of the line Double quote \" Present Double quotes Single quote \' Present Apostrophe Question mark \? Present Question Mark Back slash \\ Present back slash

BY REHAN IJAZ

C’s Variable

C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.

The value of the C variable may get change in the program. C variable might be belonging to any of the data type like int, float, char etc.

Rules for naming C variable:

1. Variable name must begin with letter or underscore.2. Variable names are case sensitive3. They can be constructed with digits, letters.4. No special symbols are allowed other than underscore.5. sum, height, _value are some examples for variable name

Declaring & initializing C variable:

Variables should be declared in the C program before to use. Memory space is not allocated for a variable while declaration. It happens only on

variable definition. Variable initialization means assigning a value to the variable.

S.No

Type Syntax Example

1 Variable declaration

data_type variable_name; int x, y, z; char flat, ch;

2 Variable initialization

data_type variable_name = value;

int x = 50, y = 30; char flag = ‘x’, ch=’l’;

Variable types

There are three types of variables in C program They are,

1. Local variable

BY REHAN IJAZ

2. Global variable3. Environment variable

1. Example program for local variable in C:

The scope of local variables will be within the function only. These variables are declared within the function and can’t be accessed outside the

function. In the below example, m and n variables are having scope within the main function only.

These are not visible to test function. Like wise, a and b variables are having scope within the test function only. These are not

visible to main function.

#include<stdio.h>

void test();int main(){int m = 22, n = 44;printf(“nvalues : m = %d and n = %d”, m, n);test();}

void test(){int a = 50, b = 80;printf(“nvalues : a = %d and b = %d”, a, b);}

Output:

values : m = 22 and n = 44

values : a = 50 and b = 80

BY REHAN IJAZ

2. Example program for global variable in C:

The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.

This variable is defined outside the main function. So that, this variable is visible to main function and all other sub functions.

#include<stdio.h>

void test();int m = 22, n = 44;int a = 50, b = 80;int main(){printf(“All variables are accessed from main function”);printf(“nvalues: m=%d:n=%d:a=%d:b=%d”, m,n,a,b);test();}void test(){printf(“nnAll variables are accessed from”” test function”);printf(“nvalues: m=%d:n=%d:a=%d:b=%d”, m,n,a,b);}

Output:

All variables are accessed from main functionvalues : m = 22 : n = 44 : a = 50 : b = 80

All variables are accessed from test functionvalues : m = 22 : n = 44 : a = 50 : b = 80

BY REHAN IJAZ

C’s Identifiers

Identifiers in C language:

Each program elements in a C program are given a name called identifiers. Names given to identify Variables, functions and arrays are examples for identifiers. eg.

x is a name given to integer variable in above program.

Rules for constructing identifier name in C:

1. First character should be an alphabet or underscore.2. Succeeding characters might be digits or letter.3. Punctuation and special characters aren’t allowed except underscore.4. Identifiers should not be keywords.

BY REHAN IJAZ

C’s Key words

Keywords are pre-defined words in a C compiler. Each keyword is meant to perform a specific function in a C program. Since keywords are referred names for compiler, they can’t be used as variable name.

C language supports 32 keywords which are given below.

auto

double

int struct

const

float

short

unsigned

break

else

long

switch

continu

e

for

signed

void

case

enum

register

typede

f

default

goto

sizeof

volatile

char

extern

return

union

do if static

while

BY REHAN IJAZ