How to get the following output in C programming [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
A logical question asked in the aptitude test.
if(______)
{
printf("Hi");
}
else
{
printf("Hello");
}
What condition should be provided in place of _____ to get the following output ?
HelloHello

You do not need to give control to else twice. All you need is a false condition with a side effect of printing the other "Hello", for example
if (printf("Hello") == 0) // this condition is false, because printf returns the number of chars written
{
printf("Hi");
}
else
{
printf("Hello");
}

Related

Why does this C program give me an missing integer from an For loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 months ago.
Improve this question
I am trying to get the value of "w" but when I do the For loop ,it only gives me 4 results:
Problem:
https://imgur.com/a/F9wPKF7
Code:
#include <stdio.h>
#include <math.h>
int main() {
int x,e,w;
float a = 2.5;
for (x=1; x<6; x++) {
if (x>a) {
w = x*cbrt(x-a);
}
else if (x=a) {
w = x*sin(a*x);
}
else if (x<a) {
w = pow(e,(-a*x))*cos(a*x);
}
printf("%d ",x);
}
return 0;
}
Output:
2 3 4 5
I know in this program i dont search for the value of w but I wanted to see why does it give me only 4 number instead of 5? why is the one missing and ow can I solve it? Thank you
else if (x=a) should beelse if (x==a)

Using Regular Expression to extract word in a string in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to extract a small substring from a string of words in c. I did it in a previous program written in python using this code:
out_block = "".join(re.findall(r'.FT off(.*?).FT on',finaltext,re.DOTALL))
The code extracts all the characters in between .FT off and .FT onand passes it to out_block as a string.
I wanted to know how to do the same using regular expressions but in C
How would I convert this code to a C code that does the exact same thing?
Basicly you do this:
#include <sys/types.h>
#include <regex.h>
regex_t finder;
if (0 != regcomp(&finder, "regex-string", 0)) { error handling }
You might need to adjust your regex string above.
After that you can do any number of times:
regmatch_t match[N_MATCH];
if (0 == regexec(&finder, "haystack", N_MATCH, match, 0)) {
process matches
} else {
no match
}
Clean up using regfree and get error messages (in regcomp) using regerror.
For more details, consult man page regex(3).

Easy C Program While loop Not Working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
hey im lost on why this loop doesnt work it all seems right but nothing inside the while works please help the rest of the code is in other files if you need them i can post them
#include <stdio.h>
#include "weatherstation.h"
int dunits = METRIC;
void main(void)
{
char test;
InitializeWeatherStation();
while(1);
{
UpdateWeatherStation();
printf("Enter m for Metric units, b for British units, or q to quit");
scanf_s("%c",&test);
if(test == 'm')
{
dunits = METRIC;
}
else if(test == 'b')
{
dunits = BRITISH;
}
else if(test == 'q')
{
return;
}
DisplayWeatherData(dunits);
}
}
while(1);
{
something;
}
is exactly the same as:
while(1)
{
}
{
something;
}
In other words, what you have there is an infinite loop followed by a scoped block of code (which will never be reached).
Get rid of the semicolon and it should fix that particular problem.
You must not end the while(1) with a semi-colon dude. Because that's a null statement you wrote in there.

How does this program work C? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
void fade(ImagenBMP *imagen1, ImagenBMP *imagen2, int f) {
float v = (float)f/255;
for (int i = 0; i < imagen1->tamanyo; i++) {
imagen1->datos[i] = (imagen1->datos[i] - imagen2->datos[i])*v + imagen2->datos[i];
}}; //end of fade
It's supose to fade two images into a single one.
If you rewrite the equation, you will end up with:
imagen1->datos[i] = v*imagen1->datos[i] + (1-v)*imagen2->datos[i];
This is how the blend works. You are specifying how much of the first image (out of 255 parts) should be included in the function parameter f, 255-f parts will be included from the second image.

How to use kbhit and getch (C programming) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm trying to create a function that will printf a certain string if the user presses any button on the keyboard EXCEPT for capital P, if the user presses P then it will break the loop.
However I don't think I'm using _kbhit and _getch properly. I use the number 80 because that is the ASCII symbol for 80....sorry for any confusion
void activateAlarm(int channelID) {
int key = 0;
while(temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit
||temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) {
beep(350,100);
if (_kbhit()) {
key = _getch();
if(key == 'P');
break;
}
}
}
No need to explain, the code talks better :
#include <conio.h>
// ...
printf("please press P key to pause \n ");
int key = 0;
while(1)
{
if (_kbhit())
{
key =_getch();
if (key == 'P')
break;
}
}

Resources