Variable in C not printing the given value [closed] - c

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 1 year ago.
Improve this question
Alright, so basically recently I started studying C as a hobby, and I wanted to create a small program.Everything works fine, but when I make the variable "Age" like this:
int myAge = "14";
printf("My age is%d\n", myAge);
It prints out 1642.
I have tried switching to
printf('My age is%i\n', age);
But it did the same.
I also tested changing the number to a string but it obviously failed, because this isn't Python.
Anybody can help?

Save time, enable all compiler warnings.
Perhaps receive a warning like:
warning: initialization of 'int' from 'char *' makes integer from pointer without a cast [-Wint-conversion]
int myAge = "14"; sets myAge to the address of the string literal "14".
Instead, initialize with an int.
int myAge = 14;

This is a fixed version of your program. Just declaring the age variable as an integer is enough to fix it.
#include <stdio.h>
int main() {
int age = 14;
printf("My age is %d", age);
}
Friendly note:
C is not like Python. Arrays don't work the same, strings don't exist, and programming in C is fundamentally different in every way. I would advise that you definitely take a course rather than trying to teach yourself from scratch.

u should write int myAge = 14 , writting 14 in double quotes means its a string (thx HAL)

We use double quotes when we have string and single quote when we have character but 14 is an integer we should write int myAge=14; or if u want "14" u should write char myAge[2]="14"; then printf("%s",myAge);

Related

Two same type of C codes giving different output . Need help understanding this [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 10 months ago.
Improve this question
int n;
scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 5.
n=scanf("%d",&n); //input from keyboard=5
printf("%d ",n); //gives output 1.
Need help understanding the second one.
scanf returns the number of fields it assigns. So, first, it assigns n as 5. Then it returns 1 as it has assigned to only 1 variable. Then, n takes that value.
That's not the correct way to use scanf(). In C standards scanf() always returns the number of conversion it did from stdin, in your case only 1 conversion is done, hence 1 is assigned to n.
In C, if you want to modify a parameter's value inside a function, you need to pass the address of the variable (&var) to that function, and the parameter of the function should be a pointer (type *). Then, you need to de-reference the pointer like (*ptr).
Now, we need to change the value of n inside scanf() function, that's why we need to give the address of n.

Program with array to find a sum in c [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 4 years ago.
Improve this question
I was thinking of a code to find the sum of numbers.
So i wrote bellow code :
#include <stdio.h>
#include <math.h>
int main()
{
int n,i,s=0,a[100];
printf("enter number of numbers");
scanf("%d",n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
s=0;
for (i=1;i<=n;i++)
{s=s+a[i];}
printf("sum is%d\n",s);
}
But in the output it shows segmentation error . I.e.
So whats the mistake?
So this line:
scanf("%d",n);
should be replaced by
scanf("%d",&n);
Explanation:
scanf() reads data from stdin according to format and store data in the location pointed by next additional argument. this case, format is %d means we want to read an integer number and this integer number will be stored in the location of n. The & operator is to get the location of a variable in C.
Use this :
scanf("%d",&n);
The reason :
You MUST put & in front of the variable used in scanf. The reason why will become clear once you learn about pointers. It is easy to forget the & sign, and when you forget it your program will almost always crash when you run it.
Examples :
scanf("%d %d", &a, &b);
printf("%d %d", a, b);
As a and b above are two variable and each has their own address assigned but instead of a and b, we send the address of a and b respectively. The reason is, scanf() needs to modify values of a and b and but they are local to scanf(). So in order to reflect changes in the variable a and b of the main function, we need to pass addresses of them. We cannot simply pass them by value.
But in case of printf function as we are only going to print the values of the variables in output console, there are no changes going to be made in variable a and b’s values. So it is not required to send their addresses.

C Programming Book Error or Programming Error? [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
int main()
{
// Initialize & Declare variable
int m = 5;
// Allocates memory for storage of an integer variable
int *itemp;
// Stores memory address of variable m in memory address itemp
itemp = &m;
// Notice after declaring pointer you don't need to reference it as a pointer
// asterick is also known as indirection operator
// indirect reference: Accessing the contents of a memory cell through a pointer variable that stores it's address
// We can rewrite the contents in the memory cell as such
*itemp = 35;
printf("%d",*itemp);
// Doubles the value of m
*itemp = 2 * *itemp;
printf("%d",*itemp);
return 0;
}
It's returning 3570 instead of 70, which is what the book says it should be returning. What am I doing wrong?
The program is correct. It is printing what it is coded to print.
To clarify,
You have two printf()s, printing 35 and 70.
You don't have a "seperator" [for example, a newline (\n)] in your printf()s to distinguish the outputs of two print statements.
Result: You're seeing the final output 3570 as the combination of the output from two print statements, 35 and 70.
Solution: Add a \n or \t at the end of the format string supplied in printf() to add a visual seperator after each printf() to avoid confusion.
Just do
printf("%d\n",*itemp);
You are seeing 35 and 70 as output in the same line either add a space between them or a newline.

C concepts on arrays [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 9 years ago.
Improve this question
int main(){
int arr[2]={30,40};
printf("%dn",i[arr]);
return 0;
}
I found this question in an examination and the given solution is
40
But I think it should give an error since i is undefined.
Or, may be I am missing something.
Please explain me how 40 is the correct answer?
Thanks in advance.
You are correct, the code is wrong. Likely, it is a typo, and the intent was either to define i or to use 1[arr].
Probably it is an error, since i is not defined.
Also, probably the intention of the exercise is to take advantage of the fact that in C, you can write v[ i ] in order to access the #i element of a vector v, or i[ v ].
Both forms are equivalent.Since v[ i ] is translated as *( v + i ), there is actually not any difference between that and *( i + v ), which is what i[ v ] is translated for. This is not a common use, but is nonetheless valid.
Arrays in C, from Wikipedia
In this specific example, 1[arr] would return the expected answer.
I just wonder why they chose 40 instead of 42.
Hope this helps.
i is probably supposed to be given as 1, either in the spoken part of the examination, or in a part that is missing. As written, the question is of course inapplicable since it doesn't compile.
The real point of the question is to test whether the applicant understands that array[index] is equivalent to index[array] and (presumably) why.
In C array[index] = *(array + index) = *(index + array) = index[array]. Assuming i to be 1 (otherwise behavior is undefined), 1[arr] is equivalent to arr[1] and it contains value 40.
Hmm, let's see shall we...
matilda:~ jeremyp$ cat > foo.c
int main(){
int arr[2]={30,40};
printf("%dn",i[arr]);
return 0;
}
matilda:~ jeremyp$ cc foo.c
foo.c:4:5: warning: implicitly declaring library function 'printf' with type 'int (const char *, ...)'
printf("%dn",i[arr]);
^
foo.c:4:5: note: please include the header <stdio.h> or explicitly provide a declaration for 'printf'
foo.c:4:18: error: use of undeclared identifier 'i'
printf("%dn",i[arr]);
^
1 warning and 1 error generated.
Yes indeed, i is undefined. You either need
int i = 1;
before that statement or it isn't an i, it's a 1. Let's try that...
matilda:~ jeremyp$ cat >foo.c
#include <stdio.h> // Stops the warning
int main(){
int arr[2]={30,40};
printf("%d\n",1[arr]); // Also added a \ so we get a line feed.
return 0;
}
matilda:~ jeremyp$ cc foo.c
matilda:~ jeremyp$ ./a.out
40
That works now.

can literals used without variable in c? [closed]

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
This question is little bit weird.
Just out of curiosity, is it possible to use the literals without variable assignment in C ?
Generally, we do like as follows,
#include<stdio.h>
int main()
{
// Here we are using the literal '7' and assigning it to variable 'a' which will hold it in some address space
int a = 7;
printf("Hello : %d\n",a);
return 0;
}
So, is it possible to use the literals without variables ?
Thanks in advance.
If you mean something like this:
printf("Hello : %d\n", 7);
then yes, it's fine.
Then you don't even need %d
printf("Hello : 7 \n");
yes , they are used all the time , in return statements ,as constants etc . , even in your printf statement the %d is a placeholder for an int( or integer) , i.e. you can always replace a by 7 , however this approach of defining constants is favoured as if you will be using some number many times in your code , (ex. pi=3.14 in a math program) , its value needs to be edited at one place only and variable naming provides a easier reference system , and is hence more common practice .

Resources