초보 개발자의 기록

[C] Variables 본문

카테고리 없음

[C] Variables

bambinodeveloper 2023. 9. 9. 05:25
728x90

Variables

Variables are containers for storing data vaules.

int: Stores integers without decimals

float: Stores floating point numbers with decimals

char: Stores single characters. Char values are surrounded by single quotes

 

Declaring (Creating) Variables

To create a variable,specify the type and assign it a value

type variableName = value;

The equal sign is used to assign a value to the variable.

You can also declare a variable without assigning the value, and assign the value later.

 

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.

 

int %d

char %c

float %f

 

To combine both text and a variable, separate them with a comma inside the print() function

 

Change Variable Values

If you assign a new value to an existing variable, it will overwrite the previous value

You can also assign the value of one variable to another or copy values to empty variables

 

Add Variables together

To add a variable to another variable, use the + operator

int x = 1;
int y = 2;
int sum = x + y;

 

Declare Multiple Variables

To declare more than one variable of the same type, use a comma-separated list

int x = 1, y = 2, z = 3;

 

Variable Names

All C varialbes must be identified with unique names which are called identifiers.

Identifiers can be short names or more descriptive names

It is recommended to use descriptive names in orfer to create understanable and maintainable code

 

General rules for maing variables

- Names can contain letters, digits and underscores

- Names must begin with a letter or an underscore(_)

- Names are case sentivie

- Names cannot contain whitespaces or special characters

- Reserved words (such as int) cannot be used as names

728x90
반응형