C-language: quick n00b question

  • Thread starter kheiron1729
  • Start date
In summary, the C language is a general-purpose programming language developed in the early 1970s for creating operating systems, graphics applications, and other programs. It is a low-level language with manual memory management and offers more control over hardware resources. Its basic data types include integers, floating-point numbers, characters, and pointers. The main function is the entry point for all C programs and is where the program starts executing. To learn C as a beginner, there are many resources available, including online tutorials and joining online communities and forums.
  • #1
kheiron1729
5
0
Code:
int main() {
    int n;
    char ch;
    char* str;
    int i;
    printf("Enter the number of values you want to enter in your string: ");
    scanf("%d", &n);
    str = (char*)malloc(sizeof(char)*n);
    printf("Enter the string: ");
    getchar();
    fgets(str, n, stdin);
    printf("%s",str);
    printf("Now enter the character you want to check for: ");
    getchar();
    scanf("%c", &ch);
    printf("ok cool");
    printf("The character '%c' occurs %d times", ch, count_char(str,ch));
    return 0;
}

why is not working. I don't have much time and I am going to start with next problem. Can anyone who is adept at C help me with thing. Pretty please
 
Physics news on Phys.org
  • #2
Please be more specific than "is not working." A description of what your program is doing that it shouldn't be doing would be helpful.

You might be having a problem with these lines:
Code:
printf("Now enter the character you want to check for: ");
getchar();
scanf("%c", &ch);

Why do you have both getchar() and scanf()? getchar() might be picking up the character that is entered, but it is discarding the character. Something like
ch = getchar();

without the scanf() after it might fix things.
 

FAQ: C-language: quick n00b question

1. What is the C language?

The C language is a general-purpose programming language that was developed in the early 1970s by Dennis Ritchie at Bell Labs. It is a high-level language that is widely used for creating operating systems, graphics applications, and other programs.

2. How is C different from other programming languages?

C is a low-level language, meaning it is closer to machine code and gives the programmer more control over hardware resources. It also requires manual memory management, unlike higher-level languages that have automatic garbage collection.

3. What are the basic data types in C?

The basic data types in C include integers, floating-point numbers, characters, and pointers. There are also modifiers such as signed and unsigned that can be used to change the range of these data types.

4. What is the main function in C?

The main function is the entry point for all C programs. It is where the program starts executing and typically contains the code for the program's main functionality. It must have a return type of int and can take command-line arguments.

5. How can I learn C as a beginner?

There are many resources available for learning C as a beginner, including online tutorials, books, and courses. It is recommended to start with the basic syntax and then practice by writing simple programs. It is also helpful to join online communities and forums to ask for help and learn from others.

Similar threads

Replies
3
Views
1K
Replies
1
Views
9K
Replies
3
Views
901
Replies
12
Views
1K
Replies
12
Views
2K
Replies
7
Views
2K
Replies
1
Views
2K
Back
Top