C User Input

User Input

You have already learned that printf() is used to output values in C.

To get user input, you can use the scanf() function:

Example

Output a number entered by the user :

// Create an integer variable that will store the number we get from the user
int myNum;

// Ask the user to type a number
printf("Type a number: \n");

// Get and save the number the user types
scanf("%d", &myNum);

// Output the number the user typed
printf("Your number is: %d", myNum);

The scanf() function takes two arguments: the format specifier of the variable (%d in the example above) and the reference operator (&myNum), which stores the memory address of the variable.

 

Continue reading C User Input

C String Functions

String Functions

C also has many useful string functions, which can be used to perform certain operations on strings.

To use them, you must include the <string.h> header file in your program:

#include <string.h>

Continue reading C String Functions

C Special Characters

Strings – Special Characters

Because strings must be written within quotes, C will misunderstand this string, and generate an error:

char txt[] = “We are the so-called “Vikings” from the north.”;

The solution to avoid this problem, is to use the backslash escape character.

The backslash (\) escape character turns special characters into string characters: Continue reading C Special Characters

C Strings

Strings

Strings are used for storing text/characters.

For example, “Hello World” is a string of characters.

Unlike many other programming languages, C does not have a String type to easily create string variables. Instead, you must use the char type and create an array of characters to make a string in C:

char greetings[] = “Hello World!”;

Note that you have to use double quotes (""). Continue reading C Strings

C Multidimensional Arrays

Multidimensional Arrays

In the previous chapter, you learned about arrays, which is also known as single dimension arrays. These are great, and something you will use a lot while programming in C. However, if you want to store data as a tabular form, like a table with rows and columns, you need to get familiar with multidimensional arrays.

A multidimensional array is basically an array of arrays. Continue reading C Multidimensional Arrays

C Arrays – Examples

Real-Life Example

To demonstrate a practical example of using arrays, let’s create a program that calculates the average of different ages:

Example

// An array storing different ages
int ages[] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum = 0;
int i;

// Get the length of the array
int length = sizeof(ages) / sizeof(ages[0]);

// Loop through the elements of the array
for (i = 0; i < length; i++) {
  sum += ages[i];
}

// Calculate the average by dividing the sum by the length
avg = sum / length;

// Print the average
printf("The average age is: %.2f", avg);

Continue reading C Arrays – Examples

C Array Size

Get Array Size or Length

To get the size of an array, you can use the sizeof operator:

Example

int myNumbers[] = {10, 25, 50, 75, 100};
printf(“%lu”, sizeof(myNumbers)); // Prints 20

Why did the result show 20 instead of 5, when the array contains 5 elements?

– It is because the sizeof operator returns the size of a type in bytes.

You learned from the Data Types article that an int type is usually 4 bytes, so from the example above, 4 x 5 (4 bytes x 5 elements) = 20 bytes. Continue reading C Array Size

C Arrays

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To create an array, define the data type (like int) and specify the name of the array followed by square brackets [].

To insert values to it, use a comma-separated list inside curly braces, and make sure all values are of the same data type:

int myNumbers[] = {25, 50, 75, 100};

We have now created a variable that holds an array of four integers. Continue reading C Arrays

C Break and Continue

Break

You have already seen the break statement used in an earlier chapter of this tutorial. It was used to “jump out” of a switch statement.

The break statement can also be used to jump out of a loop. Continue reading C Break and Continue

C For Loop Examples

Real-Life Examples

To demonstrate a practical example of the for loop, let’s create a program that counts to 100 by tens:

Example

for (i = 0; i <= 100; i += 10) {
  printf("%d\n", i);
}

In this example, we create a program that only print even numbers between 0 and 10 (inclusive): Continue reading C For Loop Examples