The problem
''Ask the user to enter a year and store it in an int. Then assign to a _Bool variable the value
true if the year is a leap year. The leap year is any year whose number is a multiple of four.
Take into account that years whose numbers are multiples of 100, except of 400, are NOT
leap years. Thus 2000 was a leap year, but 1700, 1800, 1900 were not.''


I know this might seem an easy task for all those who know C by heart but i'm finding it hard to find examples online and on longish ebooks.

what is a _Bool variable?
how is the if then statement written exactly in c?

what is a _Bool variable?

A variable of boolean type. The keyword for a boolean type in C is _Bool. It holds only two values: 0 or 1, which correspond to false and true, respectively. C also provides a header called <stdbool.h> that defines more convenient macros: bool , false , and true :

#include <stdbool.h>
#include <stdio.h>

int main(void)
{
    _Bool a = 0; // Without stdbool.h macros
    bool b = false; // With stdbool.h macros

    if (a) {
        printf("True!\n");
    }
    else {
        printf("False!\n");
    }

    if (b) {
        printf("True!\n");
    }
    else {
        printf("False!\n");
    }
}

how is the if then statement written exactly in c?

if (/* boolean condition */) {
    // True case
}

how is the boolean condition written though?

The logic is that the year is either divisible by both
100 and 4 , OR
its only divisible by 4 not by hundred

how is the boolean condition written though?

Read the chapter again. Assignments aren't given without covering the necessary material, and you don't seem to have been paying attention.

apologies @Narue.

well I've been reading my topics and am currently trying to write a simple calculator program. The code is compiling but the output of the result is not outputted. What could the problem be please

#include<stdio.h> 

int main ()

{
int x;
int y;
int decision;
int add;
int subt;
int mult; 
double div;
printf("enter first number\n");
scanf("%d",&x);
printf("enter second number\n");
scanf("%d",&y);
printf("Press 1 if you want to add\n");
printf("Press 2 if you want to subtract\n");
printf("Press 3 if you want to multiply\n");
printf("Press 4 if you want to divide\n");
scanf("%d\n",&decision);
add=x+y;
subt=x-y;
mult=x*y;
div=x/y;
if (decision=='1') {
printf("The addition of the 2numbers is %d \n",add);
}
if (decision=='2') {
printf("The subtration of the 2numbers is %d \n",subt);
}
if (decision=='3') {
printf("The multiplication of the 2numbers is %d \n",mult);
}
if (decision=='4') {
printf("The division of the 2numbers is %f \n",div);
}
getchar ();
getchar ();
return 0;
}

?

The result of the if statement i.e. printf(.. etc etc) is not outputted on screen.

decision is an integer, yet your if statements are treating it as a character. Note that '1' is not the same value as 1 .

Oh , well i arranged that and added a getchar (); after the printf of each statement..

eg:

if (decision==1) {
printf("The addition of the 2numbers is %d \n",add);
getchar ();
}

Is there any other reason why the output isn't well?

I know I havent written an

else

command but I dont think it is really necessary.

scanf("%d\n",&decision);

scanf() is not printf(). Remove the newline from that format string because it doesn't do what you think it does.

thanks @narue. I modified the program to this and it worked well.

#include<stdio.h> 

int main ()

{
int x;
int y;
unsigned int decision;
int add;
int subt;
int mult; 
double div;
printf("enter first number\n");
scanf("%d",&x);
printf("enter second number\n");
scanf("%d",&y);
printf("Press 1 if you want to add\n");
printf("Press 2 if you want to subtract\n");
printf("Press 3 if you want to multiply\n");
printf("Press 4 if you want to divide\n");
scanf("%d",&decision);
add=x+y;
subt=x-y;
mult=x*y;
div=x/y;
if (decision==1) {
printf("The addition of the 2numbers is %d \n",add);
getchar ();
}
if (decision==2) {
printf("The subtration of the 2numbers is %d \n",subt);
getchar ();
}
if (decision==3) {
printf("The multiplication of the 2numbers is %d \n",mult);
getchar ();
}
if (decision==4) {
printf("The division of the 2numbers is %f \n",div);
getchar ();
}
if (decision>4) {
printf("Wrong input\n");
main ();
}
getchar ();
return 0;
}

I turned the decision to an ''unsigned int'' type and added another if statement but suppose I wanted to use the ! logical connector. How could the code be written?

I tried

if (decision ! 1 || 2 || 3|| 4) {
printf("wrong input");

but it didn't work..

and when I added an else with every if statement It gave out the answer and another 3 'Wrong Input' 's

if (decision ! 1 || 2 || 3|| 4) {
if (decision != 1 && decision != 2 && decision != 3 && decision != 4) {
    printf("wrong input\n");
}

Previously I assumed you have a book on C, is this not the case? Because a lot of your errors seem a lot like you're guessing about language fundamentals that are clearly explained in every introductory book I can remember.

Thanks @Narue , most introductory problems solved.

The reason is that I just started learning C yesterday and need to become good at least by next January. I needed a quick start and searched a lot on internet at first. Now I will start reading K&R's Book, even-though this expects some background in programming which I barely have except for pascal.

I just started learning C yesterday and need to become good at least by next January.

Why the rush? Two months isn't unreasonable for someone with programming experience, but that's predicated on them already being decent programmers and simply picking up the language. But going from zero to "good" would be pushing it, in my opinion, unless you're unusually talented. C is a language with many nuances that can only be understood through experience.

when i mean good .. i refer to being able to answer and understand simple problems. On January I have the exam..

Unfortunately university teachers leave a lot for us to research and on programming I agree since it requires practice.

I don't need to become an expert till January. This is what I am required to know.

"
Basic programming algorithms (e.g. accumulated sum/product, random number generation), basic data types and I/O, language constructs (assignment, if, case/switch, loops), higher data structures (including records/structures, arrays and strings), text files, introduction to pointers and function calls by reference "

I hope I succeed. ><

Basic programming algorithms (e.g. accumulated sum/product, random number generation), basic data types and I/O, language constructs (assignment, if, case/switch, loops), higher data structures (including records/structures, arrays and strings), text files, introduction to pointers and function calls by reference

Oh, in that case you'll have a hard time not learning the required material unless you're unusually dense. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.