+ 1

How it's happening

#include<stdio.h> int main() { int a; a=(20,30,40); printf( "%d",a); } Output 40 ---------------- Vs #include<stdio.h> int main() { int a; a=20,30,40; printf( "%d",a); } Output 20 ------------------ And other case #include<stdio.h> int main() { int a=(20,30,40); printf( "%d",a); } Output: error ---------------- Can anybody clearly explain.

21st Aug 2020, 7:55 AM
Ayesha
4 odpowiedzi
+ 3
Your first and last Question looking same it have no changes in first Question Int a; a=(20,30,40); alway last value will be print if u put bracket around the values because it behave like stack it all values will be assign to a means a=20 first then a=30 then a=40 and c language is procedural language so last value will be 40 . But in second Question u wrote Int a; int a=20,30,40; here u assign 20 to a variable but all value won't be assign in a here u should assign like this Int a=20,b=30,c=40; int a=20,30,40; this is wrong way to define random values to single variable .May be Some Compilers will give error or warnings if you will write this . But some Compiler will give output as a 20 its depend on Compiler . But if you dont want to use more variables then you can use array in this case . Hope you understood ........Thankyou......
22nd Aug 2020, 6:10 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
The error received when running the code expalins the problem (after fixing a other case errors, remember that C and C++ are case sensitive): 40 ./Playground/file0.cpp: In function 'int main()': ./Playground/file0.cpp:7:4: warning: left operand of comma operator has no effect [-Wunused-value] 7 | a=(20,30,40); | ^~ ./Playground/file0.cpp:7:10: warning: right operand of comma operator has no effect [-Wunused-value] 7 | a=(20,30,40); | ^~
21st Aug 2020, 12:25 PM
GeoK68
GeoK68 - avatar
0
When you assign like that it is same as a=20, a=30, a=40.. So 20,30 will be replaced by 40.. So 20,30 will have no effect. So final value it contains, a = 40.
21st Aug 2020, 10:52 AM
Jayakrishna 🇮🇳