Question

#include <stdio.h> #include <stdlib.h> int aaa(); int main() { int ddd,eee = aaa (ddd,eee); printf("new num...

#include <stdio.h>
#include <stdlib.h>

int aaa();

int main()
{
int ddd,eee = aaa (ddd,eee);
printf("new num 1 = %d, new num2 = %d.\n", ddd,eee);
return 0;
}


int aaa(int bbb,int ccc)
{
   bbb = 111;
   ccc = 222;
   printf("num 1 = %d, num 2 = %d.\n", bbb,ccc);
   return bbb ,ccc;  
}

Output:

num 1 = 111, num 2 = 222.
new num 1 = 0, new num2 = 222.

**************************************************************

I am learning function in C.

When I use the parameter "bbb" and "ccc", can I return the two values at the same time like "return bbb, ccc"?

How can I pass the value of "ddd" and "eee" to "bbb" and "ccc"? and print it out like "new num 1 = 111, new num 2 = 222 "

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Generally in c programming, a function can return only one value.

For the above scenario, you can use pointers to store the value in the particular address so that we can print the value in the main the changes to the variable in the function are reflected and print the values.

we can use pointers to return multiple values but we are not using any return statement but we use the address of the variables to store the value in the address of the variable

The modified code is as follows:

#include <stdio.h>
#include <stdlib.h>

int aaa();//decalring the function
int main()
{
int ddd,eee;//decalring the varibles ddd and eee
aaa (&ddd,&eee);//calling the unction by passing the values ddd,eee

printf("new num 1 = %d, new num2 = %d.\n", ddd,eee);
return 0;
}


int aaa(int* bbb,int* ccc)
{
*bbb = 111;//storing the value of the bbb in the address of the ddd
*ccc = 222;//storing the value of the ccc in the address of the ccc
printf("num 1 = %d, num 2 = %d.\n", *bbb,*ccc);
//in pointers for printing the values we need to use *variable name

}

The input and output are provided in the screenshot below:

Add a comment
Know the answer?
Add Answer to:
#include <stdio.h> #include <stdlib.h> int aaa(); int main() { int ddd,eee = aaa (ddd,eee); printf("new num...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int...

    computers. 1. How many times will this loop repeat? include <stdlib.h> #include <stdio.h> void main(void) int 10 for(= 0; i < 101) printf("d", 1); A. 10 times B. 1 time C. It will run forever D. 0 times 2. What will be the output of this program? #include <stdlib.h> #include <stdio.h> int main() int a = 100, b = 200, C = 300; if(!a >= 500) b = 300; C = 400; printf("%d, 8d, &d", a, b, c); return 0;...

  • Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main()...

    Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main() {    int i, j, n; int A[] = {2, 3, 5, 5, 5, 6, 8, 11, 11, 11};    n = sizeof(A) / sizeof(int);    for (i = 0; i < n - 1; i++){        if (A[i] != A[i + 1]) {            for (j = 1; j < n - 1; j++) {                A[j]...

  • Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float aver...

    Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float average; int customerNumbers[num]; int customerSales[num]; printf("How many customers do you want to track?\n"); scanf("%d",&num); while(i<num) { printf("Enter the customer number. "); scanf("%d",&customerNumbers[i]); printf("Enter the sales for the customer "); scanf("%d",&customerSales[i]); i++; } printf("Sales for the Customer"); printf("\nCustomer Customer"); printf("\nNumber Sales"); for(i=0;i<num;i++) { printf("\n %d \t %d",customerNumbers[i], customerSales[i]); sum=sum+customerSales[i]; } average=(int)sum/num; printf("\n Total sales are $%d",sum); printf("\n Average sales are $%.2f",average); printf("\n --------------------------------------------");...

  • What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main()...

    What is the output? #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int x = 5; int y = 2; int z = 30; x = fork(); y = fork(); if (x != 0) printf("Type 1\n"); if (y != 0) printf("Type 2\n"); z = fork(); if (x > 0 || y > 0 || z > 0) printf("Type 3\n"); if (x == 0 && y == 0 && z != 0) printf("Type 4\n"); if (x...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size,...

    sort.c #include <stdlib.h> #include <stdio.h> #include "libsort.h" int main() {     int* array;     int size, c;     float median;     printf("Enter the array size:\n");     scanf("%d", &size);     array = (int*) malloc(size * sizeof(int));     printf("Enter %d integers:\n", size);     for (c = 0; c < size; c++)         scanf("%d", &array[c]);     sort(array, size);     printf("Array sorted in ascending order:\n");     for (c = 0; c < size; c++)         printf("%d ", array[c]);     printf("\n");     median = find_median(array,...

  • #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to...

    #include<stdio.h> int main() {       int data[10], i, j, temp;       printf("Enter 10 random number to sort in ascending order:\n");       for(i = 0; i < 10; i++)             scanf("%d", &data[i]);       /* Sorting process start */     ****** INSERT YOUR CODE TO COMPLETE THE PROGRAM ******       printf("After sort\n");       for(i = 0; i < 10; i++)             printf("%d\n",data[i]);       return 0; } Looking for alternative solutions for the program code.

  • #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int...

    #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int main(){ struct Employee e; read(&e); } void read(struct Employee *e){ int a,b; printf("Enter the id employee\n"); scanf("%d",&a); printf("Enter the age employee\n"); scanf("%d",&b); e->id=a; e->age=b; } Question: Declare a pointer variable of type Employee and place the address of the variable created in the above problem in that pointer variable.

  • #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str);...

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include<ctype.h> #define MAX_LEN 255 int numWords(char *str); int numDigit(char *str); int numUppltr(char *str); int numLwrltr(char *str); int punChar(char *str); char*readString(char *str); int main() { char givString[MAX_LEN]; puts("Enter your string(Max 255 characters):"); //readString(givString); gets(givString); printf("\nnumber of words :%d",numWords(givString)); printf("\nnumber of uppercase letters %d",numUppltr(givString)); printf("\nnumber of lowercase letters %d",numLwrltr(givString)); printf("\nnumber of punctuations %d\n",punChar(givString)); printf("\nnumber of digits:%d\n",numDigit(givString)); system("pause"); return 0; } char *readString(char *str) { int ch, i=0; while((ch=getchar())!=EOF && ch!='\n') { if(i) { str[i]=ch; i++; }...

  • #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1;...

    #include <stdio.h> int main(int argc, char *argv[]) { int i; for (i = argc - 1; i > 0; i--) printf("%s ", argv[i]); printf("\n"); return 0; } can you explain this code in c and why use this function  

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT