30
C Programming Language By: Yogendra Pal [email protected] Dedicated to My mother and Father

C basics

  • View
    15

  • Download
    1

Embed Size (px)

DESCRIPTION

 

Citation preview

CProgramming Language

By:

Yogendra [email protected]

Dedicated to My mother and Father

THIS IS C BASICS

Keep Watching Keep Learning

2

t Keep your notebook with you.

yWrite important point and questions that comes in your mind

Solve Mind band exercise.

Ask Questions by call or SMS or by mail

CRewind when not clear

First C Program

• Print a line of text.

• To compile you need a “C” compiler.

• Some compilers are Borland C, Turbo C.

3

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

printf(“I am learning C”);}

Install Compiler

• Borland C and C++ compiler.

• Turbo C and C++ compiler.

• Work from

– Command line interface (CLI).

– Integrated Development Environment (IDE).

4

• Type, compile and run the previous programon your computer.

• First use CLI then run them in IDE.

• C______ L___ I________.

• I_________ D__________ E__________.

5

Mind Bend

C Character Set

• You write your C program by using some characters.

• These characters are:– Alphabets (Uppercase & Lowercase)

• A,B,C…………………..,X,Y,Z.

• a,b,c…………………...,x,y,z.

– Digits (0,1,2,3,4,5,6,7,8,9)

– Special symbols (~ ` ! @ # $ % ^ & * ( ) _ - + = | \ } ] [ { ; : ‘ “ / ? . > , < )

6

C Character Set…

7

AlphabetsdigitsSpecial SymbolsSpace

Comments

• Comment a program for easy understanding.

• Two kind of comments are available

– Single line comment

• //

– Multiple line comment

• /* */

8

Escape Sequence

Character Escape Sequence ASCII value

Newline \n 10

Horizontal tab \t 9

Vertical tab \v 11

Backspace \b 8

Bell alert \a 7

Quotation mark \” 34

Apostrophe \’ 39

Question mark \? 63

Backslash \\ 92

Null \0 0

Carriage return \r 013

9

• Using escape sequence write a program thatprint following output (Use a single printf()function).

10

Mind Bend

***

********

*******

Identifiers

• Identifiers are names given to Variables,Functions, arrays and other programmingelements.

• Used to uniquely identify each element.

• Can contain alphabets, digits and underscore.

• First character must be an alphabet orunderscore.

• Space and other special symbols are notallowed.

11

Identifiers…

A o V

12

Identifiers…

• First_name

• a1

• area_of_circle

• Pi

• TABLE

• _area

• First name

• 1a

• area-of-circle

• ^

• “TABLE”

• -area

13

Correct Incorrect

Try to make identifier meaningful and small.

Keywords

• Keywords are reserved words.

• Have standard, predefined meaning.

• Cannot be used as identifiers.

14

Keywords…

auto extern sizeof break float

static case for struct char

goto switch const if sizedef

continue int union default long

unsigned do register void double

return volatile else short while

enum signed

15

Data types

• There are several data types in C.

• Memory representation of each data type is different.

• Memory requirement is also different.

• Range of each data type varies from data type to data type.

16

Data Types…

• Character (char)

• Integer (int)

• Floating point (float)

• Double (double)

• Valueless (void)

17

Character

• Character can be any single alphabet, digit or special symbol.

• There are total 256 characters.

• Value of character can be 0 to 255.

• 1 Character = 8 bits = 1 byte

• Represent with “char” in C programs.

• 8 bits means 28 = 256 possibilities.

• Format specifier : %c18

Type Declaration

• Assign a data type to a variable.

• Declare a variable before use.

19

char choice;char c1,c2,c3;char c1=‘y’;char c1=‘y’, c2=‘n’;

Input / Output instructions

• Output

– printf(“ “);

– printf(“format specifier”,variable);

• printf(“%d”,i);

• Input

– scanf(“format specifier”,&variable);

• scanf(“%d”,&i);

– scanf take input from console.

– & operator represents the address.20

Working with data

• Create a variable of a data type.

– char c;

• Initialize it with a value.

– c = ‘a’;

• Use it in program.

– printf ( “%c” , c);

• An example

1009

cNo

value

a

21

Character constant

• Single alphabet, digit or special symbolenclosed within single inverted comma.

• Maximum length is one character.

• Correct: ‘a’ ‘!’ ‘$’

• Incorrect: a ‘30’ 30

2222

Every character have a unique ASCII value.

Integer

• Integer can be any number.

• integer : 2 bytes (16 bit) or 4 bytes (32 bit).

– 16 bit means 216 = 65536 numbers.

• Range (-32768 to 32767)

– 32 bit means 232 = 4,29,49,67,296 numbers.

• Range (-2147483648 to 2147483647)

• Represent with “int” in C programs.

• Format specifier : %d

23

Working with integer

• Create a integer type variable.

– int i;

• Initialize it with a value.

– i = 10;

• Use it in program.

– printf ( “%d” , i);

• An example

No

value

2

bytes

10

24

Integer Constant

• Must have at least one digit.

• Decimal point is not allowed.

• Can be positive or negative default is positive.

• Commas, blanks or any other symbol is notallowed.

• Correct: 12 -467 +098

• Incorrect: 12,120 12-10 13.09

25

Floating Point

• Floating number can be any number withdecimal point.

• float : 4 bytes (32 bits)

– Range (-3.4e38 to 3.4e38)

• Represent with “float” in C programs.

• Format specifier : %f

26

Float Constant

• Fractional form

– eg. 456.09

– Must have at least onedigit.

– Must have a decimalpoint.

– Positive or negativedefault is positive.

– Comma, space and otherspecial symbol is notallowed.

• Exponential form {mantissa, exponent}.

– mantissaeexponent eg.3.234e6

– Both parts can bepositive or negative.

– Default sign is positive.

– Must have at least asingle digit on both side

– Exponent can not real.

27

Mind Bend

• Are uppercase letters equivalent to lowercaseletters? (yes/no)

• Can be use digits in an identifier name?

• Can be use any special character in identifiername?

• What are keywords?

• Write the range of character, integer and floatdata types used in C.

28

• What are the escape sequences for newline,backspace and horizontal tab?

• What is the purpose of type declaration?

• Is declaration of each variable necessarybefore use?

• Can be use char as a variable name oridentifier? CHAR can be used or not?

Mind Bend

29

NEXT IS ARITHMETIC INSTRUCTIONSKeep Watching Keep Learning

To get complete benefit of this tutorial solve all the quiz on

www.learnbywatch.com

For any problem in this tutorial mail me at

[email protected]

with the subject “C”

For Other information mail at

[email protected]

30