"C Language Learning", C Primer Plus, chapter 4th, strings and formatted input/output

Source: Internet
Author: User

Learning Summary

1, String str= "Hello world!"; (Java), char[20]= "Hello world!"; (C). In fact, the implementation of Java string, is also a character array.

2. The trailing end of the string ends with a null character, so "Hello world! "The length of this character array is 13. The <string.h> Library has a strlen () function that can be the length of a character array of a computer string (excluding the null character, \).

3. The difference between scanf ("%s", name) and gets:

1#include <stdio.h>2 intMainvoid){3         Charname[ +];4printf"What ' s your name?\n");5scanf"%s", name);6printf"Hello%s!\n", name);7         return 0;8}

Operation Result:

What ' s your name?

Tom Green

Hello tom!

1#include <stdio.h>2 intMainvoid){3         Charname[ +];4printf"What ' s your name?\n");5 gets (name);6printf"Hello%s!\n", name);7         return 0;8}

Operation Result:

What ' s your name?

Tom Green

Hello Tom green!

4, #define可用于常量定义, format: #define Name value, the compiler will replace all the name with value after compilation, the equivalent of a simple "face value" substitution. If value is a combination of elements, the best brackets, such as # define SUM (X+y), should be in case of confusion caused by this "word game".

5. Another representation of a C constant can use the Const keyword, such as the const int months=12. Then months is a constant. In short, the const -Modified object is constant and cannot be modified.

6, Programming exercises (title 7):

1#include <stdio.h>2 #defineJ2s 3.7853 #defineY2G 1.6094 intMain () {5         DoubleMile,gallon;6printf"Enter your mile:");7scanf"%LF",&mile);8printf"Enter your gallon:");9scanf"%LF",&gallon);Tenprintf"your oil wear is%.1f\n", mile/gallon); Oneprintf"your CHN oil wear is%.1f\n", mile*y2g/(gallon*j2s)); A}

Operation Result:

Enter your mile:100

Enter your gallon:50

Your oil wear is 2.0

Your CHN oil wear is 0.9

"C Language Learning", C Primer Plus, chapter 4th, strings and formatted input/output

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.