c programming basics
Hello, world in C ( the structure of C program)
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
}
- In C, the function to print something to the screen is
printf
, wheref
stands for “format”, meaning we can format the printed string in different ways. Then, we use parentheses to pass in what we want to print. We have to use double quotes to surround our text so it’s understood as text, and finally, we add a semicolon;
to end this line of code.
- To make our program work, we also need another line at the top, a header line
#include <stdio.h>
that defines theprintf
function that we want to use. Somewhere there is a file on our computer,stdio.h
, that includes the code that allows us to access theprintf
function, and the#include
line tells the computer to include that file with our program.
Variables
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Types, formats, operators
There are various data types we can use for our variables
- bool, a Boolean expression of either true or false
- char, a single character like a or 2
- double, a floating-point value with even more digits
- float, a floating-point value, or real number with a decimal value
- int, integers up to a certain size, or number of bits
- long, integers with more bits, so they can count higher
- string, a string of characters
For printf, too, there are different placeholders for each type:
- %c for chars
- %f for floats, doubles
- %i for ints
- %li for longs
- %s for strings
And there are some mathematical operators we can use:
- + for addition
- - for subtraction
- * for multiplication
- / for division
- % for remainder
Data Types
In C, we have different types of variables we can use for storing data:
- bool 1 byte
- char 1 byte
- int 4 bytes
- float 4 bytes
- long 8 bytes
- double 8 bytes
- string ? bytes
Each of these types takes up a certain number of bytes per variable we create, and the sizes above are what the visual studio, IDE, and most likely your computer uses for each type in C.
Memory
- Inside our computers, we have chips called RAM, random-access memory, that stores data for short-term use. We might save a program or file to our hard drive (or SSD) for long-term storage, but when we open it, it gets copied to RAM first. Though RAM is much smaller, and temporary (until the power is turned off), it is much faster.
- We can think of bytes, stored in RAM, as though they were in a grid:
![]() |
Memory |
- In reality, there are millions or billions of bytes per chip.
- In C, when we create a variable of type char, which will be sized one byte, it will physically be stored in one of those boxes in RAM. An integer, with 4 bytes, will take up four of those boxes.
- And each of these boxes is labeled with some number, or address, from 0, to 1, to 2, and so on.
No comments:
For Query and doubts!