chapter 2 c fundamentals
DESCRIPTION
Fundamental IdeasTRANSCRIPT
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
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
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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
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
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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 -------
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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];
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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
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.
• Multiple if statement. • It is an error-prone statement.
Switch Statement :
#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.
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; }
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; }
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);
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.
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.
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*/
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; }
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.
Null statement
• Null statement consisting of only a semicolon and performs no operations.
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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(); }
C Fundamentals
• Identifiers
• Keywords
• Data Types
• Constants
• Variables
• Expressions
• Statements
• Symbolic Constants
• Operators
• Comments
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