what's the use of keeping ";" after if conditional in C? - c

Yesterday I was asked a question in an exam, the code is as follows:
#include <stdio.h>
int main()
{
int i=5;
if(i==5);
printf("I am printing\n");
return 0;
}
with and without ";" after if conditional I am getting the same result can anyone help me what's the use of keeping the ";" after an if conditional?

Here semicolon after if condition says there is NULL Statement to execute. It has no impact on next statements. It is rarely used. You can understand more with some changes in code. Try printf statement within if condition and change the value of i. Then see output changes;
int i=10;
if(i==5)
printf("I am printing\n");

When you put the semicolon after the if() it gets ignored and it prints the next statement like below
int i=5;
if(i==5);{
printf("I am Printing");
}
but if you try to run it with else statement it will show you an error that there is no if statement like below
int i=5;
if(i==5);{
printf("I am Printing");
}else{
printf("I am Not Printing)
}
So the main concept is when you put a semicolon at the end of any line it terminates that sentence and in your case when you're putting a semicolon after if it terminates the working of if statement

Related

Typing a unexpected input causes loop to fail (C)

While testing a program(in C) to prevent any bugs, I clicked \ by accident when the program asked for an integer. The while loop then starts failing. An example:
int a;
while(1){
scanf("%d",&a);printf("%d\n",a);
a--;
if(a==0){break;}
}
Whenever I type a number, it is supposed to print the same number. But when I type a character, it will print out all numbers below the previous input and then starts printing all numbers below it till 1.
Can anyone give a clue on fixing this problem? Thanks a lot.
Edit: This program is just a sample of the bug, the actual program is much larger than this.
You won't need a while loop to print just the number you entered. This would be enough :
int a;
scanf("%d",&a);
printf("%d\n",a);
Or if you want to print from the input number till One :
int a;
while(1){
scanf("%d",&a);
printf("%d\n",a);
a--;
if(a<=0){break;}
}
Use a<=0 to compare since it would not create an infinite loop even if you entered a wrong input.
In that case, you don't need a--.
int a;
while(1){
scanf("%d",&a);
printf("%d\n",a);
if(a==0)
break;
}

what happens when there are two continuous breaks in a switch statement?

#include <stdio.h>
int main()
{
int i = 0;
char c = 'a';
while (i < 2){
i++;
switch (c) {
case 'a':
printf("%c ", c);
break;
break;
}
}
printf("after loop\n");
}
What will be the output of the above code?
Does the second break mean anything?
break is a jump statement in C. It unconditionally transfers control to a different location in the code. Which means that any code between that break and the target point of the jump is unreachable, unless there's a label that allows one to reach it.
In your case there's no such label. The second break is unreachable and has no effect.
There is no use for a second break statement.
The break statement has the following two usages:
When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement.
In this case it terminates the case statement. So the second break doesn't get called. It is a useless statement.

how the break command acts in my test code

my test code is as follows:
#include <stdio.h>
int main(){
char c;
while ((c=getchar())!=EOF){
if(c>='A'&& c<='Z'){
c=c-'A'+'a';
putchar(c);
}
//else break;
}
return 0;
}
without the else break command, the while loop went forever, but when I added the "else" command line, the loop only executed for once, I typed "A" it displayed "a" on the monitor and then stopped. But I thought it should be like this: if A~Z is input, then it produces the lower case letter, if the input character is not within the range, eg. a number of something, it would break the loop and execution would stop. Where am I made the mistake? I have tried to add curly brackets after "else" around "break", but the result remained.
The getchar function will read EVERY char from stdin, even the newline character from the "Enter/Return" key.
When you enable your else part, your code also reacts to the '\n', which causes the break.
You may try to change the
else break;
by
else if (c != '\n') break;
in your code.
If you enter only A and nothing else, your program sees A and the newline character \n. The newline character doesn't match the pattern A...Z, so the break statement is executed.
You can see this by printing the code of the character:
while ((c=getchar())!=EOF){
printf("Character code: %d\n", c);
...
}
You can enter something like ABACABB to see your desired behavior.
char c;
while ((c=getchar())!=EOF){
needs to be
int c;
while ((c=getchar())!=EOF){
see http://www.tutorialspoint.com/c_standard_library/c_function_getchar.htm
IN addition your code does not say 'stop if they enter a char thats not A-Z', it says' ask them for input till they type EOF - echoing back to them when they type A-Z'. EOF is types as ctrl-D on UNix

main method not being executed

Here is a program to find prime numbers using sieve of Eratosthenes. The program is compiling but on execution, it becomes non responsive.The print statement itself is not executed. Can I know where I have gone wrong?
#include<stdio.h>
int main()
{
printf("Enter the range");
int n,i;
scanf("%d",&n);
int j;
int a[--n];
for(i=0;i<n;i++)
a[i]=i+2;
for(i=0;i<n;i++)
if(a[i])
{
printf("%d",a[i]);
for(j=2;(i*j)<n;j++)
a[i*j]=0;
}
return 0;
}
Thanks
Your program is infinite looping the first time through the loop.
When i = 0 this loop never ends:
for(j=2;(i*j)<n;j++)
Your printf call might be being buffered which means it might not actually be printed until the buffer fills up or a newline is encountered.
Try adding a newline to the end of your string or call fprintf(stderr, ...) instead (which isn't buffered).

program to find the sum of digits

I can't figure out the problem in this:
#include<stdio.h>
int main()
{
int a,b,count ;
count =0;
printf("enter the value for a ");
scanf("%d ",&a);
while(a>0)
{
b=a%10;
count=b+count;
a=a/10;
printf ("hence the simplified result is %d",count);
}
return 0;
}
There's a silent killer in your code:
scanf("%d ",&a);
The extra space in your scanf will make entering numbers harder: this will match 12<space>, but not 12. Replace the "%d " with "%d".
You do not terminate your printf() with a "\n". The output stream (stdout) is, usually, line buffered. That means that incomplete lines need not be printed unless you force them with fflush(). But there's no need for that.
Simply add a "\n" to your printf()
printf("hence the simplified result is %d\n", count);
One issue is you print the count with every loop, rather than than after the loop.
Not an issue, but C has arithmetic assignment (aka compound assignment) operators that can be more readable. For example, a /= 10 is equivalent to a = a/10.
I think the printf statement should be outside the loop.
Move the printf out side the loop. That will fix it.
Try the following:
#include<stdio.h>
int main()
{
int a,b,count ;
count =0;
printf("enter the value for a ");
scanf("%d",&a);
while(a>0)
{
b=a%10;
count=b+count;
a=a/10;
}
printf ("hence the simplified result is %d",count);
return 0;
}

Resources