Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,774 members, 7,809,998 topics. Date: Friday, 26 April 2024 at 06:29 PM

I Need Help On C - Programming (2) - Nairaland

Nairaland Forum / Science/Technology / Programming / I Need Help On C (4295 Views)

Tutor On C# Needed / Help Needed On C# / I Need Training On C# And Java (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Re: I Need Help On C by babajeje123(m): 5:18am On Mar 13, 2017
Please, I need help on this c-code

#include <stdio.h>
int main()
{
int i;
char surname[30];
char name[30], matricno[10];
int A, B, C;
FILE *file, *file1;

/* Open an existing file */
file = fopen("Broadsheett.txt","r"wink;
if(file == NULL){
printf("Error: Unable to open a file"wink;
}


file1 = fopen("newbroadsheet.txt", "w"wink;
fprintf(file1, "University of Nairaland\n"wink;
fprintf(file1, "Department of Programming\n"wink;
fprintf(file1, "================================\n"wink;

while (!feof(file)) {
fscanf(file, "%s\t%s\t%d\t%d\n", &name, &matricno, &A, &B);
fprintf(file1, "%s\t%s\t%d\t%d\n", name, matricno, A, B);
}
}



Screenshot of my output file is attached. I don't know where the enclosed came from. Please, help

Re: I Need Help On C by stack1(m): 10:46am On Mar 13, 2017
It might have to do with the file contents, somehow, what fscanf is reading in isnt matching the requirements for in A and B,
that is a mismatched type is been read, so garbage would be stored in A & B then printed out, either non-integers are being read or some kind of overflow is occuring, may be paste the content of the input file
Re: I Need Help On C by babajeje123(m): 12:08pm On Mar 13, 2017
stack1:
It might have to do with the file contents, somehow, what fscanf is reading in isnt matching the requirements for in A and B,
that is a mismatched type is been read, so garbage would be stored in A & B then printed out, either non-integers are being read or some kind of overflow is occuring, may be paste the content of the input file
Thank you @ Stack. I have corrected the data type bug. However, I still don't get the output file well formatted. Here is the screenshot with the new codes

#include <stdio.h>
int main()
{
int i, C;
char surname[30];
char name[30], matricno[30];
char MIT801[6], MIT802[6];
FILE *file, *file1;

/* Open an existing file */
file = fopen("Broadsheett.txt","r"wink;
if(file == NULL){
printf("Error: Unable to open a file"wink;
}


file1 = fopen("newbroadsheet.txt", "w"wink;
fprintf(file1, "University of Nairaland\n"wink;
fprintf(file1, "Department of Programming\n"wink;
fprintf(file1, "================================\n"wink;

while (!feof(file)) {
fscanf(file, "%s %s %s %s", name, matricno, MIT801, MIT802);
fprintf(file1, "%s\t%s\t%s\t%s\n", name, matricno, MIT801, MIT802);
}


"matricno" and "MIT801" are missing in the output file.

Re: I Need Help On C by stack1(m): 12:04am On Mar 14, 2017
babajeje123:

Thank you @ Stack. I have corrected the data type bug. However, I still don't get the output file well formatted. Here is the screenshot with the new codes

#include <stdio.h>
int main()
{
int i, C;
char surname[30];
char name[30], matricno[30];
char MIT801[6], MIT802[6];
FILE *file, *file1;

/* Open an existing file */
file = fopen("Broadsheett.txt","r"wink;
if(file == NULL){
printf("Error: Unable to open a file"wink;
}


file1 = fopen("newbroadsheet.txt", "w"wink;
fprintf(file1, "University of Nairaland\n"wink;
fprintf(file1, "Department of Programming\n"wink;
fprintf(file1, "================================\n"wink;

while (!feof(file)) {
fscanf(file, "%s %s %s %s", name, matricno, MIT801, MIT802);
fprintf(file1, "%s\t%s\t%s\t%s\n", name, matricno, MIT801, MIT802);
}


"matricno" and "MIT801" are missing in the output file.

if the output isnt well formatted, uses spaces and tabs to adjust, also use printf's minimum-width specification
Re: I Need Help On C by babajeje123(m): 9:11pm On Mar 15, 2017
I still need help o. I have read tried my best to resolve this output. Please, programmers in the house, guide me.
Re: I Need Help On C by ChinenyeN(m): 5:15am On Mar 16, 2017
What exactly needs to be resolved? Please, be as specific as you can be.
Re: I Need Help On C by babajeje123(m): 10:05am On Mar 16, 2017
@ChinenyeN.

#include <stdio.h>
int main()
{
int i, C;
char surname[30];
char name[30], matricno[30];
char MIT801[6], MIT802[6];
FILE *file, *file1;

/* Open an existing file */
file = fopen("Broadsheett.txt","r"wink;
if(file == NULL){
printf("Error: Unable to open a file"wink;
}


file1 = fopen("newbroadsheet.txt", "w"wink;
fprintf(file1, "University of Nairaland\n"wink;
fprintf(file1, "Department of Programming\n"wink;
fprintf(file1, "================================\n"wink;

while (!feof(file)) {
fscanf(file, "%s %s %s %s", name, matricno, MIT801, MIT802);
fprintf(file1, "%s\t%s\t%s\t%s\n", name, matricno, MIT801, MIT802);
}


"matricno" and "MIT801" are missing in the output file.
[/quote]
Re: I Need Help On C by ChinenyeN(m): 7:40pm On Mar 16, 2017
Babajeje123, looking at your code and the output screen shot from earlier, the first thing that comes to mind is how the data is stored in the Broadsheett.txt file. What sort of whitespace separates the fields? Is it a regular space or a tab or something else?

Try replacing the blank space in your fscanf format string with \t and tell us what happens with the output.
Re: I Need Help On C by babajeje123(m): 5:42pm On Mar 21, 2017
ChinenyeN:
Babajeje123, looking at your code and the output screen shot from earlier, the first thing that comes to mind is how the data is stored in the Broadsheett.txt file. What sort of whitespace separates the fields? Is it a regular space or a tab or something else?

Try replacing the blank space in your fscanf format string with \t and tell us what happens with the output.

I did what you suggested but nothing was printed on the new file that was created. I think the problem is with fscanf and fprintf statements. More suggestion would be appreciated.
Re: I Need Help On C by ChinenyeN(m): 5:43am On Mar 22, 2017
Quite honestly, babajeje123, I do not understand why you are having this particular problem. I just recreated your entire code almost down to the letter on my own machine and got the desired output. In other words, both matricno and MIT801 showed up. Attached are the screenshots.

The first screenshot is the code, obviously. I essentially retyped your code. The only true difference is that I changed the file names for the sake of easy typing on my part.

The second screenshot is the output after I compiled and ran the program.

The third screenshot is the .txt file where the original data is stored. Because your fscanf function is using "%s %s %s %s", I figured that the .txt file must be stored in that same manner. So, everything is separated by a single space.

As far as I can see, your code should get you the desired result. So, now I'm back to wondering how your Broadsheett.txt file is structured.

babajeje123:
I did what you suggested but nothing was printed on the new file that was created.
This is surprising. According to the man pages for fscanf, the type and number of white space technically doesn't matter. Whether \t, \n or simply a single space, the output should still have been the same. The fact that nothing printed out to the new file raises suspicions.

Earlier in this thread, you mentioned that you are working on the Windows platform with an IDE. I can't help but wonder now if this is an IDE/compiler issue as opposed to the code. It can't possibly be the code. I wrote and compiled your code in a Linux environment with gcc and the standard command line arguments. Everything runs as expected.

Re: I Need Help On C by ChinenyeN(m): 8:27am On Mar 22, 2017
Babajeje123, my suggestion to you, would be to not use fscanf. In place of fscanf, try fgets. I imagine that the records in the original data .txt file are all separated by a newline character. If so, fgets can be used to grab each record, and then strtok for instance can be used to tokenize each string in the record.

It's more code, and it would require that other parts of your program be rewritten, but at least it gives you enough fine control to help ensure that all the desired fields are accounted for. You can still use fprint to generate the new file. There's no reason why that should change, because fscanf seems to be the real problem here, as far as I can tell.
Re: I Need Help On C by babajeje123(m): 2:25pm On Mar 22, 2017
ChinenyeN:
Babajeje123, my suggestion to you, would be to not use fscanf. In place of fscanf, try fgets. If so, fgets can be used to grab each record, and then strtok for instance can be used to tokenize each string in the record.

It's more code, and it would require that other parts of your program be rewritten, but at least it gives you enough fine control to help ensure that all the desired fields are accounted for. You can still use fprint to generate the new file. There's no reason why that should change, because fscanf seems to be the real problem here, as far as I can tell.
Okay. Thank you so much. I will try the suggestions and get back to you.
Re: I Need Help On C by babajeje123(m): 7:50am On Mar 23, 2017
ChinenyeN:
Babajeje123, my suggestion to you, would be to not use fscanf. In place of fscanf, try fgets. I imagine that the records in the original data .txt file are all separated by a newline character. If so, fgets can be used to grab each record, and then strtok for instance can be used to tokenize each string in the record.

It's more code, and it would require that other parts of your program be rewritten, but at least it gives you enough fine control to help ensure that all the desired fields are accounted for. You can still use fprint to generate the new file. There's no reason why that should change, because fscanf seems to be the real problem here, as far as I can tell.
Everything is fine now. I just reformatted by input file by using a single space between each data and that was all. However, I have a similar issue when I changed the data type MIT801 & MIT802 int from char and introduced 'total' as int type.

#include <stdio.h>
int main()
{
int i, c;
char surname[30];
char name[30], matricno[30];
int mit801, mit802, total;
char grade;
FILE *file, *file1;

/* Open an existing file */
file = fopen("Broadsheett.txt","r"wink;
if(file == NULL){
printf("Error: Unable to open a file"wink;
}


file1 = fopen("newbroadsheet.txt", "w"wink;
fprintf(file1, "University of Lagos\n"wink;
fprintf(file1, "======================\n"wink;
fprintf(file1, "Faculty of Sciences\n"wink;
fprintf(file1, "======================\n"wink;
fprintf(file1, "Department of Computer Sciences\n"wink;
fprintf(file1, "================================\n"wink;

while (!feof(file)) {
fscanf(file, "%s%s%d%d", name, matricno, &mit801, &mit802);
total = mit801 + mit802;
fprintf(file1, "%s\t%s\t%d\t%d\t%d\n", name, matricno, mit801, grade, mit802, total);
}
}


What do you think I'm missing? Thanks once again.

Re: I Need Help On C by ChinenyeN(m): 6:37am On Mar 24, 2017
Well, babajeje123, the first line of your input file is technically headers. So, you'd have to read them in with the %s specifier, because the %d specifier results in an incorrect data type which then give you garbage values.

Here's my recommendation. Consider reading the first line separately in some way. In other words, write your code to consider the header names, which are strings and not integers. Then, once the headers have been taken care of, you can switch to using the %d specifier and evaluate the rest of the input file.

1 Like

Re: I Need Help On C by babajeje123(m): 10:39am On Mar 24, 2017
ChinenyeN:
Well, babajeje123, the first line of your input file is technically headers. So, you'd have to read them in with the %s specifier, because the %d specifier results in an incorrect data type which then give you garbage values.

Here's my recommendation. Consider reading the first line separately in some way. In other words, write your code to consider the header names, which are strings and not integers. Then, once the headers have been taken care of, you can switch to using the %d specifier and evaluate the rest of the input file.
You are very smart! This is what I did and I got the desired output. I think I just need to be more calm, read more and wider than what we are being taught in class.

I was thinking if you could be my c mentor. Is that possible wink?
Re: I Need Help On C by ChinenyeN(m): 4:38pm On Mar 25, 2017
I don't consider myself that great of a programmer or that smart of a person that I would be someone else's mentor. But, I don't mind being available when I can to help you out when you get stuck. It's usually just a matter of critical thinking.

1 Like

Re: I Need Help On C by babajeje123(m): 4:21pm On Mar 26, 2017
Thanks bro. I've been able to generate the broadsheet with marks and grades. However, I think the lines of codes that generate the grades should be concise. Also, I need to calculate the GPA of each student. Do you have an idea of how to go about this? My new code is given below
#include <stdio.h>
int main()
{
int i, c, tcunit = 15; //tunit means total course unit
char surname[30];
char name[30], matricno[30];
int mit801, mit802, mit803, mit805, mit821, total;
float average;
char *grade, *grade2, *grade3, *grade4, *grade5;
FILE *file, *file1;

/* Open an existing file */
file = fopen("Broadsheett.txt","r"wink;
if(file == NULL){
printf("Error: Unable to open a file"wink;
}

/*Creating and writing into a new file */
file1 = fopen("\tnewbroadsheet.txt", "w"wink;
fprintf(file1, "\tUniversity of Lagos\n"wink;
fprintf(file1, "\t======================\n"wink;
fprintf(file1, "\tFaculty of Sciences\n"wink;
fprintf(file1, "\t======================\n"wink;
fprintf(file1, "\tDepartment of Computer Sciences\n"wink;
fprintf(file1, "\t================================\n"wink;
fprintf(file1, "\tBROADSHEET FOR MIT1 STUDENTS, 2016/2017 SESSION"wink;
fprintf(file1, "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "NAME", "MATRICNO", "MIT801", "MIT802", "MIT803", "MIT805", "MIT821"wink;

while (!feof(file)) {
fscanf(file, "%s%s%d%d%d%d%d\n", name, matricno, &mit801, &mit802, &mit803, &mit805, &mit821);

/*Generating grades for scores/marks */
/* Determining the grade for MIT801 */
if (mit801 >= 70)
grade = 'A';

else if (mit801 >= 60)
grade = 'B';

else if (mit801 >= 50)
grade = 'C';

else if (mit801 >= 45)
grade = 'D';

else if (mit801 >= 40)
grade = 'E';

else
grade = 'F';

/* Determining the grade for MIT802 */

if (mit802 >= 70)
grade2 = 'A';
else if (mit802 >= 60)
grade2 = 'B';
else if (mit802 >= 50)
grade2 = 'C';
else if (mit802 >= 45)
grade2 = 'D';
else if (mit802 >= 40)
grade2 = 'E';
else
grade2 = 'F';
/* Determining the grade for MIT803 */
if (mit803 >= 70)
grade3 = 'A';
else if (mit803 >= 60)
grade3 = 'B';
else if (mit803 >= 50)
grade3 = 'C';
else if (mit803 >= 45)
grade3 = 'D';
else if (mit803 >= 40)
grade3 = 'E';
else
grade3 = 'F';
/* Determining the grade for MIT805 */
if (mit805 >= 70)
grade4 = 'A';
else if (mit805 >= 60)
grade4 = 'B';
else if (mit805 >= 50)
grade4 = 'C';
else if (mit805 >= 45)
grade4 = 'D';
else if (mit805 >= 40)
grade4 = 'E';
else
grade4 = 'F';
/* Determining the grade for MIT821 */
if (mit821 >= 70)
grade5 = 'A';
else if (mit821 >= 60)
grade5 = 'B';
else if (mit821 >= 50)
grade5 = 'C';
else if (mit821 >= 45)
grade5 = 'D';
else if (mit821 >= 40)
grade5 = 'E';
else
grade5 = 'F';


fprintf(file1, "\t%s\t%s\t%d%s\t%d%s\t%d%s\t%d%s\t%d%s\n", name, matricno, mit801, &grade, mit802, &grade2, mit803, &grade3, mit805, &grade4, mit821, &grade5);
}
}

When you run this, you'll have "assignment makes pointer from integer without a cast" as a warning. What does it mean?

Re: I Need Help On C by babajeje123(m): 4:22pm On Mar 26, 2017
ChinenyeN:
I don't consider myself that great of a programmer or that smart of a person that I would be someone else's mentor. But, I don't mind being available when I can to help you out when you get stuck. It's usually just a matter of critical thinking.
Re: I Need Help On C by ChinenyeN(m): 7:11am On Mar 27, 2017
Babajeje123, the error comes from the fact that in your code, you're attempting to assign a type char (single character) to a type char * (string).

In C, single quotes indicate single characters, i.e. 'B', while double quotes are for strings, i.e. "B". Some people might call the string a "string literal". Anyway, the point is that you have two different data types, because characters and strings are not the same thing in the C language.

So, you've got three options to solve your problem. The first would be to cast the single characters from type char to type char *:

i.e. grade2 = (char *) 'B';

You would have to do this for each line where you are assigning a char to a char *. I would not recommend this approach though, mostly because it is unnecessary.

The second option would be to change your code by changing all single quotes to double quotes:

i.e. grade2 = "B";

I would not recommend this either, mostly because it too is unnecessary. Just like the casting option, you would have to change each line where you are assigning a char to a char *.

So, those two options aside, the issue can be taken care of far quicker with this last option. In simple terms, just change your earlier declaration from char * to char.

i.e. basically remove the asterisk.
char *grade, *grade2, *grade3, *grade4, *grade5; -> char grade, grade2, grade3, grade4, grade5;

This is the option I would recommend. Not only will it affect every every line where you are assigning a char to char *, it will be much cleaner that code full of unnecessary casting. Plus, it is more sensible than changing every necessary line from single quotes to double quotes. Long story short, simply changing your char *grade, etc. declaration should take care of the compiler warning.
Re: I Need Help On C by ChinenyeN(m): 7:21am On Mar 27, 2017
As for making your code more concise....

Considering that you are repeating the same if/else if conditions, why not write a loop statement that will contain the if/else if blocks and evaluate all the mit* values within the loop. This will effectively get rid of all the repeated if/else if blocks.
Re: I Need Help On C by babajeje123(m): 1:22pm On Apr 02, 2017
ChinenyeN:
As for making your code more concise....

Considering that you are repeating the same if/else if conditions, why not write a loop statement that will contain the if/else if blocks and evaluate all the mit* values within the loop. This will effectively get rid of all the repeated if/else if blocks.
Thank you for your help. I've been able to complete the assignment! Everything works perfectly. Do you think C is such a program to give too much attention? I'm just thinking of mastering it for exam sake and dump it when exams are over.
Re: I Need Help On C by ChinenyeN(m): 5:26pm On Apr 02, 2017
Babajeje123, congratulations on finishing your assignment. It's good to know that the program compiles and executes as expected.

About C, if you never intend on doing anything relatively low-level, then you likely will never have to worry about using C after your exam. There are other general purpose programming languages that will do the same thing as C, but with less lines of code. C shines more so as a bootstrapping language (the language that interpreters and compilers are sometimes initially written in), and it shines especially more so as a systems programming language (the language that kernels and OS's and OS utilities are mostly written in).

If you don't have any need for any of these software engineering routes in the future, then you likely will have little need for C. It doesn't mean you can't use C for other things besides system programming. C is still a general purpose programming language. It can be used for practically anything.

Aside from that, C still runs the world, and it seems like it will always be around for the foreseeable future. C is just so intertwined with current computer architecture that the only way for C to become irrelevant is if we start designing and building computers differently. Until then, there will always be a need for someone to pay attention to C.

1 Like

Re: I Need Help On C by uzoexcel(m): 5:07pm On Apr 04, 2017
Take lecture vids on newBoston.org
Re: I Need Help On C by babajeje123(m): 2:27am On Apr 05, 2017
uzoexcel:
Take lecture vids on newBoston.org
undecided undecided
Re: I Need Help On C by uzoexcel(m): 6:33am On Apr 05, 2017
babajeje123:

undecided undecided

thenewboston.com
Re: I Need Help On C by uzoexcel(m): 6:33am On Apr 05, 2017
uzoexcel:
Take lecture vids on newBoston.org

Sorry it's
thenewboston.com
Re: I Need Help On C by celis(m): 7:51pm On May 02, 2017
thanks for this! Please i dont even know where to start from. I've been running from one error to the other all day. i can see you are reading from .txt . I taught the question says to read from excel file. Can you help a brother with the final code please?

babajeje123:
Thanks bro. I've been able to generate the broadsheet with marks and grades. However, I think the lines of codes that generate the grades should be concise. Also, I need to calculate the GPA of each student. Do you have an idea of how to go about this? My new code is given below
#include <stdio.h>
int main()
{
int i, c, tcunit = 15; //tunit means total course unit
char surname[30];
char name[30], matricno[30];
int mit801, mit802, mit803, mit805, mit821, total;
float average;
char *grade, *grade2, *grade3, *grade4, *grade5;
FILE *file, *file1;

/* Open an existing file */
file = fopen("Broadsheett.txt","r"wink;
if(file == NULL){
printf("Error: Unable to open a file"wink;
}

/*Creating and writing into a new file */
file1 = fopen("\tnewbroadsheet.txt", "w"wink;
fprintf(file1, "\tUniversity of Lagos\n"wink;
fprintf(file1, "\t======================\n"wink;
fprintf(file1, "\tFaculty of Sciences\n"wink;
fprintf(file1, "\t======================\n"wink;
fprintf(file1, "\tDepartment of Computer Sciences\n"wink;
fprintf(file1, "\t================================\n"wink;
fprintf(file1, "\tBROADSHEET FOR MIT1 STUDENTS, 2016/2017 SESSION"wink;
fprintf(file1, "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", "NAME", "MATRICNO", "MIT801", "MIT802", "MIT803", "MIT805", "MIT821"wink;

while (!feof(file)) {
fscanf(file, "%s%s%d%d%d%d%d\n", name, matricno, &mit801, &mit802, &mit803, &mit805, &mit821);

/*Generating grades for scores/marks */
/* Determining the grade for MIT801 */
if (mit801 >= 70)
grade = 'A';

else if (mit801 >= 60)
grade = 'B';

else if (mit801 >= 50)
grade = 'C';

else if (mit801 >= 45)
grade = 'D';

else if (mit801 >= 40)
grade = 'E';

else
grade = 'F';

/* Determining the grade for MIT802 */

if (mit802 >= 70)
grade2 = 'A';
else if (mit802 >= 60)
grade2 = 'B';
else if (mit802 >= 50)
grade2 = 'C';
else if (mit802 >= 45)
grade2 = 'D';
else if (mit802 >= 40)
grade2 = 'E';
else
grade2 = 'F';
/* Determining the grade for MIT803 */
if (mit803 >= 70)
grade3 = 'A';
else if (mit803 >= 60)
grade3 = 'B';
else if (mit803 >= 50)
grade3 = 'C';
else if (mit803 >= 45)
grade3 = 'D';
else if (mit803 >= 40)
grade3 = 'E';
else
grade3 = 'F';
/* Determining the grade for MIT805 */
if (mit805 >= 70)
grade4 = 'A';
else if (mit805 >= 60)
grade4 = 'B';
else if (mit805 >= 50)
grade4 = 'C';
else if (mit805 >= 45)
grade4 = 'D';
else if (mit805 >= 40)
grade4 = 'E';
else
grade4 = 'F';
/* Determining the grade for MIT821 */
if (mit821 >= 70)
grade5 = 'A';
else if (mit821 >= 60)
grade5 = 'B';
else if (mit821 >= 50)
grade5 = 'C';
else if (mit821 >= 45)
grade5 = 'D';
else if (mit821 >= 40)
grade5 = 'E';
else
grade5 = 'F';


fprintf(file1, "\t%s\t%s\t%d%s\t%d%s\t%d%s\t%d%s\t%d%s\n", name, matricno, mit801, &grade, mit802, &grade2, mit803, &grade3, mit805, &grade4, mit821, &grade5);
}
}

When you run this, you'll have "assignment makes pointer from integer without a cast" as a warning. What does it mean?
Re: I Need Help On C by babajeje123(m): 11:46am On May 03, 2017
celis:
thanks for this! Please i dont even know where to start from. I've been running from one error to the other all day. i can see you are reading from .txt . I taught the question says to read from excel file. Can you help a brother with the final code please?




Yeah, we were asked to read from excel file but the output wasn't given me what I wanted. So, I had to use txt. With this code you quoted, you are 80% done. Try to figure out the rest.
Re: I Need Help On C by babajeje123(m): 4:19am On May 09, 2017
ChinenyeN:
Babajeje123, congratulations on finishing your assignment. It's good to know that the program compiles and executes as expected.

About C, if you never intend on doing anything relatively low-level, then you likely will never have to worry about using C after your exam. There are other general purpose programming languages that will do the same thing as C, but with less lines of code. C shines more so as a bootstrapping language (the language that interpreters and compilers are sometimes initially written in), and it shines especially more so as a systems programming language (the language that kernels and OS's and OS utilities are mostly written in).

If you don't have any need for any of these software engineering routes in the future, then you likely will have little need for C. It doesn't mean you can't use C for other things besides system programming. C is still a general purpose programming language. It can be used for practically anything.

Aside from that, C still runs the world, and it seems like it will always be around for the foreseeable future. C is just so intertwined with current computer architecture that the only way for C to become irrelevant is if we start designing and building computers differently. Until then, there will always be a need for someone to pay attention to C.
I was attempting some mcqs and I encountered the following code blocks which left me confused. The first one

#include <stdio.h>
#include <conio.h>

int main(){
int i = 3;
printf("%d%d", i, i++);
}
and the second one

#include <stdio.h>
#include <conio.h>

int main(){
int i = 1;
j = i--- -2;
printf("%d", j);
}


Can you please explain why the answer to the first code would be 3 and not -3 and the second one, 43 and not 34? Thank you.
Re: I Need Help On C by stack1(m): 1:14pm On May 09, 2017
Actually tboth code would give you varying results depending on compiler and architecture, it has to do with stuff C terms
1. Sequence points
2. Order of evaluation

the explanation is long and twisted but please see this stack overflow post...
http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points..

U mixed up your expectations tho;
Then again its the second code that should give 3 and the first 43, heres y..
Second Code:
j = i--- -2;

would evaluate as or should in most cases, aslo it would be different on C99 and C11 compilers

j = i-- -(-2);



the i has a post increment so remains 1, for the duration of that assignment
-(-2) yields +2

so i +2 yields 3

First Code:
If it yeilds 43, then the post increment wast performed first (as i noted such statements would yield erroneous/undefined behavior).

Generally avoid stuff like this, and read the linked post thoroughly

1 Like

Re: I Need Help On C by ChinenyeN(m): 3:22pm On May 09, 2017
And I will reiterate stack1's advice. Generally stay away from overuse or convoluted use of the increment and decrement operators. They are part of the list of C features that are "implementation dependent". That and for the sake of readability.

Moderate use of the increment and decrement operators is alright though. They are still useful features.

That aside, stack1 gave an excellent response. There's not much I have to say.

2 Likes

(1) (2) (3) (Reply)

React + Node Js : Fullstack Web Development Training / Minimum Computer Specs For Programming / Loki v 1.8 http Bot | Best Botnet 2017| Programming Tutorial |all Browser

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 102
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.