C Programming For Beginner.
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 :
Subscribe to:
Posts (Atom)