Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 6, 2022 10:16 am GMT

My Simple Programs

Recently I've been learning how to create some basic programs in the C programming language with my father and brother.

hello.c

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

This is the first program we wrote, which says "Hello, world". On the first line we included stdio which is a library of functions. "stdio" stands for standard input / output.

The next part of our program is the main function. Every C program must have a main function to work.

int stands for integer, which is a whole number. The line int main(void) means that our main function returns a whole number to say if the function worked or failed. It returns 0 if it worked, or any other number if it failed.

The void means that we ignore any extra words on the command-line.

The printf stands for print formatted. This is a function that we use to output words. The
at the end of the string stands for newline, it moves onto the next line.

The last of the function return 0; means that our program succeeded.

hello_name.c

#include <stdio.h>int main(void){        char *name = "Bongo2";        printf("Hello %s
", name);}

In our second program, we changed it so that we print someone's name instead of "world", for example, "Bongo2". We created a string variable called name, and set it to "Bongo2".

In the printf %s stands for a string. In this case, the string is the name variable, which we set to "Bongo2".

maths.c

#include <stdio.h>int main(void){        int a = 18;        int b = 6;        printf("The answer is %d
", a * b);}

The last program we wrote was to do with maths. We used two int variables called a and b, and gave them the values 18 and 6.

The %d in the printf format string is going to print a whole number. The "d" stands for decimal or base 10. The number it prints is a calculation, a times b, which equals 108.


Original Link: https://dev.to/gamedev/my-simple-programs-3g97

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To