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

C 9

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 views13 pages

C 9

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/ 13
06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools 953. C Variables [crv] Looe | Variables are containers for storing data values, like numbers and characters. In C, there are different types of variables (defined with different keywords), for example: * int - stores integers (whole numbers), without decimals, such as 123 or -123 * float - stores floating point numbers, with decimals, such as 19.99 or -19.99 * char - stores single characters, such as surrounded by single quotes a’ or '8", Char values are Declaring (Creating) Variables To create a variable, specify the type and assign it a value: Syntax type variableName = value; Where type is one of C types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign a value to the variable. So, to create a variable that should store a number, look at the following example nitpsilwww:vdschools.comiele_variables php wna 06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools int myNum = 15; You can also declare a variable without assigning the value, and assign the value later: Example // Declare a variable int myNum; // Assign a value to the variable myNum = 15; Output Variables You learned from the output chapter that you can output values/print text with the printf() function: Example printf("Hello World!"); In many other programming languages (like Python, Java, and C++), you would normally use a print function to display the value of a variable. However, this is not possible in C: Example nitpsilwww:vdschools.comiele_variables php ana 06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools To output variables in C, you must get familiar with something called "format specifiers". Format Specifiers Format specifiers are used together with the printf() function to tell the compiler what type of data the variable is storing. It is basically a placeholder for the variable value A format specifier starts with a percentage sign % , followed by a character. For example, to output the value of an int variable, you must use the format specifier %d or %i surrounded by double quotes, inside the printf() function Example int myNum = 153 printf("%d", myNum); // Outputs 15 To print other types, use %c for char and %f for float Example // Create variables int myNum = 153 // Integer (whole number) float myFloatNum = 5.99; // Floating point number char myLetter 3 // Character // Print variables printf("%d\n", myNum) nitpsilwwwavdschools.comiele_variables.php ana 06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools To combine both text and a variable, separate them with a comma inside the print#() function: Example int myNum = 155 printf("My favorite number is: %d", myNum); To print different types in a single print#() function, you can use the following: Example int myNum = 15; char myLetter = 'D'; printf("My number is %d and my letter is %c", myNum, myLetter); You will learn more about Data Types in the next chapter. Change Variable Values Note: If you assign a new value to an existing variable, it will overwrite the previous value: nitpsilwww:vdschools.comiele_variables php ana 06/12/2028 06:35 Variables 3 w Tutorials~ Exercisesw Serviesey§ QO SignUp Login CSS JAVASCRIPT SQL. PYTHON. «JAVA. PHP. HOW TO. Ane myiwum = 495 077 mymun 4s 42 myNum = 10; // Now myNum is 10 Try it Yourself » You can also assign the value of one variable to another: Example int myNum = 15; int myOtherNum = 23; // Assign the value of myOtherNum (23) to myNum myNum = myOtherNum; // myNum is now 23, instead of 15 printf("%d", myNum) ; Try it Yourself » Or copy values to empty variables: Example // Create a variable and assign the value 15 to it int myNum = 153 // Declare a variable without assigning it a value int myotherNum; // Assign the value of myNum to myOtherNum myOtherNum = myNum; htipsitwnww.w3schools comicle_varables. php 513 06/12/2028 06:35 Variables 3 w Tutorials Exercisesw Servicesey§ QO Login schools Add Variables Together To add a variable to another variable, you can use the + operator: Example int x = 55 int y = 6; int sum = x + y; printf("%d", sum); Declare Multiple Variables To declare more than one variable of the same type, use a comma-separated list: Example int x = 5, y = 6, 2 = 583 print#("%d", x + y + z)5 You can also assign the same value to multiple variables of the same type! Example nitpsilwww:vdschools.comiele_variables php ena 06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools C Variable Names All C variables must be identified with unique names These unique names are called identifiers. Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume). Note: It is recommended to use descriptive names in order to create understandable and maintainable code Example /1 Good int minutesPerHour = 60; // OK, but not so easy to understand what m actually is int m = 605 The general rules for naming variables are: + Names can contain letters, digits and underscores + Names must begin with a letter or an underscore (_) + Names are case sensitive (myVar and myvar are different variables) + Names cannot contain whitespaces or special characters like !, #, %, etc. * Reserved words (such as int ) cannot be used as names Real-Life Example Often in our examples, we simplify variable names to match their data type (myInt or myNum for int types, myChar for char types etc). This is done to avoid confusion. nitpsilwww:vdschools.comiele_variables php m9 06/12/2028 06:35 Variables 3 WW tutoriaisy Exercises» Servicessy§ Q 0 schools Example // Student data int studentID = 15; int studentage = 23; float studentFee = 75.25; char studentGrade = // Print variables printf("Student id: %d\n", studentID); printf("Student age: %d\n", studentAge) ; printf("Student fee: %f\n", studentFee) ; printf("Student grade: %c", studentGrade); Calculate the Area of a Rectangle Log in In this real-life example, we create a program to calculate the area of a rectangle (by multiplying the length and width) Example // Create integer variables int length = 4; int width = 6; int area; // Calculate the area of a rectangle area = length * width; // Print the variables printf("Length is: %d\n", length); printf("Width is: %d\n", width); printf ("Area of the rectangle is: %d", area); nitpsilwww:vdschools.comiele_variables php ana 06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools C Exercises Exercise: Create a variable named myNum and assign the value 5@ to it. Start the Exercise ADVERTISEMENT nitpsilwww:vdschools.comiele_variables php gna 06/12/2028 06:35 Variables 3 w Tutorials» —Exercisese Servicese§ QU O SignUp Login acho CSS JAVASCRIPT SQL. PYTHON. «JAVA. PHP-—«S HOWTO ex Front-end Certification Program Bue hncrease your earning potential by becoming ‘aWaSchools Certified Front-End developer Ga COLOR PICKER htipsitieww.w3schools comicle_varables.php son 06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT nitpsilwww:vdschools.comiele_variables php nia 06/12/2028 06:35 Variables Tutorials» —Exercisese Servicese§ QU O SignUp Login CSS JAVASCRIPT SQL. PYTHON = JAVA. PHP-—«S HOWTO W3.CSS_— “ G w SPACES UPGRADE NEWSLETTER: schools. Top Tutorials HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial cH Tutorial Query Tutorial Top References HTML Reference CSS Reference JavaScript Reference SQL Reference Python Reference Wa.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference jQuery Reference Top Examples Get Certified HTML Certificate htipsitieww.w3schools comicle_varables.php ana 06/12/2028 06:35 Variables 3 WW tutoriaisy — Brercisese Servieesey Q O SignUp Login schools = CSS JAVASCRIPT SQL. PYTHON = JAVA. PHP-—«S HOWTO W3.CSS_— Wa3.CSS Examples Query Certificate Bootstrap Examples Java Certificate PHP Examples c++ Centficate Java Examples i Certificate XML Examples XML Certificate jQuery Examples @ @& B® © Forum asour 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. htipsfinww.w3schools.comicle_variab sana

You might also like