why doesn't this program in C language print anything? [closed] - c

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 2 years ago.
Improve this question
why doesn't this program print anything?
int main(){
int a = getchar()-'0';
getchar();
int b = getchar()-'0';
int vsota = 0;
vs = (a+b)%10;
putchar(vs);
printf("\n");
}
I put in the numbers 7 and 9 and it was supossed to outprint 6 but it does not.

putchar(vs); writes the character whose code is the value in vs. The value in vs is 6. So it writes the character with code 6. That character is not the digit “6”. You do not see anything because it is a “control character” with no visible appearance. To write the character for the digit whose value is in vs, use putchar('0' + vs);.
Also, fix this:
int vsota = 0;
vs = (a+b)%10;
That would not have compiled, so presumably you made a mistake when entering code into Stack Overflow. Use the same name in both places.

because you try to output non printable character
int main(){
int a = '1'-'0';
int b = '6'-'0';
int vsota = 0;
vsota = (a+b)%10;
putchar(vsota + '0');
printf("\n");
}
https://godbolt.org/z/NAyrNn

Related

How to sum 2 numbers in C language? [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 4 years ago.
Improve this question
I am new to programming. I am learning how to sum in C language. Please see below code, What am I missing? Why its giving error?
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3, d = 0;
d = a + b + c;
printf(d);
return 0;
}
The "f" in "printf" means "formatted". The first parameter you pass to it has to be the format that you want to print with, not what you want to print. In this case, it seems to me that you likely want to:
printf("%d\n", d);
The %d means that printf should interpret the second parameter as a signed integer (which d actually is). The \n adds a newline (and usually flushes the buffer).
You can learn more about printf and its format by googling for it, or reading a man page about it, or its page in a compiler help file.
you are printing a integer value. In order to print a integer value you have follow this way...
printf("%d",d); // for integer it's %d
// so your progam should looks like this
#include <stdio.h>
int main() {
int a = 1, b = 2, c = 3, d = 0;
d = a + b + c;
printf("%d\n", d);
return 0;
}

Filling an Array in C with 0 [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 5 years ago.
Improve this question
Ive got a problem with this code, im trying to add up array1 with array2.
I enter the numbers for array2 by Command line parameters.
When i enter 10 numbers it is working but when i add less than 10 I get an Memory access error.
My question is now: how do i fill up the missing array fields with the number 0? For example : I enter 9 numbers and the 10th field should be 0.
You are not checking how many command line arguments are passed, and when you index into the command line argument array, you will get an out-of-bounds error.
In you addiren function, you should take advantage of the argc that is passed and used that in your for loop limit.
#include <stdio.h>
#include <stdlib.h>
int addiren(int argc, char**argv){
int array_one[10] = {0,1,1,2,3,5,8,13,21,35};
int array_two[10] = {0}; //Quick way to set the array to all zeros
int array_three[10] = {0};
//Set array_two with your cmd-line args, notice the use of argc
for(int i = 1; i<argc && i<=10; i++){
array_two[i-1] = atoi(argv[i]);
}
//Add array_one with array_two to array_three
for(int i = 0; i<10; i++){
array_three[i] = array_one[i]+array_two[i];
}
//Return an int since that's what the function return type requires
return 0;
}
Hope this helps!

Address is not incrementing? [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 6 years ago.
Improve this question
It is said that,in c,b++; is equal to b=b+1; if this is the fact test++ in my code why generate a compile time error.
test+1 is working well but test++ is not working.but why?
#include<stdio.h>
int main(void)
{
char test[80]="This is a test";
int a=13;
for(;a>=0;a--)
{
printf("%c",*(test++);
}
}
The ++ and -- operators are not defined for arrays.
v++; would be the same as v = v + 1;. Assumed v was typed an array this would imply assigning to an array, which is not defined.
char test[80] = "This is a test";
char *p = test;
for(int a = 0; a < 14; a++)
{
printf("%c", *(p++));
}
Well, for one thing, b++ is not the same as b=b+1.
But even if it were -- I think you'll find you get a similar error if you try test = test + 1.

Can any one explain why this section of code is giving run time error? [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
I am not finding the correct output of this program.It giving run time error.
#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
no /= c;
} while(c--);
printf ("%d\n", no);
return 0;
}
It's division by zero. Since you are using a post-decrement in your loop counter c, it is becoming 0 in the last iteration.
Now that you know the reason for the run time error from the answer by #EugeneSh, here's how you can fix it.
do {
no /= c;
} while(--c); // Use pre-increment instead of post-increment.
In addition the all these answer above I just want to say it's better to check whether a number is zero before division -
#include <stdio.h>
int main()
{
int c = 5, no = 10;
do {
if(c!=0){
no /= c;
}
} while(c--);
printf ("%d\n", no);
return 0;
}
This will prevent these kind of runtime error.
Hope it will helps.
Thanks a lot.

Binary To Decimal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The program is suppose to convert binary number to decimal form. Only using scanf() and printf() library functions. Takes in a char array from user ---no prompt outputs decimal form, function must be used with parameter (char binaryString[]) after conversion result must be printed out in main. Program does not work don't think I'm converting the binary form to decimal form correctly in function binaryToDecimal since i cant use pow() I'm lost
#include <stdio.h>
#include <math.h>
int binaryToDecimal(char binaryString[]) {
int c, j = 1, decimalNumber = 0;
for (binaryString[c = 0]; binaryString[c] > binaryString[33];
binaryString[++c]) {
while (binaryString[c] != 0) {
remainder = binaryString[c] % 10;
decimalNumber = decimalNumber + remainder * j;
j = j * 2;
binaryString[c] = binaryString[c] / 10;
}
}
return decimalNumber;
}
int binaryToDecimalMain() {
int arraysize = 33;
char binaryString[arraysize];
scanf("%32s", binaryString);
printf("%d",binaryToDecimal(binaryString []);
return 0;
}
I not give you the algorithm because it's seems that you are learning how to program and it is important to you to learn to discover how to solve the problems that are given to you.But I can give you some hints:
use binaryString only to compare with '0' or '1'. Don't try to make any operations like '%' on it.
iterate on the binaryString character by character (no while inside for [this is only for this case, there some algorithm that is necessary to do something like this])
your logic to convert is on the right track
Also you should call your main function main.

Resources