Monday, 6 April 2015
C pointer to structure
Pointer to structure : Pointer which stores address of structure is called as “Pointer to Structure“.
Explanation :
sptr is pointer to structure address.
-> and (*). both represents the same.
These operators are used to access data member of structure by using structure’s pointer.
Explanation :
sptr is pointer to structure address.
-> and (*). both represents the same.
These operators are used to access data member of structure by using structure’s pointer.
Program :
#include<stdio.h> struct team { char *name; int members; char captain[20]; } t1 = {"India",11,"Dhoni"} , *sptr = &t1; int main() { printf("\nTeam : %s",(*sptr).name); printf("\nMemebers : %d",sptr->members); printf("\nCaptain : %s",(*sptr).captain); return 0; }
Output :
Team : India Memebers : 11 Captain : Dhoni
C Interrupts concept
What are Interrupts?
- Interrupts are messages to the Pentium chip.
- Interrupt is used to halt current activity and perform our requested job.
- When we generate an interrupt, the CPU suspends it current operation.
- CPU performs the job we requested, finishes our job and resumes its suspended job.
- We are interrupting the CPU, that is why, we call it interrupts!
Explanation Of Interrupt :
- Suppose we have sample C Program to execute , Consider following code
- Then Current Program execution is halted and Control is given to the IO device i.e Keyboard to Enter the number.
- As soon as User Enters the number , the CPU resumes execution of the remaining code.
- The process of halting the Program execution in between is called interrupt.
int main() { int num1,num2,sum; printf("Enter the number 1 : "); scanf("%d",&num1); printf("Enter the number 2 : "); scanf("%d",&num2); sum = num1 + num2; printf("Sum : %d",sum); return(0); }
Above program is handed over to CPU for execution then CPU starts executing this program.While Executing Following Statement CPU is waiting for input from the user –
scanf("%d",&num1);
C calling function examples
We have summarized different ways of calling C functions –
[crosslist]
[crosslist]
- Call function using Separate Programming Statement
- Calling Function from Expression.
- Inline Function Calling
#include<stdio.h>
int add(int,int);
int main()
{
int sum,num1,num2;
num1 = 10;
num2 = 20;
sum = add(num1,num2);
printf("Sum is : %d",sum);
return 0;
}
int add(int n1,int n2)
{
return (n1+n2);
}
Explanation :
sum = add(num1,num2);
[arrowlist]- Call Function by using function name followed by parameter list enclosed in angular brackets and followed by semicolon.
- We have occupied 1 statement for this function call.
- If function is going to return a value then we should preserve returned value.
- To preserve a value we call function and assign function call to any variable. (in above example)
sum = a + add(num1,num2) + c ;
Example :
[arrowlist]
[arrowlist]
- Suppose we have to add 4 numbers say a,b,num1,num2.
- We are adding num1 and num2 using function.
[arrowlist]
- We have function call as part of expression.
- No need to use semicolon after function call.
#include<stdio.h>
int add(int,int);
int main()
{
int sum,num1,num2;
num1 = 10;
num2 = 20;
printf("Sum is : %d",add(num1,num2));
return 0;
}
int Add(int n1,int n2)
{
return (n1+n2);
}
Explanation :
[arrowlist]
[arrowlist]
- We can directly call function from printf statement.
printf("Sum is : %d",add(num1,num2));
How this printf gets executed -
- Firstly we are going to call function add.
- add() function will return result of the type integer as its return type specified is Integer.
- Returned Result will be displayed in printf statement.
C if-else statement
If-else Statement in C Programming
We can use if-else statement in c programming so that we can check any condition and depending on the outcome of the condition we can follow appropriate path. We have true path as well as false path.
Syntax :
if(expression)
{
statement1;
statement2;
}
else
{
statement1;
statement2;
}
next_statement;
Explanation :
- If expression is True then Statement1 and Statement2 are executed
- Otherwise Statement3 and Statement4 are executed.
Sample Program on if-else Statement :
void main()
{
int marks=50;
if(marks>=40)
{
printf("Student is Pass");
}
else
{
printf("Student is Fail");
}
}
Output :
Student is Pass
Flowchart : If Else Statement
Consider Example 1 with Explanation :
Consider Following Example –
int num = 20;
if(num == 20)
{
printf("True Block");
}
else
{
printf("False Block");
}
If part Executed if Condition Statement is True.
if(num == 20)
{
printf("True Block");
}
True Block will be executed if condition is True.
Else Part executed if Condition Statement is False.
Else Part executed if Condition Statement is False.
else
{
printf("False Block");
}
Consider Example 2 with Explanation :
More than One Conditions can be Written inside If statement.
int num1 = 20;
int num2 = 40;
if(num1 == 20 && num2 == 40)
{
printf("True Block");
}
Opening and Closing Braces are required only when “Code” after if statement occupies multiple lines. Code will be executed if condition statement is True. Non-Zero Number Inside if means “TRUE Condition”
If-Else Statement :
if(conditional)
{
//True code
}
else
{
//False code
}
Note :
Consider Following Example –
int num = 20;
if(num == 20)
{
printf("True Block");
}
else
{
printf("False Block");
}
- If part Executed if Condition Statement is True.
- Else Part executed if Condition Statement is False.
- More than One Conditions can be Written inside If statement.
- Opening and Closing Braces are required only when “Code” after if statement occupies multiple lines.
- Code will be executed if condition statement is True.
- Non-Zero Number Inside if means “TRUE Condition”
if(num == 20)
{
printf("True Block");
}
True Block will be executed if condition is True.
else
{
printf("False Block");
}
int num1 = 20;
int num2 = 40;
if(num1 == 20 && num2 == 40)
{
printf("True Block");
}
Program Flow of If-Else Statement :
YOU CAN ALSO LEARN THROUGH OUR VIDEO
C if statement
Syntax :
if(expression)
statement1;
Explanation :
- Expression is Boolean Expression
- It may have true or false value
- It Checks whether the given Expression is Boolean or not !!
- If Expression is True Then it executes the statement otherwise jumps to next_instruction
Sample Program Code :
void main()
{
int a=5,b=6,c;
c = a + b ;
if (c==11)
printf("Execute me 1");
printf("Execute me 2");
}
Output :
Execute me 1
If Statement :
if(conditional)
{
Statement No 1
Statement No 2
Statement No 3
.
.
.
Statement No N
}
Note :
- More than One Conditions can be Written inside If statement.
- Opening and Closing Braces are required only when “Code” after if statement occupies multiple lines.
if(conditional)
Statement No 1
Statement No 2
Statement No 3
In the above example only Statement 1 is a part of if Statement.
- Code will be executed if condition statement is True.
- Non-Zero Number Inside if means “TRUE Condition”
if(100)
printf("True Condition");
Program flow of If Statement :
Thursday, 2 April 2015
What is Variable in C Programming
Variable in C Programming is also called as container to store the data. Variable name may have different data types to identify the type of value stored. Suppose we declare variable of type integer then it can store only integer values.Variable is considered as one of the building block of C Programming which is also called as identifier.
A Variable is a name given to the memory location where the actual data is stored.
Consider real time example , suppose we need to store water then we can store water in glass if quantity of water is small and Bucket is the quantity of water is large. And big can to store water having quantity larger than bucket similarly Variable (i.e Value container) may have different size for storing different verities of values.
Must Read : Fundamental Attributes of Variable
What is Variable in C Programming?
- Initially 5 is Stored in memory location and name x is given to it
- After We are assigning the new value (3) to the same memory location
- This would Overwrite the earlier value 5 since memory location can hold only one value at a time
- Since the location ‘x’can hold Different values at different time so it is refered as ‘Variable‘
- In short Variable is name given to Specific memory location or Group.
- Consider following Scenario –
Step 1 : Memory Before Variable Declaration
- Initially before declaring variable ,We have Only memory.Memory is block of bytes.Each byte is filled with random or garbage data.
Step 2 : Declaring Variable in C Programming
- Now we have declared a variable. (in this case we have declared character variable)
- Compiler Checks data type . Depending on data type it will allocate appropriate bytes of memory. (in this case Compile will allocate 1 byte because we have declared Character data type)
- It’s only declaration so , garbage value inside that address remains the same.
Step 3 : Initialize Variable in C Programming
- We have Initialize character variable now.
- After initializing value inside that memory block gets overwritten.
Conclusion :
- Variable Name always holds a single Value.
- Variable Name is user defined name given to Memory Address.
- During Second Run , Address of Variable may change.
- During second Run , Value set inside variable during current will be considered as garbage.
Wednesday, 1 April 2015
C Source Vs Object Code
Difference Between Source Code and Object Code
Source Code :
Source Code :
- Source Code is In the form of Text.
- Source Code is Human Readable.
- Source Code is Generated by Human.
- Source Code is Input Given to Compiler.
Object Code :
- Object Code is in the form of Binary Numbers.
- Object Code is in Machine Readable.
- Object Code is Generated by Compiler.
- Object code is Output of Compiler.
Subscribe to:
Posts (Atom)