Fundamentals of C Language: Basic Syntax and Data Types

Dear friends!

Welcome to the “beginner’s village” of C language!

Today, our first boss to defeat for leveling up is — basic syntax and data types!

It may seem unremarkable, but don’t forget: C language has no garbage collection and no type inference; every char, int, and float you write is your own responsibility!

So, follow me, and let’s solidify this foundation so that we can walk steadily on the road ahead!

1. Basic Structure of C Language

#include <stdio.h> // Header file
// Main function
int main() {  // Program code  printf("First C program\n");  return 0; // Return value}
#include <stdio.h> : This statement includes the standard input-output library, allowing the program to use output functions like printf.
int main() : This is the entry point of the C program, execution starts here. int indicates that the return type is an integer.
printf("First C program\n"); : This is an output statement that prints "First C program" to the console.

2. Data Types

(1). Basic Data Types

The data types in C language are mainly divided into the following categories:
Integer: Used to represent whole numbers.
· int: Standard integer, usually occupies 4 bytes.
· short: Short integer, usually occupies 2 bytes.
· long: Long integer, usually occupies 4 bytes (32-bit system) or 8 bytes (64-bit system).
· long long: Long long integer, usually occupies 8 bytes.
· size_t: Unsigned integer type, usually returned by the sizeof operator.
Floating-point: Used to represent decimal numbers.
· float: Single precision floating-point, usually occupies 4 bytes.
· double: Double precision floating-point, usually occupies 8 bytes.
· long double: Extended precision floating-point, usually occupies 12 or 16 bytes.
Character: Used to represent a single character.
· char: Character type, usually occupies 1 byte. Can store characters from the ASCII table.

(2). Modifiers

Data types can have different modifiers that control their size and sign:
· signed: Signed type (can represent negative, zero, and positive numbers).
· unsigned: Unsigned type (can only represent positive numbers and zero).
· short, long, etc. are used to adjust the size of data types.

(3). Constants and Variables

· Constant: The value of a constant cannot be changed during program execution. It can be declared using the const keyword.
const int a = 10; // a is a constant and cannot be modified
· Variable: A variable is a name used to store data in a program, and its value can be changed. When declaring a variable, its data type must be specified.
int a = 10; // a is a variable and can change its value

3. Examples of Common Basic Data Types

int

· Declare integer variables or functions<span><span>· Occupies bytes </span></span><span><span>4</span></span><span><span>· Represents the data range -2,147,483,648 ~ 2,147,483,647</span></span>

#include <stdio.h>
int main() {  int a = 10;  // Integer variable  printf("Output integer variable value: %d", a);  // Print integer variable  return 0;}

float

· Declare single precision floating-point variables or functions<span><span>· Occupies bytes 4</span></span><span><span>· Represents the data range 3.4E +/- 38 (7 digits)</span></span>

#include <stdio.h>
int main() {  float num = 3.14f;  // Single precision floating-point variable  printf("Output single precision floating-point variable value: %f\n", num);  // Print single precision floating-point variable  return 0;}

double

· Declare double precision variables or functions<span><span>· Occupies bytes 8</span></span><span><span>· Represents the data range 1.7E +/- 308 (15 digits)</span></span>

#include <stdio.h>
int main() {  double num = 3.14159;  // Double precision floating-point variable  printf("Output double precision floating-point variable value: %lf\n", num);  // Print double precision floating-point variable  printf("Output double precision floating-point variable value: %.2lf\n", num);  // Keep two decimal places  return 0;}

char

· Declare character variables or functions<span><span>· Occupies bytes 1</span></span><span><span>· Represents the data range -128 ~ 127</span></span>

#include <stdio.h>
int main() {  char letter = 'A';  // Character variable  printf("Output character variable value: %c\n", letter);  // Print character variable  return 0;}

long

· Declare long integer variables or functions<span><span>· Occupies bytes 4</span></span><span><span>· Represents the data range -2,147,483,648 ~ 2,147,483,647</span></span>

#include <stdio.h>
int main() {  long longNum = 1234567890L;  // Declare and initialize long integer variable  printf("Output long integer variable value: %ld\n", longNum);  // Output long integer value  return 0;}

long long

· Declare long long integer variables or functions<span><span>· Occupies bytes 8</span></span><span><span>· Represents the data range -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807</span></span>

#include <stdio.h>
int main() {  long long largeNumber = 1000000000000;  printf("%lld\n", largeNumber);  return 0;}

size_t

size_t is defined by <stddef.h> or <stdio.h> and is usually an unsigned integer type used to represent the size of memory. It may have different specific implementations on different platforms, but generally, it is a type that matches the platform’s word size, usually unsigned int or unsigned long.

Usage of size_t:
· Represents the size of an array: In many standard library functions, size_t is used to represent the size of an array or memory block.
· Memory management: For example, functions like malloc, calloc, realloc use size_t to specify the amount of memory to allocate.
· Array indexing: Although other integer types can be used as array indices, size_t is a more general choice because it guarantees the ability to represent any valid array index.
#include <stdio.h>#include <stddef.h>
int main() {  size_t size = 100; // Define size using size_t  printf("Size: %zu\n", size); // Print size_t type value  return 0;}
· %d is used to output integers.
· %f is used to output floating-point numbers.
· %c is used to output characters.
· %s is used to output strings.
· %u is used to output unsigned integers.
· %ld and %li are used to output long integers.
· %lu is used to output unsigned long integers.
· %x and %X are used to output hexadecimal numbers.
· %p is used to output pointer addresses.
· %e and %E are used to output floating-point numbers in scientific notation.
· %lld and %lli are used to output long long integer data.
· %g and %G are used to output floating-point numbers based on their size.

4. Input and Output

· Input: Use the scanf function to read data from user input.
· scanf() is the standard input function. To prevent invalid data input by the user, fgets() and sscanf() can be used instead.

scanf()

#include <stdio.h>
int main() {  int num;  printf("Please enter an integer:");  scanf("%d", &num);  return 0;}

fgets()

#include <stdio.h>
int main() {  char buffer[100];  printf("Please enter a line of text:");  fgets(buffer, sizeof(buffer), stdin);  // Read a line from standard input  printf("You entered: %s", buffer);  // Output the read text  return 0;}

sscanf()

#include <stdio.h>
int main() {  char str[] = "123 45.67 Hello";  int num1;  float num2;  char str2[20];  // Use sscanf to parse the string  int result = sscanf(str, "%d %f %s", &num1, &num2, str2);  // Output parsing results  if (result == 3) {    printf("Parsing successful:\n");    printf("Integer: %d\n", num1);    printf("Floating-point: %f\n", num2);    printf("String: %s\n", str2);  } else {    printf("Parsing failed, successfully parsed %d items of data.\n", result);  }  return 0;}

Dear friends!

Thus, we conclude the first stop of our C language fundamentals — basic syntax and data types.

This content is the cornerstone of learning C language and the starting point for stronger programming skills.Great buildings rise from the ground up; code quality starts from the basics. I hope everyone can truly understand the meaning of each syntax and each data type, and apply them freely in future coding.

I particularly like a saying from Comrade Xiaoping:

“The common people are not destined to follow us.”

I also understand that,readers are not destined to read my articles.

Because of this, I am more willing to put forth my sincerity, to write every piece of code, every sentence, and every article with care, just to retain every reader who is willing to click and learn.

I hope this article can lay a solid foundation for you and bring a little inspiration and help.

Next, we will continue to explore more core content of C language, such as operators, control flow, functions, etc.Friends, let us together, step by step, understand, master, and solidify C language!

In the next article, we will meet again!

Leave a Comment