I am fairly new to C and I am having trouble using scanf with pointers. I have been told to get user inputs for 3 int and 1 char values and then print them back out using pointers.
This is the best I could come up with so far:
int a, b, c;
char d;
int *x = &a;
int *y = &b;
int *z = &c;
char *e = &d;
scanf("Enter 3 Ints and 1 Char:%d %d %d %c", x, y, z, e);
printf("The numbers are:\n");
printf("%d\n %d\n %d\n %c\n", a, b, c, d);
return 0;
When I enter the values the following is printed out:
2 3 4 c
The numbers are:
32708
-613084440
32708
�
Again, I'm very new to programming so I apologize if this is a stupid mistake or something obvious that I have missed.
You are not checking the return value of your scanf, otherwise you would know that it returns 0, as in 'no elements read'.
Your scanf expects you to write exactly what you are putting in there, so, if you entered Enter 3 Ints and 1 Char:2 3 4 c, it would probably work.
What you want, however, is this:
printf("Enter 3 Ints and 1 Char: ");
if (scanf("%d %d %d %c", &a, &b, &c, &d) != 4)
printf("Invalid input detected\n");
else
printf("The numbers are:\n%d\n %d\n %d\n %c\n", a, b, c, d);
The first line will print the prompt to the console, the second will read the values into variables.
There is no need to create separate pointer variables for this purpose.
Related
Actually, I'm learning C langage and I have written a programm
Enter the value of 2 numbers as shown below.
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter two numbers to add\n");
scanf("%d%d", &a, &b);
printf("Sum of the numbers = %d\n", c);
return 0;
}
But if I enter an alphabet I'm getting some 1522222 numbers. Instead of
this I want it throws an error as invalid input if I type alphabet ie a,b,c.
How could I do that?
You can check the return value of scanf. If it is successful, it should return 2, since you're reading two values. If you get anything else, you know that the input is incorrect. Try this:
if (scanf("%d%d", &a, &b) != 2)
printf("Invalid input type!\n");
else
printf("Sum of the numbers = %d\n", a+b);
On a different note, you don't initialize c anywhere, so printing it is undefined behavior. You don't even need c for this, you can just print a+b instead.
I want to calculate sum of arithmetic progression in which we have to take 3 variables from user. a=first number, b= step size/increment, c=length of sequence.
If there are more than 1 test case , say three, then I have to scan a,b,c three time. How to do this?
E.g scanf (" %d %d %d", a,b,c); 3 times without affect initial values in first test case.
If you know no of test cases read it first and store it in a variable.
int calculate_ap(int a, int b, int c)
{
//Implement function to calculate Arithmetic progression and return the result
}
int main()
{
int test_cases = 0;
int a, b, c;
scanf("%d", &test_cases); //Reads no of test cases
while(test_cases--)
{
scanf("%d, %d, %d", &a, &b, &c); //read A, B, C
printf("%d\n", calculate_ap(a, b, c));
}
}
Hope this helps.
I was trying to find the sum of 5 numbers (in C Language) using tutorials from "thenewboston" on Youtube. My code is:
int main(int argc, char *argv[]) {
int a, b, c, d, e;
int array[5]={a, b, c, d, e};
int sum=0;
int i;
int j;
printf("Enter your 5 numbers: ");
scanf("%d, %d, %d, %d, %d", &a, &b, &c, &d, &e);
for (i = 0; i < 5; i++){
sum+=array[i];
}
printf("The sum of 5 numbers is:%d",sum);
return 0;
}
But the weird thing was, no matter what 5 numbers I entered, I always got the sum as 48.
Either discard variables a, b, c, d, e and the array remains or vice versa
Remove variables on your first printf:
print("Enter 5 numbers: ");
Don't put variables when you did't use them.
When you put scanf as scanf("%d, %d", &var1, &var2);, you must also input the same format as
Enter 5 numbers: 10, 20
Working example(more efficient with array):
int main() {
int input[5];
int sum;
printf("Enter 5 numbers: ");
scanf("%d, %d, %d, %d, %d", &input[0], &input[1], &input[2], &input[3], &input[4]);
int i;
for (i = 0; i < 5; i++) {
sum += input[i];
}
printf("The sum is %d", sum);
return 0;
}
You aren't storing a, b, c, d, or e into the array array. You need to store them in the array after you read them in.
Your declaration of array doesn't create an array of pointers to your variables - it creates a single pointer to a contiguous block of five integer fields. You can't update those array fields by just using the addresses of a, etc., since your array doesn't point to them.
The most obvious, clear, and simple way to store them in the array (which I recommend) is:
array[0]=a;
array[1]=b;
array[2]=c;
array[3]=d;
array[4]=e;
Do this just before the beginning of your for loop.
Let me start by saying that I am brand new to programming. I am a math major and I don't know very much about the computer programming world. That said, my assignment wants me to enter an integer and print it out as the corresponding ASCII character, decimal, float. I am ok going from a character to the corresponding integer, but the reverse has me puzzled. Please help!
#include <stdio.h>
int main (void)
{
char E = 'E';
char e = 'e';
char D = 'D';
char d = 'd';
int m;
printf("\nPlease enter an integer : ");
scanf("%d", &m);
printf("\nThe number as a character is : %c", E, e, D, d);
return 0;
} // main
This is what I have so far, but I know it's wrong.
I am not exactly sure what you want to achieve.
printf will treat a parameter as a type you want using % format specifier.
So if you have entered some value which is interpreted as signed decimal integer you can print it treating as a different type with the printf function.
If you want your m variable being printed as character do:
printf("The number as a character is %c", m);
If you want to display it as a float, use %f.
Here is some reference:
http://www.cplusplus.com/reference/cstdio/printf/
You're probably looking at the printf formats
#include <stdio.h>
int main (void)
{
int m;
printf("Please enter an integer : ");
scanf("%d", &m);
printf("The number as a character is : '%c' %02X %o %d %f", m, m, m, m, (float) m);
return 0;
}
I want to store a few numbers in an array. While giving the input though, I cannot use an enter to move from one position to the next. I need to use a space.
Eg:
input:2 3 4 5
instead of:
input: 2
3
4
5
Can someone please tell me the correct syntax?
You can do it like this
#include <stdio.h>
int main(void){
int a, b, c, d;
printf ("Enter 4 integers: ");
if (4 != scanf("%d%d%d%d", &a, &b, &c, &d))
printf ("Invalid input\n");
else
printf ("You entered %d %d %d %d\n", a, b, c, d);
return 0;
}
Program output:
Enter 4 integers: 2 3 4 5
You entered 2 3 4 5
Be aware though that if you only enter 3 integers you won't get the error message, because the program is still waiting for a fourth. You get the error if the inputs were not integers.
And note that you have to terminate the input with Enter key.
You can just go ahead with scanning an integer like
int a[5];
for(i=0;i<5;i++)
{
if(scanf("%d",&a[i]) != 1)
break;
}
%d will consider an integer input until an newline or a space is encountered.