C Language
C Language
C Language
What is C?
C is a general-purpose programming language created
by Dennis Ritchie at the Bell Laboratories in 1972.
It is a very popular language, despite being old.
C is strongly associated with UNIX, as it was developed
to write the UNIX operating system.
Why Learn C?
It is one of the most popular programming
C New Lines
New Lines
To insert a new line, you can use the \n character:
Example
#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
You can also output multiple lines with a
single printf() function. However, this could make the
code harder to read:
Example
#include <stdio.h>
int main() {
printf("Hello World!\nI am learning C.\nAnd it is
awesome!");
return 0;
}
Two \n characters after each other will create a blank
line:
Example
#include <stdio.h>
int main() {
printf("Hello World!\n\n");
printf("I am learning C.");
return 0;
}
What is \n exactly?
The newline character (\n) is called an escape sequence,
and it forces the cursor to change its position to the
beginning of the next line on the screen. This results in
a new line.
, < > . _
( ) ; $ :
% [ ] # ?
^ ! * / |
- \ ~ +
White space Characters
Blank space, newline, horizontal tab, carriage return
and form feed.
C Keywords
Keywords are predefined, reserved words used in
programming that have special meanings to the
compiler. Keywords are part of the syntax and they
cannot be used as an identifier. For example:
int money;
Here, int is a keyword that indicates money is
a variable of type int (integer).
As C is a case sensitive language, all keywords must be
written in lowercase. Here is a list of all keywords
allowed in ANSI C.
C Keywords
do if static while
Variables
In programming, a variable is a container (storage area)
to hold data.
To indicate the storage area, each variable should be
given a unique name (identifier). Variable names are
just the symbolic representation of a memory location.
For example:
int playerScore = 95;
Here, playerScore is a variable of int type. Here, the
variable is assigned an integer value 95.
The value of a variable can be changed, hence the name
variable.
char ch = 'a';
Rules for naming a variable
1. A variable name can only have letters (both
uppercase and lowercase letters), digits and
underscore.
2. The first letter of a variable should be either a letter
or an underscore.
3. There is no rule on how long a variable name
(identifier) can be. However, you may run into
problems in some compilers if the variable name is
longer than 31 characters.
Note: You should always try to give meaningful names
to variables. For example: firstName is a better variable
name than fn.
C is a strongly typed language. This means that the
variable type cannot be changed once it is declared. For
example:
int number = 5; // integer variable
number = 5.5; // error
double number; // error
Here, the type of number variable is int. You cannot
assign a floating-point (decimal) value 5.5 to this
variable. Also, you cannot redefine the data type of the
variable to double. By the way, to store the decimal
values in C, you need to declare its type to
either double or float.
Constants
If you want to define a variable whose value cannot be
changed, you can use the const keyword. This will
create a constant. For example,
const double PI = 3.14;
Notice, we have added keyword const.
Here, PI is a symbolic constant; its value cannot be
changed.
const double PI = 3.14;
PI = 2.9; //Error
C Data Types
In C programming, data types are declarations for
variables. This determines the type and size of data
associated with variables. For example,
int myVar;
Here, myVar is a variable of int (integer) type. The size
of int is 4 bytes.
Basic types
Here's a table containing commonly used types in C
programming for quick access.
Format
Type Size (bytes)
Specifier
char 1 %c
float 4 %f
double 8 %lf
signed char 1 %c
unsigned char 1 %c
negative numbers
unsigned - allows for storage of only positive
numbers
For example,
// valid codes
unsigned int x = 35;
int y = -35; // signed int
int z = 36; // signed int