Control Structure and Input-Output in C With Examples

Control Structure in C defines how the statements in the program are going to execute. A statement is a single line or instruction in a program.

Control Structure in C

Control Structure, also known as Control Flow tells that in which order each statement is going to execute. We will discuss following types

    1. Sequential Flow

It says that statements are executed in sequence order and each statement is executed exactly once.

    1. Conditional Flow

In Conditional Flow, whether execution of particular statement(s) happens or not is dependent on other statement that specifies a condition.
if, if-else, switch

    1. Iterative Flow

The flow by which user can execute particular statement(s) number of times without rewriting them, also known as Looping statements.
for, while, do-while

    1. Jump Statements

These are statements to interrupt the flow and move to other statement as per requirement.
break, continue, goto, return

Input and Output in C

C programming has multiple ways to take input and output. In our context, Input is something which user enters from keyboard and Output is the result or outcome shown by the program on console (Command prompt/Terminal).
We will discuss 2 functions, printf() for the output and scanf() for the input.
Basically functions are like workers which perform a specific task when they are used.

 

printf-scanf

printf

Parameters are passed to printf() to show output on the console in the following way.

    1. Strings

//String Output. "n" is used for next line.
printf("Hello World");
printf("n");
printf("Hello World in next Line");
    1. integer, character, float/double values

//To print int, "%d" is used
int a = 10;
printf("%d",a);
printf("n") \Next Line
//To print char, "%c" is used
char ch="f";
printf("%c",ch);
printf("n") \Next Line
//for double and float, %f is used
double d = 10.564;
printf("%f",d);

scanf

It takes input from user by keyboard and assigns it to a variable (identifier). Detailed understanding of scanf() is related to “Pointers in C” which is an advanced topic.
Here & is “AddressOf” operator, and scanf(“%d”,&a) means something like “take the value from keyboard, consider it integer (as %d is for int) and put it on the address of a“.


//int input
int a;
printf("Enter a value of a: ");
scanf("%d",&a); //It would prompt user to enter a value
printf("Value of a is %d",a);
//char input
int ch;
printf("Enter a value of ch: ");
scanf("%c",&ch); //It would prompt user to enter a value
printf("Value of a is %c",ch);

Comments help us in providing information about the code statements. The compiler ignores the comments while compilation. Let’s see how to provide comments in C programming.

  • “//” (Double slash) for single line comment
  • ” /* …..Some lines of comment here….. */ ” for multi-line comments.

/*
This is Multi-Line Comment.
Comments are ignored by the compiler.
*/
//This is a single line Comment
int a = 10; //Declaring an integer variable and assigning value as 10;

Sumary

We have covered up the conceptual understanding of control structure and Input-output in programming. It is important to understand the basic usage before going into the specific cases explained in further articles.

By admin

Leave a Reply

%d bloggers like this: