0% found this document useful (0 votes)
17 views9 pages

C 14

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

C 14

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
06/12/2028 06:38 Booleans 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools C Booleans [crv] Looe | Booleans Very often, in programming, you will need a data type that can only have one of two values, like: * YES / NO * ON / OFF * TRUE / FALSE For this, Chas a bool data type, which is known as booleans. Booleans represent values that are either true or false Boolean Variables In C, the bool type is not a built-in data type, like int or char. It was introduced in C99, and you must import the following header file to use it: #include A boolean variable is declared with the bool keyword and can only take the values true or false: nitpsilwww:vdschools.comiele_booleans.php 9 06/12/2028 06:38 Booleans 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Before trying to print the boolean variables, you should know that boolean values are returned as integers: * 1 (or any other number that is not 0) represents true * © represents false Therefore, you must use the %d format specifier to print a boolean value Example // Create boolean variables bool isProgrammingFun = true; bool isFishTasty = false; // Return boolean values print#("Xd", isProgranmingFun); // Returns 1 (true) printf("%d", isFishTasty); // Returns @ (false) However, it is more common to return a boolean value by comparing values and variables. Comparing Values and Variables Comparing values are useful in programming, because it helps us to find answers and make decisions. For example, you can use a comparison operator, such as the greater than ( >) operator, to compare two values Example nitpsilwww:vdschools.comiele_booleans.php 219 06/12/2028 06:38 Booleans 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools From the example above, you can see that the return value is a boolean value (1) You can also compare two variables: Example int x = 16; int y = 93 printf("%d", x > y)5 In the example below, we use the equal to ( == ) operator to compare different values Example printf("%d", 10 printf("%d", 10 printf("%d", 5 10); // Returns 1 (true), because 10 is equal to 10 15); // Returns @ (false), because 1@ is not equal to 15 55); // Returns @ (false) because 5 is not equal to 55 You are not limited to only compare numbers. You can also compare boolean variables, or even special structures, like arrays (which you will learn more about in a later chapter): Example nitpsilwww:vdschools.comiele_booleans.php a9 06/12/2028 06:38 Booleans 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools printf("%d", isHamburgerTasty = isPizzaTasty) ; Remember to include the header file when working with bool variables. Real Life Example Let's think of a "real life example" where we need to find out if a person is old enough to vote, In the example below, we use the >= comparison operator to find out if the age ( 25 ) is greater than OR equal to the voting age limit, which is set to 18 : Example int myAge = 25; int votingAge = 18; printf("%d", myAge >= votingAge); // Returns 1 (true), meaning 25 year olds are allowed to vote! Cool, right? An even better approach (since we are on a roll now), would be to wrap the code above in an if...else statement, so we can perform different actions depending on the result: Example nitpsilwww:vdschools.comiele_booleans.php 49 06/12/2028 06:38 Booleans 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools sQ PYTHON JAVA PHP. int votingAge = 18; if (myAge >= votingAge) { printf("Old enough to vote!"); } else { printf("Not old enough to vote." Booleans are the basis for all comparisons and conditions. You will learn more about conditions else ) in the next chapter. ADVERTISEMENT nitpsilwww:vdschools.comiele_booleans.php 59 06/12/2028 06:38 Tutorials schools htipsifinww.w3schools.comicle_booleans. php Exercises ¥ CSS JAVASCRIPT SQL. PYTHON. JAVA PHP C Booleans Q 0 Services ‘Sponsored by: Alpes vaudoises: Skipass Alpes vaudoises -50% sur votre skipass en réservant en ligne dés maintenant ! me ~~ Front-end Certification Program BU hncrease your earning potential by becoming ‘aWaSchools Certified Front-End developer Lf. COLOR PICKER as 06/12/2028 06:38 Booleans 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools ADVERTISEMENT ADVERTISEMENT nitpsilwww:vdschools.comiele_booleans.php 719 06/12/2028 06:38 C Booleans Tutorials» —Exercisese Servicese§ QO Sign Up CSS JAVASCRIPT SQL. PYTHON «= JAVA. PHP-—S HOW TO Login w3.css ADVERTISEMENT Skipass Alpes vaudoises -50% sur votre skipass en réservant ¢ maintenant ! WW Co srnces schools GET CERTIFIED Top Tutorials HTML Tutorial SS Tutorial JavaScript Tutoriat How To Tutorial SQL Tutorial Python Tutorial Wa.css Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial tipsufwaew.wdechools.comicle_booleans. php UPGRADE NEWSLETTER REPORT ERROR c as 06/12/2028 06:38 3 WW tutoriaise exercises schools = CSS JAVASCRIPT SQL Reference Python Reference Wa.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference ‘Query Reference Top Examples HTML Examples CSS Examples JavaScript Examples How To Examples ‘SQL Examples Python Examples W23.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples Query Examples G@ @& B® © Forum asour C Booleans Q 0 SignUp Login JAVA PHP = HOWTO —W3.CSS Get Certified HTML Certificate css Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate Query Certificate Java Certificate c++ Centficate i Certificate XML Certificate W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS. htipsitieww.w3schools comiele_boal

You might also like