C Programming Tutorial
HTTP://www.programiz.com/c-programming/c-data-types
C programming is a popular programming language used for creating system and application software. Despite being fairly old, it is widely used because of its efficiency and control.
This C programming tutorial is intended for beginners who do not have any prior knowledge or have very little knowledge of computer programming. All the basic features are included in detail with explanation and output.
C Programming Keywords and Identifiers
In this tutorial, you will learn about keywords. Keywords are the reserved words in programming and are part of the syntax. Also, you will learn about identifiers and proper way to name an identifier.
Character set
Character set is a set of alphabets, letters and some special characters that are valid in C language.
Alphabets
Uppercase: A B C .................. X Y Z
Lower case: a b c ................... x y z
Digits0 1 2 3 4 5 6 7 8 9
Special Characters
HTTP://www.programiz.com/c-programming/c-data-types
C programming is a popular programming language used for creating system and application software. Despite being fairly old, it is widely used because of its efficiency and control.
This C programming tutorial is intended for beginners who do not have any prior knowledge or have very little knowledge of computer programming. All the basic features are included in detail with explanation and output.
C Programming Keywords and Identifiers
In this tutorial, you will learn about keywords. Keywords are the reserved words in programming and are part of the syntax. Also, you will learn about identifiers and proper way to name an identifier.
Character set
Character set is a set of alphabets, letters and some special characters that are valid in C language.
Alphabets
Uppercase: A B C .................. X Y Z
Lower case: a b c ................... x y z
Digits0 1 2 3 4 5 6 7 8 9
Special Characters
White space Characters
blank space, new line, horizontal tab, carriage return and form feed
KeywordsKeywords are predefined, reserved words used in programming that have special meaning. Keywords are part of the syntax and they cannot be used as an identifier.
For example:
blank space, new line, horizontal tab, carriage return and form feed
KeywordsKeywords are predefined, reserved words used in programming that have special meaning. Keywords are part of the syntax and they cannot be used as an identifier.
For example:
Here, int is a keyword that indicates 'money' is a variable of type integer.
As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.
As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.
Along with these keywords, C supports other numerous keywords depending upon the compiler.
All these keywords, their syntax and application will be discussed in their respective topics. However, if you want a brief information on these keywords without going further, visit list of all keywords in C programming.
Identifiers
Identifiers are the names you can give to entities such as variables, functions, structures etc.
Identifier names must be unique. They are created to give unique name to a C entity to identify it during the execution of a program. For example:
All these keywords, their syntax and application will be discussed in their respective topics. However, if you want a brief information on these keywords without going further, visit list of all keywords in C programming.
Identifiers
Identifiers are the names you can give to entities such as variables, functions, structures etc.
Identifier names must be unique. They are created to give unique name to a C entity to identify it during the execution of a program. For example:
Here, money and accountBalance are identifiers.
Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.
Rules for writing an identifier
C Programming Variables and Constants
Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.
Rules for writing an identifier
- A valid identifier can have letters (both uppercase and lowercase letters), digits and underscore only.
- The first letter of an identifier should be either a letter or an underscore. However, it is discouraged to start an identifier name with an underscore. It is because identifier that starts with an underscore can conflict with system names.
In such cases, compiler will complain about it. Some system names that start with underscore are _file no, _iob, _ open etc. - There is no rule on the length of an identifier. However, the first 31 characters of identifiers are discriminated by the compiler. So, the first 31 letters of two identifiers in a program should be different.
C Programming Variables and Constants
In this tutorial, you will learn about variables, rules for naming a variable, constants and different type of constants in C programming.
VariablesIn programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:
VariablesIn programming, a variable is a container (storage area) to hold data.
To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example:
Here, playerScore is a variable of integer type. The variable is holding 95 in the above code.
The value of a variable can be changed, hence the name 'variable'.
In C programming, you have to declare a variable before you can use it.
Rules for writing variable name in C
Constants/LiteralsA constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy" etc.
As mentioned, an identifier also can be defined as a constant.
The value of a variable can be changed, hence the name 'variable'.
In C programming, you have to declare a variable before you can use it.
Rules for writing variable name in C
- A variable name can have letters (both uppercase and lowercase letters), digits and underscore only.
- The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error.
- There is no rule on how long a variable can be. However, the first 31 characters of a variable are discriminated by the compiler. So, the first 31 letters of two variables in a program should be different.
Constants/LiteralsA constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy" etc.
As mentioned, an identifier also can be defined as a constant.
Integer constant sA integer constant is a numeric constant (associated with number) without any fractional or exponential part. There are three types of integer constants in C programming:
- decimal constant(base 10)
- octal constant(base 8)
- hexadecimal constant(base 16)
In C programming, octal constant starts with a 0 and hexadecimal constant starts with a 0x.
Floating-point constant
sA floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:
Floating-point constant
sA floating point constant is a numeric constant that has either a fractional form or an exponent form. For example:
Note: E-5 = 10-5
Character constants A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F'
Escape SequencesSometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: new line(enter), tab, question mark etc. In order to use these characters, escape sequence is used.
For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.
Character constants A character constant is a constant which uses single quotation around characters. For example: 'a', 'l', 'm', 'F'
Escape SequencesSometimes, it is necessary to use characters which cannot be typed or has special meaning in C programming. For example: new line(enter), tab, question mark etc. In order to use these characters, escape sequence is used.
For example: \n is used for newline. The backslash ( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.
String constantsString constants are the constants which are enclosed in a pair of double-quote marks. For example:
Enumeration constantsKeyword enum is used to define enumeration types. For example:
enum color {yellow, green, black, white};
Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively. For more information.
C Programming Data Types
Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively. For more information.
C Programming Data Types
In C programming, variables or memory locations should be declared before it can be used. Similarly, a function also needs to be declared before use.
Data types simply refer to the type and size of data associated with variables and functions.
Data types in C
Integer data typesIntegers are whole numbers that can have both positive and negative values, but no decimal values. Example: 0, -5, 10
In C programming, keyword int is used for declaring integer variable. For example:
Data types simply refer to the type and size of data associated with variables and functions.
Data types in C
- Fundamental Data Types
- Integer types
- Floating type
- Character type
- Derived Data Types
Integer data typesIntegers are whole numbers that can have both positive and negative values, but no decimal values. Example: 0, -5, 10
In C programming, keyword int is used for declaring integer variable. For example:
Here, id is a variable of type integer.
Floating types
Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare a floating point variable in C by using either float or double keyword. For example:
Floating types
Floating type variables can hold real numbers such as: 2.34, -9.382, 5.0 etc. You can declare a floating point variable in C by using either float or double keyword. For example:
Here, both accountBalance and bookPrice are floating type variables.
In C, floating values can be represented in exponential form as well. For example:
In C, floating values can be represented in exponential form as well. For example:
Difference between float and double
The size of float (single precision float data type) is 4 bytes. And the size of double(double precision float data type) is 8 bytes. Floating point variables has a precision of 6 digits whereas the precision of double is 14 digits.
Character typesKeyword char is used for declaring character type variables. For example:
The size of float (single precision float data type) is 4 bytes. And the size of double(double precision float data type) is 8 bytes. Floating point variables has a precision of 6 digits whereas the precision of double is 14 digits.
Character typesKeyword char is used for declaring character type variables. For example:
Here, test is a character variable. The value of test is 'h'.
The size of character variable is 1 byte.
C QualifiersQualifiers alters the meaning of base data types to yield a new data type.
Constant qualifiersAn identifier can be declared as a constant. To do so const keyword is used.
The size of character variable is 1 byte.
C QualifiersQualifiers alters the meaning of base data types to yield a new data type.
Constant qualifiersAn identifier can be declared as a constant. To do so const keyword is used.
The value of cost cannot be changed in the program.
Volatile qualifiersA variable should be declared volatile whenever its value can be changed by some external sources outside the program. Keyword volatile is used for creating volatile variables.
C Programming Input Output (I/O): printf() and scanf()
This tutorial focuses on two in-build functions printf() and scanf() to perform I/O task in C programming. Also, you will learn how you write a valid program in C.
Volatile qualifiersA variable should be declared volatile whenever its value can be changed by some external sources outside the program. Keyword volatile is used for creating volatile variables.
C Programming Input Output (I/O): printf() and scanf()
This tutorial focuses on two in-build functions printf() and scanf() to perform I/O task in C programming. Also, you will learn how you write a valid program in C.
C programming has several in-build library functions to perform input and output tasks.
Two commonly used functions for I/O (Input/Output) are printf() and scanf().
The scanf() function reads formatted input from standard input (keyboard) whereas theprintf() function sends formatted output to the standard output (screen).
Example #1: C Output
Two commonly used functions for I/O (Input/Output) are printf() and scanf().
The scanf() function reads formatted input from standard input (keyboard) whereas theprintf() function sends formatted output to the standard output (screen).
Example #1: C Output
Output
How this program works?
- All valid C program must contain the main() function. The code execution begins from the start of main() function.
- The printf() is a library function to send formatted output to the screen. The printf()function is declared in "stdio.h" header file.
- Here, stdio.h is a header file (standard input output header file) and #include is a pre processor directive to paste the code from the header file when necessary. When the compiler encounters printf() function and doesn't find stdio.h header file, compiler shows error.
- The return 0; statement is the "Exit status" of the program. In simple terms, program ends.
Output
Inside the quotation of printf() function, there is a format string "%d" (for integer). If the format string matches the argument (testInteger in this case), it is displayed on the screen.
Example #3: C Integer Input/Output
Example #3: C Integer Input/Output
Output
The format string "%f" is used to read and display formatted in case of floats.
Example #4: C Character I/O
Example #4: C Character I/O
Output
Format string %c is used in case of character types.
Little bit on ASCII codeWhen a character is entered in the above program, the character itself is not stored. Instead a numeric value(ASCII value) is stored. And when we displayed that value using"%c" text format, the entered character is displayed.
Example #5: C ASCII Code
Little bit on ASCII codeWhen a character is entered in the above program, the character itself is not stored. Instead a numeric value(ASCII value) is stored. And when we displayed that value using"%c" text format, the entered character is displayed.
Example #5: C ASCII Code
Output
You can display a character if you know ASCII code of that character. This is shown by following example.
Example #6: C ASCII Code
Example #6: C ASCII Code
Output
Click here to learn more about the complete ASCII reference.
More on Input/Output of floats and IntegersInteger and floats can be displayed in different formats in C programming.
Example #7: I/O of Floats and Integers
More on Input/Output of floats and IntegersInteger and floats can be displayed in different formats in C programming.
Example #7: I/O of Floats and Integers
Output
C Programming Operators
C programming has various operators to perform tasks including arithmetic, conditional and bitwise operations. You will learn about various C operators and how to use them in this tutorial.
C programming has various operators to perform tasks including arithmetic, conditional and bitwise operations. You will learn about various C operators and how to use them in this tutorial.
An operator is a symbol which operates on a value or a variable. For example: + is an operator to perform addition.
C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as:
C programming has wide range of operators to perform various operations. For better understanding of operators, these operators can be classified as:
C Arithmetic OperatorsAn arithmetic operator performs mathematical operations such as addition, subtraction and multiplication on numerical values (constants and variables).
Example #1: Arithmetic Operators
Output
The operators +, - and * computes addition, subtraction and multiplication respectively as you might have expected.
In normal calculation, 9/4 = 2.25. However, the output is 2 in the program. It is because both variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a = 9 is divided by b = 4, the remainder is 1. The % operator can only be used with integers.
In normal calculation, 9/4 = 2.25. However, the output is 2 in the program. It is because both variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a = 9 is divided by b = 4, the remainder is 1. The % operator can only be used with integers.
Increment and decrement operatorsC programming has two operators increment ++ and decrement -- to change the value of an operand (constant or variable) by 1.
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.
Example #2: Increment and Decrement Operators
Increment ++ increases the value by 1 whereas decrement -- decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.
Example #2: Increment and Decrement Operators
Output
Here, the operators ++ and -- are used as prefix. These two operators can also be used as postfix like a++ and a--. Visit this page to learn more on how increment and decrement operators work when used as postfix.
C Assignment OperatorsAn assignment operator is used for assigning a value to a variable. The most common assignment operator is =
C Assignment OperatorsAn assignment operator is used for assigning a value to a variable. The most common assignment operator is =
Example #3: Assignment Operators
Example #4: Relational Operators
C Logical OperatorsAn expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making in C programming
Example #5: Logical Operators
Explanation of logical operator program
Bitwise operators are used in C programming to perform bit-level operations.
- (a = b) && (c > 5) evaluates to 1 because both operands (a = b) and (c > b) is 1 (true).
- (a = b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
- (a = b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
- (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
- !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
- !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
Bitwise operators are used in C programming to perform bit-level operations.
Other OperatorsComma OperatorComma operators are used to link related expressions together. For example:
The size of operatorThe size of is an unary operator which returns the size of data (constant, variables, array, structure etc).
Example #6: size of Operator
Example #6: size of Operator
Conditional Operator Syntax
The conditional operator works as follows:
- The first expression conditionalExpression is evaluated at first. This expression evaluates to 1 if it's and evaluates to 0 if it's false.
- If conditionalExpression is true, expression1 is evaluated.
- If conditionalExpression is false, expression2 is evaluated.
Output
C Programming if, if...else and Nested if...else Statement
Decision making is used to specify the order in which statements are executed. In this tutorial, you will learn to put decision making in a C program using if, if..else and nested if...else statement
Decision making is used to specify the order in which statements are executed. In this tutorial, you will learn to put decision making in a C program using if, if..else and nested if...else statement
C if statement
Flowchart of if statement
Example #1: C if statement
When user enters -2, the test expression (number < 0) becomes true. Hence, You entered -2 is displayed on the screen.
Output 2
Output 2
When user enters 5, the test expression (number < 0) becomes false and the statement inside the body of if is skipped.
C if...else statementThe if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).
Syntax of if...else
C if...else statementThe if...else statement executes some code if the test expression is true (nonzero) and some other code if the test expression is false (0).
Syntax of if...else
If test expression is true, code inside the body of if statement is executed; and code inside the body of else statement is skipped.
If test expression is false, code inside the body of else statement is executed; and code inside the body of if statement is skipped.
Flowchart of if...else statement
If test expression is false, code inside the body of else statement is executed; and code inside the body of if statement is skipped.
Flowchart of if...else statement
figure :flowchart of if ...else stetement
Example #2: C if...else statement
Output
When user enters 7, the test expression ( number%2 == 0 ) is evaluated to false. Hence, the statement inside the body of else statement printf("%d is an odd integer"); is executed and the statement inside the body of if is skipped.
Nested if...else statement (if...elseif....else Statement)The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
Syntax of nested if...else statement
Nested if...else statement (if...elseif....else Statement)The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The nested if...else statement allows you to check for multiple test expressions and execute different codes for more than two conditions.
Syntax of nested if...else statement
Example #3: C nested if...else statement
C Programming for Loop
Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn to create a for loop in C programming.
Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:
Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn to create a for loop in C programming.
Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:
- for loop
- while loop
- do...while loop
How for loop works?The initialization statement is executed only once.
Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated. This process repeats until the test expression is false.
The for loop is commonly used when the number of iterations is known.
To learn more on test expression (when test expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators.
Example: for loop
Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated. This process repeats until the test expression is false.
The for loop is commonly used when the number of iterations is known.
To learn more on test expression (when test expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators.
Example: for loop
Output
The value entered by the user is stored in variable n. Suppose the user entered 10.
The count is initialized to 1 and the test expression is evaluated. Since, the test expression count <= n (1 less than or equal to 10) is true, the body of for loop is executed and the value of sum will be equal to 1.
Then, the update statement ++count is executed and count will be equal to 2. Again, the test expression is evaluated. The test expression is evaluated to true and the body of for loop is executed and the sum will be equal to 3. And, this process goes on.
Eventually, the count is increased to 11. When the count is 11, the test expression is evaluated to 0 (false) and the loop terminates.
C programming while and do...while Loop
Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn how to create a while and do...while loop in C programming.
Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:
The count is initialized to 1 and the test expression is evaluated. Since, the test expression count <= n (1 less than or equal to 10) is true, the body of for loop is executed and the value of sum will be equal to 1.
Then, the update statement ++count is executed and count will be equal to 2. Again, the test expression is evaluated. The test expression is evaluated to true and the body of for loop is executed and the sum will be equal to 3. And, this process goes on.
Eventually, the count is increased to 11. When the count is 11, the test expression is evaluated to 0 (false) and the loop terminates.
C programming while and do...while Loop
Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn how to create a while and do...while loop in C programming.
Loops are used in programming to repeat a specific block until some end condition is met. There are three loops in C programming:
- for loop
- while loop
- do...while loop
How while loop works?The while loop evaluates the test expression.
If the test expression is true (nonzero), codes inside the body of while loop is evaluated. Then, again the test expression is evaluated. The process goes on until the test expression is false.
When the test expression is false, the while loop is terminated.
Flowchart of while loop
If the test expression is true (nonzero), codes inside the body of while loop is evaluated. Then, again the test expression is evaluated. The process goes on until the test expression is false.
When the test expression is false, the while loop is terminated.
Flowchart of while loop
figure flowchart of while loop
Example #1: while loop
To learn more on test expression (when test expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators.
do...while loopThe do..while loop is similar to the while loop with one important difference. The body ofdo...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once.
do...while loopThe do..while loop is similar to the while loop with one important difference. The body ofdo...while loop is executed once, before checking the test expression. Hence, the do...while loop is executed at least once.
C Programming break and continue Statement
Syntax of break statement
Syntax of break statement
How break statement works?
Example #1: break statement
This program calculates the sum of maximum of 10 numbers. It's because, when the user enters negative number, the break statement is executed and loop is terminated.
In C programming, break statement is also used with switch...case statement.
continue StatementThe continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else.
Syntax of continue Statement
In C programming, break statement is also used with switch...case statement.
continue StatementThe continue statement skips some statements inside the loop. The continue statement is used with decision making statement such as if...else.
Syntax of continue Statement
Flowchart of continue Statement
How continue statement works?
Example #2: continue statement
In the program, when the user enters positive number, the sum is calculated using sum += number; statement.
When the user enters negative number, the continue statement is executed and skips the negative number from calculation.
C Programming switch...case Statement
When the user enters negative number, the continue statement is executed and skips the negative number from calculation.
C Programming switch...case Statement
In this tutorial, you will learn to put a switch statement in C programming with the help of an example.
The nested if...else statement allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in nested if...else statement, it is better to use switch statement.
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
Syntax of switch...case
The nested if...else statement allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in nested if...else statement, it is better to use switch statement.
The switch statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.
Syntax of switch...case
When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.
In the above pseudo code, suppose the value of n is equal to constant2. The compiler will execute the block of code associate with the case statement until the end of switch block, or until the break statement is encountered.
The break statement is used to prevent the code running into the next case.
Example: switch Statement
The - operator entered by the user is stored in operator variable. And, the two operands, 32.5 and 12.4 are stored in variable firstNumber and secondNumber respectively.
Then, control of the program jumps to
Then, control of the program jumps to
Finally, the break; statement ends the switch statement.
C Programming goto Statement
Syntax of goto statement
C Programming goto Statement
Syntax of goto statement
The label is an identifier. When goto statement is encountered, control of the program jumps to label: and starts executing the code.
Example: goto Statement
Example: goto Statement
Reasons to avoid goto statementThe use of goto statement may lead to code that is buggy and hard to follow. For example:
Also, goto statement allows you to do bad stuff such as jump out of scope.
That being said, goto statement can be useful sometimes. For example: to break from nested loops.
Should I or shouldn't I use goto statement?If you think the use of goto statement simplifies your program. By all means use it. The goal here is to create code that your fellow programmer can understand easily.
That being said, goto statement can be useful sometimes. For example: to break from nested loops.
Should I or shouldn't I use goto statement?If you think the use of goto statement simplifies your program. By all means use it. The goal here is to create code that your fellow programmer can understand easily.