chapter 2 c fundamentals

35
Vocational crash course on “C with introduction to embedded C Course instructor : Prof. Kumar Anand Singh (Asst. Professor, EC, MEFGI) Course assistances: Prof. Hetal Dave (Asst. Professor, EC, MEFGI) Mr. Saumil Vora (Student, PG-VLSI, 1 st year) Chapter 2

Upload: jatin-adroja

Post on 17-Feb-2016

239 views

Category:

Documents


3 download

DESCRIPTION

Fundamental Ideas

TRANSCRIPT

Page 1: Chapter 2 C Fundamentals

Vocational crash course on “C with introduction to embedded C

Course instructor : Prof. Kumar Anand Singh (Asst. Professor, EC, MEFGI)

Course assistances:

Prof. Hetal Dave (Asst. Professor, EC, MEFGI)

Mr. Saumil Vora (Student, PG-VLSI, 1st year)

Chapter 2

Page 2: Chapter 2 C Fundamentals

Time table chart

S.No. Topic Date Total Hours

TH (hrs.) PR(hrs.)

1. Introduction 1 0.5

2. C Fundamentals 4 2

3.. I/O Functions 2 1

4.. Control statements 4 2

5. Arrays 4 2

6. String handling function 2 1

7. Functions 2 1

8. Structure & Unions 4 2

9. Pointers 4 2

10. Dynamic Memory Allocation 2 1

11. File handling 2 1

12. General Interview Questions 4 2

13. Introduction to Embedded C 4 2

TOTAL (12 days,10th Dec 2014 to 23rd Dec 2014) 40 20

Page 3: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 4: Chapter 2 C Fundamentals

Identifiers : Identifiers are the names that are given to various program

elements such as variables, symbolic constants and functions.

The following are illegal identifiers

float :e;

float for;

float 9PI;

float .3.14;

float 7g;

Some legal identifiers: float _number; float a; int this_is_a_very_detailed_name_for_an_identifier; http://cprogrammingexpert.com/C/Tutorial

Page 5: Chapter 2 C Fundamentals

Rules for Identifiers :

• Identifier name must be a sequence of letter and digits, and must

begin with a letter.

• The underscore character („_‟) is considered as letter.

• Names shouldn't be a keyword (such as int , float, if ,break, for etc)

• Both upper-case letter and lower-case letter characters are allowed.

However, they're not interchangeable.

• No identifier may be keyword.

• No special characters, such as semicolon,period,blank space, slash

or comma are permitted

Page 6: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 7: Chapter 2 C Fundamentals

Keywords :

• Keywords are standard identifiers that have standard predefined meaning in C.

• Uppercase & Lower case character have different meaning in C.

void char int float double signed

unsigned short long auto static register

const struct union if else goto

for continue switch case break default

do while sizeof volatile enum extern

typedef return

Page 8: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 9: Chapter 2 C Fundamentals

Data Types (“Primitive Types”)

Four fundamental Data types : 1. Character (char) 2. Integer (int) 3. Floating point (float) 4. Double Floating point (double)

Memory requirements :

Data Type Meaning Size (byte) Minimal Range

char Character 1 -128 to +127

int Integer 2 -32768 to +32767

float Single Precision real no.

4 3.4E-38 to 3.4E+38

double Double Precision real no.

8 1.7E-308 to 1.7E+308

void No Data 0 -------

Page 10: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 11: Chapter 2 C Fundamentals

Constants

• The term constant means that it does not change during the execution of

program.

• Constant and is the data with a constant value that does not change in the

program.

Four basic types of constants in C :

1. Integer Constants

2. Floating point Constants

3. Character Constants

4. String Constants

Page 12: Chapter 2 C Fundamentals
Page 13: Chapter 2 C Fundamentals
Page 14: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 15: Chapter 2 C Fundamentals

Variables :

• Variables are means for location in memory used by a program to store data.

• The size of that block depends upon the range over which the variable is allowed to vary.

The format for declaring a variable in C. [Storage-class] type data variable name [= initial value];

Page 16: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 17: Chapter 2 C Fundamentals

Expressions :

• An expression is a sequence of operators and operands that specifies computation of a value.

• An expression may consist of single entity or some combination of such entities interconnected by one or more operators.

• All expression represents a logical connection that's either true or false.

Ex : A+b 3.14*r*r a*a+2*a*b+b*b

Page 18: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 19: Chapter 2 C Fundamentals

Statements :

The statements of a C program control the flow of program execution.

• if statement

• switch statement

• goto statement

• for statement

• while statement

• do-while statement

• break statement

• continue statement

• expression statement

• compound statement

• return statement

• null statement

Page 20: Chapter 2 C Fundamentals

if statement :

Syntax : : if (expression) statement;

void main() { char sex; printf(“Enter your sex (m/f) “); scanf(“%c”,&sex); if(sex==„m‟) { printf(“Male”); } else printf(“Famale”); getch(); }

• It is a conditional branching statement.

• In conditional branching statement a condition is evaluated,

• if it is evaluate true a group of statement is executed.

Page 21: Chapter 2 C Fundamentals

• Multiple if statement. • It is an error-prone statement.

Switch Statement :

Page 22: Chapter 2 C Fundamentals

#include<stdio.h> #include<conio.h> void main() { clrscr(); printf(“Marwadi “); goto x; y: printf(“Foundation”); goto z; x: printf(“Education”); goto y; z: printf(“Group of Institution”); getch(); }

goto statement:

The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program unconditionally.

Page 23: Chapter 2 C Fundamentals

for loop

• Most general looping construct • loop header contains three parts:

• initialization • continuation condition • increment/decrement

Syntax: for (initialization, condition, step) { Statement 1; Statement 2; …………... Statement n; }

Page 24: Chapter 2 C Fundamentals

while loop

• Another looping construct • It evaluates the test expression before every loop. • it can execute zero times if the condition is initially false.

Syntax: while (expression) { Statement 1; Statement 2; …………... Statement n; }

Page 25: Chapter 2 C Fundamentals

do while loop

• Loop condition is tested at the end of the body of the loop. • loop is executed at least one. • unpopular loop statement.

do { Statement 1; Statement 2; …………... Statement n; } while(expression);

Page 26: Chapter 2 C Fundamentals

break statement

• It is a jump instruction. • used inside a switch construct, for loop, while loop and do-while loop. • break statement causes immediate exit from the concern construct.

Page 27: Chapter 2 C Fundamentals

continue statement

• It is a jump instruction. • used inside a switch construct, for loop, while loop and do-while loop. • Execution of these statement does not cause an exit from the loop but it suspend the execution of the loop for that iteration and transfer control back to the loop for the next iteration.

Page 28: Chapter 2 C Fundamentals

Expression statements

• It consists of an expression followed by a semicolon. • The execution of an expression causes the expression to be evaluated.

A=10; A+=20; printf("Hello, Good morning!\n"); display(a,b); c=a+b; ; /*null statement*/

Page 29: Chapter 2 C Fundamentals

Compound statements

• A compound statement (also called a "block"). • It appears as the body of another statement. • Used in if statement, for statement, while statement,

etc. Syntax : { pi=3.14; area=pi*radius*radius; }

Page 30: Chapter 2 C Fundamentals

Return statement

• A function may or may not return a value.

• A return statement returns a value to the calling function and assigns to the variable in the left side of the calling function.

• If a function does not return a value, the return type in the function definition and declaration is specified as void.

Page 31: Chapter 2 C Fundamentals

Null statement

• Null statement consisting of only a semicolon and performs no operations.

Page 32: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 33: Chapter 2 C Fundamentals

Symbolic constant in c Language

• It substitute for a sequence of character that cannot be changed.

• Character may represent a numeric constant, a character constant, or a string.

• Usually defined at the beginning of the program.

example #define PI 3.141593 #define TRUE 1 #define FALSE 0

#include<stdio.h>

#include<conio.h>

#define TRUE 1

#define PI 3.141593

void main()

{

float a; float b; float c;

float d=PI;

clrscr();

if(TRUE)

{

a=100;

b=a*10;

c=b-a;

}

printf("\na=%f\nb=%f\nc=%f\nPI=%f",

a,b,c,d);

getch(); }

Page 34: Chapter 2 C Fundamentals

C Fundamentals

• Identifiers

• Keywords

• Data Types

• Constants

• Variables

• Expressions

• Statements

• Symbolic Constants

• Operators

• Comments

Page 35: Chapter 2 C Fundamentals

Operators

• It is an is a symbol that operates on a certain data type.

commonly used operators are :

• Arithmetic operator

• Relational operator

• Logical operator

• Assignment operator

• Conditional operator

• Comma operator

• Unary operator

• Bitwise operator