Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
17 views
13 pages
C 9
Uploaded by
hlemorvan
AI-enhanced title
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
Download
Save
Save c9 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
17 views
13 pages
C 9
Uploaded by
hlemorvan
AI-enhanced title
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
Carousel Previous
Carousel Next
Download
Save
Save c9 For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 13
Search
Fullscreen
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 wna06/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 ana06/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 ana06/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 ana06/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 51306/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 ena06/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 m906/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 ana06/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 gna06/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 son06/12/2028 06:35 Variables 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools ADVERTISEMENT ADVERTISEMENT ADVERTISEMENT nitpsilwww:vdschools.comiele_variables php nia06/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 ana06/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
CHAPTER 2 FUNDAMENTALS OF C LANGUAGE - 2.1 Edited
PDF
No ratings yet
CHAPTER 2 FUNDAMENTALS OF C LANGUAGE - 2.1 Edited
29 pages
C Syntax To Variable
PDF
No ratings yet
C Syntax To Variable
6 pages
DS - Unit - 1
PDF
No ratings yet
DS - Unit - 1
209 pages
CP Training Notes
PDF
No ratings yet
CP Training Notes
16 pages
Output
PDF
No ratings yet
Output
63 pages
C Prog. M1 Full Notes
PDF
No ratings yet
C Prog. M1 Full Notes
71 pages
Unit 2
PDF
No ratings yet
Unit 2
56 pages
CS100 P1
PDF
No ratings yet
CS100 P1
64 pages
4 - Get Started With C (F)
PDF
No ratings yet
4 - Get Started With C (F)
52 pages
C Language V26554
PDF
No ratings yet
C Language V26554
41 pages
W-02 - L-01 - Variable and Data Type
PDF
No ratings yet
W-02 - L-01 - Variable and Data Type
24 pages
PSPC Lecture 4 - Variables Input and Output
PDF
No ratings yet
PSPC Lecture 4 - Variables Input and Output
24 pages
Slide 2
PDF
No ratings yet
Slide 2
24 pages
Lecture 3
PDF
No ratings yet
Lecture 3
23 pages
C Programming
PDF
No ratings yet
C Programming
18 pages
Language Programming: Lesson 3: C Language-Part2
PDF
No ratings yet
Language Programming: Lesson 3: C Language-Part2
46 pages
C Variables
PDF
No ratings yet
C Variables
17 pages
C Programming Lecture 02
PDF
No ratings yet
C Programming Lecture 02
31 pages
Week 2 - Final
PDF
No ratings yet
Week 2 - Final
22 pages
CPCS301 - Topic3 - Variable Names, Binding, Scope and Life Time
PDF
No ratings yet
CPCS301 - Topic3 - Variable Names, Binding, Scope and Life Time
13 pages
C Introduction
PDF
No ratings yet
C Introduction
18 pages
CUITM114 Lecture 3 Identifiers Variables
PDF
No ratings yet
CUITM114 Lecture 3 Identifiers Variables
20 pages
Computer9.U2Lesson2 VARIABLES
PDF
No ratings yet
Computer9.U2Lesson2 VARIABLES
26 pages
Use of The Variables in C - Operators
PDF
No ratings yet
Use of The Variables in C - Operators
12 pages
C Programing
PDF
No ratings yet
C Programing
12 pages
C Program - Unit 1
PDF
No ratings yet
C Program - Unit 1
14 pages
Lecture 04 C Variables and Constants
PDF
No ratings yet
Lecture 04 C Variables and Constants
15 pages
Variables, Values, and Types
PDF
No ratings yet
Variables, Values, and Types
53 pages
Ee1101 Workshop03
PDF
No ratings yet
Ee1101 Workshop03
10 pages
Ics 2102 Lecture 2
PDF
No ratings yet
Ics 2102 Lecture 2
20 pages
C Programming - Variables - Wikibooks, Open Books For An Open World
PDF
No ratings yet
C Programming - Variables - Wikibooks, Open Books For An Open World
11 pages
CSC 102 - 044247
PDF
No ratings yet
CSC 102 - 044247
28 pages
C 29
PDF
No ratings yet
C 29
11 pages
LP 2
PDF
No ratings yet
LP 2
19 pages
Programming Fundamentals: Lecture # 4 Nauman Riaz Chaudhry
PDF
No ratings yet
Programming Fundamentals: Lecture # 4 Nauman Riaz Chaudhry
14 pages
Intro To Variable
PDF
No ratings yet
Intro To Variable
8 pages
C Prorgramming 4
PDF
No ratings yet
C Prorgramming 4
20 pages
1 3variables
PDF
No ratings yet
1 3variables
19 pages
3 - Variables, Constants
PDF
No ratings yet
3 - Variables, Constants
7 pages
C 17
PDF
No ratings yet
C 17
9 pages
C 14
PDF
No ratings yet
C 14
9 pages
Variables in C
PDF
No ratings yet
Variables in C
7 pages
Variable and Data Types
PDF
No ratings yet
Variable and Data Types
32 pages
C 45
PDF
No ratings yet
C 45
8 pages
C 30
PDF
No ratings yet
C 30
8 pages
C 31
PDF
No ratings yet
C 31
8 pages
C 26
PDF
No ratings yet
C 26
8 pages
C 25
PDF
No ratings yet
C 25
8 pages
C 1
PDF
No ratings yet
C 1
8 pages
CHAA
PDF
No ratings yet
CHAA
7 pages
C - Variables
PDF
No ratings yet
C - Variables
7 pages
Values Can Be Assigned To Variables Using The Assignment Operator As Follows: - Further Examples Are
PDF
No ratings yet
Values Can Be Assigned To Variables Using The Assignment Operator As Follows: - Further Examples Are
11 pages
C 52
PDF
No ratings yet
C 52
7 pages
C 8
PDF
No ratings yet
C 8
7 pages
C 11
PDF
No ratings yet
C 11
7 pages
C 43
PDF
No ratings yet
C 43
6 pages
C 36
PDF
No ratings yet
C 36
6 pages
C 32
PDF
No ratings yet
C 32
6 pages
C 33
PDF
No ratings yet
C 33
6 pages
C 22
PDF
No ratings yet
C 22
6 pages
C 12
PDF
No ratings yet
C 12
6 pages
Variables in C
PDF
No ratings yet
Variables in C
5 pages
C 55
PDF
No ratings yet
C 55
5 pages
C 50
PDF
No ratings yet
C 50
5 pages
C 27
PDF
No ratings yet
C 27
5 pages
C 41
PDF
No ratings yet
C 41
5 pages
C 5
PDF
No ratings yet
C 5
5 pages
C 6
PDF
No ratings yet
C 6
5 pages
C 2
PDF
No ratings yet
C 2
5 pages
Variables
PDF
No ratings yet
Variables
4 pages
Key Rules in C
PDF
No ratings yet
Key Rules in C
4 pages
C 39
PDF
No ratings yet
C 39
8 pages
C++ Lessons 6.1 - 6.4 (Variables)
PDF
No ratings yet
C++ Lessons 6.1 - 6.4 (Variables)
3 pages
Variables: - Symbol Representing A Place To Store Information
PDF
No ratings yet
Variables: - Symbol Representing A Place To Store Information
29 pages
Data Types
PDF
No ratings yet
Data Types
8 pages
Lecture 6
PDF
No ratings yet
Lecture 6
3 pages
Module 3
PDF
No ratings yet
Module 3
5 pages
C 3
PDF
No ratings yet
C 3
6 pages
C 21
PDF
No ratings yet
C 21
9 pages
CH 04
PDF
No ratings yet
CH 04
5 pages
Explain Variable in C
PDF
No ratings yet
Explain Variable in C
2 pages
Class 02 Guide
PDF
No ratings yet
Class 02 Guide
3 pages
VARIABLES
PDF
No ratings yet
VARIABLES
1 page
C Notes Complete
PDF
No ratings yet
C Notes Complete
18 pages
C 44
PDF
No ratings yet
C 44
9 pages
C 20
PDF
No ratings yet
C 20
5 pages
Unit 8 C Variables
PDF
No ratings yet
Unit 8 C Variables
20 pages