Comp 1010 Programming 1: C variables, data types

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Type specifiers: Signed

signed int student_ID; • Need to reserve one bit for the sign • The signed qualifier can be used to explicitly tell the compiler that a particular variable is a signed quantity.

Doubles and storage

• Variables declared to be of type double can store roughly twice as many significant digits as can a variable of type float. • Most computers represent double values using 64 bits.

is the char constant '\n' a valid character?

Yes.(still prints a new line) • The character constant '\n'—the newline character—is a valid character

floats and scientific notation

You can express a float in scientific notation, e.g. 1.7e4 • 1.7 is called the mantissa • 4 is called the exponent • e can be written in lower or upper case

Integer expression in Hexadecimal number system

• If first digit is 0 and letter x, e.g 0x then the integer is expressed as Base 16 • E.g.0x5FE

Type specifiers: long, long long

• if the specifier long is placed directly before the int declaration, the declared integer variable is of extended range on some computer systems long int student_ID; • A constant value of type long int is formed by optionally appending the letter L(upper- or lowercase) onto the end of an integer constant with no SPACES

How to use long long?

• long long integer data type is guaranteed to be at least 64 bits wide • Instead of a single letter l, two ls are used in the printf string to display long long integers, as in "%lli"

How to sign?

•Need to reserve one bit for the sign. •Three ways: • Sign-Magnitude • 1's Complement • 2's Complement

Definition

Definition Defining something means providing all of the necessary information to create that thing in its entirety including where it is stored. • Often, the compiler only needs to have a declaration for something in order to compile a file into an object file, expecting that the linker can find the definition from another file. • If no source file ever defines a symbol, but it is declared, you will get errors at link time complaining about undefined symbols.

Basic data types

#include <stdio.h> int main (void){ //variable declaration int integerVar = 100; float floatingVar = 331.79; double doubleVar = 8.44e+11; char charVar = 'W'; _Bool boolVar = 0; //outputs to the console printf ("integerVar = %i\n", integerVar); printf ("floatingVar = %f\n", floatingVar); printf ("doubleVar = %e\n", doubleVar); printf ("doubleVar = %g\n", doubleVar); printf ("charVar = %c\n", charVar); printf ("boolVar = %i\n", boolVar); return 0;

is a char a string?

No. • Do not confuse a character constant, (a single character enclosed in single quotes); • with a character string, which is any number of characters enclosed in double quotes.

Working with variables

Something To Declare Declare it. state its type and then give its name. int a, b, c; Assignment Give a variable some value a =1 //This is called initialisation (You are giving the variable itsfirst //value

Actually using a float?

To use printf(), you can use %e for scientific notation • %g lets the printf() decide whether to use scientific or not • %f prints the float.

Type specifiers: Unsigned

unsigned int student_ID; • Only positive values. • Can represent whole numbers from 0 to 65,535 i.e. (0 to 216 - 1) • Restricting the use of an integer variable to the exclusive storage of positive integers, extends the accuracy of the stored value • An unsigned int constant is formed by placing the letter u (or U) after the constant unsigned int student_ID=1234567U; • When declaring variables to be of type long long int, long int, short int, or unsigned int, you can omit the keyword int. unsigned student_ID; • You can also declare char variables to be unsigned.

The Boolean Data

• A _Bool variable is defined in the language to be large enough to store just the values 0 and 1. • The precise amount of memory that is used is unspecified. • By convention, 0 is used to indicate a false value, and 1 indicates a true value.

char

• A char variable can be used to store a single character.1 • A character constant is formed by enclosing the character within a pair of single quotation marks. So 'a', ';', and '0' are all valid examples of character constants.

Floats in hexadecimal

• A hexadecimal floating constant consists of a leading 0x or 0X, followed by one or more decimal or hexadecimal digits, followed by a p or P, followed by an optionally signed binary exponent. • For example, 0x0.3p10 represents the value 3/16 × 210 = 0.5

How to use a long or long long double?(hint: its all in the capitilization!)

• A long double constant is written as a floating constant with the letter l or L • To display a long double, the L modifier is used. So, %Lf displays a long doublevalue in floating-point notation, %Le displays the same value in scientific notation, and %Lg tells printf() to choose between %Lf and %Le

Arithmetic expressions

• C, just as in virtually all programming languages: • the plus sign (+) is used to add two values, • the minus sign (−) is used to subtract two values, • the asterisk (*) is used to multiply two values, • and the slash (/) is used to divide two values.

Integers

• From the word Integral, i.e only represents the integral or whole number • No embedded spaces are NOT permitted • The size of int is either 2 bytes(In older PC's) or 4 bytes. • If you consider an integer having size of 4 byte( equal to 32 bits), it can take 232 distinct states as: -231 ,-231+1, ...,-2, -1, 0, 1, 2, ..., 231-2, 231-1. • If you try to store larger number than 231-1, i.e,+2147483647 and smaller number than -231 , i.e, -2147483648, program will not run correctly.

Type specifiers: short

• Store fairly small integer values. • The motivation for using short variables is primarily one of conserving memory space, which can be an issue in situations in which the program needs a lot of memory and the amount of available memory is limited. • A short int takes up half the amount of storage as a regular int variable does. • You are guaranteed that the amount of space allocated for a short int will NOT be less than 16 bits. • There is no way to explicitly write a constant of type short int in C. • To display a short int variable, place the letter h in front of any of the normal integer conversion characters: %hi, %ho, or %hx. • Alternatively, you can also use any of the integer conversion characters to display short ints, due to the way they can be converted into integers when they are passed as arguments to the printf()routine.

float

• Store values with a decimal place • You can omit digits afer the decimal or before the decimal • E.g 3. , .900, 2.3 etc are all valid

Double

• The double type is very similar to the float type, • Used whenever the range provided by a float variable is not sufficient.

Actually using char?

• The format characters %c can be used in a printf() call to display the value of a char variable at the terminal

Actually using the double?

• To display a double value, the format characters %f, %e, or %g, which are the same format characters used to display a float value, can be used(SAME AS FLOAT)

How to use long?

• To display the value of a long int using printf(), the letter l is used as a modifier before the integer format characters i, o, and x

Standard header for Booleans?

• To make it easier to work with _Bool variables in your program, the standard header file <stdbool.h> defines the values bool, true, and false

Integer expression in Octal number system

• Two special forms can be used to express an integer in Either base 8 (Octal) or Base 16 (Hexa) • If first digit is 0, then the integer is expressed as Base 8 • E.g 050 • All the digits after 0 must be valid base 8 digits • What is 0177 in decimal?(hint hint!)

What do you mean all floats are assumed to be Doubles in C???

• Unless told otherwise, all floating-point constants are taken as double values by the C compiler. • To explicitly express a float constant, append either an f or F to the end of the number, as follows: 12.5f


संबंधित स्टडी सेट्स

Physical Design (Low-Fi Prototypes)

View Set

PPR Competencies: Domain I (001)

View Set

ch24: International and Space Law

View Set

Chapter 16: Nursing Management During the Postpartum Period

View Set

Chapter 16 - Retailing and Omnichannel Marketing

View Set

NJ Laws, Rules, and Regulations Common to Life, Accident, and Health Property and Casualty

View Set

Medicine Bag CommonLit Questions & A Worn Path (English Week 17, Module 5)

View Set