You are on page 1of 4

C repetition

Contents
C repetition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Code template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Function: printf . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Conditional statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1
C repetition
The following PDF was created to remind you of the most commonly used elements of the C language.
It includes examples of correct syntax and models of major constructs, including loops and conditional
statements. The function of individual instructions is not explored, so you should approach reading this
repetition with a basic knowledge of the C language.

Code template

#include <stdio.h>

int main(int argc, char *argv[])


{
// Insert your code here

return 0;
}

The number of arguments given to the program is stored in the variable argc, and these arguments in the
table argv.
Note: The first position (argv[0]) always contains the name of the executable file, so we count the given
positional arguments from the second element.

Function: printf

int printf ( const char * text, ...);

We use the printf function to print information to the standard output. As the first argument, we can
provide a string in the form of c-string or formatted text, to which variables will be given in subsequent
arguments.
Example of use:
#include <stdio.h>

int main(int argc, char *argv[])


{
int a = 10;
printf("a is equal to %d\n", a);
return 0;
}

Output:
a is equal to 10

2
Comments

// Single line comment

/* Multi
line
comment */

One-line comments begin with the symbols //. A multi-line comment block begins with /* and ends with
*/.

Data types
There are four basic data types in C:
• char - stores a character,
• int - stores an integer,
• float - stores a floating point number,
• double - stores a double precision floating point number.

Conditional statements
if statement:
if ( <condition> )
{
<statements>
}

if/else statement:
if ( <condition> )
{
<statements1>
}
else
{
<statements2>
}

if/else if/else statement:


if ( <condition1> )
{
<statements1>
}
else if ( <condition2> )
{
<statements2>
}
else
{
<statements3>
}

3
switch statement:
switch ( <condition> )
{
case <value1>:
<statements1>
break;
case <value2>:
<statements2>
break;
default:
<statements3>
}

Loops
while loop:
while ( <condition> )
{
<statements>
}

do-while loop:
do
{
<statements>
} while ( <condition> );

for loop:
for (int i = 0; i < count;i++)
{
<statements>
}

Functions
Declaring your own function:
return_type function_name(argument_type argument)
{
<statements>
}

Example of use:
void say_hello()
{
printf("Hello!");
}

You might also like