C programming basics

Hello, World

The following code is just a basic Hello, World program where text is printed toa screen.

#include <stdio.h>
int main(void) {
    printf("Hello, World\n");
    return 0;
}

Going line by line:

  • #include <stdio.h> - this includes a standard IO header file from your systemin your program, so the printf function works. In C programs there are manystandard library header files used from the system and they come with thecompiler, so you don't need to reinvent everything and you have best practicesat your disposal.
  • int main(void) - each C program starts with a main() function. The intkeyword indicates that main returns a value of integer type. The voidkeyword indicates that function doeesn't take any arguments.
  • { and } - curly brackets wrap a block of code such as main function.
  • printf("Hello, World\n"); - this prints text Hello, World text and a newline on your screen when the program is compiled and run. The additional \n standsfor a new line character.
  • return 0; - the main function returns a value 0.

Compiling

In order to run your Hello, World program, you need to first compile it.

To compile a C program, you need to have a compiler. In the above case, we will use agcc compiler.

gcc hello.c -o hello.o
  • gcc is a command for running a compiler
  • hello.c is a C source code file with the above code
  • -o hello.o is optional and defines a binary file that is created. If left outthe output file will be automatically set by the compiler. For example, a.out.

You can then run this C program by executing

./hello.o

Which prints in the command line:

Hello, World

Arguments and return values

You can pass arguments to main():

#include <stdio.h>

int main(int argc, char **argv) {
    printf("Hello, %s\n", argv[1]);
    return 0;
}
  • int main(...) - means that the main() function will return a value of integertype.
  • int argc - first argument defines the number of arguments that will be passedto the compiled program.
  • char **argv - the second argument defines an array of character strings.

Let's compile and run the program:

gcc hello.c -o hello.o
./hello.o "PHP"

The output of the above program will be:

Hello, PHP

Code comments

In C there are two types of comments:

  • Single line comments
#include <stdio.h>

// This is a single line comment.
int main(void) {
    printf("Hello, World\n");
    return 0;
}
  • Multi-line comments

They are wrapped between /* and */:

#include <stdio.h>

/*
 * This is a multi-line comment.
 */
int main(void) {
    printf("Hello, World\n");
    return 0;
}

Function printf()

Above has already been used the printf() function.

Arrays, loops, and break

Array can be imagined as a container with a fixed number of elements, where eachelement is indexed from 0 incrementally:

| Array element | H | e | l | l | o || Array index | 0 | 1 | 2 | 3 | 4 |

Let's check a code example:

#include <stdio.h>

char *strings[5];

int main() {
    int i;

    strings[0] = "H";
    strings[1] = "e";
    strings[2] = "l";
    strings[3] = "l";
    strings[4] = "o";

    for (i = 0; i < 5; i++) {
        printf("%s\n", strings[i]);
    }
}
SCO Hosting Tutorials

About SCO Hosting

SCO Hosting Tutorials best site where you can find different tutorials collected from around the internet.