Home Education C Programming in More Details

C Programming in More Details

0

In this blog, we are going into more details, here we will learn to get Inputs, Loops, Functions, and also even try to do some C programming.

Let’s start with getting Inputs and displaying them as Output on the screen.

Do you completed our previous course C programming Language?

Here’s What you can find in this blog:

  1. Getting Input and Displaying it
  2. Operators
  3. Control Statements
  4. Loops
  5. Conclusion
  6. FAQ

Getting Input and Displaying it

Before getting into deep, do you check our previous blog C Basics.

As I said before, in the previous blog, to use the Input and Output functions we have to use stdio.h.

To get input from the user-side we use scanf and to display output to the user we use printf.

Let’s talk on Printf:

Printf() :- Display 

To get input in a program we use printf function, where () is the sign of ‘function’, that means printf function is equal to printf().

Whatever we have to print/display, we have to put that in the braces () and have to write between the double quotes ( “ ” ). If you want to write your name or anything, you just have to use printf with two braces and the text you want, surrounded by double-quotes.

For example, printf(“Hey, Jimmy.”); or printf(“djdlkfjfkdfjdk 123 @() _ kjksjfdkljf”);

#code for the above example

//to display hey, jimmy
#include<stdio.h>
void main()
{
 printf(“Hey, Jimmy”);
}

Now, let’s talk about how to get input from the user and display it.

Scanf() :- Input 

As the name suggests, scanf() is the function for getting an input – text/number, that is provided by the user. 

If I am writing a program to calculate the radius and want to get the radius from the user to calculate, then I have to specify which data type and the location, it’s going to be stored.

For example,

scanf(“%d”, &radius);

Let’s look in details,

scanf()%d&radius
This is the Input function.Above is the 1st parameter. We used this because %d is the reference for ‘ I(user) am entering an Integer value.&(ampersand) used to store the scanf value to the location, i.e. Radius.

code for the above example

//Getting input from the user
#include<stdio.h>
void main()
{
int radius, result;  // here we declare two integers that are going to be used in this program.

printf(“Enter radius:”);  // here we display instructions for the user.

scanf(“%d”,&radius); // getting input and storing the value in radius(variable).

Result = 3.14 * radius * radius;  // Here we calculate the radius( r2) and store the value in result.

printf(result); //displaying the result value to the user that we calculated in the above statement.

}

You can also use more than one input in a single statement like:

scanf(“%d %f”, &roll_no, &marks);

Here in the above statement, we used two data types and two storing locations i.e. %d(integer) is going to be stored in roll_no variable and %f(float value.i.e. Decimal value) is going to be stored in marks.

Operators in C programming

Here we are going to learn to use the most common operator.

OperatorDescription ( use )
+, -, / . *Addition, Subtraction, Multiplication, Division
%Modulo Division (It returns the remaining value after the first division.Ex.12 % 8 =4Because after the first division 12/8, the remaining value is 4.7/5 =2
><>=<=Greater thanLess thanGreater than or equal toLess than or equal to
=Assignment operator. ( To assign a value )( we used this to store value into a variable )
&Address ( we used in scanf )
!Logical Negation
++ —Increment( by one )Decrement( by one )
==!=Equality (to check something is equal or not)Inequality ( not equal to )
&&||AND (Bitwise)OR (Bitwise) 
?:Conditional

Control Statements of C programming

Normally, the statements of a program are executed from the first to the last. Sometimes, we need to perform the execution of the program in a way that, if our condition meets then only the statements executed otherwise it will skip that. Even sometimes, we require some statements that need to be executed repeatedly.

This involves decision-making and looping.

If, Else Statements

Take a little time to imagine how you are going to create a calculator. If you are thinking that I will create a ‘+’ button and when someone chooses it then it will add then you are going in the right direction. It’s funny. I know but this is how you are going to create a calculator i.e. from scratch to the best. Just like I said we have to perform addition, but before that, we have to link it with the user’s choice i.e. + button.

We talked about the calculator but we are not going to create it here, it will be in the examples section i.e the last topic of this page.

But before, we need to learn the basics.

Let’s go deep.

  • ‘if-else’ statements

If statements are the statements with a condition i.e if it is satisfied then only it will perform the execution just like a bouncer outside a pub asking you ‘ are you 18+’, if you said yes(True) then only you get entry otherwise(False) you got kicked.

Let’s understand ‘if’ statements with a c programming example:

Take an example of a Birthday wishing program, to make it basic we are just going to accept ‘y’(yes) or ‘n’(no).

#include<stdio.h>
void main()
{
 char result;
 printf("Is it your Birthday Today?\n Press y to Yes\n Press n to No\n");
 printf("\nEnter your choice:");
 scanf("%c",&result);
 if(result == 'y')
 {
  printf(" OMG! Let’s Party, but with your money.\n");
  printf(" Happy Birthday…");
 }
 else
 {
   printf("Not a problem. Happy Birthday in Advance");
 }
}
  • Using only ‘if’

If you have some conditions and don’t want to use the else, then you can skip the else part.

  • Nested ‘if-else’ Statements

Let’s start this with an example, this time we are going to use the Happy Birthday program again. What if we add a gift to our program once the user says ‘yes’.

Here’s the modified code:

#include<stdio.h>
void main()
{
 char result;
 printf("Is it your Birthday Today?\n Press y to Yes\n Press n to No\n");
 printf("\nEnter your choice:");
 scanf("%c",&result);
 if(result == 'y')
 {
  printf(" OMG! Let’s Party, but with your money.\n");
  printf(" Happy Birthday…");
  printf(“Do you want gift from me:”);
  scanf(“%c”, &result);
  if(result == ‘y’)
  {
   printf(“Ok! Wait “);
   }
  else
   printf(“
 }
 else
 {
   printf("Not a problem. Happy Birthday in Advance");
 }
}

Switch statements

Whenever we want to make decisions among multiple alternatives, nested if-else statements can be used but the structure will become very complicated and also difficult to read. For this, we are going to use the built-in function i.e the Switch statements.

Let’s understand in detail with examples.

Format:

switch(expression)
{
 case const-expr1 : statement;
 case const-expr2 : statement;
 case const-expr3 : statement;
.
.
.
 default: statement;
}
  1. From the first line, switch(expression), the switch is the keyword, so we have to use it as it is. The expression contains a variable which states to which variable we are going to execute decision making.
  2. { } contains the code for the decision making.
  3. case is also a keyword to define the decision-making statement.
  4. const-expr1: is the label for the case and must end with a colon(:).
  5. Statements; are the usual statements where we are going to write the code.
  6. … states that there are no limits to the cases.
  7. default: statements; default is a keyword(which is optional), as the name suggests if no value matched the case expression then the default statement going to execute.

Note: All case expressions must be different.

Break & Continue (C programming)

Break is a built-in variable to exit a control structure/statement. When the break is called, the cursor of the program is transferred to the closing brace without going further for the statements in the control structure.

For example, 

Let’s execute the above switch code logically,

If a number matches case 1 then after the execution of the statements, it is going to traverse the next case that is case 2, then case 3, and so on, until the default case.  It means we are going to traverse every case and we don’t want that.

To solve this above problem we have break; keyword.

So, if we use the break keyword then,

case const-expr1: statement;

break;

Program for switch statement using break;

//displaying numbers using the switch statement
#include<stdio.h>
void main()
{
 int num;
 prinf(“Enter a number between 1 and 3:”);
 scanf(“%d”,&num);
 switch(num);
 {
  case 1: printf(“you entered 1\n”);
  break;
  case 2: printf(“you entered 2\n”);
  break;
  case 3: printf(“you entered 3\n”); 
  break;
  default: printf(“Out of range.”);
 }
}

Here we used the keyword break. 

Let’s go deep into the program to get the full explanation.

The above program is a program to display numbers, however, the user going to type using switch.

At first in the code section, we declare num as a variable to hold integers.

After that we print and accept the value as num.

In switch expression, we used num, in other words, if we have to print the number that the user entered, then we have to know the number first. For that, we assist the switch() by the variable num, so that, it will check it with the cases inside it and perform the operations of statements.

Suppose, I am the user who typed 2 and pressed the Enter button.

Now, after storing 2 in num, the compiler goes for the switch(num), here the switch is going to match the case expression(case 1, 2, 3) with 2 and send the compiler to case 2. As per the statement of case 2, it is going to print ‘ You entered 2 ’ and break(exit) because there’s no code after the end-brace of switch().

If I entered 5,10,100,113 etc on the console, which is not in the case, then the switch is going to perform the default case i.e. ‘Out of range

Loops in C programming:

Image created through Canva.com

The loops are the block of statements that are executed repeatedly. The statements are going to repeat until some condition is satisfied.

While Loop

It is the simplest loop structure.

Syntax:

while(expression)
statement;

Example:

//program to accept numbers and multiply them until the user enters a negative no. or 0.
#include<stdio.h>
void main()
{
 int num, res=0;
 printf(“Enter a number:”);
 scanf(“%d”,&num);
 while(res>=0)
 {
  res=res +num;
  scanf(“%d”,&num); // we are going to accept numbers until the conditions met
 }
 printf(“\nThe result for multiplication is: %d”, sum);.
}

So, how it works:

  1. At first, the compiler enters the loop only if the condition is met (condition is res>=0). Although, if we enter 0 or less than 0 our condition states it as false, which means the code of while is skipped.
  2. After entering while loop, the compiler is going to traverse all the statements, and after the last statement of the while loop, the compiler again going to check for the condition, if it meets then repeats.
  3. If we don’t have a statement that refers to the condition(or don’t have a stop condition) then an infinite loop will be performed by the compiler.

If you want to stop, then unfortunately you have to force close it.

Want to try:

while(num>=0)
{
 printf(“THIS IS AN INFINITE LOOP”);
}

Do-while Loop 

It is the opposite of while loop in other words it is exit controlled.

This means the condition is on the bottom of the loop.

Syntax:

do
{
 Statements;
}while(expression);

The use of this loop is that the compiler is going to evaluate all the statements(enters the loop) and at the last if the condition meets then only it will execute.

Thus this loop is mostly used for debugging and for analysis.

Ex.

#include<stdio.h>
void main()
{
 int num, res=0;
 char choice;
 do
 {
  printf(“Enter a value to multiply”);
  scanf(“%d”,&num);
  res+=num; // alternative of res=res+num;
  printf(“Do you want to multiply one more no.:-”);
  scanf(“%c”,&choice);
 }while(choice==’y’);
 printf(“The sum is %d”,sum);
}

For Loop- C programming

The for loop is a very flexible, powerful and commonly used loop in c.

It’s little bit hard, but if you understand how it works then probably you are going to use this(for) loop most of the time.

Syntax:

for(initialization;condition;increment/decrement)
Statement;

How it works:

Since the initialization part is executed only once, at the start of the loop. Hence, the expression is used for the initialization of the variable that we used for condition and increment/decrement.

Then after the initialization, the compiler checks for condition, if the condition is met then only the compiler goes forward otherwise it will skip. The condition is going to check after every repetition. The compiler is not going to traverse the increment/decrement for the first time.

After the initialization, the compiler traverses for condition and then goes for the code.

 

The sequence of the compiler when entering the loop for the first time is :

Initialization →  Condition

That’s it, as I said above, Increment/Decrement is not going to be traversed by the compiler for the first time.

After checking the condition, the compiler executes the statements and goes back to the for loop expressions, but here’s a twist(it is advanced):

When the loop goes back to expressions for its repetition, then the compiler will go for the Increment/Decrement and then the condition.

For repetitions:

Increment/Decrement  → Condition


In simple words, when the compiler enters the loop it executes Initialization, then Conditions, and then goes for the statements. Increment/Decrement is not going to be traversed for the first time.

For repetitions, firstly the Increment/Decrement part is executed and then the condition and then the statements.

Note: 

  1. The initialization part is only for the first execution, thereafter no use of it.
  2. If the variable is already initialized in the code, then there is no need for initialization, just refer to the variable there.

Example

//program to print numbers until 100
#include<stdio.h>
void main()
{
 int n;
 printf(“ Displaying no. 1 - 100\n”); // \n is used for new line
 for(i=1;i<=100;i++)
  printf(“%d\n”,i);
}

Note: Using braces( { } ) in control statements(if-else) and loops(while, do-while, for) are optional only if there is only one line statement. Otherwise, if there are more than 1 line statements then you have to use the braces.

Conclusion

You still have to learn much more to create something.

By the way, here we just covered the basics, if you want to switch programming language after completing the C programming language, then you easily can.

Because understanding the basics means you are ready to face simple real-world problems. Hence, if you want to learn more through solving problems then I especially recommend the Codchef, Hackerrank.

In these websites, you find problems for all the language from C programming to Python programming. Also, you can get a job from these sites.

Hey you done with C Basics. Do lots of Practice.

Btw, want to start Android development? We have something for you.

Learn the basics of Java Here :- Java Programming FREE | Basics

FAQ

Q1. What makes you a good programmer?

Question is hard to answer but you should have a great logic.

Q2. Do all programming examples are same?

May be yes or may be not.

Q3. Where can I apply C programming?

To create Drivers and for hardware projects.

Q4. What software do I use?

Codeblocks, Turbo C

Q5. Is programming hard?

No it’s just Logic that you feels hard to implement.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version