Photo by Anete Lūsiņa on Unsplash
Getting Started with C Programming: A Beginner's Guide
Will also be helpful for those taking the 1-year ALX Software Engineering course
C programming is a powerful and widely-used language that has been around for decades. It's known for its low-level access to the system, high performance, and versatility, making it a great choice for many types of projects.
Who Invented C?
C was developed by Dennis Ritchie and Brian Kernighan at Bell Labs in the early 1970s. Ritchie is often referred to as the "father of C" for his significant contributions to the language. Linus Torvalds, the creator of Linux, also wrote much of the Linux kernel in C.
What Happens When You Type gcc main.c
?
When you type gcc main.c
, you are compiling the main.c
file using the GCC (GNU Compiler Collection) compiler. The GCC compiler translates the C code in the file into machine code that can be executed by a computer. The output of the compiler is an executable file, which can be run to see the results of the program.
What is an Entry Point?
Similar to planet re-entry
The entry point of a C program is the first function that is executed when the program runs. The entry point of a C program is typically the main
function.
What is main
?
The main
function is the entry point of a C program. It is the first function that is executed when the program runs. The main
function is where you write the code that makes up your program.
How to Print Text Using printf
, puts
and putchar
?
To print text to the console in a C program, you can use the printf
function. This function allows you to specify a format string and a set of arguments that will be printed to the console. For example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
You can also use the puts
function to print a string to the console, followed by a newline:
#include <stdio.h>
int main() {
puts("Hello, World!\n");
return 0;
}
And if you just want to print a single character, you can use the putchar
function:
#include <stdio.h>
int main() {
putchar('H');
putchar('e');
putchar('l');
putchar('l');
putchar('o');
putchar(',');
putchar(' ');
putchar('W');
putchar('o');
putchar('r');
putchar('l');
putchar('d');
putchar('!');
putchar('\n');
return 0;
}
You might be asked to create your own putchar function in a separate _putchar.c file
#include <unistd.h>
/**
* _putchar - writes the character c to stdout
* @c: The character to print
*
* Return: On success 1.
* On error, -1 is returned, and errno is set appropriately.
*/
int _putchar(char c)
{
return (write(1, &c, 1));
}
How to Get the Size of a Specific Type Using the Unary Operator sizeof
?
The sizeof
operator in C can be used to determine the size of a specific type or variable in bytes. For example:
#include <stdio.h>
int main() {
printf("The size of an int is %ld bytes\n", sizeof(int));
return 0;
}
How to Compile Using GCC?
To compile a C program using GCC, you simply need to type gcc
followed by the name of the source file and the -o
option, which specifies the name of the output file. For example:
gcc main.c -o main
This command will compile the main.c
file and produce an executable file named main
. You can then run the program by typing ./main
.
What is the Default Program Name When Compiling with GCC?
If you do not specify the -o
option when compiling with GCC, the default program name will be a.out
.
What is the Official C Coding Style and How to Check Your Code with betty-style
?
The official C coding style is known as the "K&R style" after its authors, Brian Kernighan, and Dennis Ritchie. It's a widely-used and well-established style that is easy to read and understand.
To check your code against the K&R style, you can use the betty-style
tool. Simply type betty-style
followed by the name of the source file:
betty-style main.c
How to Find the Right Header to Include in Your Source Code When Using a Standard Library Function?
C comes with a standard library that provides a variety of functions for common tasks, such as input/output, memory management, and string manipulation. To use a function from the standard library, you need to include the appropriate header file in your source code.
Nonetheless, you might be asked at times to create your own Header File. In the below example, my header file is called main.h
and it contains all the prototypes for my programs:
#ifndef MAIN_H
#define MAIN_H
int _putchar(char c);
int _isupper(int c);
int _isdigit(int c);
int mul(int a, int b);
void print_numbers(void);
void print_most_numbers(void);
void more_numbers(void);
void print_line(int n);
void print_diagonal(int n);
void print_square(int size);
void print_triangle(int size);
void print_number(int n);
#endif
How do we include this in our program?
To use the standard printf
function, as well as other defined functions and prototypes like the ones we just created, you need to include the <stdio.h>
header file, as well as the header file you created main.h
:
#include <stdio.h>
#include "main.h"
int main() {
printf("Hello, World!\n");
return 0;
}
We will cover the use of the "main.h"
in another post, so don't worry.
What is the difference between <headerfile.h>
and "headerfile.h"
?
Header files can be included in a program using either angle brackets <headerfile.h>
or double quotes "headerfile.h"
.
When a header file is included using angle brackets
<headerfile.h>
, the compiler searches for the file in the standard system directories. These directories contain header files that are part of the C standard library or other system libraries.On the other hand, when a header file is included using double quotes
"headerfile.h"
, the compiler searches for the file in the current directory first. If it is not found there, it searches for it in the standard system directories.
In general, angle brackets are used to include system headers, while double quotes are used to include user-defined headers.
How does the main
Function Influence the Return Value of the Program?
The return value of the program is determined by the return
statement in the main
function. A return value of 0
typically indicates that the program ran successfully, while a non-zero return value typically indicates an error.
For example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
In this program, the main
function returns 0
, indicating that the program ran successfully.
In conclusion, C programming is a great language for beginners to learn, and this guide has covered some of the essential topics to get you started. Happy coding!"